// File: /workspace/github_cpp_makefile_star2_O2/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 0x14dca2(%rip), %rdi    # 0x15aec0
xorl %ebx, %ebx
testq %rdi, %rdi
je 0xd49e
xorl %esi, %esi
callq 0x93c0
testl %eax, %eax
je 0xd49e
movq 0x14dc81(%rip), %rdi    # 0x15aec0
leaq -0xf0(%rbp), %r14
movq %r14, %rsi
callq 0x9550
movl (%r14), %eax
cmpl $0x2, %eax
je 0xd383
cmpl $0x7, %eax
je 0xd368
cmpl $0x8, %eax
je 0xd2b2
cmpl $0x21, %eax
je 0xd34c
cmpl $0x16, %eax
je 0xd2ca
cmpl $0xc, %eax
jne 0xd49e
cmpl $0x0, -0xb8(%rbp)
jne 0xd49e
cmpb $0x0, 0x1f5631(%rip)    # 0x2028c4
je 0xd2a1
leaq 0xe85d4(%rip), %rdi     # 0xf5870
callq 0x95d0
movl $0x3e8, 0x1f5599(%rip)  # imm = 0x3E8
pushq $0x1
jmp 0xd362
andl $0x0, 0x31cc3b(%rip)    # 0x329ef4
movl -0xc0(%rbp), %eax
movl %eax, 0x31cd8b(%rip)    # 0x32a050
jmp 0xd49e
movl -0xb8(%rbp), %esi
movl %esi, 0x1f557a(%rip)    # 0x202850
movl -0xb4(%rbp), %edx
movl %edx, 0x1f5572(%rip)    # 0x202854
movl -0xc0(%rbp), %r8d
movl -0xbc(%rbp), %ecx
movl %r8d, 0x14dc02(%rip)    # 0x15aef8
movl %ecx, 0x14dc00(%rip)    # 0x15aefc
testl %r8d, %r8d
jns 0xd30b
andl $0x0, 0x14dbf0(%rip)    # 0x15aef8
xorl %r8d, %r8d
testl %ecx, %ecx
jns 0xd318
andl $0x0, 0x14dbe6(%rip)    # 0x15aefc
xorl %ecx, %ecx
movl $0x1, 0x1f552a(%rip)    # 0x20284c
pushq $0x2
popq %rbx
cmpb $0x0, 0x1f5598(%rip)    # 0x2028c4
je 0xd49e
movl 0x1f7f23(%rip), %r9d    # 0x20525c
leaq 0xe6bc0(%rip), %rdi     # 0xf3f00
xorl %eax, %eax
callq 0x9030
jmp 0xd49e
movq -0xb8(%rbp), %rax
cmpq 0x31cc9e(%rip), %rax    # 0x329ff8
je 0xd5af
pushq $0x6
popq %rbx
jmp 0xd49e
movl $0x1, 0x31cb82(%rip)    # 0x329ef4
movl -0xc0(%rbp), %eax
movl %eax, 0x31ccce(%rip)    # 0x32a04c
jmp 0xd49e
leaq -0x30(%rbp), %rsi
movb $0x0, (%rsi)
leaq -0xf0(%rbp), %rdi
pushq $0x14
popq %rdx
leaq -0x18(%rbp), %rbx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x9300
movq (%rbx), %rcx
leaq -0xff50(%rcx), %rdx
pushq $0x3
popq %rbx
cmpq $0x13, %rdx
ja 0xd3d0
leaq 0xe5e53(%rip), %rsi     # 0xf3210
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
movl $0x147, %edi            # imm = 0x147
jmp 0xd499
leaq -0xff08(%rcx), %rdx
cmpq $0x5, %rdx
ja 0xd3f4
leaq 0xe5e14(%rip), %rsi     # 0xf31f8
movslq (%rsi,%rdx,4), %rdx
addq %rsi, %rdx
jmpq *%rdx
pushq $0x8
jmp 0xd498
cmpq $0xff1b, %rcx           # imm = 0xFF1B
je 0xd46e
cmpq $0xffff, %rcx           # imm = 0xFFFF
jne 0xd410
movl $0x153, %edi            # imm = 0x153
jmp 0xd499
testl %eax, %eax
sete %al
leaq -0x20(%rcx), %rdx
cmpq $0x5f, %rdx
setae %dl
orb %al, %dl
jne 0xd472
movsbl -0x30(%rbp), %edi
jmp 0xd499
movl $0x151, %edi            # imm = 0x151
jmp 0xd499
movl $0x149, %edi            # imm = 0x149
jmp 0xd499
movl $0x14d, %edi            # imm = 0x14D
jmp 0xd499
movl $0x148, %edi            # imm = 0x148
jmp 0xd499
movl $0x14b, %edi            # imm = 0x14B
jmp 0xd499
movl $0x150, %edi            # imm = 0x150
jmp 0xd499
movl $0x14f, %edi            # imm = 0x14F
jmp 0xd499
movl $0x152, %edi            # imm = 0x152
jmp 0xd499
pushq $0xb
jmp 0xd498
pushq $0xa
jmp 0xd498
pushq $0x9
jmp 0xd498
pushq $0x1b
jmp 0xd498
leaq -0xff80(%rcx), %rax
cmpq $0x49, %rax
ja 0xd4ac
leaq 0xe5dda(%rip), %rcx     # 0xf3260
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x13b, %edi            # imm = 0x13B
jmp 0xd499
pushq $0xd
popq %rdi
callq 0xd140
movl %ebx, %eax
addq $0xe0, %rsp
popq %rbx
popq %r14
popq %rbp
retq
cmpq $0xff6a, %rcx           # imm = 0xFF6A
je 0xd4df
cmpq $0xff6b, %rcx           # imm = 0xFF6B
jne 0xd49e
movl $0x1, 0x313b5c(%rip)    # 0x321024
jmp 0xd49e
movl $0x13c, %edi            # imm = 0x13C
jmp 0xd499
movl $0x13d, %edi            # imm = 0x13D
jmp 0xd499
movl $0x13e, %edi            # imm = 0x13E
jmp 0xd499
pushq $0x20
jmp 0xd498
movl $0x144, %edi            # imm = 0x144
jmp 0xd499
pushq $0x39
jmp 0xd498
pushq $0x38
jmp 0xd498
movl $0x1, 0x31ca04(%rip)    # 0x329f00
movl $0x1869f, 0x1f52e2(%rip) # imm = 0x1869F
movb $0x1, 0x2ea960(%rip)    # 0x2f7e6d
callq 0xd5b6
jmp 0xd49e
movl $0x142, %edi            # imm = 0x142
jmp 0xd499
xorl %edi, %edi
jmp 0xd499
pushq $0x36
jmp 0xd498
pushq $0x2b
jmp 0xd498
pushq $0x32
jmp 0xd498
pushq $0x2a
jmp 0xd498
pushq $0x34
jmp 0xd498
pushq $0x2d
jmp 0xd498
pushq $0x2e
jmp 0xd498
pushq $0x2f
jmp 0xd498
pushq $0x31
jmp 0xd498
pushq $0x33
jmp 0xd498
pushq $0x30
jmp 0xd498
pushq $0x37
jmp 0xd498
pushq $0x3d
jmp 0xd498
pushq $0x35
jmp 0xd498
movl $0x13f, %edi            # imm = 0x13F
jmp 0xd499
movl $0x140, %edi            # imm = 0x140
jmp 0xd499
movl $0x141, %edi            # imm = 0x141
jmp 0xd499
movl $0x143, %edi            # imm = 0x143
jmp 0xd499
xorl %edi, %edi
callq 0xd8d5