Add forgotten files 1.4.70b
[polyglot.git] / gui.cpp
diff --git a/gui.cpp b/gui.cpp
index 9905800..71d0cc0 100644 (file)
--- a/gui.cpp
+++ b/gui.cpp
@@ -3,6 +3,7 @@
 // includes
 
 #include <cstdarg>
+#include <csignal>
 
 #include "gui.h"
 #include "main.h"
@@ -17,18 +18,35 @@ gui_t GUI[1];
 
 // functions
 
+// sig_quit()
+
+static void sig_quit(int dummy){
+    my_log("POLYGLOT *** SIGINT Received ***\n");
+    quit();
+}
+
 
 // gui_init()
 
 void gui_init(gui_t *gui){
-    #ifdef _WIN32
-   (gui->pipeStdin).Open();
+
+// the following is nice if the "GUI" is a console!
+    signal(SIGINT,sig_quit);
+#ifdef _WIN32
+    signal(SIGTERM,SIG_IGN);
+#ifdef SIGPIPE
+    signal(SIGPIPE,SIG_IGN);
+#endif
+#endif
+    
+#ifdef _WIN32
+   (gui->io).Open();
 #else
    
    gui->io->in_fd = STDIN_FILENO;
    gui->io->out_fd = STDOUT_FILENO;
    gui->io->name = "GUI";
-
+   
    io_init(gui->io);
 #endif
 }
@@ -36,28 +54,31 @@ void gui_init(gui_t *gui){
 
 // gui_get_non_blocking()
 
-// this is only non_blocking on windows!
-
 bool gui_get_non_blocking(gui_t * gui, char string[], int size) {
 
    ASSERT(gui!=NULL);
    ASSERT(string!=NULL);
    ASSERT(size>=256);
 #ifndef _WIN32
-   if (!io_get_line(gui->io,string,size)) { // EOF
-      my_log("POLYGLOT *** EOF from GUI ***\n");
-      quit();
+   if(io_line_ready(gui->io)){
+       gui_get(GUI,string,StringSize);
+       return true;
+   }else{
+       string[0]='\0';
+       return false;
    }
-   return true;
 #else
-   if ((gui->pipeStdin).LineInput(string)) {
+   if((gui->io).EOF_()){
+        my_log("POLYGLOT *** EOF from GUI ***\n");
+        quit();
+        return true; // we never get here
+   }else if ((gui->io).GetBuffer(string)) {
        my_log("GUI->Adapter: %s\n", string);
        return true;
    } else {
         string[0]='\0';
         return false;
    }
-   
 #endif
 }
 
@@ -65,14 +86,19 @@ bool gui_get_non_blocking(gui_t * gui, char string[], int size) {
 
 void gui_get(gui_t * gui, char string[], int size) {
     bool data_available;
-    while(true){
-        data_available=gui_get_non_blocking(gui, string, size);
-        if(!data_available){
-            Idle();
-        }else{
-            break;
-        }
+#ifdef _WIN32
+    if((gui->io).EOF_()){
+        my_log("POLYGLOT *** EOF from GUI ***\n");
+        quit();
     }
+    (gui->io).LineInput(string);
+    my_log("GUI->Adapter: %s\n", string);
+#else
+    if (!io_get_line(gui->io,string,size)) { // EOF
+        my_log("POLYGLOT *** EOF from GUI ***\n");
+        quit();
+    }
+#endif
 }
 
 
@@ -97,8 +123,7 @@ void gui_send(gui_t * gui, const char format[], ...) {
 #ifndef _WIN32
    io_send(gui->io,"%s",string);
 #else
-   printf("%s\n",string);
-   fflush(stdout);
+   gui->io.LineOutput(string);
    my_log("Adapter->GUI: %s\n",string);
 #endif
 }