// File: jkingan[P]ladyheather/heather.cpp
// Function: get_x11_event()
int get_x11_event()
{
XEvent report;
KeySym key;
char buf[20];
int button;
int i;

   // This is the X11 event handler.

   if(display == 0) return 0;
   button = 0;

   if(!XEventsQueued(display, QueuedAlready)) {  // no events available to process
      return 0;
   }
   XNextEvent(display, &report);

   switch(report.type) {  // act on the event
      case Expose:
          if(report.xexpose.count != 0 ) break;
if(1 && show_debug_info) printf("Expose event seen\n");  // zork - show_debug_info
          need_redraw = 1000;
          return 1;
          break;


      case ConfigureNotify:
          /*  Store new window desired window width/height  and top/left  */
          new_width = report.xconfigure.width;
          new_height = report.xconfigure.height;
          x11_left = report.xconfigure.x;
          x11_top = report.xconfigure.y;
          if(x11_left < 0) x11_left = 0;
          if(x11_top < 0) x11_top = 0;
          need_resize = 1;
if(1 && show_debug_info) printf("configure:  wh%d %d  tl:%d %d  mouse:%d\n", new_width,new_height, x11_top,x11_left, this_button);  // zork - show_debug_info
          return 2;
          break;


      case KeyPress:
         buf[0] = 0;
         i = XLookupString(&report.xkey, &buf[0],20, &key, 0);
         if     (key ==  XK_Home)      add_kbd(HOME_CHAR);
         else if(key ==  XK_Up)        add_kbd(UP_CHAR);
         else if(key ==  XK_Page_Up)   add_kbd(PAGE_UP);
         else if(key ==  XK_Left)      add_kbd(LEFT_CHAR);
         else if(key ==  XK_Right)     add_kbd(RIGHT_CHAR);
         else if(key ==  XK_End)       add_kbd(END_CHAR);
         else if(key ==  XK_Down)      add_kbd(DOWN_CHAR);
         else if(key ==  XK_Page_Down) add_kbd(PAGE_DOWN);
         else if(key ==  XK_Insert)    add_kbd(INS_CHAR);
         else if(key ==  XK_Delete)    add_kbd(DEL_CHAR);
         else if(key ==  XK_BackSpace) add_kbd(0x08);
         else if(key ==  XK_Tab)       add_kbd(0x09);
         else if(key ==  XK_Linefeed)  add_kbd(0x0A);
         else if(key ==  XK_Clear)     add_kbd(0x0B);
         else if(key ==  XK_Return)    add_kbd(0x0D);
         else if(key ==  XK_Escape)    add_kbd(ESC_CHAR);
         else if(i && (key >= ' ') && (key < 0x7F)) add_kbd(buf[0]);
         else if(key ==  XK_Break)     break_flag = 1;
         else if(key ==  XK_F1)        add_kbd(F1_CHAR);
         else if(key ==  XK_F2)        add_kbd(F2_CHAR);
         else if(key ==  XK_F3)        add_kbd(F3_CHAR);
         else if(key ==  XK_F4)        add_kbd(F4_CHAR);
         else if(key ==  XK_F5)        add_kbd(F5_CHAR);
         else if(key ==  XK_F6)        add_kbd(F6_CHAR);
         else if(key ==  XK_F7)        add_kbd(F7_CHAR);
         else if(key ==  XK_F8)        add_kbd(F8_CHAR);
         else if(key ==  XK_F9)        add_kbd(F9_CHAR);
         else if(key ==  XK_F10)       add_kbd(F10_CHAR);
         else if(key ==  XK_F11) {  // piss
            f11_flag = 1;
            need_screen_init = 99999;
            not_safe = 1;
            config_options();

         }
         else if(key ==  XK_F12)         add_kbd(0);
         else if(key ==  XK_Help)        add_kbd(' ');

         else if(key ==  XK_KP_Space)    add_kbd(' ');      // keypad keys
         else if(key ==  XK_KP_Tab)      add_kbd(0x08);
         else if(key ==  XK_KP_Enter)    add_kbd(0x0D);
         else if(key ==  XK_KP_F1)       add_kbd(F1_CHAR);
         else if(key ==  XK_KP_F2)       add_kbd(F2_CHAR);
         else if(key ==  XK_KP_F3)       add_kbd(F3_CHAR);
         else if(key ==  XK_KP_F4)       add_kbd(F4_CHAR);
         else if(key ==  XK_KP_Equal)    add_kbd('=');
         else if(key ==  XK_KP_Multiply) add_kbd('*');
         else if(key ==  XK_KP_Add)      add_kbd('+');
         else if(key ==  XK_KP_Subtract) add_kbd('-');
         else if(key ==  XK_KP_Decimal)  add_kbd('.');
         else if(key ==  XK_KP_Divide)   add_kbd('/');
         else if(key ==  XK_KP_0)        add_kbd('0');
         else if(key ==  XK_KP_1)        add_kbd('1');
         else if(key ==  XK_KP_2)        add_kbd('2');
         else if(key ==  XK_KP_3)        add_kbd('3');
         else if(key ==  XK_KP_4)        add_kbd('4');
         else if(key ==  XK_KP_5)        add_kbd('5');
         else if(key ==  XK_KP_6)        add_kbd('6');
         else if(key ==  XK_KP_7)        add_kbd('7');
         else if(key ==  XK_KP_8)        add_kbd('8');
         else if(key ==  XK_KP_9)        add_kbd('9');

         return 3;
         break;


      case EnterNotify:
         my_mouse = 1;
         enter_mouse = report.xcrossing.subwindow;
         break;


      case LeaveNotify:
         my_mouse = 0;
         leave_mouse = report.xcrossing.subwindow;
         break;


//    case MotionNotify:
//       mouse_x = report.xmotion.x_root;
//       mouse_y = report.xmotion.y_root;
//       mouse_y -= 48;
//       x11_mouse = 1;
//       return 4;
//       break;

#ifdef PRESLEY       // we now use XQueryPointer in get_mouse_info to do this
      case ButtonPress:            /*  Fall through  */
          button = (int) report.xbutton.button;
          if(button == 1) {
             last_button = this_button;
             this_button = 1;
          }
          else if(button == 3) {
             last_button = this_button;
             this_button = 2;
          }
          else {
             last_button = this_button;
             this_button = 0;
          }
if(0 && show_debug_info) printf("Button press: this:%d  last:%d!\n", this_button, last_button);
          return 5;
          break;

       case ButtonRelease:
if(0 && show_debug_info) printf("Button release\n");
//        last_button = this_button;
//        this_button = 0;
          return 6;
          break;
#endif


    case ClientMessage:
if(0 && show_debug_info) printf("Client message:%ld\n", report.xclient.data.l[0]); //zork - show_debug_info
        if((unsigned long) report.xclient.data.l[0] == (unsigned long) wmDeleteMessage) {
           shut_down(0);
        }
        return 6;
        break;
   }

   return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
cmpq $0x0, 0x1c0c3d(%rip)    # 0x1d00a0
jne 0xf471
movl $0x0, -0x4(%rbp)
jmp 0xfcb6
movl $0x0, -0xf4(%rbp)
movq 0x1c0c1e(%rip), %rdi    # 0x1d00a0
xorl %esi, %esi
callq 0x93d0
cmpl $0x0, %eax
jne 0xf49a
movl $0x0, -0x4(%rbp)
jmp 0xfcb6
movq 0x1c0bff(%rip), %rdi    # 0x1d00a0
leaq -0xc8(%rbp), %rsi
callq 0x9540
movl -0xc8(%rbp), %eax
addl $-0x2, %eax
movl %eax, %ecx
movq %rcx, -0x100(%rbp)
subl $0x1f, %eax
ja 0xfcaf
movq -0x100(%rbp), %rax
leaq 0x15fdca(%rip), %rcx    # 0x16f2a0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
cmpl $0x0, -0x90(%rbp)
je 0xf4ed
jmp 0xfcaf
cmpb $0x0, 0x2685b0(%rip)    # 0x277aa4
je 0xf504
leaq 0x160945(%rip), %rdi    # 0x16fe42
movb $0x0, %al
callq 0x9030
movl $0x3e8, 0x268516(%rip)  # imm = 0x3E8
movl $0x1, -0x4(%rbp)
jmp 0xfcb6
movl -0x90(%rbp), %eax
movl %eax, 0x26850a(%rip)    # 0x277a30
movl -0x8c(%rbp), %eax
movl %eax, 0x268502(%rip)    # 0x277a34
movl -0x98(%rbp), %eax
movl %eax, 0x1c0b9a(%rip)    # 0x1d00d8
movl -0x94(%rbp), %eax
movl %eax, 0x1c0b92(%rip)    # 0x1d00dc
cmpl $0x0, 0x1c0b87(%rip)    # 0x1d00d8
jge 0xf55d
movl $0x0, 0x1c0b7b(%rip)    # 0x1d00d8
cmpl $0x0, 0x1c0b78(%rip)    # 0x1d00dc
jge 0xf570
movl $0x0, 0x1c0b6c(%rip)    # 0x1d00dc
movl $0x1, 0x2684b2(%rip)    # 0x277a2c
cmpb $0x0, 0x268523(%rip)    # 0x277aa4
je 0xf5b1
movl 0x2684a7(%rip), %esi    # 0x277a30
movl 0x2684a5(%rip), %edx    # 0x277a34
movl 0x1c0b47(%rip), %ecx    # 0x1d00dc
movl 0x1c0b3c(%rip), %r8d    # 0x1d00d8
movl 0x26ae99(%rip), %r9d    # 0x27a43c
leaq 0x1608ab(%rip), %rdi    # 0x16fe55
movb $0x0, %al
callq 0x9030
movl $0x2, -0x4(%rbp)
jmp 0xfcb6
movb $0x0, -0xf0(%rbp)
leaq -0xf0(%rbp), %rsi
leaq -0xc8(%rbp), %rdi
movl $0x14, %edx
leaq -0xd0(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0x9320
movl %eax, -0xf8(%rbp)
cmpq $0xff50, -0xd0(%rbp) # imm = 0xFF50
jne 0xf60a
movl $0x147, %edi            # imm = 0x147
callq 0xf320
jmp 0xfc54
cmpq $0xff52, -0xd0(%rbp) # imm = 0xFF52
jne 0xf626
movl $0x148, %edi            # imm = 0x148
callq 0xf320
jmp 0xfc52
cmpq $0xff55, -0xd0(%rbp) # imm = 0xFF55
jne 0xf642
movl $0x149, %edi            # imm = 0x149
callq 0xf320
jmp 0xfc50
cmpq $0xff51, -0xd0(%rbp) # imm = 0xFF51
jne 0xf65e
movl $0x14b, %edi            # imm = 0x14B
callq 0xf320
jmp 0xfc4e
cmpq $0xff53, -0xd0(%rbp) # imm = 0xFF53
jne 0xf67a
movl $0x14d, %edi            # imm = 0x14D
callq 0xf320
jmp 0xfc4c
cmpq $0xff57, -0xd0(%rbp) # imm = 0xFF57
jne 0xf696
movl $0x14f, %edi            # imm = 0x14F
callq 0xf320
jmp 0xfc4a
cmpq $0xff54, -0xd0(%rbp) # imm = 0xFF54
jne 0xf6b2
movl $0x150, %edi            # imm = 0x150
callq 0xf320
jmp 0xfc48
cmpq $0xff56, -0xd0(%rbp) # imm = 0xFF56
jne 0xf6ce
movl $0x151, %edi            # imm = 0x151
callq 0xf320
jmp 0xfc46
cmpq $0xff63, -0xd0(%rbp) # imm = 0xFF63
jne 0xf6ea
movl $0x152, %edi            # imm = 0x152
callq 0xf320
jmp 0xfc44
cmpq $0xffff, -0xd0(%rbp) # imm = 0xFFFF
jne 0xf706
movl $0x153, %edi            # imm = 0x153
callq 0xf320
jmp 0xfc42
cmpq $0xff08, -0xd0(%rbp) # imm = 0xFF08
jne 0xf722
movl $0x8, %edi
callq 0xf320
jmp 0xfc40
cmpq $0xff09, -0xd0(%rbp) # imm = 0xFF09
jne 0xf73e
movl $0x9, %edi
callq 0xf320
jmp 0xfc3e
cmpq $0xff0a, -0xd0(%rbp) # imm = 0xFF0A
jne 0xf75a
movl $0xa, %edi
callq 0xf320
jmp 0xfc3c
cmpq $0xff0b, -0xd0(%rbp) # imm = 0xFF0B
jne 0xf776
movl $0xb, %edi
callq 0xf320
jmp 0xfc3a
cmpq $0xff0d, -0xd0(%rbp) # imm = 0xFF0D
jne 0xf792
movl $0xd, %edi
callq 0xf320
jmp 0xfc38
cmpq $0xff1b, -0xd0(%rbp) # imm = 0xFF1B
jne 0xf7ae
movl $0x1b, %edi
callq 0xf320
jmp 0xfc36
cmpl $0x0, -0xf8(%rbp)
je 0xf7dc
cmpq $0x20, -0xd0(%rbp)
jb 0xf7dc
cmpq $0x7f, -0xd0(%rbp)
jae 0xf7dc
movsbl -0xf0(%rbp), %edi
callq 0xf320
jmp 0xfc34
cmpq $0xff6b, -0xd0(%rbp) # imm = 0xFF6B
jne 0xf7f8
movl $0x1, 0x386a11(%rip)    # 0x396204
jmp 0xfc32
cmpq $0xffbe, -0xd0(%rbp) # imm = 0xFFBE
jne 0xf814
movl $0x13b, %edi            # imm = 0x13B
callq 0xf320
jmp 0xfc30
cmpq $0xffbf, -0xd0(%rbp) # imm = 0xFFBF
jne 0xf830
movl $0x13c, %edi            # imm = 0x13C
callq 0xf320
jmp 0xfc2e
cmpq $0xffc0, -0xd0(%rbp) # imm = 0xFFC0
jne 0xf84c
movl $0x13d, %edi            # imm = 0x13D
callq 0xf320
jmp 0xfc2c
cmpq $0xffc1, -0xd0(%rbp) # imm = 0xFFC1
jne 0xf868
movl $0x13e, %edi            # imm = 0x13E
callq 0xf320
jmp 0xfc2a
cmpq $0xffc2, -0xd0(%rbp) # imm = 0xFFC2
jne 0xf884
movl $0x13f, %edi            # imm = 0x13F
callq 0xf320
jmp 0xfc28
cmpq $0xffc3, -0xd0(%rbp) # imm = 0xFFC3
jne 0xf8a0
movl $0x140, %edi            # imm = 0x140
callq 0xf320
jmp 0xfc26
cmpq $0xffc4, -0xd0(%rbp) # imm = 0xFFC4
jne 0xf8bc
movl $0x141, %edi            # imm = 0x141
callq 0xf320
jmp 0xfc24
cmpq $0xffc5, -0xd0(%rbp) # imm = 0xFFC5
jne 0xf8d8
movl $0x142, %edi            # imm = 0x142
callq 0xf320
jmp 0xfc22
cmpq $0xffc6, -0xd0(%rbp) # imm = 0xFFC6
jne 0xf8f4
movl $0x143, %edi            # imm = 0x143
callq 0xf320
jmp 0xfc20
cmpq $0xffc7, -0xd0(%rbp) # imm = 0xFFC7
jne 0xf910
movl $0x144, %edi            # imm = 0x144
callq 0xf320
jmp 0xfc1e
cmpq $0xffc8, -0xd0(%rbp) # imm = 0xFFC8
jne 0xf942
movl $0x1, 0x38f7b9(%rip)    # 0x39f0e0
movl $0x1869f, 0x268097(%rip) # imm = 0x1869F
movb $0x1, 0x35d715(%rip)    # 0x36d04d
callq 0xfcd0
jmp 0xfc1c
cmpq $0xffc9, -0xd0(%rbp) # imm = 0xFFC9
jne 0xf95b
xorl %edi, %edi
callq 0xf320
jmp 0xfc1a
cmpq $0xff6a, -0xd0(%rbp) # imm = 0xFF6A
jne 0xf977
movl $0x20, %edi
callq 0xf320
jmp 0xfc18
cmpq $0xff80, -0xd0(%rbp) # imm = 0xFF80
jne 0xf993
movl $0x20, %edi
callq 0xf320
jmp 0xfc16
cmpq $0xff89, -0xd0(%rbp) # imm = 0xFF89
jne 0xf9af
movl $0x8, %edi
callq 0xf320
jmp 0xfc14
cmpq $0xff8d, -0xd0(%rbp) # imm = 0xFF8D
jne 0xf9cb
movl $0xd, %edi
callq 0xf320
jmp 0xfc12
cmpq $0xff91, -0xd0(%rbp) # imm = 0xFF91
jne 0xf9e7
movl $0x13b, %edi            # imm = 0x13B
callq 0xf320
jmp 0xfc10
cmpq $0xff92, -0xd0(%rbp) # imm = 0xFF92
jne 0xfa03
movl $0x13c, %edi            # imm = 0x13C
callq 0xf320
jmp 0xfc0e
cmpq $0xff93, -0xd0(%rbp) # imm = 0xFF93
jne 0xfa1f
movl $0x13d, %edi            # imm = 0x13D
callq 0xf320
jmp 0xfc0c
cmpq $0xff94, -0xd0(%rbp) # imm = 0xFF94
jne 0xfa3b
movl $0x13e, %edi            # imm = 0x13E
callq 0xf320
jmp 0xfc0a
cmpq $0xffbd, -0xd0(%rbp) # imm = 0xFFBD
jne 0xfa57
movl $0x3d, %edi
callq 0xf320
jmp 0xfc08
cmpq $0xffaa, -0xd0(%rbp) # imm = 0xFFAA
jne 0xfa73
movl $0x2a, %edi
callq 0xf320
jmp 0xfc06
cmpq $0xffab, -0xd0(%rbp) # imm = 0xFFAB
jne 0xfa8f
movl $0x2b, %edi
callq 0xf320
jmp 0xfc04
cmpq $0xffad, -0xd0(%rbp) # imm = 0xFFAD
jne 0xfaab
movl $0x2d, %edi
callq 0xf320
jmp 0xfc02
cmpq $0xffae, -0xd0(%rbp) # imm = 0xFFAE
jne 0xfac7
movl $0x2e, %edi
callq 0xf320
jmp 0xfc00
cmpq $0xffaf, -0xd0(%rbp) # imm = 0xFFAF
jne 0xfae3
movl $0x2f, %edi
callq 0xf320
jmp 0xfbfe
cmpq $0xffb0, -0xd0(%rbp) # imm = 0xFFB0
jne 0xfaff
movl $0x30, %edi
callq 0xf320
jmp 0xfbfc
cmpq $0xffb1, -0xd0(%rbp) # imm = 0xFFB1
jne 0xfb1b
movl $0x31, %edi
callq 0xf320
jmp 0xfbfa
cmpq $0xffb2, -0xd0(%rbp) # imm = 0xFFB2
jne 0xfb37
movl $0x32, %edi
callq 0xf320
jmp 0xfbf8
cmpq $0xffb3, -0xd0(%rbp) # imm = 0xFFB3
jne 0xfb53
movl $0x33, %edi
callq 0xf320
jmp 0xfbf6
cmpq $0xffb4, -0xd0(%rbp) # imm = 0xFFB4
jne 0xfb6f
movl $0x34, %edi
callq 0xf320
jmp 0xfbf4
cmpq $0xffb5, -0xd0(%rbp) # imm = 0xFFB5
jne 0xfb88
movl $0x35, %edi
callq 0xf320
jmp 0xfbf2
cmpq $0xffb6, -0xd0(%rbp) # imm = 0xFFB6
jne 0xfba1
movl $0x36, %edi
callq 0xf320
jmp 0xfbf0
cmpq $0xffb7, -0xd0(%rbp) # imm = 0xFFB7
jne 0xfbba
movl $0x37, %edi
callq 0xf320
jmp 0xfbee
cmpq $0xffb8, -0xd0(%rbp) # imm = 0xFFB8
jne 0xfbd3
movl $0x38, %edi
callq 0xf320
jmp 0xfbec
cmpq $0xffb9, -0xd0(%rbp) # imm = 0xFFB9
jne 0xfbea
movl $0x39, %edi
callq 0xf320
jmp 0xfbec
jmp 0xfbee
jmp 0xfbf0
jmp 0xfbf2
jmp 0xfbf4
jmp 0xfbf6
jmp 0xfbf8
jmp 0xfbfa
jmp 0xfbfc
jmp 0xfbfe
jmp 0xfc00
jmp 0xfc02
jmp 0xfc04
jmp 0xfc06
jmp 0xfc08
jmp 0xfc0a
jmp 0xfc0c
jmp 0xfc0e
jmp 0xfc10
jmp 0xfc12
jmp 0xfc14
jmp 0xfc16
jmp 0xfc18
jmp 0xfc1a
jmp 0xfc1c
jmp 0xfc1e
jmp 0xfc20
jmp 0xfc22
jmp 0xfc24
jmp 0xfc26
jmp 0xfc28
jmp 0xfc2a
jmp 0xfc2c
jmp 0xfc2e
jmp 0xfc30
jmp 0xfc32
jmp 0xfc34
jmp 0xfc36
jmp 0xfc38
jmp 0xfc3a
jmp 0xfc3c
jmp 0xfc3e
jmp 0xfc40
jmp 0xfc42
jmp 0xfc44
jmp 0xfc46
jmp 0xfc48
jmp 0xfc4a
jmp 0xfc4c
jmp 0xfc4e
jmp 0xfc50
jmp 0xfc52
jmp 0xfc54
movl $0x3, -0x4(%rbp)
jmp 0xfcb6
movl $0x1, 0x38f46d(%rip)    # 0x39f0d4
movq -0x98(%rbp), %rax
movl %eax, 0x38f5b8(%rip)    # 0x39f22c
jmp 0xfcaf
movl $0x0, 0x38f454(%rip)    # 0x39f0d4
movq -0x98(%rbp), %rax
movl %eax, 0x38f5a3(%rip)    # 0x39f230
jmp 0xfcaf
movq -0x90(%rbp), %rax
cmpq 0x38f53b(%rip), %rax    # 0x39f1d8
jne 0xfca6
xorl %edi, %edi
callq 0x100f0
movl $0x6, -0x4(%rbp)
jmp 0xfcb6
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopw %cs:(%rax,%rax)