]> git.sesse.net Git - pistorm/blobdiff - emulator.c
support stealing the keyboard from the input layer
[pistorm] / emulator.c
index fec99a82e3c4248b74f230e0252eb2bfd04d4df9..ba4dcf1caf68f809bfa219fa2970e6f42163c7f2 100644 (file)
@@ -4,7 +4,6 @@
 #include "input/input.h"
 
 #include "platforms/amiga/Gayle.h"
-#include "platforms/amiga/gayle-ide/ide.h"
 #include "platforms/amiga/amiga-registers.h"
 #include "platforms/amiga/rtg/rtg.h"
 #include "platforms/amiga/hunk-reloc.h"
@@ -109,6 +108,8 @@ char keyboard_file[256] = "/dev/input/event1";
 
 uint64_t trig_irq = 0, serv_irq = 0;
 uint16_t irq_delay = 0;
+unsigned int amiga_reset=0, amiga_reset_last=0;
+unsigned int do_reset=0;
 
 void *ipl_task(void *args) {
   printf("IPL thread running\n");
@@ -139,6 +140,24 @@ void *ipl_task(void *args) {
         //usleep(0);
       }
     }
+    if(do_reset==0)
+    {
+      amiga_reset=(value & (1 << PIN_RESET));
+      if(amiga_reset!=amiga_reset_last)
+      {
+        amiga_reset_last=amiga_reset;
+        if(amiga_reset==0)
+        {
+          printf("Amiga Reset is down...\n");
+          do_reset=1;
+          M68K_END_TIMESLICE;
+        }
+        else
+        {
+          printf("Amiga Reset is up...\n");
+        }
+      }
+    }
 
     /*if (gayle_ide_enabled) {
       if (((gayle_int & 0x80) || gayle_a4k_int) && (get_ide(0)->drive[0].intrq || get_ide(0)->drive[1].intrq)) {
@@ -211,6 +230,15 @@ cpu_loop:
       last_irq = 0;
     }
   }*/
+  if (do_reset) {
+    cpu_pulse_reset();
+    m68k_pulse_reset();
+    do_reset=0;
+    usleep(1000000); // 1sec
+//    while(amiga_reset==0);
+//    printf("CPU emulation reset.\n");
+  }
+
 
   if (mouse_hook_enabled && (mouse_extra != 0x00)) {
     // mouse wheel events have occurred; unlike l/m/r buttons, these are queued as keypresses, so add to end of buffer
@@ -230,44 +258,58 @@ cpu_loop:
   }
   goto cpu_loop;
 
-stop_cpu_emulation:
+//stop_cpu_emulation:
   printf("[CPU] End of CPU thread\n");
 }
 
 void *keyboard_task() {
-  struct pollfd kbdfd[1];
-  int kpoll;
+  struct pollfd kbdpoll[1];
+  int kpollrc;
+  char grab_message[] = "[KBD] Grabbing keyboard from input layer\n",
+       ungrab_message[] = "[KBD] Ungrabbing keyboard\n";
 
   printf("[KBD] Keyboard thread started\n");
 
-  kbdfd[0].fd = keyboard_fd;
-  kbdfd[0].events = POLLIN;
+  // because we permit the keyboard to be grabbed on startup, quickly check if we need to grab it
+  if (kb_hook_enabled && cfg->keyboard_grab) {
+    printf(grab_message);
+    grab_device(keyboard_fd);
+  }
+
+  kbdpoll[0].fd = keyboard_fd;
+  kbdpoll[0].events = POLLIN;
 
 key_loop:
-  kpoll = poll(kbdfd, 1, KEY_POLL_INTERVAL_MSEC);
-  if ((kpoll > 0) && (kbdfd[0].revents & POLLHUP)) {
+  kpollrc = poll(kbdpoll, 1, KEY_POLL_INTERVAL_MSEC);
+  if ((kpollrc > 0) && (kbdpoll[0].revents & POLLHUP)) {
     // in the event that a keyboard is unplugged, keyboard_task will whiz up to 100% utilisation
     // this is undesired, so if the keyboard HUPs, end the thread without ending the emulation
     printf("[KBD] Keyboard node returned HUP (unplugged?)\n");
     goto key_end;
   }
 
-  // if kpoll > 0 then it contains number of events to pull, also check if POLLIN is set in revents
-  if ((kpoll <= 0) || !(kbdfd[0].revents & POLLIN)) {
+  // if kpollrc > 0 then it contains number of events to pull, also check if POLLIN is set in revents
+  if ((kpollrc <= 0) || !(kbdpoll[0].revents & POLLIN)) {
     goto key_loop;
   }
 
   while (get_key_char(&c, &c_code, &c_type)) {
     if (c && c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
       kb_hook_enabled = 1;
-      printf("Keyboard hook enabled.\n");
-    }
-    else if (kb_hook_enabled) {
+      printf("[KBD] Keyboard hook enabled.\n");
+      if (cfg->keyboard_grab) {
+        grab_device(keyboard_fd);
+        printf(grab_message);
+      }
+    } else if (kb_hook_enabled) {
       if (c == 0x1B && c_type) {
         kb_hook_enabled = 0;
-        printf("Keyboard hook disabled.\n");
-      }
-      else {
+        printf("[KBD] Keyboard hook disabled.\n");
+        if (cfg->keyboard_grab) {
+          release_device(keyboard_fd);
+          printf(ungrab_message);
+        }
+      } else {
         if (queue_keypress(c_code, c_type, cfg->platform->id) && int2_enabled && last_irq != 2) {
           //last_irq = 0;
           //M68K_SET_IRQ(2);
@@ -332,6 +374,7 @@ key_loop:
 
 key_end:
   printf("[KBD] Keyboard thread ending\n");
+  return (void*)NULL;
 }
 
 void stop_cpu_emulation(uint8_t disasm_cur) {
@@ -458,7 +501,11 @@ int main(int argc, char *argv[]) {
     }
   }
 
-  keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
+  if (cfg->keyboard_file)
+    keyboard_fd = open(cfg->keyboard_file, O_RDONLY | O_NONBLOCK);
+  else
+    keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
+
   if (keyboard_fd == -1) {
     printf("Failed to open keyboard event source.\n");
   }
@@ -558,7 +605,7 @@ void cpu_pulse_reset(void) {
   if (cfg->platform->handle_reset)
     cfg->platform->handle_reset(cfg);
 
-  m68k_write_memory_16(INTENA, 0x7FFF);
+  //m68k_write_memory_16(INTENA, 0x7FFF);
   ovl = 1;
   m68k_write_memory_8(0xbfe201, 0x0001);  // AMIGA OVL
   m68k_write_memory_8(0xbfe001, 0x0001);  // AMIGA OVL high (ROM@0x0)