// File: /workspace/github_cpp_makefile_star2_O3/mikeakohn[P]cloudtari/build/../src/TelevisionVNC.cxx
// Function: TelevisionVNC::handle_events()
int TelevisionVNC::handle_events()
{
  uint8_t buffer[128];
  uint32_t key;
  int n, count;

  if (net_has_data())
  {
    uint8_t message_type = 0xff;

    net_recv(&message_type, 1);

    switch (message_type)
    {
      case 0:
        printf("From Client: SetPixelFormat\n");
        net_recv(buffer + 1, 19);
        print_pixel_format(buffer);
        break;
      case 2:
        printf("From Client: SetEncodings\n");
        net_recv(buffer, 3);
        count = (buffer[1] << 8) | buffer[2];

        for (n = 0; n < count; n++)
        {
          net_recv(buffer, 4);
          print_encoding(buffer);
        }
        break;
      case 3:
        //printf("From Client: FramebufferUpdateRequest\n");
        net_recv(buffer + 1, 9);
        send_image_update(
          (buffer[2] << 8) | buffer[3],
          (buffer[4] << 8) | buffer[5],
          (buffer[6] << 8) | buffer[7],
          (buffer[8] << 8) | buffer[9],
          buffer[1]);
        break;
      case 4:
        //printf("From Client: KeyEvent\n");
        net_recv(buffer + 1, 7);
        key =
          (buffer[4] << 24) |
          (buffer[5] << 16) |
          (buffer[6] << 8) |
           buffer[7];

        // If buffer[1] is not 0 then it's a keydown.
        if (buffer[1] != 0)
        {
          // Escape key quits the game.
          if (key == 0xff1b) { return KEY_QUIT; }

          // Tab key was pressed.
          if (key == 0xff09) { return KEY_SELECT_DOWN; }
          if (key == 0xff0d) { return KEY_RESET_DOWN; }
          if (key == 0xff51) { return KEY_LEFT_DOWN; }
          if (key == 0xff54) { return KEY_UP_DOWN; }
          if (key == 0xff53) { return KEY_RIGHT_DOWN; }
          if (key == 0xff54) { return KEY_DOWN_DOWN; }
          if (key == ' ') { return KEY_FIRE_DOWN; }
        }
          else
        {
          // Tab key was released.
          if (key == 0xff09) { return KEY_SELECT_UP; }
          if (key == 0xff0d) { return KEY_RESET_UP; }
          if (key == 0xff51) { return KEY_LEFT_UP; }
          if (key == 0xff54) { return KEY_UP_UP; }
          if (key == 0xff53) { return KEY_RIGHT_UP; }
          if (key == 0xff54) { return KEY_DOWN_UP; }
          if (key == ' ') { return KEY_FIRE_UP; }
        }

        break;
      case 5:
        //printf("From Client: PointerEvent\n");
        net_recv(buffer + 1, 5);
        break;
      case 6:
        printf("From Client: ClientCutText\n");
        net_recv(buffer + 1, 7);
        count =
          (buffer[4] << 24) |
          (buffer[5] << 16) |
          (buffer[6] << 8) |
           buffer[7];
        // FIXME: This is not efficient, but don't really expect this one.
        for (n = 0; n < count; n++) { net_recv(buffer, 1); }
        break;
    }
  }

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x90, %rsp
movq %rdi, %r15
leaq 0x20(%rdi), %r14
movq %r14, %rdi
callq 0x74c0
xorl %ebx, %ebx
testb %al, %al
je 0x9b4b
leaq 0xf(%rsp), %r12
movb $-0x1, (%r12)
movq %r14, %rdi
movq %r12, %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x7358
movzbl (%r12), %eax
cmpq $0x6, %rax
ja 0x9b4b
leaq 0x2cc5(%rip), %rcx      # 0xc56c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x2f9f(%rip), %rdi      # 0xc856
callq 0x4310
leaq 0x11(%rsp), %rsi
movq %r14, %rdi
movl $0x13, %edx
movl $0x1, %ecx
callq 0x7358
leaq 0x10(%rsp), %rsi
callq 0x9b5e
jmp 0x9b4b
leaq 0x11(%rsp), %r12
movq %r14, %rdi
movq %r12, %rsi
movl $0x9, %edx
movl $0x1, %ecx
callq 0x7358
movb (%r12), %al
cmpb $0x1, 0x2d(%r15)
jne 0x990c
movb $0x0, 0x2d(%r15)
testb %al, %al
jne 0x9b4b
movzbl 0x12(%rsp), %eax
movzbl 0x14(%rsp), %ecx
shll $0x8, %eax
movzbl 0x13(%rsp), %edx
orl %eax, %edx
shll $0x8, %ecx
movzbl 0x15(%rsp), %eax
orl %ecx, %eax
orl %edx, %eax
jne 0x9b4b
movzwl 0x16(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %eax
cmpl %eax, 0x8(%r15)
jne 0x9b4b
movzwl 0x18(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %eax
cmpl %eax, 0xc(%r15)
jne 0x9b4b
movb $0x1, 0x2c(%r15)
jmp 0x9b4b
leaq 0x2eaa(%rip), %rdi      # 0xc821
callq 0x4310
leaq 0x11(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
movl $0x7, %edx
movl $0x1, %ecx
callq 0x7358
movl 0x3(%r15), %ebp
bswapl %ebp
testl %ebp, %ebp
jle 0x9b4b
leaq 0x10(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x7358
decl %ebp
jne 0x99a9
jmp 0x9b4b
leaq 0x2e6e(%rip), %rdi      # 0xc83c
callq 0x4310
leaq 0x10(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
movl $0x3, %edx
movl $0x1, %ecx
callq 0x7358
movzwl 0x1(%r15), %eax
rolw $0x8, %ax
testw %ax, %ax
je 0x9b4b
movzwl %ax, %ebp
leaq 0x10(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
movl $0x4, %edx
movl $0x1, %ecx
callq 0x7358
movq %r15, %rsi
callq 0x9c2c
decl %ebp
jne 0x9a07
jmp 0x9b4b
leaq 0x11(%rsp), %r15
movl $0x1, %ebp
movq %r14, %rdi
movq %r15, %rsi
movl $0x7, %edx
movl $0x1, %ecx
callq 0x7358
movl 0x3(%r15), %eax
bswapl %eax
cmpb $0x0, (%r15)
je 0x9aa6
cmpl $0xff1a, %eax           # imm = 0xFF1A
jle 0x9acf
cmpl $0xff52, %eax           # imm = 0xFF52
jg 0x9b05
cmpl $0xff1b, %eax           # imm = 0xFF1B
je 0x9b49
cmpl $0xff51, %eax           # imm = 0xFF51
jne 0x9b4b
movl $0x6, %ebp
jmp 0x9b49
leaq 0x11(%rsp), %rsi
movq %r14, %rdi
movl $0x5, %edx
movl $0x1, %ecx
callq 0x7358
jmp 0x9b4b
cmpl $0xff50, %eax           # imm = 0xFF50
jg 0x9ae9
cmpl $0x20, %eax
je 0x9b2f
cmpl $0xff09, %eax           # imm = 0xFF09
je 0x9b3d
cmpl $0xff0d, %eax           # imm = 0xFF0D
jne 0x9b4b
movl $0x5, %ebp
jmp 0x9b49
cmpl $0x20, %eax
je 0x9b1a
cmpl $0xff09, %eax           # imm = 0xFF09
je 0x9b28
cmpl $0xff0d, %eax           # imm = 0xFF0D
jne 0x9b4b
movl $0x4, %ebp
jmp 0x9b49
cmpl $0xff51, %eax           # imm = 0xFF51
je 0x9b36
cmpl $0xff53, %eax           # imm = 0xFF53
je 0x9b44
cmpl $0xff54, %eax           # imm = 0xFF54
jne 0x9b4b
movl $0xb, %ebp
jmp 0x9b49
cmpl $0xff53, %eax           # imm = 0xFF53
je 0x9b21
cmpl $0xff54, %eax           # imm = 0xFF54
jne 0x9b4b
movl $0xa, %ebp
jmp 0x9b49
movl $0xe, %ebp
jmp 0x9b49
movl $0x8, %ebp
jmp 0x9b49
movl $0x2, %ebp
jmp 0x9b49
movl $0xf, %ebp
jmp 0x9b49
movl $0x7, %ebp
jmp 0x9b49
movl $0x3, %ebp
jmp 0x9b49
movl $0x9, %ebp
movl %ebp, %ebx
movl %ebx, %eax
addq $0x90, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop