// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r14
pushq %rbx
subq $0xe0, %rsp
movq 0x163901(%rip), %rdi    # 0x170ec0
xorl %ebx, %ebx
testq %rdi, %rdi
je 0xd859
xorl %esi, %esi
callq 0x93c0
testl %eax, %eax
je 0xd859
movq 0x1638e0(%rip), %rdi    # 0x170ec0
leaq -0xf0(%rbp), %r14
movq %r14, %rsi
callq 0x9550
movl (%r14), %eax
cmpl $0xb, %eax
jg 0xd629
cmpl $0x2, %eax
je 0xd67a
cmpl $0x7, %eax
je 0xd6f7
cmpl $0x8, %eax
jne 0xd859
movl $0x0, 0x3328dc(%rip)    # 0x33fef4
movl -0xc0(%rbp), %eax
movl %eax, 0x332a2c(%rip)    # 0x340050
jmp 0xd859
cmpl $0x21, %eax
je 0xd6d7
cmpl $0x16, %eax
je 0xd712
cmpl $0xc, %eax
jne 0xd859
cmpl $0x0, -0xb8(%rbp)
jne 0xd859
cmpb $0x0, 0x20b26c(%rip)    # 0x2188c4
je 0xd666
leaq 0xfe194(%rip), %rdi     # 0x10b7f5
callq 0x95d0
movl $0x3e8, 0x20b1d4(%rip)  # imm = 0x3E8
movl $0x1, %ebx
jmp 0xd859
leaq -0x30(%rbp), %rsi
movb $0x0, (%rsi)
leaq -0xf0(%rbp), %rdi
leaq -0x18(%rbp), %rbx
movl $0x14, %edx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x9310
movq (%rbx), %rcx
cmpq $0xff4f, %rcx           # imm = 0xFF4F
jle 0xd79c
leaq -0xff50(%rcx), %rdx
cmpq $0x13, %rdx
ja 0xd7d3
leaq 0xfbb4c(%rip), %rsi     # 0x109210
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
movl $0x147, %edi            # imm = 0x147
jmp 0xd84f
movq -0xb8(%rbp), %rax
movl $0x6, %ebx
cmpq 0x33290e(%rip), %rax    # 0x33fff8
jne 0xd859
xorl %edi, %edi
callq 0xdcff
movl $0x1, 0x3327f3(%rip)    # 0x33fef4
movl -0xc0(%rbp), %eax
movl %eax, 0x33293f(%rip)    # 0x34004c
jmp 0xd859
movl -0xb8(%rbp), %esi
movl %esi, 0x20b132(%rip)    # 0x218850
movl -0xb4(%rbp), %edx
movl %edx, 0x20b12a(%rip)    # 0x218854
movl -0xc0(%rbp), %r8d
movl -0xbc(%rbp), %ecx
movl %r8d, 0x1637ba(%rip)    # 0x170ef8
movl %ecx, 0x1637b8(%rip)    # 0x170efc
testl %r8d, %r8d
jns 0xd756
movl $0x0, 0x1637a5(%rip)    # 0x170ef8
xorl %r8d, %r8d
testl %ecx, %ecx
jns 0xd766
movl $0x0, 0x163798(%rip)    # 0x170efc
xorl %ecx, %ecx
movl $0x1, 0x20b0dc(%rip)    # 0x21884c
movl $0x2, %ebx
cmpb $0x0, 0x20b148(%rip)    # 0x2188c4
je 0xd859
movl 0x20dad3(%rip), %r9d    # 0x21b25c
leaq 0xfc720(%rip), %rdi     # 0x109eb0
xorl %eax, %eax
callq 0x9030
jmp 0xd859
leaq -0xff08(%rcx), %rdx
cmpq $0x5, %rdx
ja 0xd7c3
leaq 0xfba48(%rip), %rsi     # 0x1091f8
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
movl $0x8, %edi
jmp 0xd84f
cmpq $0xff1b, %rcx           # imm = 0xFF1B
jne 0xd7e3
movl $0x1b, %edi
jmp 0xd84f
cmpq $0xffff, %rcx           # imm = 0xFFFF
jne 0xd7e3
movl $0x153, %edi            # imm = 0x153
jmp 0xd84f
testl %eax, %eax
sete %al
leaq -0x20(%rcx), %rdx
cmpq $0x5f, %rdx
setae %dl
orb %al, %dl
jne 0xd867
movsbl -0x30(%rbp), %edi
jmp 0xd84f
movl $0x9, %edi
jmp 0xd84f
movl $0xb, %edi
jmp 0xd84f
movl $0xa, %edi
jmp 0xd84f
movl $0xd, %edi
jmp 0xd84f
movl $0x14f, %edi            # imm = 0x14F
jmp 0xd84f
movl $0x152, %edi            # imm = 0x152
jmp 0xd84f
movl $0x149, %edi            # imm = 0x149
jmp 0xd84f
movl $0x14b, %edi            # imm = 0x14B
jmp 0xd84f
movl $0x151, %edi            # imm = 0x151
jmp 0xd84f
movl $0x148, %edi            # imm = 0x148
jmp 0xd84f
movl $0x14d, %edi            # imm = 0x14D
jmp 0xd84f
movl $0x150, %edi            # imm = 0x150
callq 0xd4c8
movl $0x3, %ebx
movl %ebx, %eax
addq $0xe0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
movl $0x3, %ebx
leaq -0xff80(%rcx), %rax
cmpq $0x49, %rax
ja 0xd893
leaq 0xfb9e0(%rip), %rcx     # 0x109260
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x13b, %edi            # imm = 0x13B
jmp 0xd9ba
cmpq $0xff6a, %rcx           # imm = 0xFF6A
je 0xd8cf
cmpq $0xff6b, %rcx           # imm = 0xFF6B
jne 0xd859
movl $0x1, 0x329775(%rip)    # 0x337024
jmp 0xd859
movl $0x13c, %edi            # imm = 0x13C
jmp 0xd9ba
movl $0x13d, %edi            # imm = 0x13D
jmp 0xd9ba
movl $0x13e, %edi            # imm = 0x13E
jmp 0xd9ba
movl $0x20, %edi
jmp 0xd9ba
movl $0x144, %edi            # imm = 0x144
jmp 0xd9ba
movl $0x39, %edi
jmp 0xd9ba
movl $0x38, %edi
jmp 0xd9ba
movl $0x1, 0x3325ff(%rip)    # 0x33ff00
movl $0x1869f, 0x20aedd(%rip) # imm = 0x1869F
movb $0x1, 0x30055b(%rip)    # 0x30de6d
callq 0xd9c4
jmp 0xd859
movl $0x142, %edi            # imm = 0x142
jmp 0xd9ba
xorl %edi, %edi
jmp 0xd9ba
movl $0x36, %edi
jmp 0xd9ba
movl $0xd, %edi
jmp 0xd9ba
movl $0x8, %edi
jmp 0xd9ba
movl $0x2b, %edi
jmp 0xd9ba
movl $0x32, %edi
jmp 0xd9ba
movl $0x2a, %edi
jmp 0xd9ba
movl $0x34, %edi
jmp 0xd9ba
movl $0x2d, %edi
jmp 0xd9ba
movl $0x2e, %edi
jmp 0xd9ba
movl $0x2f, %edi
jmp 0xd9ba
movl $0x31, %edi
jmp 0xd9ba
movl $0x33, %edi
jmp 0xd9ba
movl $0x30, %edi
jmp 0xd9ba
movl $0x37, %edi
jmp 0xd9ba
movl $0x3d, %edi
jmp 0xd9ba
movl $0x35, %edi
jmp 0xd9ba
movl $0x13f, %edi            # imm = 0x13F
jmp 0xd9ba
movl $0x140, %edi            # imm = 0x140
jmp 0xd9ba
movl $0x141, %edi            # imm = 0x141
jmp 0xd9ba
movl $0x143, %edi            # imm = 0x143
callq 0xd4c8
jmp 0xd859