// File: blak3mill3r[P]mouser/src/mouser.cpp
// Function: Mouser::xevent_handler()
void Mouser::xevent_handler() {
  XEvent ev;
  while (_running && channel_op_status::success == _xevent_channel.pop(ev) ) {
    switch(ev.type) {
    case KeyPress:
    case KeyRelease:
      bool is_down = (KeyPress == ev.type);
      char *s;
      unsigned int kc;

      kc = ((XKeyPressedEvent*)&ev)->keycode;
      s = XKeysymToString(XKeycodeToKeysym(_d, kc, 0));

      //if(s) printf("KEY: %s\n", s);
      if(!strcmp(s, "q")) { stop(); break; }

      // probably cleaner to make KeyboardActions out of the button presses too...
      if(!strcmp(s, "f"))          _xtest_action_channel.push(CmdButton(1,   is_down));
      if(!strcmp(s, "d"))          _xtest_action_channel.push(CmdButton(2,   is_down));
      if(!strcmp(s, "s"))          _xtest_action_channel.push(CmdButton(3,   is_down));
      if(!strcmp(s, "h"))          _keyboard_action_channel.push(KeyboardAction(k_left,    is_down));
      if(!strcmp(s, "l"))          _keyboard_action_channel.push(KeyboardAction(k_right,   is_down));
      if(!strcmp(s, "k"))          _keyboard_action_channel.push(KeyboardAction(k_up,      is_down));
      if(!strcmp(s, "j"))          _keyboard_action_channel.push(KeyboardAction(k_down,    is_down));
      if(!strcmp(s, "a"))          _keyboard_action_channel.push(KeyboardAction(k_brakes,  is_down));
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1f0, %rsp            # imm = 0x1F0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rcx
xorl %eax, %eax
testb $0x1, 0x12c(%rcx)
movb %al, -0x169(%rbp)
je 0xbfd8
movq -0x168(%rbp), %rdi
addq $0x8, %rdi
leaq -0xc8(%rbp), %rsi
callq 0xdbe0
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
sete %al
movb %al, -0x169(%rbp)
movb -0x169(%rbp), %al
testb $0x1, %al
jne 0xbfe7
jmp 0xc468
movl -0xc8(%rbp), %eax
addl $-0x2, %eax
subl $0x1, %eax
ja 0xc463
jmp 0xbffb
movq -0x168(%rbp), %rax
movl $0x2, %ecx
cmpl -0xc8(%rbp), %ecx
sete %cl
andb $0x1, %cl
movb %cl, -0xc9(%rbp)
movl -0x74(%rbp), %ecx
movl %ecx, -0xdc(%rbp)
movq 0x120(%rax), %rdi
movl -0xdc(%rbp), %eax
xorl %edx, %edx
movzbl %al, %esi
callq 0x4130
movq %rax, %rdi
callq 0x4510
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rdi
leaq 0xea9c(%rip), %rsi      # 0x1aaf2
callq 0x43f0
cmpl $0x0, %eax
jne 0xc071
movq -0x168(%rbp), %rdi
callq 0xc480
jmp 0xc463
movq -0xd8(%rbp), %rdi
leaq 0xea75(%rip), %rsi      # 0x1aaf4
callq 0x43f0
cmpl $0x0, %eax
jne 0xc12f
movq -0x168(%rbp), %rax
addq $0xd0, %rax
movq %rax, -0x180(%rbp)
movzbl -0xc9(%rbp), %edx
andl $0x1, %edx
leaq -0xf8(%rbp), %rdi
movq %rdi, -0x190(%rbp)
movl $0x1, %esi
movl %esi, -0x184(%rbp)
callq 0xeec0
movq -0x190(%rbp), %rsi
movl -0x184(%rbp), %edx
leaq -0xf0(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0xeef0
movq -0x180(%rbp), %rdi
movq -0x178(%rbp), %rsi
callq 0xe590
jmp 0xc0fe
leaq -0xf0(%rbp), %rdi
callq 0xef60
jmp 0xc12f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0xef60
jmp 0xc471
movq -0xd8(%rbp), %rdi
leaq 0xe9eb(%rip), %rsi      # 0x1ab28
callq 0x43f0
cmpl $0x0, %eax
jne 0xc1e6
movq -0x168(%rbp), %rax
addq $0xd0, %rax
movq %rax, -0x1a0(%rbp)
movzbl -0xc9(%rbp), %edx
andl $0x1, %edx
leaq -0x120(%rbp), %rdi
movq %rdi, -0x1a8(%rbp)
movl $0x2, %esi
callq 0xeec0
movq -0x1a8(%rbp), %rsi
leaq -0x118(%rbp), %rdi
movq %rdi, -0x198(%rbp)
movl $0x1, %edx
callq 0xeef0
movq -0x1a0(%rbp), %rdi
movq -0x198(%rbp), %rsi
callq 0xe590
jmp 0xc1b5
leaq -0x118(%rbp), %rdi
callq 0xef60
jmp 0xc1e6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
leaq -0x118(%rbp), %rdi
callq 0xef60
jmp 0xc471
movq -0xd8(%rbp), %rdi
leaq 0xe902(%rip), %rsi      # 0x1aaf6
callq 0x43f0
cmpl $0x0, %eax
jne 0xc29d
movq -0x168(%rbp), %rax
addq $0xd0, %rax
movq %rax, -0x1b8(%rbp)
movzbl -0xc9(%rbp), %edx
andl $0x1, %edx
leaq -0x138(%rbp), %rdi
movq %rdi, -0x1c0(%rbp)
movl $0x3, %esi
callq 0xeec0
movq -0x1c0(%rbp), %rsi
leaq -0x130(%rbp), %rdi
movq %rdi, -0x1b0(%rbp)
movl $0x1, %edx
callq 0xeef0
movq -0x1b8(%rbp), %rdi
movq -0x1b0(%rbp), %rsi
callq 0xe590
jmp 0xc26c
leaq -0x130(%rbp), %rdi
callq 0xef60
jmp 0xc29d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
leaq -0x130(%rbp), %rdi
callq 0xef60
jmp 0xc471
movq -0xd8(%rbp), %rdi
leaq 0xe84d(%rip), %rsi      # 0x1aaf8
callq 0x43f0
cmpl $0x0, %eax
jne 0xc2f8
movq -0x168(%rbp), %rax
addq $0x80, %rax
movq %rax, -0x1c8(%rbp)
movb -0xc9(%rbp), %al
leaq -0x140(%rbp), %rdi
movl $0x2, %esi
andb $0x1, %al
movzbl %al, %edx
callq 0xf8b0
movq -0x1c8(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0xef90
movq -0xd8(%rbp), %rdi
leaq 0xe7f4(%rip), %rsi      # 0x1aafa
callq 0x43f0
cmpl $0x0, %eax
jne 0xc353
movq -0x168(%rbp), %rax
addq $0x80, %rax
movq %rax, -0x1d0(%rbp)
movb -0xc9(%rbp), %al
leaq -0x148(%rbp), %rdi
movl $0x3, %esi
andb $0x1, %al
movzbl %al, %edx
callq 0xf8b0
movq -0x1d0(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0xef90
movq -0xd8(%rbp), %rdi
leaq 0xe79b(%rip), %rsi      # 0x1aafc
callq 0x43f0
cmpl $0x0, %eax
jne 0xc3ab
movq -0x168(%rbp), %rax
addq $0x80, %rax
movq %rax, -0x1d8(%rbp)
movb -0xc9(%rbp), %al
leaq -0x150(%rbp), %rdi
xorl %esi, %esi
andb $0x1, %al
movzbl %al, %edx
callq 0xf8b0
movq -0x1d8(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0xef90
movq -0xd8(%rbp), %rdi
leaq 0xe745(%rip), %rsi      # 0x1aafe
callq 0x43f0
cmpl $0x0, %eax
jne 0xc406
movq -0x168(%rbp), %rax
addq $0x80, %rax
movq %rax, -0x1e0(%rbp)
movb -0xc9(%rbp), %al
leaq -0x158(%rbp), %rdi
movl $0x1, %esi
andb $0x1, %al
movzbl %al, %edx
callq 0xf8b0
movq -0x1e0(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0xef90
movq -0xd8(%rbp), %rdi
leaq 0xe6ec(%rip), %rsi      # 0x1ab00
callq 0x43f0
cmpl $0x0, %eax
jne 0xc461
movq -0x168(%rbp), %rax
addq $0x80, %rax
movq %rax, -0x1e8(%rbp)
movb -0xc9(%rbp), %al
leaq -0x160(%rbp), %rdi
movl $0x7, %esi
andb $0x1, %al
movzbl %al, %edx
callq 0xf8b0
movq -0x1e8(%rbp), %rdi
leaq -0x160(%rbp), %rsi
callq 0xef90
jmp 0xc463
jmp 0xbf9a
addq $0x1f0, %rsp            # imm = 0x1F0
popq %rbp
retq
movq -0x100(%rbp), %rdi
callq 0x44d0
nopl (%rax)