// File: /workspace/github_c_makefile_star2_O2/odknt[P]st/x.c
// Function: mousereport
void
mousereport(XEvent *e)
{
	int len, btn, code;
	int x = evcol(e), y = evrow(e);
	int state = e->xbutton.state;
	char buf[40];
	static int ox, oy;

	if (e->type == MotionNotify) {
		if (x == ox && y == oy)
			return;
		if (!IS_SET(MODE_MOUSEMOTION) && !IS_SET(MODE_MOUSEMANY))
			return;
		/* MODE_MOUSEMOTION: no reporting if no button is pressed */
		if (IS_SET(MODE_MOUSEMOTION) && buttons == 0)
			return;
		/* Set btn to lowest-numbered pressed button, or 12 if no
		 * buttons are pressed. */
		for (btn = 1; btn <= 11 && !(buttons & (1<<(btn-1))); btn++)
			;
		code = 32;
	} else {
		btn = e->xbutton.button;
		/* Only buttons 1 through 11 can be encoded */
		if (btn < 1 || btn > 11)
			return;
		if (e->type == ButtonRelease) {
			/* MODE_MOUSEX10: no button release reporting */
			if (IS_SET(MODE_MOUSEX10))
				return;
			/* Don't send release events for the scroll wheel */
			if (btn == 4 || btn == 5)
				return;
		}
		code = 0;
	}

	ox = x;
	oy = y;

	/* Encode btn into code. If no button is pressed for a motion event in
	 * MODE_MOUSEMANY, then encode it as a release. */
	if ((!IS_SET(MODE_MOUSESGR) && e->type == ButtonRelease) || btn == 12)
		code += 3;
	else if (btn >= 8)
		code += 128 + btn - 8;
	else if (btn >= 4)
		code += 64 + btn - 4;
	else
		code += btn - 1;

	if (!IS_SET(MODE_MOUSEX10)) {
		code += ((state & ShiftMask  ) ?  4 : 0)
		      + ((state & Mod1Mask   ) ?  8 : 0) /* meta key: alt */
		      + ((state & ControlMask) ? 16 : 0);
	}

	if (IS_SET(MODE_MOUSESGR)) {
		len = snprintf(buf, sizeof(buf), "\033[<%d;%d;%d%c",
				code, x+1, y+1,
				e->type == ButtonRelease ? 'm' : 'M');
	} else if (x < 223 && y < 223) {
		len = snprintf(buf, sizeof(buf), "\033[M%c%c%c",
				32+code, 32+x+1, 32+y+1);
	} else {
		return;
	}

	ttywrite(buf, len, 0);
}

// Assembly:
pushq %r14
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r14
callq 0xf0f6
movl %eax, %ebx
movq %r14, %rdi
callq 0xf11a
movl 0x50(%r14), %esi
movl (%r14), %edx
cmpl $0x6, %edx
jne 0xef3a
cmpl 0xfb21(%rip), %ebx      # 0x1e9fc
jne 0xeee9
cmpl 0xfb1d(%rip), %eax      # 0x1ea00
je 0xf049
movl 0xfa55(%rip), %edi      # 0x1e944
testw $0x8010, %di            # imm = 0x8010
je 0xf049
movl %edi, %ecx
shrb $0x4, %cl
movl 0xfaf2(%rip), %r9d      # 0x1e9f8
testl %r9d, %r9d
sete %r8b
testb %r8b, %cl
jne 0xf049
pushq $0x1
popq %r10
pushq $0xc
popq %r8
pushq $0x20
popq %rcx
cmpl $0xc, %r10d
je 0xef7f
leal -0x1(%r10), %r11d
movzbl %r11b, %r11d
btl %r11d, %r9d
jb 0xef7c
incl %r10d
jmp 0xef21
movl 0x54(%r14), %r8d
leal -0xc(%r8), %ecx
cmpl $-0xb, %ecx
jb 0xf049
xorl %ecx, %ecx
movl 0xf9f1(%rip), %edi      # 0x1e944
cmpl $0x5, %edx
jne 0xef7f
movl %edi, %r9d
shrl $0xe, %r9d
andl $0x1, %r9d
movl %r8d, %r10d
andl $0xe, %r10d
cmpl $0x4, %r10d
sete %r10b
orb %r9b, %r10b
je 0xef7f
jmp 0xf049
movl %r10d, %r8d
movl %ebx, 0xfa77(%rip)      # 0x1e9fc
movl %eax, 0xfa75(%rip)      # 0x1ea00
btl $0x9, %edi
jb 0xefb7
cmpl $0x5, %edx
sete %r9b
cmpl $0xc, %r8d
sete %r10b
orb %r9b, %r10b
jne 0xefbd
cmpl $0x8, %r8d
jl 0xf051
addl %r8d, %ecx
addl $0x78, %ecx
jmp 0xefc0
cmpl $0xc, %r8d
jne 0xefa5
orl $0x3, %ecx
btl $0xe, %edi
jb 0xefd8
movl %esi, %r8d
andl $0x8, %r8d
andl $0x5, %esi
leal (%r8,%rsi,4), %esi
addl %ecx, %esi
movl %esi, %ecx
btl $0x9, %edi
jb 0xf008
movl $0xdf, %edx
cmpl %edx, %ebx
setl %sil
cmpl %edx, %eax
setl %dl
andb %sil, %dl
cmpb $0x1, %dl
jne 0xf049
addl $0x20, %ecx
addl $0x21, %ebx
addl $0x21, %eax
leaq 0x268b(%rip), %rdx      # 0x11691
jmp 0xf025
incl %ebx
incl %eax
xorl %esi, %esi
cmpl $0x5, %edx
sete %sil
shll $0x5, %esi
orl $0x4d, %esi
movl %esi, (%rsp)
leaq 0x265e(%rip), %rdx      # 0x11683
leaq 0x10(%rsp), %rdi
pushq $0x28
popq %rsi
movl %ebx, %r8d
movl %eax, %r9d
xorl %eax, %eax
callq 0x60c0
movslq %eax, %rsi
leaq 0x10(%rsp), %rdi
xorl %edx, %edx
callq 0x7aae
addq $0x38, %rsp
popq %rbx
popq %r14
retq
addl %r8d, %ecx
cmpl $0x4, %r8d
jl 0xf062
addl $0x3c, %ecx
jmp 0xefc0
decl %ecx
jmp 0xefc0