// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/fglut/freeglut_menu.c
// Function: fgCheckActiveMenu
GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
                              int mouse_x, int mouse_y )
{
    /*
     * Near as I can tell, this is the menu behaviour:
     *  - Down-click the menu button, menu not active:  activate
     *    the menu with its upper left-hand corner at the mouse
     *    location.
     *  - Down-click any button outside the menu, menu active:
     *    deactivate the menu
     *  - Down-click any button inside the menu, menu active:
     *    select the menu entry and deactivate the menu
     *  - Up-click the menu button, menu not active:  nothing happens
     *  - Up-click the menu button outside the menu, menu active:
     *    nothing happens
     *  - Up-click the menu button inside the menu, menu active:
     *    select the menu entry and deactivate the menu
     * Since menus can have submenus, we need to check this recursively.
     */
    if( window->ActiveMenu )
    {
        if( window == window->ActiveMenu->ParentWindow )
        {
            window->ActiveMenu->Window->State.MouseX =
                                       mouse_x - window->ActiveMenu->X;
            window->ActiveMenu->Window->State.MouseY =
                                       mouse_y - window->ActiveMenu->Y;
        }

        /* In the menu, invoke the callback and deactivate the menu */
        if( fghCheckMenuStatus( window->ActiveMenu ) )
        {
            /*
             * Save the current window and menu and set the current
             * window to the window whose menu this is
             */
            SFG_Window *save_window = fgStructure.CurrentWindow;
            SFG_Menu *save_menu = fgStructure.CurrentMenu;
            SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
            fgSetWindow( parent_window );
            fgStructure.CurrentMenu = window->ActiveMenu;

            /* Execute the menu callback */
            fghExecuteMenuCallback( window->ActiveMenu );
            fgDeactivateMenu( parent_window );

            /* Restore the current window and menu */
            fgSetWindow( save_window );
            fgStructure.CurrentMenu = save_menu;
        }
        else if( pressed )
            /*
             * Outside the menu, deactivate if it's a downclick
             *
             * XXX This isn't enough.  A downclick outside of
             * XXX the interior of our freeglut windows should also
             * XXX deactivate the menu.  This is more complicated.
             */
            fgDeactivateMenu( window->ActiveMenu->ParentWindow );

        /*
         * XXX Why does an active menu require a redisplay at
         * XXX this point?  If this can come out cleanly, then
         * XXX it probably should do so; if not, a comment should
         * XXX explain it.
         */
        if( ! window->IsMenu )
            window->State.Redisplay = GL_TRUE;

        return GL_TRUE;
    }

    /* No active menu, let's check whether we need to activate one. */
    if( ( 0 <= button ) &&
        ( FREEGLUT_MAX_MENUS > button ) &&
        ( window->Menu[ button ] ) &&
        pressed )
    {
        /* XXX Posting a requisite Redisplay seems bogus. */
        window->State.Redisplay = GL_TRUE;
        fghActivateMenu( window, button );
        return GL_TRUE;
    }

    return GL_FALSE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebp
movq %rdi, %rbx
movq 0x150(%rdi), %rdi
testq %rdi, %rdi
je 0xbc283
cmpq %rbx, 0x68(%rdi)
jne 0xbc1f3
subl 0x4c(%rdi), %ecx
movq 0x60(%rdi), %rax
movl %ecx, 0x60(%rax)
subl 0x50(%rdi), %r8d
movl %r8d, 0x64(%rax)
callq 0xbbeff
testb %al, %al
je 0xbc3d5
leaq 0x7b689(%rip), %r12     # 0x137890
movq 0x30(%r12), %r14
movq 0x38(%r12), %r13
movq 0x150(%rbx), %rax
movq 0x68(%rax), %r15
movq %r15, %rdi
callq 0xbde17
movq 0x150(%rbx), %rbp
movq %rbp, 0x38(%r12)
movq %rbp, %rdx
movq %rdx, %rax
leaq 0x20(%rdx), %rcx
movq (%rcx), %rcx
testq %rcx, %rcx
je 0xbc269
cmpb $0x0, 0x28(%rcx)
je 0xbc23a
movq 0x20(%rcx), %rdx
testq %rdx, %rdx
jne 0xbc233
movq 0x30(%rax), %rdx
testq %rdx, %rdx
je 0xbc269
movq %rax, 0x38(%r12)
movl 0x10(%rcx), %edi
callq *%rdx
movq %rbp, 0x38(%r12)
movq %r15, %rdi
callq 0xbc408
movq %r14, %rdi
callq 0xbde17
movq %r13, 0x38(%r12)
jmp 0xbc3ea
cmpl $0x2, %esi
ja 0xbc3d1
movl %esi, %eax
movq 0x138(%rbx,%rax,8), %r14
testq %r14, %r14
sete %al
testb %bpl, %bpl
sete %cl
orb %al, %cl
jne 0xbc3d1
movb $0x1, 0x48(%rbx)
leaq 0x7b5db(%rip), %rax     # 0x137890
movq 0x68(%r14), %rcx
testq %rcx, %rcx
je 0xbc2c6
andq $0x0, 0x150(%rcx)
movq 0x30(%rax), %r15
movq %r14, 0x150(%rbx)
movb $0x1, 0x40(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xbc497
leaq 0x76ef8(%rip), %rax     # 0x1331e0
incl 0x70(%rax)
movq 0x68(%r14), %rdi
leaq 0x4(%rsp), %r12
movq %rsp, %rdx
movq %r12, %rsi
callq 0xbcaaa
movq %rbx, %rdi
callq 0xbde17
movl 0x60(%rbx), %ebp
pushq $0x64
popq %rdi
callq 0xbcc17
addl %ebp, %eax
movl %eax, 0x4c(%r14)
movl 0x64(%rbx), %ebp
pushq $0x65
popq %rdi
callq 0xbcc17
addl %ebp, %eax
movl %eax, 0x50(%r14)
movl 0x44(%r14), %edx
movl 0x4c(%r14), %ecx
leal (%rdx,%rcx), %esi
cmpl (%r12), %esi
jle 0xbc340
subl %edx, %ecx
movl %ecx, 0x4c(%r14)
movl 0x48(%r14), %ecx
leal (%rcx,%rax), %edx
cmpl (%rsp), %edx
jle 0xbc359
subl %ecx, %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovgl %eax, %ecx
movl %ecx, 0x50(%r14)
movl 0x60(%rbx), %ebp
pushq $0x64
popq %rdi
callq 0xbcc17
addl %ebp, %eax
subl 0x4c(%r14), %eax
movq 0x60(%r14), %rcx
movl %eax, 0x60(%rcx)
movl 0x64(%rbx), %ebx
pushq $0x65
popq %rdi
callq 0xbcc17
addl %ebx, %eax
subl 0x50(%r14), %eax
movq 0x60(%r14), %rdi
movl %eax, 0x64(%rdi)
callq 0xbde17
movl 0x4c(%r14), %edi
movl 0x50(%r14), %esi
callq 0xbe9b7
movl 0x44(%r14), %edi
movl 0x48(%r14), %esi
callq 0xbe8de
callq 0xbeaa7
callq 0xbe5fe
movq 0x60(%r14), %rax
movq %r14, 0x150(%rax)
movq %r14, %rdi
callq 0xbbeff
movq %r15, %rdi
callq 0xbde17
movb $0x1, %al
jmp 0xbc3f9
xorl %eax, %eax
jmp 0xbc3f9
testb %bpl, %bpl
je 0xbc3ea
movq 0x150(%rbx), %rax
movq 0x68(%rax), %rdi
callq 0xbc408
movb $0x1, %al
cmpb $0x0, 0x170(%rbx)
jne 0xbc3f9
movb $0x1, 0x48(%rbx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq