// File: havluj[P]spreadsheet/src/CRuntime.cpp
// Function: CRuntime::run()
void CRuntime::run() {
    noecho();
    curs_set(0);

    int ch;
    int prevController = m_currentView;
    while (true) {
	  /* get the terminal dimensions */
	  getmaxyx(stdscr, m_terminalHeigth, m_terminalWidth);

	  /* make sure the terminal is large enough */
	  if (m_terminalWidth < 78 || m_terminalHeigth < 27) {
		if (m_currentView != SPLASHSCREEN_VIEW) {
		    prevController = m_currentView;
		}
		try {
		    changeView(SPLASHSCREEN_VIEW);
		} catch (const std::invalid_argument& e) {
		    throw e;
		}
	  } else {
		if (m_currentView == SPLASHSCREEN_VIEW) {
		    try {
			  changeView(prevController);
		    } catch (const std::invalid_argument& e) {
			  throw e;
		    }
		}
	  }

	  /* wait for a character to be pressed */
	  ch = getch();

	  switch (ch) {
		case KEY_F(4): // end the program
		    return;

		case KEY_RESIZE: // redraw the window
		    try {
			  changeView(m_currentView);
		    } catch (const std::invalid_argument& e) {
			  throw e;
		    }
		    break;

		case KEY_F(3): // show spreadsheet / help
		    if (m_currentView == TABLE_VIEW) {
			  try {
				changeView(HELP_VIEW);
			  } catch (const std::invalid_argument& e) {
				throw e;
			  }
		    } else {
			  try {
				changeView(TABLE_VIEW);
			  } catch (const std::invalid_argument& e) {
				throw e;
			  }
		    }
		    break;

		default:
		    m_controller->handleKey(ch);
		    break;
	  }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x50(%rbp)
callq 0x54d0
xorl %edi, %edi
callq 0x5380
movq -0x50(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, -0x10(%rbp)
xorl %eax, %eax
movq 0x17b32(%rip), %rcx     # 0x27fb8
cmpq (%rcx), %rax
je 0x104a1
movq 0x17b26(%rip), %rax     # 0x27fb8
movq (%rax), %rax
movswl 0x4(%rax), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x104ab
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x54(%rbp)
jmp 0x104ab
movq -0x50(%rbp), %rax
movl -0x54(%rbp), %ecx
movl %ecx, 0x4c(%rax)
xorl %eax, %eax
movq 0x17afa(%rip), %rcx     # 0x27fb8
cmpq (%rcx), %rax
je 0x104d9
movq 0x17aee(%rip), %rax     # 0x27fb8
movq (%rax), %rax
movswl 0x6(%rax), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x104e3
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0x58(%rbp)
jmp 0x104e3
movq -0x50(%rbp), %rax
movl -0x58(%rbp), %ecx
movl %ecx, 0x48(%rax)
cmpl $0x4e, 0x48(%rax)
jl 0x10501
movq -0x50(%rbp), %rax
cmpl $0x1b, 0x4c(%rax)
jge 0x105a0
movq -0x50(%rbp), %rax
cmpl $0x0, 0x38(%rax)
je 0x10515
movq -0x50(%rbp), %rax
movl 0x38(%rax), %eax
movl %eax, -0x10(%rbp)
movq -0x50(%rbp), %rdi
xorl %esi, %esi
callq 0x10290
jmp 0x10522
jmp 0x1059b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x10849
movq -0x18(%rbp), %rdi
callq 0x5180
movq %rax, -0x28(%rbp)
movl $0x10, %edi
callq 0x5200
movq %rax, %rdi
movq %rdi, -0x60(%rbp)
movq -0x28(%rbp), %rsi
callq 0x10b00
movq -0x60(%rbp), %rdi
movq 0x17a6e(%rip), %rsi     # 0x27fe0
movq 0x17a4f(%rip), %rdx     # 0x27fc8
callq 0x56f0
jmp 0x1085a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
callq 0x5690
jmp 0x10596
jmp 0x10849
jmp 0x10639
movq -0x50(%rbp), %rax
cmpl $0x0, 0x38(%rax)
jne 0x10637
movq -0x50(%rbp), %rdi
movl -0x10(%rbp), %esi
callq 0x10290
jmp 0x105bc
jmp 0x10635
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x10849
movq -0x18(%rbp), %rdi
callq 0x5180
movq %rax, -0x30(%rbp)
movl $0x10, %edi
callq 0x5200
movq %rax, %rdi
movq %rdi, -0x68(%rbp)
movq -0x30(%rbp), %rsi
callq 0x10b00
movq -0x68(%rbp), %rdi
movq 0x179d4(%rip), %rsi     # 0x27fe0
movq 0x179b5(%rip), %rdx     # 0x27fc8
callq 0x56f0
jmp 0x1085a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
callq 0x5690
jmp 0x10630
jmp 0x10849
jmp 0x10637
jmp 0x10639
movq 0x17978(%rip), %rax     # 0x27fb8
movq (%rax), %rdi
callq 0x5150
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0x6c(%rbp)
subl $0x10b, %eax            # imm = 0x10B
je 0x1070e
jmp 0x1065e
movl -0x6c(%rbp), %eax
subl $0x10c, %eax            # imm = 0x10C
je 0x10679
jmp 0x1066a
movl -0x6c(%rbp), %eax
subl $0x19a, %eax            # imm = 0x19A
je 0x10682
jmp 0x10833
addq $0x90, %rsp
popq %rbp
retq
movq -0x50(%rbp), %rdi
movl 0x38(%rdi), %esi
callq 0x10290
jmp 0x10690
jmp 0x10709
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x10849
movq -0x18(%rbp), %rdi
callq 0x5180
movq %rax, -0x38(%rbp)
movl $0x10, %edi
callq 0x5200
movq %rax, %rdi
movq %rdi, -0x78(%rbp)
movq -0x38(%rbp), %rsi
callq 0x10b00
movq -0x78(%rbp), %rdi
movq 0x17900(%rip), %rsi     # 0x27fe0
movq 0x178e1(%rip), %rdx     # 0x27fc8
callq 0x56f0
jmp 0x1085a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
callq 0x5690
jmp 0x10704
jmp 0x10849
jmp 0x10844
movq -0x50(%rbp), %rax
cmpl $0x2, 0x38(%rax)
jne 0x107aa
movq -0x50(%rbp), %rdi
movl $0x3, %esi
callq 0x10290
jmp 0x1072c
jmp 0x107a5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x10849
movq -0x18(%rbp), %rdi
callq 0x5180
movq %rax, -0x40(%rbp)
movl $0x10, %edi
callq 0x5200
movq %rax, %rdi
movq %rdi, -0x80(%rbp)
movq -0x40(%rbp), %rsi
callq 0x10b00
movq -0x80(%rbp), %rdi
movq 0x17864(%rip), %rsi     # 0x27fe0
movq 0x17845(%rip), %rdx     # 0x27fc8
callq 0x56f0
jmp 0x1085a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
callq 0x5690
jmp 0x107a0
jmp 0x10849
jmp 0x10831
movq -0x50(%rbp), %rdi
movl $0x2, %esi
callq 0x10290
jmp 0x107ba
jmp 0x1082f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x10849
movq -0x18(%rbp), %rdi
callq 0x5180
movq %rax, -0x48(%rbp)
movl $0x10, %edi
callq 0x5200
movq %rax, %rdi
movq %rdi, -0x88(%rbp)
movq -0x48(%rbp), %rsi
callq 0x10b00
movq -0x88(%rbp), %rdi
movq 0x177d4(%rip), %rsi     # 0x27fe0
movq 0x177b5(%rip), %rdx     # 0x27fc8
callq 0x56f0
jmp 0x1085a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
callq 0x5690
jmp 0x1082d
jmp 0x10849
jmp 0x10831
jmp 0x10844
movq -0x50(%rbp), %rax
movq 0x40(%rax), %rdi
movl -0xc(%rbp), %esi
movq (%rdi), %rax
callq *0x10(%rax)
jmp 0x1047d
movq -0x18(%rbp), %rdi
callq 0x5730
movq %rax, %rdi
callq 0x7670
nopw (%rax,%rax)