// File: /workspace/github_c_makefile_star2_O1/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 0x1955e(%rip), %r15     # 0x1d190
movq 0x1956f(%rip), %r14     # 0x1d1a8
movq 0x195f0(%rip), %rsi     # 0x1d230
testq %rsi, %rsi
je 0x3c6e
cmpl $0x5, 0x19524(%rip)     # 0x1d170
jne 0x3c6e
movq 0x14ba3(%rip), %rdi     # 0x187f8
xorl %edx, %edx
callq 0x21c0
movq 0x195cd(%rip), %r15     # 0x1d230
movq $0x0, 0x195c2(%rip) # 0x1d230
movq 0x195bb(%rip), %rsi     # 0x1d230
testq %rsi, %rsi
je 0x3c93
movq 0x14b77(%rip), %rdi     # 0x187f8
xorl %edx, %edx
callq 0x21c0
movq $0x0, 0x1959d(%rip) # 0x1d230
leaq 0x14ec6(%rip), %rax     # 0x18b60
xorl %ebp, %ebp
cmpq (%rax), %r15
je 0x3cb3
incq %rbp
addq $0x8, %rax
cmpq $0x69, %rbp
jne 0x3c9c
movl $0x69, %ebp
cmpl $0x69, %ebp
je 0x43b9
movl %ebp, %eax
leaq (%rax,%rax,2), %rcx
leaq 0x4677(%rip), %rdx      # 0x8340
movl (%rdx,%rcx,8), %ebx
cmpl %ebx, 0x1929e(%rip)     # 0x1cf70
jne 0x3cdd
xorl %edx, %edx
movb $0x1, %r13b
xorl %ecx, %ecx
jmp 0x3d1a
movb $0x1, %r13b
xorl %ecx, %ecx
leaq 0x1928b(%rip), %rsi     # 0x1cf74
xorl %edx, %edx
cmpl %ebx, (%rsi)
je 0x3d18
cmpl %ebx, 0x4(%rsi)
je 0x3d1a
cmpl %ebx, 0x8(%rsi)
je 0x3d1a
cmpq $0x7, %rdx
setb %r13b
je 0x3f76
incq %rdx
leaq 0x40(%rsi), %rdi
movl %edx, %ecx
cmpl %ebx, 0x3c(%rsi)
movq %rdi, %rsi
jne 0x3ceb
movl %edx, %ecx
cmpl $0x5, 0x1944f(%rip)     # 0x1d170
je 0x43b9
movslq %ebx, %rsi
cmpq 0x19507(%rip), %r15     # 0x1d238
jne 0x3dfd
movq %r14, %rdi
subq 0x194ff(%rip), %rdi     # 0x1d240
cmpq $0x1f3, %rdi            # imm = 0x1F3
ja 0x3dfd
xorl %ecx, %ecx
cmpl $0x0, 0x194f1(%rip)     # 0x1d248
sete %cl
movl %ecx, 0x194e8(%rip)     # 0x1d248
movq 0x14cb9(%rip), %rcx     # 0x18a20
movq 0x14cba(%rip), %rbx     # 0x18a28
movq %rbx, 0x14cab(%rip)     # 0x18a20
movq %rcx, 0x14cac(%rip)     # 0x18a28
jne 0x3de7
leaq 0x151cb(%rip), %rcx     # 0x18f50
movq (%rcx,%rsi,8), %r14
testq %r14, %r14
je 0x3de7
movq 0x14a63(%rip), %r15     # 0x187f8
leaq 0x14dc4(%rip), %rcx     # 0x18b60
movq (%rcx,%rax,8), %r12
movl 0x14c8a(%rip), %ecx     # 0x18a30
leal (,%rcx,8), %eax
movl 0x14c81(%rip), %esi     # 0x18a34
cltd
idivl %esi
movl %eax, %ebp
leal (%rcx,%rcx,4), %eax
leal (%rax,%rax,4), %eax
addl %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 0x3ace
movq %r14, 0x1943c(%rip)     # 0x1d240
leaq 0x14d55(%rip), %rdi     # 0x18b60
movl $0x1, %r12d
shll %cl, %r12d
movq (%rdi,%rax,8), %r14
movq %r14, 0x19419(%rip)     # 0x1d238
movl 0x15123(%rip), %eax     # 0x18f48
btl %ecx, %eax
jae 0x3ee7
movl %r12d, %ecx
notl %ecx
andl %ecx, %eax
movl %eax, 0x1510d(%rip)     # 0x18f48
movl $0x5, 0x1932b(%rip)     # 0x1d170
leaq 0x19104(%rip), %rax     # 0x1cf50
movl (%rax,%rdx,4), %ebp
movslq %ebp, %rax
leaq (%rax,%rax,2), %rcx
leaq 0x44e3(%rip), %rdx      # 0x8340
movslq (%rdx,%rcx,8), %rbx
leaq 0x150e8(%rip), %rcx     # 0x18f50
movq (%rcx,%rbx,8), %r14
testq %r14, %r14
je 0x3f79
movq 0x1497c(%rip), %rcx     # 0x187f8
movq %rcx, 0x8(%rsp)
movq (%rdi,%rax,8), %rax
movq %rax, 0x10(%rsp)
movq 0x14b7f(%rip), %rax     # 0x18a10
movq %rax, 0x20(%rsp)
movl 0x14b94(%rip), %ecx     # 0x18a30
leal (,%rcx,8), %eax
movl 0x14b8b(%rip), %esi     # 0x18a34
cltd
idivl %esi
movl %eax, 0x1c(%rsp)
leal (%rcx,%rcx,4), %eax
leal (%rax,%rax,4), %eax
addl %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 0x3f6f
orl %r12d, %eax
movl %eax, 0x15058(%rip)     # 0x18f48
leaq 0x19059(%rip), %rax     # 0x1cf50
movl %ebp, (%rax,%rdx,4)
leaq 0x1504f(%rip), %rax     # 0x18f50
movq (%rax,%rsi,8), %r15
testq %r15, %r15
je 0x3f79
movq 0x148e7(%rip), %rax     # 0x187f8
movq %rax, 0x10(%rsp)
movq 0x14b03(%rip), %rax     # 0x18a20
movq %rax, 0x8(%rsp)
movl 0x14b08(%rip), %ecx     # 0x18a30
leal (,%rcx,8), %eax
movl 0x14aff(%rip), %esi     # 0x18a34
cltd
idivl %esi
movl %eax, 0x20(%rsp)
leal (%rcx,%rcx,4), %eax
leal (%rax,%rax,4), %eax
addl %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
jmp 0x3f79
xorl %r12d, %r12d
movl 0x191f1(%rip), %esi     # 0x1d170
xorl %edx, %edx
xorl %eax, %eax
cmpl $0x4, %esi
sete %dl
setne %al
orl $0x2, %eax
addl %edx, %edx
movslq %ebp, %rcx
cmpl $0x4, %esi
jne 0x3fbb
testb $0x1, %r13b
jne 0x3fbb
leaq 0x14bba(%rip), %rax     # 0x18b60
movq (%rax,%rcx,8), %rax
movq %rax, 0x1927f(%rip)     # 0x1d230
movl $0x2, %eax
movl $0x2, %edx
movl %eax, 0x191af(%rip)     # 0x1d170
movq 0x14830(%rip), %rdi     # 0x187f8
leaq 0x14b91(%rip), %rbp     # 0x18b60
movq (%rbp,%rcx,8), %rsi
callq 0x21c0
testb $-0x6e, %r12b
je 0x3ff2
cmpl $0x3, 0x1918a(%rip)     # 0x1d170
jne 0x3ff2
movl $0x2, 0x1917e(%rip)     # 0x1d170
cmpl $0x0, 0x19253(%rip)     # 0x1d24c
je 0x4097
movq %rbp, %r15
movl 0x14f40(%rip), %ebp     # 0x18f48
movl %ebp, %eax
andl $0xf, %eax
leal 0x10(%rax), %ecx
testl %eax, %eax
cmovel %eax, %ecx
andl $0xe0, %ebp
orl %ecx, %ebp
movq 0x147d4(%rip), %rdi     # 0x187f8
leaq 0x30(%rsp), %r14
leaq 0x2c(%rsp), %rdx
movq %r14, %rsi
callq 0x23c0
movl 0x19134(%rip), %eax     # 0x1d170
leaq 0x38(%rsp), %r8
movl %eax, (%r8)
movq (%r14), %rsi
movq %rsi, 0x20(%r8)
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%r8)
movq $0x0, 0x38(%r8)
movl $0x1, 0x10(%r8)
movl %ebx, 0x54(%r8)
movl %ebp, 0x50(%r8)
movq %r15, %rbp
movq 0x14783(%rip), %rdi     # 0x187f8
movl $0x3, %ecx
movl $0x1, %edx
callq 0x2030
movq 0x1476d(%rip), %rdi     # 0x187f8
movl $0x1, %esi
callq 0x2250
jmp 0x40b3
movq 0x1475a(%rip), %rdi     # 0x187f8
xorl %edx, %edx
cmpl $0x2, 0x190c9(%rip)     # 0x1d170
sete %dl
movl %ebx, %esi
xorl %ecx, %ecx
callq 0x22a0
cmpl $0x2, 0x190b6(%rip)     # 0x1d170
setne %al
orb %al, %r13b
testb $0x1, %r13b
jne 0x4323
cmpl $0x0, 0x19177(%rip)     # 0x1d248
jne 0x4323
testb $0x1, 0x14e6a(%rip)     # 0x18f48
je 0x419d
movslq 0x18e65(%rip), %r15     # 0x1cf50
leaq (%r15,%r15,2), %rax
leaq 0x424a(%rip), %rcx      # 0x8340
movslq (%rcx,%rax,8), %rbx
movq 0x146f7(%rip), %rdi     # 0x187f8
movq (%rbp,%r15,8), %rsi
xorl %edx, %edx
callq 0x21c0
movq 0x146e4(%rip), %rdi     # 0x187f8
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22a0
leaq 0x14e2a(%rip), %rax     # 0x18f50
movq (%rax,%rbx,8), %rbx
testq %rbx, %rbx
je 0x419d
movq 0x146c2(%rip), %rax     # 0x187f8
movq %rax, 0x8(%rsp)
movq (%rbp,%r15,8), %r15
movq 0x148c9(%rip), %rax     # 0x18a10
movq %rax, 0x10(%rsp)
movl 0x148de(%rip), %ecx     # 0x18a30
leal (,%rcx,8), %eax
movl 0x148d5(%rip), %esi     # 0x18a34
cltd
idivl %esi
movq %rbp, %r14
movl %eax, %ebp
leal (%rcx,%rcx,4), %eax
leal (%rax,%rax,4), %eax
addl %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, 0x14da4(%rip)     # 0x18f48
je 0x4263
movslq 0x18da7(%rip), %r15     # 0x1cf58
leaq (%r15,%r15,2), %rax
leaq 0x4184(%rip), %rcx      # 0x8340
movslq (%rcx,%rax,8), %rbx
movq 0x14631(%rip), %rdi     # 0x187f8
movq (%rbp,%r15,8), %rsi
xorl %edx, %edx
callq 0x21c0
movq 0x1461e(%rip), %rdi     # 0x187f8
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22a0
leaq 0x14d64(%rip), %rax     # 0x18f50
movq (%rax,%rbx,8), %rbx
testq %rbx, %rbx
je 0x4263
movq 0x145fc(%rip), %rax     # 0x187f8
movq %rax, 0x8(%rsp)
movq (%rbp,%r15,8), %r15
movq 0x14803(%rip), %rax     # 0x18a10
movq %rax, 0x10(%rsp)
movl 0x14818(%rip), %ecx     # 0x18a30
leal (,%rcx,8), %eax
movl 0x1480f(%rip), %esi     # 0x18a34
cltd
idivl %esi
movq %rbp, %r14
movl %eax, %ebp
leal (%rcx,%rcx,4), %eax
leal (%rax,%rax,4), %eax
addl %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, 0x14cde(%rip)     # 0x18f48
je 0x431c
movslq 0x18ce5(%rip), %r15     # 0x1cf5c
leaq (%r15,%r15,2), %rax
leaq 0x40be(%rip), %rcx      # 0x8340
movslq (%rcx,%rax,8), %rbx
movq 0x1456b(%rip), %rdi     # 0x187f8
movq (%rbp,%r15,8), %rsi
xorl %edx, %edx
callq 0x21c0
movq 0x14558(%rip), %rdi     # 0x187f8
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22a0
leaq 0x14c9e(%rip), %rax     # 0x18f50
movq (%rax,%rbx,8), %rbx
testq %rbx, %rbx
je 0x431c
movq 0x14536(%rip), %r14     # 0x187f8
movq (%rbp,%r15,8), %r15
movq 0x14742(%rip), %rax     # 0x18a10
movq %rax, 0x8(%rsp)
movl 0x14757(%rip), %ecx     # 0x18a30
leal (,%rcx,8), %eax
movl 0x1474e(%rip), %esi     # 0x18a34
cltd
idivl %esi
movl %eax, %ebp
leal (%rcx,%rcx,4), %eax
leal (%rax,%rax,4), %eax
addl %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, 0x14c25(%rip)    # 0x18f48
testb $-0x6e, %r12b
je 0x43ad
testb $0x2, %r12b
je 0x4358
movq 0x144be(%rip), %rdi     # 0x187f8
movslq 0x18c13(%rip), %rax     # 0x1cf54
leaq (%rax,%rax,2), %rax
leaq 0x3ff4(%rip), %rcx      # 0x8340
movl (%rcx,%rax,8), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22a0
testb $0x10, %r12b
je 0x4383
movq 0x14493(%rip), %rdi     # 0x187f8
movslq 0x18bf4(%rip), %rax     # 0x1cf60
leaq (%rax,%rax,2), %rax
leaq 0x3fc9(%rip), %rcx      # 0x8340
movl (%rcx,%rax,8), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22a0
testb %r12b, %r12b
jns 0x43ad
movq 0x14469(%rip), %rdi     # 0x187f8
movslq 0x18bd6(%rip), %rax     # 0x1cf6c
leaq (%rax,%rax,2), %rax
leaq 0x3f9f(%rip), %rcx      # 0x8340
movl (%rcx,%rax,8), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x22a0
movq 0x14444(%rip), %rdi     # 0x187f8
callq 0x22f0
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq