// File: /workspace/github_c_makefile_star2_O2/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 0xa922
movq %rdx, %rdi
callq 0xae80
cmpl $-0x1, %eax
jne 0xa940
cmpb $0x1, (%r14)
jne 0xa9b8
movq 0x769d(%rip), %rax      # 0x11fd0
movq (%rax), %rdi
callq 0x53c0
cmpl $-0x1, %eax
je 0xa9b4
movl %eax, %edi
pushq $0x3
popq %rax
leal -0x1b(%rdi), %ecx
cmpl $0x22, %ecx
jbe 0xa987
leal -0x6e(%rdi), %ecx
cmpl $0x8, %ecx
jbe 0xa9c3
leal -0x102(%rdi), %ecx
cmpl $0x5, %ecx
jbe 0xa9da
leal -0x5b(%rdi), %eax
cmpl $0x8, %eax
ja 0xa9ff
leaq 0x2bb9(%rip), %rcx      # 0xd52c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0xfffffffb, (%rbx)     # imm = 0xFFFFFFFB
jmp 0xaba1
leaq 0x2b12(%rip), %rdx      # 0xd4a0
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cmpb $0x0, 0xf192(%rip)      # 0x19b30
je 0xaa65
cmpb $0x1, 0xf185(%rip)      # 0x19b30
jne 0xa9b8
callq 0xa817
jmp 0xa9b8
movb $0x0, (%r14)
pushq $-0x1
popq %rax
addq $0x8, %rsp
popq %rbx
popq %r14
retq
leaq 0x2b86(%rip), %rax      # 0xd550
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
callq 0xb094
jmp 0xa9b8
leaq 0x2b93(%rip), %rdx      # 0xd574
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
leaq 0x7a57(%rip), %rax      # 0x12448
movl 0x14(%rax), %eax
movl %eax, (%rbx)
leaq 0x2ca3(%rip), %rdi      # 0xd6a0
jmp 0xaa57
cmpl $0xa, %edi
je 0xab86
cmpl $0xc, %edi
jne 0xa9b8
movq 0x75bc(%rip), %rax      # 0x11fd0
movq (%rax), %rdi
callq 0x52d0
jmp 0xa9b8
leaq 0x7a23(%rip), %rax      # 0x12448
movl 0x14(%rax), %ecx
movl 0x18(%rax), %esi
leal (%rcx,%rsi), %eax
decl %eax
cltd
idivl %esi
movl %edx, (%rbx)
leaq 0x2c5a(%rip), %rdi      # 0xd696
jmp 0xaa57
leaq 0x7a03(%rip), %rcx      # 0x12448
movl 0x14(%rcx), %eax
incl %eax
cltd
idivl 0x18(%rcx)
movl %edx, (%rbx)
leaq 0x2c39(%rip), %rdi      # 0xd690
xorl %eax, %eax
callq 0x99c6
pushq $0x2
jmp 0xa9ba
addl $-0x30, %edi
movl %edi, (%rbx)
pushq $0x1
jmp 0xa9ba
leaq 0x79d0(%rip), %rax      # 0x12448
movss 0x1c(%rax), %xmm1
addss 0x2a0f(%rip), %xmm1     # 0xd494
movss 0x15cf(%rip), %xmm0     # 0xc05c
minss %xmm1, %xmm0
jmp 0xaaae
leaq 0x79ae(%rip), %rax      # 0x12448
movss 0x1c(%rax), %xmm1
addss 0x29e9(%rip), %xmm1     # 0xd490
xorps %xmm0, %xmm0
maxss %xmm1, %xmm0
mulss 0x159e(%rip), %xmm0     # 0xc054
callq 0x50d0
cvttss2si %xmm0, %eax
movl %eax, (%rbx)
pushq $0x4
jmp 0xa9ba
leaq 0x7979(%rip), %rax      # 0x12448
xorl %ecx, %ecx
cmpl $0x1, 0x24(%rax)
setne %cl
jmp 0xab67
movq 0x74ec(%rip), %rax      # 0x11fd0
movq (%rax), %rdi
callq 0x53c0
cmpl $-0x1, %eax
jne 0xa9b8
xorl %eax, %eax
cmpb $0x1, 0xf032(%rip)      # 0x19b30
jne 0xa9bb
pushq $0xa
popq %rdi
jmp 0xa9ad
cmpb $0x1, 0xf01d(%rip)      # 0x19b30
jne 0xab9e
leaq 0x7928(%rip), %rax      # 0x12448
cmpb $0x0, 0x5(%rax)
jne 0xa9b8
movq 0x7877(%rip), %rax      # 0x123a8
leaq 0xf008(%rip), %rcx      # 0x19b40
cmpq %rcx, %rax
je 0xa9b8
leaq -0x1(%rax), %rcx
movq %rcx, 0x785c(%rip)      # 0x123a8
movb $0x0, -0x1(%rax)
jmp 0xa9b8
leaq 0x78ec(%rip), %rax      # 0x12448
xorl %ecx, %ecx
cmpl $0x2, 0x24(%rax)
setne %cl
addl %ecx, %ecx
movl %ecx, 0x24(%rax)
movq 0x745f(%rip), %rax      # 0x11fd0
movq (%rax), %rdi
callq 0x52a0
jmp 0xa9b8
movl $0x5, (%rbx)
jmp 0xaba1
pushq $0xa
popq %rdi
callq 0xa817
cmpl $-0x1, %eax
je 0xa9b8
movl %eax, (%rbx)
jmp 0xaa5e
andl $0x0, (%rbx)
pushq $0x5
jmp 0xa9ba