// File: /workspace/github_c_makefile_star2_O1/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
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpb $0x1, (%rdx)
jne 0xb052
movq %rdx, %rdi
callq 0xb603
cmpl $-0x1, %eax
jne 0xb07a
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpb $0x1, (%r14)
jne 0xb07a
movq 0x5f67(%rip), %rax      # 0x10fd0
movq (%rax), %rdi
callq 0x53b0
cmpl $-0x1, %eax
je 0xb18b
movl $0x3, %ecx
cmpl $0x5a, %eax
jg 0xb0c6
leal -0x1b(%rax), %edx
cmpl $0x22, %edx
ja 0xb194
leaq 0x23e9(%rip), %rsi      # 0xd480
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
cmpb $0x0, 0xda89(%rip)      # 0x18b30
je 0xb1fe
cmpb $0x1, 0xda7c(%rip)      # 0x18b30
jne 0xb2ca
movl %eax, %edi
callq 0xaf45
jmp 0xb2ca
leal -0x6e(%rax), %edx
cmpl $0x8, %edx
jbe 0xb136
leal -0x102(%rax), %edx
cmpl $0x5, %edx
ja 0xb164
leaq 0x2470(%rip), %rax      # 0xd554
movslq (%rax,%rdx,4), %rdx
addq %rax, %rdx
jmpq *%rdx
cmpb $0x1, 0xda3c(%rip)      # 0x18b30
jne 0xb2f7
leaq 0x6347(%rip), %rax      # 0x11448
cmpb $0x0, 0x5(%rax)
jne 0xb2ca
movq 0x628e(%rip), %rax      # 0x113a0
leaq 0xda27(%rip), %rcx      # 0x18b40
cmpq %rcx, %rax
je 0xb2ca
leaq -0x1(%rax), %rcx
movq %rcx, 0x6273(%rip)      # 0x113a0
movb $0x0, -0x1(%rax)
jmp 0xb2ca
leaq 0x23f3(%rip), %rax      # 0xd530
movslq (%rax,%rdx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
leaq 0x62fb(%rip), %rcx      # 0x11448
movl 0x14(%rcx), %eax
incl %eax
cltd
idivl 0x18(%rcx)
movl %edx, (%rbx)
leaq 0x2511(%rip), %rdi      # 0xd670
jmp 0xb1ed
addl $-0x5b, %eax
cmpl $0x8, %eax
ja 0xb2ca
leaq 0x2395(%rip), %rcx      # 0xd50c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0xfffffffb, (%rbx)     # imm = 0xFFFFFFFB
jmp 0xb2fd
movb $0x0, (%r14)
jmp 0xb2ca
cmpl $0xa, %eax
je 0xb2e1
cmpl $0xc, %eax
jne 0xb2ca
movq 0x5e23(%rip), %rax      # 0x10fd0
movq (%rax), %rdi
callq 0x52d0
jmp 0xb2ca
leaq 0x6287(%rip), %rax      # 0x11448
movl 0x14(%rax), %ecx
movl 0x18(%rax), %esi
leal (%rcx,%rsi), %eax
decl %eax
cltd
idivl %esi
movl %edx, (%rbx)
leaq 0x249e(%rip), %rdi      # 0xd676
jmp 0xb1ed
leaq 0x6267(%rip), %rax      # 0x11448
movl 0x14(%rax), %eax
movl %eax, (%rbx)
leaq 0x2493(%rip), %rdi      # 0xd680
xorl %eax, %eax
callq 0xa06c
movl $0x2, %ecx
jmp 0xb2cf
addl $-0x30, %eax
movl %eax, (%rbx)
movl $0x1, %ecx
jmp 0xb2cf
leaq 0x6234(%rip), %rax      # 0x11448
movss 0x1c(%rax), %xmm1
addss 0x2257(%rip), %xmm1     # 0xd478
movss 0xde3(%rip), %xmm0      # 0xc00c
minss %xmm1, %xmm0
jmp 0xb24a
leaq 0x6212(%rip), %rax      # 0x11448
movss 0x1c(%rax), %xmm1
addss 0x2231(%rip), %xmm1     # 0xd474
xorps %xmm0, %xmm0
maxss %xmm1, %xmm0
mulss 0xdb6(%rip), %xmm0      # 0xc008
callq 0x50d0
cvttss2si %xmm0, %eax
movl %eax, (%rbx)
movl $0x4, %ecx
jmp 0xb2cf
leaq 0x61dd(%rip), %rax      # 0x11448
xorl %ecx, %ecx
cmpl $0x1, 0x24(%rax)
setne %cl
jmp 0xb2b8
callq 0xb839
jmp 0xb2ca
movq 0x5d4c(%rip), %rax      # 0x10fd0
movq (%rax), %rdi
callq 0x53b0
cmpl $-0x1, %eax
jne 0xb2ca
xorl %ecx, %ecx
cmpb $0x1, 0xd896(%rip)      # 0x18b30
jne 0xb2cf
movl $0xa, %edi
jmp 0xb0bc
leaq 0x619b(%rip), %rax      # 0x11448
xorl %ecx, %ecx
cmpl $0x2, 0x24(%rax)
setne %cl
addl %ecx, %ecx
movl %ecx, 0x24(%rax)
movq 0x5d0e(%rip), %rax      # 0x10fd0
movq (%rax), %rdi
callq 0x52a0
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq
movl $0x5, (%rbx)
jmp 0xb2fd
movl $0xa, %edi
callq 0xaf45
cmpl $-0x1, %eax
je 0xb2ca
movl %eax, (%rbx)
jmp 0xb1f4
movl $0x0, (%rbx)
movl $0x5, %ecx
jmp 0xb2cf