// File: kazzmir[P]xborder/src/main.cpp
// Function: OptionWindow::handleEvent(_XEvent*, int*)
void handleEvent(XEvent * event, int * glow){
        if (event->xany.window == this->window){
            bool redraw = false;
            switch (event->type){
                case ClientMessage: {
                    Atom wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", false);
                    Atom wm_protocols_atom = XInternAtom(display, "WM_PROTOCOLS", true);

                    if (event->xclient.message_type == wm_protocols_atom &&
                        (Atom) event->xclient.data.l[0] == wm_delete_window){

                        destroy = true;
                        /*
                        XFreeGC(display, graphics);
                        graphics = NULL;
                        XUnmapWindow(display, option_window);
                        XDestroyWindow(display, option_window);
                        option_window = 0;
                        */
                    }
                    break;
                }
                case Expose: {
                    redraw = true;
                    break;
                }
                case MotionNotify: {
                    if (left_button_pressed){
                        int x = event->xmotion.x;
                        int use = (x - glow_line_x) / glow_line_step;
                        if (use < 0){
                            use = 0;
                        }
                        if (use > glow_max_speed){
                            use = glow_max_speed;
                        }
                        *glow = use;
                        glow_value = *glow;
                        redraw = true;
                    }
                    break;
                }
                case ButtonPress: {
                    int x = event->xbutton.x;
                    int y = event->xbutton.y;
                    if (y >= palette_start_y && y < palette_start_y + (palette_y + 1) * palette_size_block &&
                        x >= 0 && x < (palette_x + 1) * palette_size_block){

                        change_background_color(display, xborder_window, get_rgb(x / palette_size_block, (y - palette_start_y) / palette_size_block));
                    } else if (y >= glow_line_y - glow_line_height / 2 &&
                               y <= glow_line_y + glow_line_height / 2 &&
                               x >= glow_line_x &&
                               x <= glow_line_x + glow_line_width){
                        *glow = (x - glow_line_x) / glow_line_step;
                        if (*glow >= glow_max_speed){
                            *glow = glow_max_speed;
                        }
                        glow_value = *glow;
                        left_button_pressed = true;
                        redraw = true;
                    }
                    break;
                }
                case ButtonRelease: {
                    /* FIXME: check for left button */
                    left_button_pressed = false;
                    break;
                }
                case KeyPress: {
                    KeySym sym = XLookupKeysym(&event->xkey, 0);
                    if (sym == XK_BackSpace){
                        redraw = true;
                        if (window_title.length() > 0){
                            window_title = window_title.substr(0, window_title.size() - 1);
                        }
                    } else {
                        char * ascii = XKeysymToString(sym);
                        /* FIXME: handle shift, ctrl-w, ctrl-u */
                        if (ascii != NULL){
                            std::string add;
                            if (std::string(ascii) == "space"){
                                add = " ";
                            } else if (strlen(ascii) == 1){
                                add = ascii;
                            }

                            if (add != ""){
                                window_title += add;
                                redraw = true;
                            }
                        }
                    }
                    XStoreName(display, xborder_window, window_title.c_str());
                    break;
                }
            }

            if (redraw){
                draw(glow);
            }
        }
    }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rcx
movq %rcx, -0xe8(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
cmpq 0x8(%rcx), %rax
jne 0x6204
movb $0x0, -0x19(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
addl $-0x2, %eax
movl %eax, %ecx
movq %rcx, -0xf0(%rbp)
subl $0x1f, %eax
ja 0x61ec
movq -0xf0(%rbp), %rax
leaq 0x136b(%rip), %rcx      # 0x706c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0xe8(%rbp), %rax
movq 0x20(%rax), %rdi
leaq 0x1626(%rip), %rsi      # 0x7342
xorl %edx, %edx
callq 0x3230
movq %rax, %rcx
movq -0xe8(%rbp), %rax
movq %rcx, -0x28(%rbp)
movq 0x20(%rax), %rdi
leaq 0x1617(%rip), %rsi      # 0x7353
movl $0x1, %edx
callq 0x3230
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
cmpq -0x30(%rbp), %rax
jne 0x5d71
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
cmpq -0x28(%rbp), %rax
jne 0x5d71
movq -0xe8(%rbp), %rax
movb $0x1, 0x28(%rax)
jmp 0x61ec
movb $0x1, -0x19(%rbp)
jmp 0x61ec
movq -0xe8(%rbp), %rax
testb $0x1, 0x50(%rax)
je 0x5de5
movq -0xe8(%rbp), %rcx
movq -0x10(%rbp), %rax
movl 0x40(%rax), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
subl 0x5c(%rcx), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jge 0x5dbb
movl $0x0, -0x38(%rbp)
cmpl $0x64, -0x38(%rbp)
jle 0x5dc8
movl $0x64, -0x38(%rbp)
movq -0xe8(%rbp), %rax
movl -0x38(%rbp), %edx
movq -0x18(%rbp), %rcx
movl %edx, (%rcx)
movq -0x18(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, 0x64(%rax)
movb $0x1, -0x19(%rbp)
jmp 0x61ec
movq -0x10(%rbp), %rax
movl 0x40(%rax), %eax
movl %eax, -0x3c(%rbp)
movq -0x10(%rbp), %rax
movl 0x44(%rax), %eax
movl %eax, -0x40(%rbp)
cmpl $0x28, -0x40(%rbp)
jl 0x5ea0
cmpl $0x91, -0x40(%rbp)
jge 0x5ea0
cmpl $0x0, -0x3c(%rbp)
jl 0x5ea0
cmpl $0x13b, -0x3c(%rbp)     # imm = 0x13B
jge 0x5ea0
movq -0xe8(%rbp), %rax
movq 0x20(%rax), %rcx
movq %rcx, -0x100(%rbp)
movq 0x10(%rax), %rax
movq %rax, -0xf8(%rbp)
movl -0x3c(%rbp), %eax
movl $0xf, %ecx
cltd
idivl %ecx
movl %eax, %esi
movl -0x40(%rbp), %eax
subl $0x28, %eax
movl $0xf, %ecx
cltd
idivl %ecx
movl %eax, %edx
leaq -0x58(%rbp), %rdi
callq 0x41b0
movq -0x100(%rbp), %rdi
movq -0xf8(%rbp), %rsi
leaq -0x58(%rbp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x4470
jmp 0x5f69
movq -0xe8(%rbp), %rax
movl -0x40(%rbp), %ecx
movl %ecx, -0x104(%rbp)
movl 0x54(%rax), %ecx
movl 0x58(%rax), %eax
movl $0x2, %esi
cltd
idivl %esi
movl %eax, %edx
movl -0x104(%rbp), %eax
subl %edx, %ecx
cmpl %ecx, %eax
jl 0x5f67
movq -0xe8(%rbp), %rax
movl -0x40(%rbp), %ecx
movl %ecx, -0x108(%rbp)
movl 0x54(%rax), %ecx
movl 0x58(%rax), %eax
movl $0x2, %esi
cltd
idivl %esi
movl %eax, %edx
movl -0x108(%rbp), %eax
addl %edx, %ecx
cmpl %ecx, %eax
jg 0x5f67
movq -0xe8(%rbp), %rcx
movl -0x3c(%rbp), %eax
cmpl 0x5c(%rcx), %eax
jl 0x5f67
movq -0xe8(%rbp), %rdx
movl -0x3c(%rbp), %eax
movl 0x5c(%rdx), %ecx
addl 0x60(%rdx), %ecx
cmpl %ecx, %eax
jg 0x5f67
movq -0xe8(%rbp), %rcx
movl -0x3c(%rbp), %eax
subl 0x5c(%rcx), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
cmpl $0x64, (%rax)
jl 0x5f4f
movq -0x18(%rbp), %rax
movl $0x64, (%rax)
movq -0xe8(%rbp), %rax
movq -0x18(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, 0x64(%rax)
movb $0x1, 0x50(%rax)
movb $0x1, -0x19(%rbp)
jmp 0x5f69
jmp 0x61ec
movq -0xe8(%rbp), %rax
movb $0x0, 0x50(%rax)
jmp 0x61ec
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x3450
movq %rax, -0x60(%rbp)
cmpq $0xff08, -0x60(%rbp)    # imm = 0xFF08
jne 0x600c
movq -0xe8(%rbp), %rdi
movb $0x1, -0x19(%rbp)
addq $0x30, %rdi
callq 0x3550
cmpq $0x0, %rax
jbe 0x6007
movq -0xe8(%rbp), %rdi
movq %rdi, %rax
addq $0x30, %rax
movq %rax, -0x110(%rbp)
addq $0x30, %rdi
callq 0x3210
movq -0x110(%rbp), %rsi
movq %rax, %rcx
subq $0x1, %rcx
leaq -0x80(%rbp), %rdi
xorl %eax, %eax
movl %eax, %edx
callq 0x3360
movq -0xe8(%rbp), %rdi
addq $0x30, %rdi
leaq -0x80(%rbp), %rsi
callq 0x3400
leaq -0x80(%rbp), %rdi
callq 0x31d0
jmp 0x61b0
movq -0x60(%rbp), %rdi
callq 0x3670
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x88(%rbp)
je 0x61ae
leaq -0xa8(%rbp), %rdi
callq 0x3490
movq -0x88(%rbp), %rax
movq %rax, -0x120(%rbp)
leaq -0xc9(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0x3630
movq -0x120(%rbp), %rsi
movq -0x118(%rbp), %rdx
leaq -0xc8(%rbp), %rdi
callq 0x5bc0
jmp 0x6073
leaq 0x1345(%rip), %rsi      # 0x73bf
leaq -0xc8(%rbp), %rdi
callq 0x5c70
movb %al, -0x121(%rbp)
jmp 0x608e
leaq -0xc8(%rbp), %rdi
callq 0x31d0
leaq -0xc9(%rbp), %rdi
callq 0x33a0
movb -0x121(%rbp), %al
testb $0x1, %al
jne 0x60b2
jmp 0x6123
leaq 0x1312(%rip), %rsi      # 0x73cb
leaq -0xa8(%rbp), %rdi
callq 0x3470
jmp 0x60c7
jmp 0x614e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd8(%rbp)
movl %eax, -0xdc(%rbp)
jmp 0x60fe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd8(%rbp)
movl %eax, -0xdc(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x31d0
leaq -0xc9(%rbp), %rdi
callq 0x33a0
jmp 0x61a0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd8(%rbp)
movl %eax, -0xdc(%rbp)
jmp 0x61a0
movq -0x88(%rbp), %rdi
callq 0x3150
cmpq $0x1, %rax
jne 0x614c
movq -0x88(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x3470
jmp 0x614a
jmp 0x614c
jmp 0x614e
leaq 0x1054(%rip), %rsi      # 0x71a9
leaq -0xa8(%rbp), %rdi
callq 0x6420
movb %al, -0x122(%rbp)
jmp 0x6169
movb -0x122(%rbp), %al
testb $0x1, %al
jne 0x6175
jmp 0x6192
movq -0xe8(%rbp), %rdi
addq $0x30, %rdi
leaq -0xa8(%rbp), %rsi
callq 0x3440
jmp 0x618e
movb $0x1, -0x19(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x31d0
jmp 0x61ae
leaq -0xa8(%rbp), %rdi
callq 0x31d0
jmp 0x620d
jmp 0x61b0
movq -0xe8(%rbp), %rdi
movq 0x20(%rdi), %rax
movq %rax, -0x138(%rbp)
movq 0x10(%rdi), %rax
movq %rax, -0x130(%rbp)
addq $0x30, %rdi
callq 0x3140
movq -0x138(%rbp), %rdi
movq -0x130(%rbp), %rsi
movq %rax, %rdx
callq 0x3070
testb $0x1, -0x19(%rbp)
je 0x6202
movq -0xe8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x6450
jmp 0x6204
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
movq -0xd8(%rbp), %rdi
callq 0x3620
nopl (%rax)