version 1.4w10UCIb17
[polyglot.git] / gui.cpp
1 // gui.cpp
2
3 // includes
4
5 #include <cstdarg>
6 #include <csignal>
7
8 #include "gui.h"
9 #include "main.h"
10
11 // constants
12
13 static const int StringSize = 4096;
14
15 // variables
16
17 gui_t GUI[1];
18
19 // functions
20
21 // sig_quit()
22
23 static void sig_quit(int dummy){
24     my_log("POLYGLOT *** SIGINT Received ***\n");
25     quit();
26 }
27
28
29 // gui_init()
30
31 void gui_init(gui_t *gui){
32
33 // the following is nice if the "GUI" is a console!
34     signal(SIGINT,sig_quit);
35 #ifdef _WIN32
36     signal(SIGTERM,SIG_IGN);
37 #ifdef SIGPIPE
38     signal(SIGPIPE,SIG_IGN);
39 #endif
40 #endif
41     
42 #ifdef _WIN32
43    (gui->pipeStdin).Open();
44 #else
45    
46    gui->io->in_fd = STDIN_FILENO;
47    gui->io->out_fd = STDOUT_FILENO;
48    gui->io->name = "GUI";
49    
50    io_init(gui->io);
51 #endif
52 }
53
54
55 // gui_get_non_blocking()
56
57 // this is only non_blocking on windows!
58
59 bool gui_get_non_blocking(gui_t * gui, char string[], int size) {
60
61    ASSERT(gui!=NULL);
62    ASSERT(string!=NULL);
63    ASSERT(size>=256);
64 #ifndef _WIN32
65    if (!io_get_line(gui->io,string,size)) { // EOF
66       my_log("POLYGLOT *** EOF from GUI ***\n");
67       quit();
68    }
69    return true;
70 #else
71    if ((gui->pipeStdin).LineInput(string)) {
72        my_log("GUI->Adapter: %s\n", string);
73        return true;
74    } else {
75         string[0]='\0';
76         return false;
77    }
78    
79 #endif
80 }
81
82 // gui_get()
83
84 void gui_get(gui_t * gui, char string[], int size) {
85     bool data_available;
86     while(true){
87         data_available=gui_get_non_blocking(gui, string, size);
88         if(!data_available){
89             Idle();
90         }else{
91             break;
92         }
93     }
94 }
95
96
97 // gui_send()
98
99 void gui_send(gui_t * gui, const char format[], ...) {
100
101    va_list arg_list;
102    char string[StringSize];
103
104    ASSERT(gui!=NULL);
105    ASSERT(format!=NULL);
106
107    // format
108
109    va_start(arg_list,format);
110    vsprintf(string,format,arg_list);
111    va_end(arg_list);
112
113    // send
114
115 #ifndef _WIN32
116    io_send(gui->io,"%s",string);
117 #else
118    printf("%s\n",string);
119    fflush(stdout);
120    my_log("Adapter->GUI: %s\n",string);
121 #endif
122 }
123