// File: rfotino[P]consolite-emulator/src/window.cpp
// Function: EmuWindow::eventLoop()
void EmuWindow::eventLoop() {
  // Get the file descriptor for the connection to the X server,
  // so that we can listen for input on a timer instead of blocking
  // indefinitely on XNextEvent. The reason for this is we need to wake
  // up every 1/60 of a second to repaint.
  int x11_fd = ConnectionNumber(_display);
  fd_set event_fds;
  struct timeval timer;
  // The running variable is set to false when the user
  // closes the window, which breaks out of the event loop.
  bool running = true;
  while (running) {
    // Set up the set of file descriptors for select()
    FD_ZERO(&event_fds);
    FD_SET(x11_fd, &event_fds);
    // Set timer to 1/60 of a second
    timer.tv_usec = 16667;
    timer.tv_sec = 0;
    if (0 == select(x11_fd + 1, &event_fds, 0, 0, &timer)) {
      // Timer expired
      _draw();
      continue;
    }
    // We received an event so handle it here
    XEvent event;
    XNextEvent(_display, &event);
    switch (event.type) {
    case KeyPress:
    case KeyRelease:
      _updateKeyState(event.xkey);
      break;
    case Expose:
      // Draw to the window
      _draw();
      break;
    case ConfigureNotify:
      // The window was resized, redraw scaled
      if (event.xconfigure.width != _width ||
          event.xconfigure.height != _height) {
        _width = event.xconfigure.width;
        _height = event.xconfigure.height;
        cairo_xlib_surface_set_size(_surface, _width, _height);
        _draw();
      }
      break;
    case ClientMessage:
      // Hit the exit button
      if ((unsigned)event.xclient.data.l[0] == _wmDeleteMessage) {
        running = false;
      }
      break;
    default:
      break;
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x178(%rbp)
movq 0x10(%rax), %rax
movl 0x10(%rax), %eax
movl %eax, -0xc(%rbp)
movb $0x1, -0xa1(%rbp)
testb $0x1, -0xa1(%rbp)
je 0x5a5a
jmp 0x589a
leaq -0x90(%rbp), %rax
movq %rax, -0xb0(%rbp)
movl $0x0, -0xa8(%rbp)
movl -0xa8(%rbp), %eax
cmpq $0x10, %rax
jae 0x58e4
movq -0xb0(%rbp), %rax
movl -0xa8(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xa8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x58b2
jmp 0x58e6
movl -0xc(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0xc(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x90(%rbp,%rax,8), %rcx
movq %rcx, -0x90(%rbp,%rax,8)
movq $0x411b, -0x98(%rbp) # imm = 0x411B
movq $0x0, -0xa0(%rbp)
movl -0xc(%rbp), %edi
addl $0x1, %edi
leaq -0x90(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0xa0(%rbp), %r8
movq %rcx, %rdx
callq 0x32a0
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x596c
movq -0x178(%rbp), %rdi
callq 0x5520
jmp 0x588b
movq -0x178(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x170(%rbp), %rsi
callq 0x3430
movl -0x170(%rbp), %eax
addl $-0x2, %eax
movl %eax, %ecx
movq %rcx, -0x180(%rbp)
subl $0x1f, %eax
ja 0x5a53
movq -0x180(%rbp), %rax
leaq 0x474c(%rip), %rcx      # 0xa0f8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x178(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x5730
jmp 0x5a55
movq -0x178(%rbp), %rdi
callq 0x5520
jmp 0x5a55
movq -0x178(%rbp), %rcx
movl -0x138(%rbp), %eax
cmpl 0x2c(%rcx), %eax
jne 0x59ff
movq -0x178(%rbp), %rcx
movl -0x134(%rbp), %eax
cmpl 0x30(%rcx), %eax
je 0x5a32
movq -0x178(%rbp), %rax
movl -0x138(%rbp), %ecx
movl %ecx, 0x2c(%rax)
movl -0x134(%rbp), %ecx
movl %ecx, 0x30(%rax)
movq (%rax), %rdi
movl 0x2c(%rax), %esi
movl 0x30(%rax), %edx
callq 0x3350
movq -0x178(%rbp), %rdi
callq 0x5520
jmp 0x5a55
movq -0x178(%rbp), %rcx
movq -0x138(%rbp), %rax
movl %eax, %eax
cmpq 0x18(%rcx), %rax
jne 0x5a51
movb $0x0, -0xa1(%rbp)
jmp 0x5a55
jmp 0x5a55
jmp 0x588b
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax)