// File: jkingan[P]ladyheather/heather.cpp
// Function: serve_gps(int)
S32 serve_gps(int why)  // called from either foreground loop or WM_TIMER (e.g., while dragging)
{
S32 i;
int served;
int old_survey;

   if(break_flag) return 0;        // ctrl-break pressed, so exit the program
   if(take_a_dump) return 1;
   if(in_terminal) return 1;

   while(1) {

      served = 0;
      #ifdef WIN_VFX
         if(!timer_serve) {
            served = 1;
            serve_vfx();
         }
      #endif
      #ifdef USE_X11
         if(!timer_serve) {
            get_x11_event();
            served = 1;
         }
      #endif
      #ifdef USE_SDL
         if(!timer_serve) {
    	     get_sdl_event();
	         served = 1;
	     }
      #endif
fake_second_check(1);
fine_tick_check();


      i = serial_input_check();  // see if OK to process the receiver serial port data

      if(i == 0) {    // no data available,  we are not too busy
if(rcvr_type == Z12_RCVR) {
   get_rcvr_message();
}
         // no data available,  we are not too busy, or taking a breather from high nav-rate updates
         system_busy = 0;
         check_com_timer(RCVR_PORT);
         get_mouse_info();

         if(rcvr_type == NO_RCVR) ;
         else if(rcvr_type == TIDE_RCVR) ;
         else if(com[RCVR_PORT].user_disabled_com) ;
         else if(com[RCVR_PORT].com_data_lost == DATA_LOSS_REINIT) { // attempt to restart the receiver
            com[RCVR_PORT].com_data_lost = DATA_LOSS_NO_INIT;  // prevents futile re-init attempts
            if(com[RCVR_PORT].process_com) {
               vidstr(0,0, RED, "NO DATA SEEN ON COM DEVICE      ");
               refresh_page();
               ++restart_count;
               if((com[RCVR_PORT].com_recover && !com[RCVR_PORT].user_disabled_com) || rcvr_reset || POLLED_RCVR || (rcvr_type == ZODIAC_RCVR)) {    // recover from hard reset
                  com_retry:
                  old_survey = do_survey;
                  do_survey = 0;
                  rcvr_reset = 0;
                  tsip_sync = 0;
                  tsip_wptr = 0;
                  tsip_rptr = 0;
                  if(com[RCVR_PORT].com_recover && !com[RCVR_PORT].user_disabled_com) {
                     init_com(RCVR_PORT, 5);
                     if(first_key == 0) Sleep(1000);
                  }

                  if(debug_file) {
                     fprintf(debug_file, "!!! RCVR_PORT DATA LOSS RESET #%d: %d !!!\n", restart_count, rcvr_reset);
                  }
                  init_messages(0, 0);
                  do_survey = old_survey;
               }
reset_kbd_timer();
               refresh_page();
            }
            else {
               vidstr(0,0, RED, "COM DATA DEVICE UNAVAILABLE     ");
               refresh_page();
               if((com[RCVR_PORT].com_recover && !com[RCVR_PORT].user_disabled_com) && com[RCVR_PORT].com_running) {
                  if(com[RCVR_PORT].com_port || com[RCVR_PORT].usb_port || com[RCVR_PORT].IP_addr[0]) com[RCVR_PORT].process_com = 3;
                  goto com_retry;
               }
            }
         }

         if(rcvr_type == NO_RCVR) get_rcvr_message();
         else if(rcvr_type == TIDE_RCVR) ;
         else if(com[RCVR_PORT].process_com == 0) refresh_page();

process_extra_ports(3);
         break;
      }
      else {
         reset_com_timer(RCVR_PORT);
      }

      get_device_messages(i);  // process any data from the input devices
      process_extra_ports(4);   // process the extra devices

      if(get_mouse_info() == 0) {     // show queue data at the mouse cursor
         if(i == 0) refresh_page();   // keep refreshing page in case no serial data
      }
   }
process_extra_ports(5);

   return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movl %edi, -0x8(%rbp)
cmpl $0x0, 0x36ee92(%rip)    # 0x396204
je 0x27380
movl $0x0, -0x4(%rbp)
jmp 0x2768b
cmpl $0x0, 0x276da9(%rip)    # 0x29e130
je 0x27395
movl $0x1, -0x4(%rbp)
jmp 0x2768b
cmpl $0x0, 0x378464(%rip)    # 0x39f800
je 0x273aa
movl $0x1, -0x4(%rbp)
jmp 0x2768b
jmp 0x273ac
movl $0x0, -0x10(%rbp)
movl 0x25303f(%rip), %eax    # 0x27a3f8
cmpl $0x0, %eax
jne 0x273ca
callq 0xf450
movl $0x1, -0x10(%rbp)
movl $0x1, %edi
callq 0xc61e0
callq 0x2bcd0
callq 0x2b930
movl %eax, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jne 0x27642
movabsq $0x100000000, %rax      # imm = 0x100000000
cmpq %rax, 0x2506b4(%rip)    # 0x277ab0
jne 0x27403
callq 0x109470
movb $0x0, 0x36edfe(%rip)    # 0x396208
movl $0xa, %edi
callq 0x2b610
callq 0x28690
cmpq $0x400, 0x25068c(%rip) # imm = 0x400
jne 0x2742b
jmp 0x27601
cmpq $0x800000, 0x25067a(%rip) # imm = 0x800000
jne 0x2743d
jmp 0x275ff
cmpl $0x0, 0x35da64(%rip)    # 0x384ea8
je 0x2744b
jmp 0x275fd
movzbl 0x35da7e(%rip), %eax    # 0x384ed0
cmpl $0x1, %eax
jne 0x275fb
movb $0x2, 0x35da6e(%rip)    # 0x384ed0
cmpl $0x0, 0x35da4b(%rip)    # 0x384eb4
je 0x27598
xorl %esi, %esi
movl $0xc, %edx
leaq 0x14998b(%rip), %rcx    # 0x170e08
movl %esi, %edi
callq 0x10ee0
callq 0xb0d0
movl 0x36ed5d(%rip), %eax    # 0x3961ec
addl $0x1, %eax
movl %eax, 0x36ed54(%rip)    # 0x3961ec
cmpl $0x0, 0x35da35(%rip)    # 0x384ed4
je 0x274aa
cmpl $0x0, 0x35da00(%rip)    # 0x384ea8
je 0x274db
cmpl $0x0, 0x252d57(%rip)    # 0x27a208
jne 0x274db
movabsq $0x101b0e5018, %rax     # imm = 0x101B0E5018
andq 0x2505ec(%rip), %rax    # 0x277ab0
cmpq $0x0, %rax
jne 0x274db
cmpq $0x100, 0x2505db(%rip) # imm = 0x100
jne 0x2758c
jmp 0x274dd
movq 0x345b74(%rip), %rax    # 0x36d058
movl %eax, -0x14(%rbp)
movq $0x0, 0x345b66(%rip) # 0x36d058
movl $0x0, 0x252d0c(%rip)    # 0x27a208
movw $0x0, 0x36ff31(%rip)    # 0x397436
movw $0x0, 0x36ff24(%rip)    # 0x397432
movw $0x0, 0x36ff1d(%rip)    # 0x397434
cmpl $0x0, 0x35d9b6(%rip)    # 0x384ed4
je 0x2754d
cmpl $0x0, 0x35d981(%rip)    # 0x384ea8
jne 0x2754d
movl $0xa, %edi
movl $0x5, %esi
callq 0xc0e0
cmpl $0x0, 0x276bf9(%rip)    # 0x29e138
jne 0x2754b
movl $0x3e8, %edi            # imm = 0x3E8
callq 0xb8b0
jmp 0x2754d
cmpq $0x0, 0x27601b(%rip)    # 0x29d570
je 0x27578
movq 0x276012(%rip), %rdi    # 0x29d570
movl 0x36ec88(%rip), %edx    # 0x3961ec
movl 0x252c9e(%rip), %ecx    # 0x27a208
leaq 0x1498b8(%rip), %rsi    # 0x170e29
movb $0x0, %al
callq 0x9550
xorl %esi, %esi
movl %esi, %edi
callq 0x86290
movslq -0x14(%rbp), %rax
movq %rax, 0x345acc(%rip)    # 0x36d058
callq 0xb610
callq 0xb0d0
jmp 0x275f9
xorl %esi, %esi
movl $0xc, %edx
leaq 0x1498ae(%rip), %rcx    # 0x170e54
movl %esi, %edi
callq 0x10ee0
callq 0xb0d0
cmpl $0x0, 0x35d91b(%rip)    # 0x384ed4
je 0x275f7
cmpl $0x0, 0x35d8e6(%rip)    # 0x384ea8
jne 0x275f7
cmpl $0x0, 0x35d8e5(%rip)    # 0x384eb0
je 0x275f7
cmpl $0x0, 0x35b6ac(%rip)    # 0x382c80
jne 0x275e8
cmpl $0x0, 0x35b6a7(%rip)    # 0x382c84
jne 0x275e8
cmpb $0x0, 0x35b7a7(%rip)    # 0x382d8d
je 0x275f2
movl $0x3, 0x35d8c2(%rip)    # 0x384eb4
jmp 0x274dd
jmp 0x275f9
jmp 0x275fb
jmp 0x275fd
jmp 0x275ff
jmp 0x27601
cmpq $0x400, 0x2504a4(%rip) # imm = 0x400
jne 0x27615
callq 0x109470
jmp 0x27636
cmpq $0x800000, 0x250490(%rip) # imm = 0x800000
jne 0x27624
jmp 0x27634
cmpl $0x0, 0x35d889(%rip)    # 0x384eb4
jne 0x27632
callq 0xb0d0
jmp 0x27634
jmp 0x27636
movl $0x3, %edi
callq 0x2b510
jmp 0x2767a
movl $0xa, %edi
callq 0x2bd30
movl -0xc(%rbp), %edi
callq 0x2b5b0
movl $0x4, %edi
callq 0x2b510
callq 0x28690
cmpl $0x0, %eax
jne 0x27675
cmpl $0x0, -0xc(%rbp)
jne 0x27673
callq 0xb0d0
jmp 0x27675
jmp 0x273ac
movl $0x5, %edi
callq 0x2b510
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)