// File: /workspace/github_c_makefile_star2_O0/baines[P]MiniGBS/ui.c
// Function: ui_action
int ui_action(int* out_val, bool* tui_events, bool* x11_events){
	int key = -1;

	if(*x11_events){
		key = x11_action(x11_events);
	}

	if(key == -1 && *tui_events){
		key = getch();
		if(key == -1){
			*tui_events = false;
			return -1;
		}
	}

	// TODO: more consistency here w.r.t ui messages being done here or at callsite etc.

	switch(key){
		case 27: // Escape
			if(getch() != -1) break;
		case 'q': {
			if(ui_in_cmd_mode){
				ui_cmd('\n');
			} else {
				return ACT_QUIT;
			}
		} break;

		case '1' ... '4': {
			if(!ui_in_cmd_mode){
				*out_val = key - '0';
				return ACT_CHAN_TOGGLE;
			}
		} // fall-through
		case '0':
		case '5' ... '9': {
			if(ui_in_cmd_mode){
				ui_cmd(key);
			}
		} break;

		case 'n':
		case KEY_RIGHT:
			*out_val = (cfg.song_no + 1) % cfg.song_count;
			ui_msg_set("Next\n");
			return ACT_TRACK_SET;

		case 'p':
		case KEY_LEFT:
			*out_val = (cfg.song_no + cfg.song_count - 1) % cfg.song_count;
			ui_msg_set("Previous\n");
			return ACT_TRACK_SET;

		case 'r':
		case KEY_UP:
			*out_val = cfg.song_no;
			ui_msg_set("Replay\n");
			return ACT_TRACK_SET;

		case 'c':
			cfg.ui_mode = (cfg.ui_mode != UI_MODE_CHART) ? UI_MODE_CHART : UI_MODE_REGISTERS;
			erase();
			break;

		case 'v':
			cfg.ui_mode = (cfg.ui_mode != UI_MODE_VOLUME) ? UI_MODE_VOLUME : UI_MODE_REGISTERS;
			erase();
			break;

		case ' ':
		case KEY_DOWN:
			return ACT_PAUSE;

		case '=':
		case '+': {
			float vol = MIN(1.0f, cfg.volume + 0.05f);
			*out_val = (int)roundf(100.0f * vol);
			return ACT_VOL;
		} break;

		case '-':
		case '_': {
			float vol = MAX(0.0f, cfg.volume - 0.05f);
			*out_val = (int)roundf(100.0f * vol);
			return ACT_VOL;
		} break;

		case '[':
			*out_val = -5;
			return ACT_SPEED;

		case ']':
			*out_val = 5;
			return ACT_SPEED;

		case KEY_BACKSPACE: {
			if(ui_in_cmd_mode){
				ui_cmd(key);
			} else {
				*out_val = 0;
				return ACT_SPEED;
			}
		} break;

		case '\n': {
			int track = ui_cmd(key);
			if(track != -1){
				*out_val = track;
				return ACT_TRACK_SET;
			}
		} break;

		case 'o': {
			x11_toggle();
		} break;

		case 12: // CTRL-L
			clear();
			break;
	}

	return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl $0xffffffff, -0x24(%rbp) # imm = 0xFFFFFFFF
movq -0x20(%rbp), %rax
testb $0x1, (%rax)
je 0xef10
movq -0x20(%rbp), %rdi
callq 0xf670
movl %eax, -0x24(%rbp)
cmpl $-0x1, -0x24(%rbp)
jne 0xef4c
movq -0x18(%rbp), %rax
testb $0x1, (%rax)
je 0xef4c
movq 0x60aa(%rip), %rax      # 0x14fd0
movq (%rax), %rdi
callq 0x53b0
movl %eax, -0x24(%rbp)
cmpl $-0x1, -0x24(%rbp)
jne 0xef4a
movq -0x18(%rbp), %rax
movb $0x0, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0xf2a6
jmp 0xef4c
movl -0x24(%rbp), %eax
addl $-0xa, %eax
movl %eax, %ecx
movq %rcx, -0x50(%rbp)
subl $0xfd, %eax
ja 0xf29f
movq -0x50(%rbp), %rax
leaq 0x2b32(%rip), %rcx      # 0x11aa0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x6052(%rip), %rax      # 0x14fd0
movq (%rax), %rdi
callq 0x53b0
cmpl $-0x1, %eax
je 0xef90
jmp 0xf29f
jmp 0xef92
testb $0x1, 0xf067(%rip)      # 0x1e000
je 0xefa7
movl $0xa, %edi
callq 0xeda0
jmp 0xefb3
movl $0x0, -0x4(%rbp)
jmp 0xf2a6
jmp 0xf29f
testb $0x1, 0xf041(%rip)      # 0x1e000
jne 0xefd9
movl -0x24(%rbp), %ecx
subl $0x30, %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movl $0x1, -0x4(%rbp)
jmp 0xf2a6
jmp 0xefdb
jmp 0xefdd
testb $0x1, 0xf01c(%rip)      # 0x1e000
je 0xefee
movl -0x24(%rbp), %edi
callq 0xeda0
jmp 0xf29f
leaq 0x791e(%rip), %rax      # 0x16918
movl 0x14(%rax), %eax
addl $0x1, %eax
leaq 0x7911(%rip), %rcx      # 0x16918
cltd
idivl 0x18(%rcx)
movq -0x10(%rbp), %rax
movl %edx, (%rax)
leaq 0x2f48(%rip), %rdi      # 0x11f60
movb $0x0, %al
callq 0xd6e0
movl $0x2, -0x4(%rbp)
jmp 0xf2a6
leaq 0x78e6(%rip), %rax      # 0x16918
movl 0x14(%rax), %eax
leaq 0x78dc(%rip), %rcx      # 0x16918
addl 0x18(%rcx), %eax
subl $0x1, %eax
leaq 0x78cf(%rip), %rcx      # 0x16918
cltd
idivl 0x18(%rcx)
movq -0x10(%rbp), %rax
movl %edx, (%rax)
leaq 0x2f0c(%rip), %rdi      # 0x11f66
movb $0x0, %al
callq 0xd6e0
movl $0x2, -0x4(%rbp)
jmp 0xf2a6
leaq 0x78a4(%rip), %rax      # 0x16918
movl 0x14(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
leaq 0x2eec(%rip), %rdi      # 0x11f70
movb $0x0, %al
callq 0xd6e0
movl $0x2, -0x4(%rbp)
jmp 0xf2a6
leaq 0x787a(%rip), %rax      # 0x16918
movl 0x24(%rax), %edx
xorl %ecx, %ecx
movl $0x2, %eax
cmpl $0x2, %edx
cmovnel %eax, %ecx
leaq 0x7863(%rip), %rax      # 0x16918
movl %ecx, 0x24(%rax)
movq 0x5f11(%rip), %rax      # 0x14fd0
movq (%rax), %rdi
callq 0x52a0
jmp 0xf29f
leaq 0x7845(%rip), %rax      # 0x16918
movl 0x24(%rax), %edx
xorl %ecx, %ecx
movl $0x1, %eax
cmpl $0x1, %edx
cmovnel %eax, %ecx
leaq 0x782e(%rip), %rax      # 0x16918
movl %ecx, 0x24(%rax)
movq 0x5edc(%rip), %rax      # 0x14fd0
movq (%rax), %rdi
callq 0x52a0
jmp 0xf29f
movl $0x3, -0x4(%rbp)
jmp 0xf2a6
movss 0xeef(%rip), %xmm0      # 0x10004
movss %xmm0, -0x2c(%rbp)
movss 0x2972(%rip), %xmm0     # 0x11a94
leaq 0x77ef(%rip), %rax      # 0x16918
addss 0x1c(%rax), %xmm0
movss %xmm0, -0x30(%rbp)
movss -0x2c(%rbp), %xmm1
movss -0x30(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jb 0xf14e
movss -0x2c(%rbp), %xmm0
movss %xmm0, -0x54(%rbp)
jmp 0xf158
movss -0x30(%rbp), %xmm0
movss %xmm0, -0x54(%rbp)
movss -0x54(%rbp), %xmm0
movss %xmm0, -0x34(%rbp)
movss -0x34(%rbp), %xmm0
movss %xmm0, -0x28(%rbp)
movss 0xe9c(%rip), %xmm0      # 0x10010
mulss -0x28(%rbp), %xmm0
callq 0x50d0
cvttss2si %xmm0, %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movl $0x4, -0x4(%rbp)
jmp 0xf2a6
xorps %xmm0, %xmm0
movss %xmm0, -0x3c(%rbp)
leaq 0x7775(%rip), %rax      # 0x16918
movss 0x1c(%rax), %xmm0
movss 0x28e4(%rip), %xmm1     # 0x11a94
subss %xmm1, %xmm0
movss %xmm0, -0x40(%rbp)
movss -0x3c(%rbp), %xmm0
ucomiss -0x40(%rbp), %xmm0
jbe 0xf1d0
movss -0x3c(%rbp), %xmm0
movss %xmm0, -0x58(%rbp)
jmp 0xf1da
movss -0x40(%rbp), %xmm0
movss %xmm0, -0x58(%rbp)
movss -0x58(%rbp), %xmm0
movss %xmm0, -0x44(%rbp)
movss -0x44(%rbp), %xmm0
movss %xmm0, -0x38(%rbp)
movss 0xe1a(%rip), %xmm0      # 0x10010
mulss -0x38(%rbp), %xmm0
callq 0x50d0
cvttss2si %xmm0, %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movl $0x4, -0x4(%rbp)
jmp 0xf2a6
movq -0x10(%rbp), %rax
movl $0xfffffffb, (%rax)     # imm = 0xFFFFFFFB
movl $0x5, -0x4(%rbp)
jmp 0xf2a6
movq -0x10(%rbp), %rax
movl $0x5, (%rax)
movl $0x5, -0x4(%rbp)
jmp 0xf2a6
testb $0x1, 0xedbd(%rip)      # 0x1e000
je 0xf24f
movl -0x24(%rbp), %edi
callq 0xeda0
jmp 0xf262
movq -0x10(%rbp), %rax
movl $0x0, (%rax)
movl $0x5, -0x4(%rbp)
jmp 0xf2a6
jmp 0xf29f
movl -0x24(%rbp), %edi
callq 0xeda0
movl %eax, -0x48(%rbp)
cmpl $-0x1, -0x48(%rbp)
je 0xf287
movl -0x48(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movl $0x2, -0x4(%rbp)
jmp 0xf2a6
jmp 0xf29f
callq 0xf9d0
jmp 0xf29f
movq 0x5d39(%rip), %rax      # 0x14fd0
movq (%rax), %rdi
callq 0x52d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nop