// File: /workspace/github_cpp_makefile_star2_O1/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 %rbx
subq $0x98, %rsp
movq %rdi, %r14
leaq 0x20(%rdi), %rbx
movq %rbx, %rdi
callq 0x74a8
testb %al, %al
je 0x9985
leaq 0xf(%rsp), %r15
movb $-0x1, (%r15)
movq %rbx, %rdi
movq %r15, %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x72e2
movzbl (%r15), %eax
cmpq $0x6, %rax
ja 0x997f
leaq 0x1c5f(%rip), %rcx      # 0xb3ec
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x1f39(%rip), %rdi      # 0xb6d6
callq 0x4300
leaq 0x11(%rsp), %rsi
movq %rbx, %rdi
movl $0x13, %edx
movl $0x1, %ecx
callq 0x72e2
leaq 0x10(%rsp), %rsi
callq 0x9a3a
jmp 0x997f
leaq 0x11(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
movl $0x9, %edx
movl $0x1, %ecx
callq 0x72e2
movb (%r15), %al
cmpb $0x1, 0x2d(%r14)
jne 0x97f1
movb $0x0, 0x2d(%r14)
testb %al, %al
jne 0x997f
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 0x997f
movzwl 0x16(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %eax
cmpl %eax, 0x8(%r14)
jne 0x997f
movzwl 0x18(%rsp), %eax
rolw $0x8, %ax
movzwl %ax, %eax
cmpl %eax, 0xc(%r14)
jne 0x997f
movb $0x1, 0x2c(%r14)
jmp 0x997f
leaq 0x1e45(%rip), %rdi      # 0xb6a1
callq 0x4300
leaq 0x11(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
movl $0x7, %edx
movl $0x1, %ecx
callq 0x72e2
movl 0x3(%r14), %ebp
bswapl %ebp
testl %ebp, %ebp
jle 0x997f
leaq 0x10(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
movl $0x1, %edx
movl $0x1, %ecx
callq 0x72e2
decl %ebp
jne 0x988e
jmp 0x997f
leaq 0x1e09(%rip), %rdi      # 0xb6bc
callq 0x4300
leaq 0x10(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
movl $0x3, %edx
movl $0x1, %ecx
callq 0x72e2
movzwl 0x1(%r14), %eax
rolw $0x8, %ax
testw %ax, %ax
je 0x997f
movzwl %ax, %ebp
leaq 0x10(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
movl $0x4, %edx
movl $0x1, %ecx
callq 0x72e2
movq %r14, %rsi
callq 0x9b08
decl %ebp
jne 0x98ec
jmp 0x997f
leaq 0x11(%rsp), %r14
movl $0x1, %ebp
movq %rbx, %rdi
movq %r14, %rsi
movl $0x7, %edx
movl $0x1, %ecx
callq 0x72e2
movl 0x3(%r14), %eax
bswapl %eax
cmpb $0x0, (%r14)
je 0x9997
cmpl $0xff1a, %eax           # imm = 0xFF1A
jle 0x99b8
cmpl $0xff52, %eax           # imm = 0xFF52
jg 0x99ee
cmpl $0xff1b, %eax           # imm = 0xFF1B
je 0x9a32
cmpl $0xff51, %eax           # imm = 0xFF51
jne 0x997f
movl $0x6, %ebp
jmp 0x9a32
leaq 0x11(%rsp), %rsi
movq %rbx, %rdi
movl $0x5, %edx
movl $0x1, %ecx
callq 0x72e2
movb $0x1, %al
testb %al, %al
je 0x9987
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
cmpl $0xff50, %eax           # imm = 0xFF50
jg 0x99d2
cmpl $0x20, %eax
je 0x9a18
cmpl $0xff09, %eax           # imm = 0xFF09
je 0x9a26
cmpl $0xff0d, %eax           # imm = 0xFF0D
jne 0x997f
movl $0x5, %ebp
jmp 0x9a32
cmpl $0x20, %eax
je 0x9a03
cmpl $0xff09, %eax           # imm = 0xFF09
je 0x9a11
cmpl $0xff0d, %eax           # imm = 0xFF0D
jne 0x997f
movl $0x4, %ebp
jmp 0x9a32
cmpl $0xff51, %eax           # imm = 0xFF51
je 0x9a1f
cmpl $0xff53, %eax           # imm = 0xFF53
je 0x9a2d
cmpl $0xff54, %eax           # imm = 0xFF54
jne 0x997f
movl $0xb, %ebp
jmp 0x9a32
cmpl $0xff53, %eax           # imm = 0xFF53
je 0x9a0a
cmpl $0xff54, %eax           # imm = 0xFF54
jne 0x997f
movl $0xa, %ebp
jmp 0x9a32
movl $0xe, %ebp
jmp 0x9a32
movl $0x8, %ebp
jmp 0x9a32
movl $0x2, %ebp
jmp 0x9a32
movl $0xf, %ebp
jmp 0x9a32
movl $0x7, %ebp
jmp 0x9a32
movl $0x3, %ebp
jmp 0x9a32
movl $0x9, %ebp
xorl %eax, %eax
jmp 0x9981
nop