// File: /workspace/github_c_makefile_star2_O2/9wm[P]9wm/grab.c
// Function: menuhit
int
menuhit(XButtonEvent * e, Menu * m)
{
	XEvent ev;
	int i, n, cur, old, wide, high, status, drawn, warp;
	int x, y, dx, dy, xmax, ymax;
	int tx, ty;
	ScreenInfo *s;

	if (font == 0)
		return -1;
	s = getscreen(e->root);
	if (s == 0 || e->window == s->menuwin)	/* ugly event mangling */
		return -1;

	dx = 0;
	for (n = 0; m->item[n]; n++) {
		wide = XTextWidth(font, m->item[n], strlen(m->item[n])) + 4;
		if (wide > dx)
			dx = wide;
	}
	wide = dx;
	cur = m->lasthit;
	if (cur >= n)
		cur = n - 1;

	high = font->ascent + font->descent + 1;
	dy = n * high;
	x = e->x - wide / 2;
	y = e->y - cur * high - high / 2;
	warp = 0;
	xmax = DisplayWidth(dpy, s->num);
	ymax = DisplayHeight(dpy, s->num);
	if (x < 0) {
		e->x -= x;
		x = 0;
		warp++;
	}
	if (x + wide >= xmax) {
		e->x -= x + wide - xmax;
		x = xmax - wide;
		warp++;
	}
	if (y < 0) {
		e->y -= y;
		y = 0;
		warp++;
	}
	if (y + dy >= ymax) {
		e->y -= y + dy - ymax;
		y = ymax - dy;
		warp++;
	}
	if (warp)
		setmouse(e->x, e->y, s);
	XMoveResizeWindow(dpy, s->menuwin, x, y, dx, dy);
	XSelectInput(dpy, s->menuwin, MenuMask);
	XMapRaised(dpy, s->menuwin);
	status = grab(s->menuwin, None, MenuGrabMask, None, e->time);
	if (status != GrabSuccess) {
		/*
		 * graberror("menuhit", status); 
		 */
		XUnmapWindow(dpy, s->menuwin);
		return -1;
	}
	drawn = 0;
	for (;;) {
		XMaskEvent(dpy, MenuMask, &ev);
		switch (ev.type) {
		default:
			fprintf(stderr, "9wm: menuhit: unknown ev.type %d\n", ev.type);
			break;
		case ButtonPress:
			break;
		case ButtonRelease:
			if (ev.xbutton.button != e->button)
				break;
			x = ev.xbutton.x;
			y = ev.xbutton.y;
			i = y / high;
			if (cur >= 0 && y >= cur * high - 3 && y < (cur + 1) * high + 3)
				i = cur;
			if (x < 0 || x > wide || y < -3)
				i = -1;
			else if (i < 0 || i >= n)
				i = -1;
			else
				m->lasthit = i;
			if (!nobuttons(&ev.xbutton))
				i = -1;
			ungrab(&ev.xbutton);
			XUnmapWindow(dpy, s->menuwin);
			return i;
		case MotionNotify:
			if (!drawn)
				break;
			x = ev.xbutton.x;
			y = ev.xbutton.y;
			old = cur;
			cur = y / high;
			if (old >= 0 && y >= old * high - 3 && y < (old + 1) * high + 3)
				cur = old;
			if (x < 0 || x > wide || y < -3)
				cur = -1;
			else if (cur < 0 || cur >= n)
				cur = -1;
			if (cur == old)
				break;
			if (old >= 0 && old < n)
				XFillRectangle(dpy, s->menuwin, s->gc, 0, old * high, wide, high);
			if (cur >= 0 && cur < n)
				XFillRectangle(dpy, s->menuwin, s->gc, 0, cur * high, wide, high);
			break;
		case Expose:
			XClearWindow(dpy, s->menuwin);
			for (i = 0; i < n; i++) {
				char *item = m->item[i];

				if (i < 5) {
					tx = (wide - XTextWidth(font, item, strlen(item))) / 2;
				} else {
					tx = 1;
				}
				ty = i * high + font->ascent + 1;
				XDrawString(dpy, s->menuwin, s->gc, tx, ty, item, strlen(item));
			}
			if (cur >= 0 && cur < n)
				XFillRectangle(dpy, s->menuwin, s->gc, 0, cur * high, wide, high);
			drawn = 1;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rsi, 0x30(%rsp)
leaq 0x6111(%rip), %rax      # 0xb960
pushq $-0x1
popq %rbx
cmpq $0x0, (%rax)
je 0x5d72
movq %rdi, %rbp
movq 0x28(%rdi), %rdi
xorl %eax, %eax
callq 0x303c
testq %rax, %rax
je 0x5d72
movq %rax, %r14
movq 0x20(%rbp), %rax
cmpq 0x10(%r14), %rax
je 0x5d72
xorl %r15d, %r15d
xorl %ebx, %ebx
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax,%r15,8), %r12
testq %r12, %r12
je 0x58c6
leaq 0x60bf(%rip), %rax      # 0xb960
movq (%rax), %r13
movq %r12, %rdi
callq 0x2140
movq %r13, %rdi
movq %r12, %rsi
movl %eax, %edx
callq 0x24e0
addl $0x4, %eax
cmpl %ebx, %eax
cmovgl %eax, %ebx
incq %r15
jmp 0x5889
movq 0x30(%rsp), %rax
movl 0x10(%rax), %eax
leal -0x1(%r15), %r11d
cmpl %r15d, %eax
cmovll %eax, %r11d
leaq 0x6080(%rip), %rax      # 0xb960
movq (%rax), %rax
movl 0x58(%rax), %ecx
movl 0x5c(%rax), %eax
leal 0x1(%rcx,%rax), %eax
movl 0x40(%rbp), %edi
movl 0x44(%rbp), %esi
movl %ebx, %r10d
shrl %r10d
movl %eax, %edx
imull %r11d, %edx
movl %esi, %ecx
subl %edx, %ecx
pushq $-0x2
popq %r8
movl %eax, 0x24(%rsp)
cltd
idivl %r8d
movslq (%r14), %rdx
shlq $0x7, %rdx
leaq 0x606b(%rip), %r13      # 0xb988
movq (%r13), %r12
movq 0xe8(%r12), %r8
movl 0x18(%r8,%rdx), %r9d
movl 0x1c(%r8,%rdx), %edx
movl %edi, %r8d
subl %r10d, %r8d
js 0x5945
movl $0x0, 0x28(%rsp)
jmp 0x5956
movl %r10d, 0x40(%rbp)
xorl %r8d, %r8d
pushq $0x1
popq %rdi
movl %edi, 0x28(%rsp)
movl %r10d, %edi
addl %eax, %ecx
movq %r8, 0x38(%rsp)
leal (%r8,%rbx), %r10d
movl %r9d, %eax
subl %r10d, %eax
jle 0x5970
movl 0x28(%rsp), %r8d
jmp 0x5984
pushq $0x1
popq %r8
addl %eax, %edi
movl %edi, 0x40(%rbp)
subl %ebx, %r9d
movl %r9d, %eax
movq %rax, 0x38(%rsp)
movl 0x24(%rsp), %eax
movl %eax, %r9d
imull %r15d, %r9d
testl %ecx, %ecx
movq %r11, 0x10(%rsp)
js 0x59a7
leal (%rcx,%r9), %eax
cmpl %edx, %eax
jge 0x59b6
testl %r8d, %r8d
jne 0x59c4
jmp 0x59e6
subl %ecx, %esi
movl %esi, 0x44(%rbp)
xorl %ecx, %ecx
movl %r9d, %eax
cmpl %r9d, %edx
jg 0x59c4
movl %edx, %ecx
subl %eax, %ecx
addl %ecx, %esi
movl %esi, 0x44(%rbp)
subl %r9d, %edx
movl %edx, %ecx
movq %rcx, 0x28(%rsp)
movq %r14, %rdx
movq %rbp, %r12
movq %r9, %rbp
callq 0x645d
movq %rbp, %r9
movq %r12, %rbp
movq 0x28(%rsp), %rcx
movq (%r13), %r12
movq 0x10(%r14), %rsi
movq %r12, %rdi
movq 0x38(%rsp), %rdx
movl %ebx, %r8d
callq 0x21a0
movq (%r13), %rdi
movq 0x10(%r14), %rsi
movl $0xa00c, %edx           # imm = 0xA00C
callq 0x23c0
movq (%r13), %rdi
movq 0x10(%r14), %rsi
callq 0x2350
movq 0x10(%r14), %rdi
movl 0x38(%rbp), %r8d
xorl %esi, %esi
movl $0x2200c, %edx          # imm = 0x2200C
xorl %ecx, %ecx
callq 0x575a
testl %eax, %eax
je 0x5a48
movq (%r13), %rdi
movq 0x10(%r14), %rsi
callq 0x2430
pushq $-0x1
popq %rbx
jmp 0x5d72
movq %rbp, 0x48(%rsp)
movq %rbx, 0x18(%rsp)
xorl %ebx, %ebx
movl 0x24(%rsp), %r12d
movq %r14, 0x40(%rsp)
movq %r15, 0x50(%rsp)
movq (%r13), %rdi
movl $0xa00c, %esi           # imm = 0xA00C
leaq 0x58(%rsp), %rdx
callq 0x2600
movl 0x58(%rsp), %edx
leal -0x4(%rdx), %eax
cmpl $0x8, %eax
ja 0x5cc1
leaq 0x1cc7(%rip), %rcx      # 0x7754
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl 0xac(%rsp), %eax
movq 0x48(%rsp), %rcx
cmpl 0x54(%rcx), %eax
jne 0x5a63
jmp 0x5cde
movq (%r13), %rdi
movq 0x10(%r14), %rsi
callq 0x2090
pushq $0x1
popq %rbp
xorl %ecx, %ecx
cmpq %rcx, %r15
je 0x5b7e
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax,%rcx,8), %r14
pushq $0x1
popq %rax
movq %rcx, 0x38(%rsp)
cmpq $0x4, %rcx
ja 0x5b0f
leaq 0x5e78(%rip), %rax      # 0xb960
movq (%rax), %rbx
movq %r14, %rdi
callq 0x2140
movq %rbx, %rdi
movq %r14, %rsi
movl %eax, %edx
callq 0x24e0
movq 0x18(%rsp), %rcx
subl %eax, %ecx
movl %ecx, %eax
cltd
pushq $0x2
popq %rcx
idivl %ecx
movl %eax, 0x28(%rsp)
leaq 0x5e46(%rip), %rax      # 0xb960
movq (%rax), %rax
movl 0x58(%rax), %r15d
addl %ebp, %r15d
leaq 0x5e5d(%rip), %rax      # 0xb988
movq (%rax), %r13
movq 0x40(%rsp), %rax
movl %ebp, %r12d
movq 0x10(%rax), %rbp
movq 0x20(%rax), %rbx
movq %r14, %rdi
callq 0x2140
movl %eax, (%rsp)
movq %r13, %rdi
movq %rbp, %rsi
movl %r12d, %ebp
movq %rbx, %rdx
movl 0x28(%rsp), %ecx
movl %r15d, %r8d
movq %r14, %r9
callq 0x2400
movq 0x38(%rsp), %rcx
incq %rcx
movl 0x24(%rsp), %r12d
addl %r12d, %ebp
movq 0x50(%rsp), %r15
jmp 0x5abe
pushq $0x1
popq %rbx
cmpl %r15d, 0x10(%rsp)
movq 0x40(%rsp), %r14
leaq 0x5df6(%rip), %r13      # 0xb988
jae 0x5a63
movq (%r13), %rdi
movq 0x10(%r14), %rsi
movq 0x20(%r14), %rdx
movq 0x10(%rsp), %rax
movl %eax, %r8d
imull %r12d, %r8d
movl %r12d, (%rsp)
xorl %ecx, %ecx
movq 0x18(%rsp), %r9
callq 0x2150
pushq $0x1
popq %rbx
jmp 0x5a63
testl %ebx, %ebx
movl $0x0, %ebx
je 0x5a63
movl 0x9c(%rsp), %ecx
movl %ecx, %eax
cltd
idivl %r12d
movq 0x10(%rsp), %rsi
testl %esi, %esi
js 0x5c0c
movl %esi, %edx
imull %r12d, %edx
addl $-0x3, %edx
cmpl %edx, %ecx
pushq $-0x1
popq %rdi
jl 0x5c0f
leal 0x1(%rsi), %edx
imull %r12d, %edx
addl $0x3, %edx
cmpl %edx, %ecx
cmovll %esi, %eax
jmp 0x5c0f
pushq $-0x1
popq %rdi
movl 0x98(%rsp), %edx
movl %edi, %ebp
testl %edx, %edx
js 0x5c37
cmpl 0x18(%rsp), %edx
setg %dl
cmpl $-0x3, %ecx
setl %cl
orb %dl, %cl
movl %edi, %ebp
jne 0x5c37
cmpl %r15d, %eax
cmovael %edi, %eax
movl %eax, %ebp
pushq $0x1
popq %rbx
cmpl %esi, %ebp
je 0x5a63
movq 0x10(%rsp), %r8
cmpl %r15d, %r8d
jae 0x5c6c
movq (%r13), %rdi
movq 0x10(%r14), %rsi
movq 0x20(%r14), %rdx
imull %r12d, %r8d
movl %r12d, (%rsp)
xorl %ecx, %ecx
movq 0x18(%rsp), %r9
callq 0x2150
pushq $0x1
popq %rbx
pushq $-0x1
popq %rax
movq %rax, 0x10(%rsp)
testl %ebp, %ebp
js 0x5a63
movl %ebp, %eax
movq %rax, 0x10(%rsp)
cmpl %r15d, %ebp
jge 0x5a63
movq (%r13), %rdi
movq 0x10(%r14), %rsi
movq 0x20(%r14), %rdx
movl %ebp, %r8d
imull %r12d, %r8d
movl %r12d, (%rsp)
xorl %ecx, %ecx
movq 0x18(%rsp), %r9
callq 0x2150
pushq $0x1
popq %rbx
movl %ebp, %eax
movq %rax, 0x10(%rsp)
jmp 0x5a63
movq 0x5330(%rip), %rax      # 0xaff8
movq (%rax), %rdi
leaq 0x1aa6(%rip), %rsi      # 0x7778
xorl %eax, %eax
callq 0x22c0
jmp 0x5a63
movl 0x98(%rsp), %esi
movl 0x9c(%rsp), %ecx
movl %ecx, %eax
cltd
idivl %r12d
movq 0x10(%rsp), %r8
testl %r8d, %r8d
js 0x5d22
movl %r8d, %edx
imull %r12d, %edx
addl $-0x3, %edx
cmpl %edx, %ecx
movq 0x18(%rsp), %rdi
jl 0x5d27
leal 0x1(%r8), %edx
imull %r12d, %edx
addl $0x3, %edx
cmpl %edx, %ecx
cmovll %r8d, %eax
jmp 0x5d27
movq 0x18(%rsp), %rdi
pushq $-0x1
popq %rdx
testl %esi, %esi
js 0x5d46
cmpl %edi, %esi
jg 0x5d46
cmpl $-0x3, %ecx
jl 0x5d46
cmpl %r15d, %eax
jae 0x5d46
movq 0x30(%rsp), %rcx
movl %eax, 0x10(%rcx)
movl %eax, %edx
movl $0x1f00, %eax           # imm = 0x1F00
leaq 0x58(%rsp), %rdi
andl 0x50(%rdi), %eax
leal -0x1(%rax), %ecx
andl %eax, %ecx
xorl %ebx, %ebx
negl %ecx
sbbl %ebx, %ebx
orl %edx, %ebx
callq 0x57ae
movq (%r13), %rdi
movq 0x10(%r14), %rsi
callq 0x2430
movl %ebx, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq