// File: /workspace/github_c_makefile_star2_O3/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 %r14
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
cmpb $0x1, (%rdx)
jne 0xb160
movq %rdx, %rdi
callq 0xb6df
cmpl $-0x1, %eax
jne 0xb182
cmpb $0x1, (%r14)
jne 0xb230
movq 0x5e5f(%rip), %rax      # 0x10fd0
movq (%rax), %rdi
callq 0x53b0
cmpl $-0x1, %eax
je 0xb22c
movl %eax, %edi
movl $0x3, %eax
cmpl $0x5a, %edi
jg 0xb1c7
leal -0x1b(%rdi), %ecx
cmpl $0x22, %ecx
ja 0xb28b
leaq 0x239f(%rip), %rdx      # 0xd540
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cmpb $0x0, 0xd97f(%rip)      # 0x18b30
je 0xb2ee
cmpb $0x1, 0xd972(%rip)      # 0x18b30
jne 0xb230
callq 0xb053
jmp 0xb230
leal -0x6e(%rdi), %ecx
cmpl $0x8, %ecx
jbe 0xb23d
leal -0x102(%rdi), %ecx
cmpl $0x5, %ecx
ja 0xb268
leaq 0x242f(%rip), %rdx      # 0xd614
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cmpb $0x1, 0xd93b(%rip)      # 0x18b30
jne 0xb3ef
leaq 0x6246(%rip), %rax      # 0x11448
cmpb $0x0, 0x5(%rax)
jne 0xb230
movq 0x6191(%rip), %rax      # 0x113a0
leaq 0xd92a(%rip), %rcx      # 0x18b40
cmpq %rcx, %rax
je 0xb230
leaq -0x1(%rax), %rcx
movq %rcx, 0x617a(%rip)      # 0x113a0
movb $0x0, -0x1(%rax)
jmp 0xb230
movb $0x0, (%r14)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x8, %rsp
popq %rbx
popq %r14
retq
leaq 0x23ac(%rip), %rax      # 0xd5f0
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
leaq 0x61f4(%rip), %rcx      # 0x11448
movl 0x14(%rcx), %eax
incl %eax
cltd
idivl 0x18(%rcx)
movl %edx, (%rbx)
leaq 0x24ca(%rip), %rdi      # 0xd730
jmp 0xb2dd
addl $-0x5b, %edi
cmpl $0x8, %edi
ja 0xb230
leaq 0x2355(%rip), %rax      # 0xd5cc
movslq (%rax,%rdi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movl $0xfffffffb, (%rbx)     # imm = 0xFFFFFFFB
jmp 0xb3f5
cmpl $0xa, %edi
je 0xb3d5
cmpl $0xc, %edi
jne 0xb230
movq 0x5d30(%rip), %rax      # 0x10fd0
movq (%rax), %rdi
callq 0x52d0
jmp 0xb230
leaq 0x6197(%rip), %rax      # 0x11448
movl 0x14(%rax), %ecx
movl 0x18(%rax), %esi
leal (%rcx,%rsi), %eax
decl %eax
cltd
idivl %esi
movl %edx, (%rbx)
leaq 0x246e(%rip), %rdi      # 0xd736
jmp 0xb2dd
leaq 0x6177(%rip), %rax      # 0x11448
movl 0x14(%rax), %eax
movl %eax, (%rbx)
leaq 0x2463(%rip), %rdi      # 0xd740
xorl %eax, %eax
callq 0x9fa4
movl $0x2, %eax
jmp 0xb235
addl $-0x30, %edi
movl %edi, (%rbx)
movl $0x1, %eax
jmp 0xb235
leaq 0x6144(%rip), %rax      # 0x11448
movss 0x1c(%rax), %xmm1
addss 0x21b3(%rip), %xmm1     # 0xd4c4
movss 0xd43(%rip), %xmm0      # 0xc05c
minss %xmm1, %xmm0
jmp 0xb33a
leaq 0x6122(%rip), %rax      # 0x11448
movss 0x1c(%rax), %xmm1
addss 0x218d(%rip), %xmm1     # 0xd4c0
xorps %xmm0, %xmm0
maxss %xmm1, %xmm0
mulss 0xd12(%rip), %xmm0      # 0xc054
callq 0x50d0
cvttss2si %xmm0, %eax
movl %eax, (%rbx)
movl $0x4, %eax
jmp 0xb235
leaq 0x60ea(%rip), %rax      # 0x11448
xorl %ecx, %ecx
cmpl $0x1, 0x24(%rax)
setne %cl
jmp 0xb3b6
callq 0xb915
jmp 0xb230
movq 0x5c56(%rip), %rax      # 0x10fd0
movq (%rax), %rdi
callq 0x53b0
cmpl $-0x1, %eax
jne 0xb230
xorl %eax, %eax
cmpb $0x1, 0xd79c(%rip)      # 0x18b30
jne 0xb235
movl $0xa, %edi
jmp 0xb1c0
leaq 0x609d(%rip), %rax      # 0x11448
xorl %ecx, %ecx
cmpl $0x2, 0x24(%rax)
setne %cl
addl %ecx, %ecx
movl %ecx, 0x24(%rax)
movq 0x5c10(%rip), %rax      # 0x10fd0
movq (%rax), %rdi
callq 0x52a0
jmp 0xb230
movl $0x5, (%rbx)
jmp 0xb3f5
movl $0xa, %edi
callq 0xb053
cmpl $-0x1, %eax
je 0xb230
movl %eax, (%rbx)
jmp 0xb2e4
movl $0x0, (%rbx)
movl $0x5, %eax
jmp 0xb235
nop