// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
addq $0x20, %rdi
callq 0x9fe0
testb $0x1, %al
jne 0xdeec
jmp 0xe379
movq -0xa8(%rbp), %rdi
movb $-0x1, -0x9d(%rbp)
addq $0x20, %rdi
leaq -0x9d(%rbp), %rsi
movl $0x1, %ecx
movl %ecx, %edx
callq 0x9d70
movzbl -0x9d(%rbp), %eax
movq %rax, -0xb0(%rbp)
subq $0x6, %rax
ja 0xe377
movq -0xb0(%rbp), %rax
leaq 0x2545(%rip), %rcx      # 0x1047c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x2561(%rip), %rdi      # 0x104a8
movb $0x0, %al
callq 0x4050
movq -0xa8(%rbp), %rdi
addq $0x20, %rdi
leaq -0x90(%rbp), %rsi
addq $0x1, %rsi
movl $0x13, %edx
movl $0x1, %ecx
callq 0x9d70
movq -0xa8(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0xe390
jmp 0xe377
leaq 0x2533(%rip), %rdi      # 0x104c5
movb $0x0, %al
callq 0x4050
movq -0xa8(%rbp), %rdi
addq $0x20, %rdi
leaq -0x90(%rbp), %rsi
movl $0x3, %edx
movl $0x1, %ecx
callq 0x9d70
movzbl -0x8f(%rbp), %eax
shll $0x8, %eax
movzbl -0x8e(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x9c(%rbp)
movl $0x0, -0x98(%rbp)
movl -0x98(%rbp), %eax
cmpl -0x9c(%rbp), %eax
jge 0xe030
movq -0xa8(%rbp), %rdi
addq $0x20, %rdi
leaq -0x90(%rbp), %rsi
movl $0x4, %edx
movl $0x1, %ecx
callq 0x9d70
movq -0xa8(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0xe4c0
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0xdfdd
jmp 0xe377
movq -0xa8(%rbp), %rdi
addq $0x20, %rdi
leaq -0x90(%rbp), %rsi
addq $0x1, %rsi
movl $0x9, %edx
movl $0x1, %ecx
callq 0x9d70
movq -0xa8(%rbp), %rdi
movzbl -0x8e(%rbp), %esi
shll $0x8, %esi
movzbl -0x8d(%rbp), %eax
orl %eax, %esi
movzbl -0x8c(%rbp), %edx
shll $0x8, %edx
movzbl -0x8b(%rbp), %eax
orl %eax, %edx
movzbl -0x8a(%rbp), %ecx
shll $0x8, %ecx
movzbl -0x89(%rbp), %eax
orl %eax, %ecx
movzbl -0x88(%rbp), %r8d
shll $0x8, %r8d
movzbl -0x87(%rbp), %eax
orl %eax, %r8d
cmpb $0x0, -0x8f(%rbp)
setne %al
movzbl %al, %r9d
andl $0x1, %r9d
callq 0xe6b0
jmp 0xe377
movq -0xa8(%rbp), %rdi
addq $0x20, %rdi
leaq -0x90(%rbp), %rsi
addq $0x1, %rsi
movl $0x7, %edx
movl $0x1, %ecx
callq 0x9d70
movzbl -0x8c(%rbp), %eax
shll $0x18, %eax
movzbl -0x8b(%rbp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movzbl -0x8a(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x89(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x94(%rbp)
movzbl -0x8f(%rbp), %eax
cmpl $0x0, %eax
je 0xe1f4
cmpl $0xff1b, -0x94(%rbp)    # imm = 0xFF1B
jne 0xe14a
movl $0x1, -0x4(%rbp)
jmp 0xe380
cmpl $0xff09, -0x94(%rbp)    # imm = 0xFF09
jne 0xe162
movl $0x2, -0x4(%rbp)
jmp 0xe380
cmpl $0xff0d, -0x94(%rbp)    # imm = 0xFF0D
jne 0xe17a
movl $0x4, -0x4(%rbp)
jmp 0xe380
cmpl $0xff51, -0x94(%rbp)    # imm = 0xFF51
jne 0xe192
movl $0x6, -0x4(%rbp)
jmp 0xe380
cmpl $0xff54, -0x94(%rbp)    # imm = 0xFF54
jne 0xe1aa
movl $0xa, -0x4(%rbp)
jmp 0xe380
cmpl $0xff53, -0x94(%rbp)    # imm = 0xFF53
jne 0xe1c2
movl $0x8, -0x4(%rbp)
jmp 0xe380
cmpl $0xff54, -0x94(%rbp)    # imm = 0xFF54
jne 0xe1da
movl $0xc, -0x4(%rbp)
jmp 0xe380
cmpl $0x20, -0x94(%rbp)
jne 0xe1ef
movl $0xe, -0x4(%rbp)
jmp 0xe380
jmp 0xe29b
cmpl $0xff09, -0x94(%rbp)    # imm = 0xFF09
jne 0xe20c
movl $0x3, -0x4(%rbp)
jmp 0xe380
cmpl $0xff0d, -0x94(%rbp)    # imm = 0xFF0D
jne 0xe224
movl $0x5, -0x4(%rbp)
jmp 0xe380
cmpl $0xff51, -0x94(%rbp)    # imm = 0xFF51
jne 0xe23c
movl $0x7, -0x4(%rbp)
jmp 0xe380
cmpl $0xff54, -0x94(%rbp)    # imm = 0xFF54
jne 0xe254
movl $0xb, -0x4(%rbp)
jmp 0xe380
cmpl $0xff53, -0x94(%rbp)    # imm = 0xFF53
jne 0xe26c
movl $0x9, -0x4(%rbp)
jmp 0xe380
cmpl $0xff54, -0x94(%rbp)    # imm = 0xFF54
jne 0xe284
movl $0xd, -0x4(%rbp)
jmp 0xe380
cmpl $0x20, -0x94(%rbp)
jne 0xe299
movl $0xf, -0x4(%rbp)
jmp 0xe380
jmp 0xe29b
jmp 0xe377
movq -0xa8(%rbp), %rdi
addq $0x20, %rdi
leaq -0x90(%rbp), %rsi
addq $0x1, %rsi
movl $0x5, %edx
movl $0x1, %ecx
callq 0x9d70
jmp 0xe377
leaq 0x220f(%rip), %rdi      # 0x104e0
movb $0x0, %al
callq 0x4050
movq -0xa8(%rbp), %rdi
addq $0x20, %rdi
leaq -0x90(%rbp), %rsi
addq $0x1, %rsi
movl $0x7, %edx
movl $0x1, %ecx
callq 0x9d70
movzbl -0x8c(%rbp), %eax
shll $0x18, %eax
movzbl -0x8b(%rbp), %ecx
shll $0x10, %ecx
orl %ecx, %eax
movzbl -0x8a(%rbp), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movzbl -0x89(%rbp), %ecx
orl %ecx, %eax
movl %eax, -0x9c(%rbp)
movl $0x0, -0x98(%rbp)
movl -0x98(%rbp), %eax
cmpl -0x9c(%rbp), %eax
jge 0xe375
movq -0xa8(%rbp), %rdi
addq $0x20, %rdi
leaq -0x90(%rbp), %rsi
movl $0x1, %ecx
movl %ecx, %edx
callq 0x9d70
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0xe338
jmp 0xe377
jmp 0xe379
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)