// File: tomfunkhouser[P]gaps/apps/confview/confview.cpp
// Function: GLUTMouse(int, int, int, int)
void GLUTMouse(int button, int state, int x, int y)
{
  // Invert y coordinate
  y = GLUTwindow_height - y;

  // Mouse is going down
  if (state == GLUT_DOWN) {
    // Reset mouse drag
    GLUTmouse_drag = 0;

    // Process thumbwheel
    if (button == 3) viewer.ScaleWorld(center, 0.9);
    else if (button == 4) viewer.ScaleWorld(center, 1.1);
  }
  else if (button == 0) {
    // Check for double click  
    static RNBoolean double_click = FALSE;
    static RNTime last_mouse_up_time;
    double_click = (!double_click) && (last_mouse_up_time.Elapsed() < 0.4);
    last_mouse_up_time.Read();

    // Check for click (rather than drag)
    if (GLUTmouse_drag < 100) {
      // Reset selected indices
      selected_surface_index = -1;
      selected_image_index = -1;

      // Select image or surface
      selected_position = R3unknown_point;
      RGBDImage *selected_image = NULL;
      RGBDSurface *selected_surface = NULL;
      if (Pick(x, y, &selected_surface, &selected_image, &selected_position)) {
        // Remember selected indices
        if (!selected_image) selected_image = configuration.FindImage(selected_position);
        if (selected_surface) selected_surface_index = selected_surface->ConfigurationIndex();
        if (selected_image) selected_image_index = selected_image->ConfigurationIndex();
        
        // Print message
        if (selected_surface) {
          printf("Selected surface %d at %g %g %g\n", selected_surface->ConfigurationIndex(),
            selected_position.X(), selected_position.Y(), selected_position.Z());
        }
        if (selected_image) {
          printf("Selected image %d %s and %g %g %g\n", selected_image->ConfigurationIndex(), selected_image->Name(),
            selected_position.X(), selected_position.Y(), selected_position.Z());
        }
        
        // Set viewing center of rotation and scale if double-click
        // if (double_click) center = selected_position;
        center = selected_position;
      }
    }
    else {
      // Process rubber box 
      if (rubber_box_active) {
        DrawRubberBox();
        ProcessRubberBox();
        rubber_box_corners[0] = R2zero_point;
        rubber_box_corners[1] = R2zero_point;
        rubber_box_active = FALSE;
      }
    }
  }

  // Remember button state 
  int b = (button == GLUT_LEFT_BUTTON) ? 0 : ((button == GLUT_MIDDLE_BUTTON) ? 1 : 2);
  GLUTbutton[b] = (state == GLUT_DOWN) ? 1 : 0;

  // Remember modifiers 
  GLUTmodifiers = glutGetModifiers();

   // Remember mouse position 
  GLUTmouse[0] = x;
  GLUTmouse[1] = y;

  // Redraw
  glutPostRedisplay();
}

// Assembly:
subq $0x78, %rsp
movl %edi, 0x74(%rsp)
movl %esi, 0x70(%rsp)
movl %edx, 0x6c(%rsp)
movl %ecx, 0x68(%rsp)
movl 0x20b2d6(%rip), %eax    # 0x235ae0
subl 0x68(%rsp), %eax
movl %eax, 0x68(%rsp)
cmpl $0x0, 0x70(%rsp)
jne 0x2a870
movl $0x0, 0x2115b9(%rip)    # 0x23bddc
cmpl $0x3, 0x74(%rsp)
jne 0x2a847
leaq 0x2114af(%rip), %rdi    # 0x23bce0
leaq 0x211490(%rip), %rsi    # 0x23bcc8
movsd 0x19c810(%rip), %xmm0   # 0x1c7050
callq 0x4aff0
jmp 0x2a86b
cmpl $0x4, 0x74(%rsp)
jne 0x2a869
leaq 0x21148b(%rip), %rdi    # 0x23bce0
leaq 0x21146c(%rip), %rsi    # 0x23bcc8
movsd 0x19c7e4(%rip), %xmm0   # 0x1c7048
callq 0x4aff0
jmp 0x2a86b
jmp 0x2ab23
cmpl $0x0, 0x74(%rsp)
jne 0x2ab21
cmpb $0x0, 0x211586(%rip)    # 0x23be08
jne 0x2a8af
leaq 0x21157d(%rip), %rdi    # 0x23be08
callq 0x25330
cmpl $0x0, %eax
je 0x2a8af
leaq 0x21155c(%rip), %rdi    # 0x23bdf8
callq 0x127730
jmp 0x2a8a3
leaq 0x21155e(%rip), %rdi    # 0x23be08
callq 0x24670
xorl %eax, %eax
cmpl $0x0, 0x21153c(%rip)    # 0x23bdf4
movb %al, 0x43(%rsp)
jne 0x2a8e0
leaq 0x211533(%rip), %rdi    # 0x23bdf8
callq 0x1277f0
movaps %xmm0, %xmm1
movsd 0x19c76b(%rip), %xmm0   # 0x1c7040
ucomisd %xmm1, %xmm0
seta %al
movb %al, 0x43(%rsp)
movb 0x43(%rsp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, 0x211505(%rip)    # 0x23bdf4
leaq 0x211502(%rip), %rdi    # 0x23bdf8
callq 0x2e970
cmpl $0x64, 0x2114da(%rip)   # 0x23bddc
jge 0x2aad6
movl $0xffffffff, 0x20b1d2(%rip) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x20b1cc(%rip) # imm = 0xFFFFFFFF
leaq 0x211355(%rip), %rdi    # 0x23bc78
leaq 0x214aee(%rip), %rsi    # 0x23f418
callq 0xed840
movq $0x0, 0x50(%rsp)
movq $0x0, 0x48(%rsp)
movl 0x6c(%rsp), %edi
movl 0x68(%rsp), %esi
leaq 0x48(%rsp), %rdx
leaq 0x50(%rsp), %rcx
leaq 0x21131e(%rip), %r8     # 0x23bc78
movl $0xa, %r9d
callq 0x2abb0
cmpl $0x0, %eax
je 0x2aad4
cmpq $0x0, 0x50(%rsp)
jne 0x2a9af
leaq 0x211263(%rip), %rdi    # 0x23bbe0
leaq 0x2112f4(%rip), %rsi    # 0x23bc78
callq 0x37140
movq %rax, 0x50(%rsp)
jmp 0x2a9af
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x60(%rsp)
movl %eax, 0x5c(%rsp)
leaq 0x211463(%rip), %rdi    # 0x23be08
callq 0x24650
jmp 0x2ab9b
cmpq $0x0, 0x48(%rsp)
je 0x2a9c7
movq 0x48(%rsp), %rdi
callq 0x2e990
movl %eax, 0x20b11d(%rip)    # 0x235ae4
cmpq $0x0, 0x50(%rsp)
je 0x2a9df
movq 0x50(%rsp), %rdi
callq 0x2e9a0
movl %eax, 0x20b109(%rip)    # 0x235ae8
cmpq $0x0, 0x48(%rsp)
je 0x2aa46
movq 0x48(%rsp), %rdi
callq 0x2e990
movl %eax, 0x2c(%rsp)
leaq 0x21127c(%rip), %rdi    # 0x23bc78
callq 0x2e9b0
movsd %xmm0, 0x38(%rsp)
leaq 0x21126a(%rip), %rdi    # 0x23bc78
callq 0x2e9c0
movsd %xmm0, 0x30(%rsp)
leaq 0x211258(%rip), %rdi    # 0x23bc78
callq 0x2e9d0
movl 0x2c(%rsp), %esi
movsd 0x30(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0x38(%rsp), %xmm0
leaq 0x19c88c(%rip), %rdi    # 0x1c72cb
movb $0x3, %al
callq 0x24090
cmpq $0x0, 0x50(%rsp)
je 0x2aac1
movq 0x50(%rsp), %rdi
callq 0x2e9a0
movl %eax, 0xc(%rsp)
movq 0x50(%rsp), %rdi
callq 0x2e9e0
movq %rax, 0x10(%rsp)
leaq 0x211206(%rip), %rdi    # 0x23bc78
callq 0x2e9b0
movsd %xmm0, 0x20(%rsp)
leaq 0x2111f4(%rip), %rdi    # 0x23bc78
callq 0x2e9c0
movsd %xmm0, 0x18(%rsp)
leaq 0x2111e2(%rip), %rdi    # 0x23bc78
callq 0x2e9d0
movl 0xc(%rsp), %esi
movq 0x10(%rsp), %rdx
movsd 0x18(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0x20(%rsp), %xmm0
leaq 0x19c832(%rip), %rdi    # 0x1c72ec
movb $0x3, %al
callq 0x24090
leaq 0x211200(%rip), %rdi    # 0x23bcc8
leaq 0x2111a9(%rip), %rsi    # 0x23bc78
callq 0xed840
jmp 0x2ab1f
cmpl $0x0, 0x211307(%rip)    # 0x23bde4
je 0x2ab1d
callq 0x2a5f0
callq 0x2b0a0
leaq 0x2111a0(%rip), %rdi    # 0x23bc90
leaq 0x2152f9(%rip), %rsi    # 0x23fdf0
callq 0x11d890
leaq 0x21118d(%rip), %rdi    # 0x23bc90
addq $0x10, %rdi
leaq 0x2152e2(%rip), %rsi    # 0x23fdf0
callq 0x11d890
movl $0x0, 0x2112c7(%rip)    # 0x23bde4
jmp 0x2ab1f
jmp 0x2ab21
jmp 0x2ab23
cmpl $0x0, 0x74(%rsp)
jne 0x2ab32
xorl %eax, %eax
movl %eax, 0x8(%rsp)
jmp 0x2ab4a
movl 0x74(%rsp), %edx
movl $0x2, %eax
movl $0x1, %ecx
cmpl $0x1, %edx
cmovel %ecx, %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0x44(%rsp)
movl 0x70(%rsp), %ecx
xorl %edx, %edx
movl $0x1, %eax
cmpl $0x0, %ecx
cmovel %eax, %edx
movslq 0x44(%rsp), %rcx
leaq 0x211279(%rip), %rax    # 0x23bde8
movl %edx, (%rax,%rcx,4)
callq 0x1bad10
movl %eax, 0x211263(%rip)    # 0x23bde0
movl 0x6c(%rsp), %eax
movl %eax, 0x21124d(%rip)    # 0x23bdd4
movl 0x68(%rsp), %eax
movl %eax, 0x211247(%rip)    # 0x23bdd8
callq 0x1b2ae0
addq $0x78, %rsp
retq
movq 0x60(%rsp), %rdi
callq 0x252b0
nopw %cs:(%rax,%rax)