// File: /workspace/github_c_makefile_star2_O2/kyx0r[P]xkeyboard/xkeyboard.c
// Function: do_button_event
void do_button_event(void)
{
	int i, j, mod, sc, lockmap = 0;
	char *name;
	static Window lastkeywin = 0;
	static Window lastmodkeywin = 0;
	Window win = myevent.xbutton.window;
	static Time laststamp = 0;
	Time stamp = myevent.xbutton.time;

	if (lastkeywin && myevent.type == ButtonRelease) {
		XSetWindowBorderWidth(dpy, lastkeywin, BORDER);
		win = lastkeywin;
		lastkeywin = 0;
	}
	if (lastkeywin) {	/* sanity */
		XSetWindowBorderWidth(dpy, lastkeywin, BORDER);
		lastkeywin = 0;
	}
	for (i = 0; i < MAX_KEYS; i++) {
		if (win == mykey[i])
			break;
	}
	if (i == MAX_KEYS) {
		/* unexpected event window */
		return;
	}
	sc = xk[i].scancode;
	for (j = 0; j < 8; j++) {
		if (modifiermap[j][0] == sc)
			break;
		if (modifiermap[j][1] == sc)
			break;
		if (modifiermap[j][2] == sc)
			break;
		if (modifiermap[j][3] == sc)
			break;
	}
	mod = 0;
	if (j < 8) {
		if (myevent.type == ButtonRelease)
			return;
		if(win == lastmodkeywin && stamp - laststamp < 500)
		{
			lockmod = !lockmod ? 1 : 0;
			GC tmp = mygcled;
			mygcled = mygcred;
			mygcred = tmp;
			if(lockmod)
			{
				name = scanname[0][sc];
				if(name)
					XDrawString(dpy, mykey[i], mygcled,
							8 * sn / sd, 26 * sn / sd,
							name, strlen(name));
			}
			do_control();
			return;
		} 
		laststamp = stamp;
		lastmodkeywin = mykey[i];
		mod = (1 << j);
		if (keystate & mod) {
                       keystate &= ~mod;
                       lockmap |= mod;
                       myevent.type = ButtonRelease;
                       i = modindex[j];
                       sc = xk[i].scancode;
                       name = scanname[0][sc];
                       if (name)
                               XDrawString(dpy, mykey[i], mygc,
                                       8 * sn / sd, 26 * sn / sd,
                                       name, strlen(name));
		} else { 
			keystate |= mod; 
			lockmap |= mod;
			modindex[j] = i;
			name = scanname[0][sc];
			if (name)
			{
				XDrawString(dpy, mykey[i], mygcled,
						8 * sn / sd, 26 * sn / sd,
						name, strlen(name));
			}
		}
	}
	if (myevent.type == ButtonPress) {
		if (!mod)
			lastkeywin = mykey[i];
		myevent.type = KeyPress;
		XSetWindowBorderWidth(dpy, mykey[i], 2);
	} else {
		myevent.type = KeyRelease;
		XSetWindowBorderWidth(dpy, mykey[i], BORDER);
	}
	if ((lockmap & 0x92) && myevent.type == KeyRelease)
		myevent.type = KeyPress;

	if(sendkey)
	{
		int revert;
		Window focus;
		int statemask = 0;

		if (keystate & 0x01) { 	/* shift */
			statemask |= ShiftMask;
		}
		if (keystate & 0x02) { 	/* caps */
			statemask |= LockMask;
		}
		if (keystate & 0x04) {	/* control */
			statemask |= ControlMask;
		}
		if (keystate & 0x08) { 	/* alt */
			statemask |= Mod1Mask;
		}
		if (keystate & 0x0f) {
			statemask |= Mod2Mask;
		}
		if (keystate & 0x20) { 
			statemask |= Mod3Mask;
		}
		if (keystate & 0x40) { 	/* win key */
			statemask |= Mod4Mask;
		}
		if (keystate & 0x80) { 	
			statemask |= Mod5Mask;
		}

		XGetInputFocus(dpy, &focus, &revert);

		XEvent event;
		event.xkey.type = myevent.type;
		event.xkey.window = focus;
		event.xkey.root = event.xkey.subwindow = None;
		event.xkey.time = 0;
		event.xkey.send_event = 1;
		event.xkey.keycode = sc;
		event.xkey.state = statemask;
		XSendEvent(dpy, focus, True, KeyPressMask | KeyReleaseMask, &event);
		XSync(dpy, 1);
	}
	else
	{
		XTestFakeKeyEvent(dpy, sc, (myevent.type == KeyPress), 0);
	}
	if (myevent.type == KeyPress && !(mod)) {
		/* automagically turn off some modifiers */
		if(!lockmod)
		{
			if (keystate & 0x01) { 	/* shift */
				i = modindex[0];
				sc = xk[i].scancode;
				XSetWindowBorderWidth(dpy, mykey[i], BORDER);
				XTestFakeKeyEvent(dpy, sc, 0, 0);
				name = scanname[0][sc];
				if (name)
					XDrawString(dpy, mykey[i], mygc,
							8 * sn / sd, 26 * sn / sd,
							name, strlen(name));
			}
			if (keystate & 0x04) {	/* control */
				i = modindex[2];
				sc = xk[i].scancode;
				XSetWindowBorderWidth(dpy, mykey[i], BORDER);
				XTestFakeKeyEvent(dpy, sc, 0, 0);
				name = scanname[0][sc];
				if (name)
					XDrawString(dpy, mykey[i], mygc,
							8 * sn / sd, 26 * sn / sd,
							name, strlen(name));
			}
			if (keystate & 0x08) { 	/* alt */
				i = modindex[3];
				sc = xk[i].scancode;
				XSetWindowBorderWidth(dpy, mykey[i], BORDER);
				XTestFakeKeyEvent(dpy, sc, 0, 0);
				name = scanname[0][sc];
				if (name)
					XDrawString(dpy, mykey[i], mygc,
							8 * sn / sd, 26 * sn / sd,
							name, strlen(name));
			}
			keystate &= ~0x0d;
		}
	}
	if (lockmap & 0x92) {	/* Need to generate keyup events */
		if (lockmap & 0x02) {	/* Lock */
			XTestFakeKeyEvent(dpy,
				xk[modindex[1]].scancode, 0, 0);
		}
		if (lockmap & 0x10) {	/* Mod1 */
			XTestFakeKeyEvent(dpy,
				xk[modindex[4]].scancode, 0, 0);
		}
		if (lockmap & 0x80) {	/* Mod5 */
			XTestFakeKeyEvent(dpy,
				xk[modindex[7]].scancode, 0, 0);
		}
	}
	XFlush(dpy);
	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq 0x18690(%rip), %r15     # 0x1c190
movq 0x186a1(%rip), %r14     # 0x1c1a8
movq 0x18722(%rip), %rsi     # 0x1c230
testq %rsi, %rsi
je 0x3b33
cmpl $0x5, 0x18656(%rip)     # 0x1c170
jne 0x3b33
movq 0x13cd5(%rip), %rdi     # 0x177f8
xorl %edx, %edx
callq 0x21c0
movq 0x186ff(%rip), %r15     # 0x1c230
jmp 0x3b46
testq %rsi, %rsi
je 0x3b4e
movq 0x13cb9(%rip), %rdi     # 0x177f8
xorl %edx, %edx
callq 0x21c0
andq $0x0, 0x186e2(%rip)     # 0x1c230
leaq 0x1400b(%rip), %rax     # 0x17b60
leaq 0x37e4(%rip), %rcx      # 0x7340
xorl %ebp, %ebp
cmpq $0x69, %rbp
je 0x4222
cmpq (%rax), %r15
je 0x3b7a
incq %rbp
addq $0x8, %rax
addq $0x18, %rcx
jmp 0x3b5e
movl (%rcx), %ebx
movslq %ebx, %rdx
leaq 0x183ca(%rip), %rsi     # 0x1bf50
leaq 0x183ef(%rip), %rdi     # 0x1bf7c
xorl %ecx, %ecx
cmpq $0x8, %rcx
je 0x3d6e
cmpl %ebx, -0xc(%rdi)
je 0x3bb9
cmpl %ebx, -0x8(%rdi)
je 0x3bb9
cmpl %ebx, -0x4(%rdi)
je 0x3bb9
cmpl %ebx, (%rdi)
je 0x3bb9
incq %rcx
addq $0x4, %rsi
addq $0x40, %rdi
jmp 0x3b8f
cmpq $0x8, %rcx
setb %r13b
cmpl $0x5, 0x185a8(%rip)     # 0x1c170
je 0x4222
cmpq 0x18663(%rip), %r15     # 0x1c238
jne 0x3c94
movq %r14, %rdi
subq 0x1865b(%rip), %rdi     # 0x1c240
cmpq $0x1f3, %rdi            # imm = 0x1F3
ja 0x3c94
xorl %ecx, %ecx
cmpl $0x0, 0x1864d(%rip)     # 0x1c248
sete %cl
movl %ecx, 0x18644(%rip)     # 0x1c248
movq 0x13e15(%rip), %rcx     # 0x17a20
movq 0x13e16(%rip), %rbx     # 0x17a28
movq %rbx, 0x13e07(%rip)     # 0x17a20
movq %rcx, 0x13e08(%rip)     # 0x17a28
jne 0x3c7e
leaq 0x14327(%rip), %rcx     # 0x17f50
movq (%rcx,%rdx,8), %r14
testq %r14, %r14
je 0x3c7e
movq 0x13bbf(%rip), %r15     # 0x177f8
movq (%rax), %r12
movl 0x13dee(%rip), %ecx     # 0x17a30
leal (,%rcx,8), %eax
movl 0x13de5(%rip), %esi     # 0x17a34
cltd
idivl %esi
movl %eax, %ebp
imull $0x1a, %ecx, %eax
cltd
idivl %esi
movl %eax, %r13d
movq %r14, %rdi
callq 0x20c0
movl %eax, (%rsp)
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movl %ebp, %ecx
movl %r13d, %r8d
movq %r14, %r9
callq 0x2270
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x39c3
movq %r14, 0x185a5(%rip)     # 0x1c240
pushq $0x1
popq %r12
shll %cl, %r12d
movq (%rax), %r14
movq %r14, 0x1858c(%rip)     # 0x1c238
movl 0x14296(%rip), %eax     # 0x17f48
btl %ecx, %eax
jae 0x3d92
movl %r12d, %ecx
notl %ecx
andl %ecx, %eax
movl %eax, 0x14280(%rip)     # 0x17f48
movl $0x5, 0x1849e(%rip)     # 0x1c170
movl (%rsi), %ebp
movslq %ebp, %rax
imulq $0x18, %rax, %rcx
leaq 0x365e(%rip), %rdx      # 0x7340
movslq (%rcx,%rdx), %rbx
leaq 0x14263(%rip), %rcx     # 0x17f50
movq (%rcx,%rbx,8), %r14
testq %r14, %r14
je 0x3e12
movq 0x13af7(%rip), %rcx     # 0x177f8
movq %rcx, 0x8(%rsp)
leaq 0x13e53(%rip), %rcx     # 0x17b60
movq (%rcx,%rax,8), %rax
movq %rax, 0x10(%rsp)
movq 0x13cf3(%rip), %rax     # 0x17a10
movq %rax, 0x20(%rsp)
movl 0x13d08(%rip), %ecx     # 0x17a30
leal (,%rcx,8), %eax
movl 0x13cff(%rip), %esi     # 0x17a34
cltd
idivl %esi
movl %eax, 0x1c(%rsp)
imull $0x1a, %ecx, %eax
cltd
idivl %esi
movl %eax, %r15d
movq %r14, %rdi
callq 0x20c0
movl %eax, (%rsp)
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rdx
movl 0x1c(%rsp), %ecx
movl %r15d, %r8d
movq %r14, %r9
jmp 0x3e0d
cmpl $0x4, 0x183fb(%rip)     # 0x1c170
jne 0x3e27
movq (%rax), %rax
movq %rax, 0x184ab(%rip)     # 0x1c230
xorl %r13d, %r13d
pushq $0x2
popq %rax
movl %eax, %edx
jmp 0x3e2f
orl %r12d, %eax
movl %eax, 0x141ad(%rip)     # 0x17f48
movl %ebp, (%rsi)
leaq 0x141ac(%rip), %rax     # 0x17f50
movq (%rax,%rdx,8), %r15
testq %r15, %r15
je 0x3e12
movq 0x13a44(%rip), %rax     # 0x177f8
movq %rax, 0x10(%rsp)
movq 0x13c60(%rip), %rax     # 0x17a20
movq %rax, 0x8(%rsp)
movl 0x13c65(%rip), %ecx     # 0x17a30
leal (,%rcx,8), %eax
movl 0x13c5c(%rip), %esi     # 0x17a34
cltd
idivl %esi
movl %eax, 0x20(%rsp)
imull $0x1a, %ecx, %eax
cltd
idivl %esi
movl %eax, 0x1c(%rsp)
movq %r15, %rdi
callq 0x20c0
movl %eax, (%rsp)
movq 0x10(%rsp), %rdi
movq %r14, %rsi
movq 0x8(%rsp), %rdx
movl 0x20(%rsp), %ecx
movl 0x1c(%rsp), %r8d
movq %r15, %r9
callq 0x2270
xorl %edx, %edx
cmpl $0x4, 0x18355(%rip)     # 0x1c170
sete %dl
movl %edx, %eax
xorl $0x3, %eax
addl %edx, %edx
jmp 0x3e32
xorl %edx, %edx
pushq $0x3
popq %rax
xorl %r13d, %r13d
xorl %r12d, %r12d
movl %eax, 0x18338(%rip)     # 0x1c170
movq 0x139b9(%rip), %rdi     # 0x177f8
movslq %ebp, %rax
leaq 0x13d17(%rip), %rbp     # 0x17b60
movq (%rbp,%rax,8), %rsi
callq 0x21c0
testb $-0x6e, %r12b
setne %cl
movl 0x18310(%rip), %eax     # 0x1c170
cmpl $0x3, %eax
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x3e7a
pushq $0x2
popq %rax
movl $0x2, 0x182f6(%rip)     # 0x1c170
cmpl $0x0, 0x183cb(%rip)     # 0x1c24c
je 0x3f17
movq %rbp, %r15
movl 0x140b8(%rip), %ebp     # 0x17f48
movl %ebp, %eax
andl $0xf, %eax
leal 0x10(%rax), %ecx
testl %eax, %eax
cmovel %eax, %ecx
andl $0xe0, %ebp
orl %ecx, %ebp
movq 0x1394c(%rip), %rdi     # 0x177f8
leaq 0x30(%rsp), %r14
leaq 0x2c(%rsp), %rdx
movq %r14, %rsi
callq 0x23c0
movl 0x182ac(%rip), %eax     # 0x1c170
leaq 0x38(%rsp), %r8
movl %eax, (%r8)
movq (%r14), %rsi
movq %rsi, 0x20(%r8)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r8)
andq $0x0, 0x38(%r8)
movl $0x1, 0x10(%r8)
movl %ebx, 0x54(%r8)
movl %ebp, 0x50(%r8)
movq %r15, %rbp
movq 0x138fe(%rip), %rdi     # 0x177f8
pushq $0x1
popq %rbx
pushq $0x3
popq %rcx
movl %ebx, %edx
callq 0x2030
movq 0x138ea(%rip), %rdi     # 0x177f8
movl %ebx, %esi
callq 0x2250
jmp 0x3f2f
movq 0x138da(%rip), %rdi     # 0x177f8
xorl %edx, %edx
cmpl $0x2, %eax
sete %dl
movl %ebx, %esi
xorl %ecx, %ecx
callq 0x22a0
cmpl $0x2, 0x1823a(%rip)     # 0x1c170
setne %al
orb %al, %r13b
jne 0x418c
cmpl $0x0, 0x182ff(%rip)     # 0x1c248
jne 0x418c
testb $0x1, 0x13ff2(%rip)     # 0x17f48
je 0x4010
movslq 0x17fed(%rip), %r15     # 0x1bf50
imulq $0x18, %r15, %rax
leaq 0x33d2(%rip), %rcx      # 0x7340
movslq (%rax,%rcx), %rbx
movq 0x1387f(%rip), %rdi     # 0x177f8
movq (%rbp,%r15,8), %rsi
xorl %edx, %edx
callq 0x21c0
movq 0x1386c(%rip), %rdi     # 0x177f8
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22a0
leaq 0x13fb2(%rip), %rax     # 0x17f50
movq (%rax,%rbx,8), %rbx
testq %rbx, %rbx
je 0x4010
movq 0x1384a(%rip), %rax     # 0x177f8
movq %rax, 0x8(%rsp)
movq (%rbp,%r15,8), %r15
movq 0x13a51(%rip), %rax     # 0x17a10
movq %rax, 0x10(%rsp)
movl 0x13a66(%rip), %ecx     # 0x17a30
leal (,%rcx,8), %eax
movl 0x13a5d(%rip), %esi     # 0x17a34
cltd
idivl %esi
movq %rbp, %r14
movl %eax, %ebp
imull $0x1a, %ecx, %eax
cltd
idivl %esi
movl %eax, %r13d
movq %rbx, %rdi
callq 0x20c0
movl %eax, (%rsp)
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rdx
movl %ebp, %ecx
movq %r14, %rbp
movl %r13d, %r8d
movq %rbx, %r9
callq 0x2270
testb $0x4, 0x13f31(%rip)     # 0x17f48
je 0x40d1
movslq 0x17f34(%rip), %r15     # 0x1bf58
imulq $0x18, %r15, %rax
leaq 0x3311(%rip), %rcx      # 0x7340
movslq (%rax,%rcx), %rbx
movq 0x137be(%rip), %rdi     # 0x177f8
movq (%rbp,%r15,8), %rsi
xorl %edx, %edx
callq 0x21c0
movq 0x137ab(%rip), %rdi     # 0x177f8
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22a0
leaq 0x13ef1(%rip), %rax     # 0x17f50
movq (%rax,%rbx,8), %rbx
testq %rbx, %rbx
je 0x40d1
movq 0x13789(%rip), %rax     # 0x177f8
movq %rax, 0x8(%rsp)
movq (%rbp,%r15,8), %r15
movq 0x13990(%rip), %rax     # 0x17a10
movq %rax, 0x10(%rsp)
movl 0x139a5(%rip), %ecx     # 0x17a30
leal (,%rcx,8), %eax
movl 0x1399c(%rip), %esi     # 0x17a34
cltd
idivl %esi
movq %rbp, %r14
movl %eax, %ebp
imull $0x1a, %ecx, %eax
cltd
idivl %esi
movl %eax, %r13d
movq %rbx, %rdi
callq 0x20c0
movl %eax, (%rsp)
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq 0x10(%rsp), %rdx
movl %ebp, %ecx
movq %r14, %rbp
movl %r13d, %r8d
movq %rbx, %r9
callq 0x2270
testb $0x8, 0x13e70(%rip)     # 0x17f48
je 0x4185
movslq 0x17e77(%rip), %r15     # 0x1bf5c
imulq $0x18, %r15, %rax
leaq 0x3250(%rip), %rcx      # 0x7340
movslq (%rax,%rcx), %rbx
movq 0x136fd(%rip), %rdi     # 0x177f8
movq (%rbp,%r15,8), %rsi
xorl %edx, %edx
callq 0x21c0
movq 0x136ea(%rip), %rdi     # 0x177f8
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22a0
leaq 0x13e30(%rip), %rax     # 0x17f50
movq (%rax,%rbx,8), %rbx
testq %rbx, %rbx
je 0x4185
movq 0x136c8(%rip), %r14     # 0x177f8
movq (%rbp,%r15,8), %r15
movq 0x138d4(%rip), %rax     # 0x17a10
movq %rax, 0x8(%rsp)
movl 0x138e9(%rip), %ecx     # 0x17a30
leal (,%rcx,8), %eax
movl 0x138e0(%rip), %esi     # 0x17a34
cltd
idivl %esi
movl %eax, %ebp
imull $0x1a, %ecx, %eax
cltd
idivl %esi
movl %eax, %r13d
movq %rbx, %rdi
callq 0x20c0
movl %eax, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq 0x8(%rsp), %rdx
movl %ebp, %ecx
movl %r13d, %r8d
movq %rbx, %r9
callq 0x2270
andb $-0xe, 0x13dbc(%rip)    # 0x17f48
testb $-0x6e, %r12b
je 0x4216
testb $0x2, %r12b
je 0x41c1
movq 0x13655(%rip), %rdi     # 0x177f8
movslq 0x17daa(%rip), %rax     # 0x1bf54
imulq $0x18, %rax, %rax
leaq 0x318b(%rip), %rcx      # 0x7340
movl (%rax,%rcx), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22a0
testb $0x10, %r12b
je 0x41ec
movq 0x1362a(%rip), %rdi     # 0x177f8
movslq 0x17d8b(%rip), %rax     # 0x1bf60
imulq $0x18, %rax, %rax
leaq 0x3160(%rip), %rcx      # 0x7340
movl (%rax,%rcx), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22a0
testb %r12b, %r12b
jns 0x4216
movq 0x13600(%rip), %rdi     # 0x177f8
movslq 0x17d6d(%rip), %rax     # 0x1bf6c
imulq $0x18, %rax, %rax
leaq 0x3136(%rip), %rcx      # 0x7340
movl (%rax,%rcx), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22a0
movq 0x135db(%rip), %rdi     # 0x177f8
callq 0x22f0
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq