// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %r15
leaq 0x20(%rdi), %r14
movq %r14, %rdi
callq 0x6f88
xorl %ebx, %ebx
testb %al, %al
je 0x8edd
leaq 0xf(%rsp), %r12
movb $-0x1, (%r12)
pushq $0x1
popq %rcx
movq %r14, %rdi
movq %r12, %rsi
movl %ecx, %edx
callq 0x6e4a
movzbl (%r12), %eax
cmpq $0x6, %rax
ja 0x8edd
leaq 0x2732(%rip), %rcx      # 0xb3fc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x2a0c(%rip), %rdi      # 0xb6e6
callq 0x4340
leaq 0x11(%rsp), %rsi
pushq $0x13
popq %rdx
pushq $0x1
popq %rcx
movq %r14, %rdi
callq 0x6e4a
leaq 0x10(%rsp), %rsi
callq 0x8ef2
jmp 0x8edd
leaq 0x11(%rsp), %r12
pushq $0x9
popq %rdx
pushq $0x1
popq %rcx
movq %r14, %rdi
movq %r12, %rsi
callq 0x6e4a
movzwl 0x1(%r12), %eax
movzwl 0x3(%r12), %ecx
rolw $0x8, %ax
movzwl %ax, %esi
rolw $0x8, %cx
movzwl %cx, %edx
movzwl 0x5(%r12), %eax
rolw $0x8, %ax
movzwl %ax, %ecx
movzwl 0x7(%r12), %eax
rolw $0x8, %ax
movzwl %ax, %r8d
xorl %r9d, %r9d
cmpb $0x0, (%r12)
setne %r9b
movq %r15, %rdi
callq 0x908a
jmp 0x8edd
leaq 0x2945(%rip), %rdi      # 0xb6b1
callq 0x4340
leaq 0x11(%rsp), %r15
pushq $0x7
popq %rdx
pushq $0x1
popq %rbp
movq %r14, %rdi
movq %r15, %rsi
movl %ebp, %ecx
callq 0x6e4a
movl 0x3(%r15), %eax
bswapl %eax
xorl %r12d, %r12d
testl %eax, %eax
cmovgl %eax, %r12d
leaq 0x10(%rsp), %r15
subl $0x1, %r12d
jb 0x8edd
movq %r14, %rdi
movq %r15, %rsi
movl %ebp, %edx
movl %ebp, %ecx
callq 0x6e4a
jmp 0x8d9d
leaq 0x290d(%rip), %rdi      # 0xb6cc
callq 0x4340
leaq 0x10(%rsp), %r15
pushq $0x3
popq %rdx
pushq $0x1
popq %rbp
movq %r14, %rdi
movq %r15, %rsi
movl %ebp, %ecx
callq 0x6e4a
movzwl 0x1(%r15), %eax
rolw $0x8, %ax
movzwl %ax, %r13d
pushq $0x4
popq %r12
subl $0x1, %r13d
jb 0x8edd
movq %r14, %rdi
movq %r15, %rsi
movl %r12d, %edx
movl %ebp, %ecx
callq 0x6e4a
movq %r15, %rsi
callq 0x8fc0
jmp 0x8ded
leaq 0x11(%rsp), %r12
pushq $0x7
popq %r15
pushq $0x1
popq %rbp
movq %r14, %rdi
movq %r12, %rsi
movl %r15d, %edx
movl %ebp, %ecx
callq 0x6e4a
movl 0x3(%r12), %eax
bswapl %eax
cmpb $0x0, (%r12)
je 0x8e87
cmpl $0x20, %eax
je 0x8ebb
cmpl $0xff54, %eax           # imm = 0xFF54
je 0x8ec3
cmpl $0xff0d, %eax           # imm = 0xFF0D
je 0x8ebf
cmpl $0xff1b, %eax           # imm = 0xFF1B
je 0x8edb
cmpl $0xff51, %eax           # imm = 0xFF51
je 0x8eb7
cmpl $0xff53, %eax           # imm = 0xFF53
je 0x8eb3
cmpl $0xff09, %eax           # imm = 0xFF09
jne 0x8edd
pushq $0x2
jmp 0x8eda
leaq 0x11(%rsp), %rsi
pushq $0x5
popq %rdx
pushq $0x1
popq %rcx
movq %r14, %rdi
callq 0x6e4a
jmp 0x8edd
cmpl $0x20, %eax
je 0x8ed8
cmpl $0xff09, %eax           # imm = 0xFF09
je 0x8ed4
cmpl $0xff54, %eax           # imm = 0xFF54
je 0x8ec7
cmpl $0xff51, %eax           # imm = 0xFF51
je 0x8ecf
cmpl $0xff53, %eax           # imm = 0xFF53
je 0x8ecb
cmpl $0xff0d, %eax           # imm = 0xFF0D
jne 0x8edd
pushq $0x5
jmp 0x8eda
pushq $0x8
jmp 0x8eda
pushq $0x6
jmp 0x8eda
pushq $0xe
jmp 0x8eda
pushq $0x4
jmp 0x8eda
pushq $0xa
jmp 0x8eda
pushq $0xb
jmp 0x8eda
pushq $0x9
jmp 0x8eda
movl %r15d, %ebp
jmp 0x8edb
pushq $0x3
jmp 0x8eda
pushq $0xf
popq %rbp
movl %ebp, %ebx
movl %ebx, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop