Add forgotten files 1.4.70b
[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->io).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 bool gui_get_non_blocking(gui_t * gui, char string[], int size) {
58
59    ASSERT(gui!=NULL);
60    ASSERT(string!=NULL);
61    ASSERT(size>=256);
62 #ifndef _WIN32
63    if(io_line_ready(gui->io)){
64        gui_get(GUI,string,StringSize);
65        return true;
66    }else{
67        string[0]='\0';
68        return false;
69    }
70 #else
71    if((gui->io).EOF_()){
72         my_log("POLYGLOT *** EOF from GUI ***\n");
73         quit();
74         return true; // we never get here
75    }else if ((gui->io).GetBuffer(string)) {
76        my_log("GUI->Adapter: %s\n", string);
77        return true;
78    } else {
79         string[0]='\0';
80         return false;
81    }
82 #endif
83 }
84
85 // gui_get()
86
87 void gui_get(gui_t * gui, char string[], int size) {
88     bool data_available;
89 #ifdef _WIN32
90     if((gui->io).EOF_()){
91         my_log("POLYGLOT *** EOF from GUI ***\n");
92         quit();
93     }
94     (gui->io).LineInput(string);
95     my_log("GUI->Adapter: %s\n", string);
96 #else
97     if (!io_get_line(gui->io,string,size)) { // EOF
98         my_log("POLYGLOT *** EOF from GUI ***\n");
99         quit();
100     }
101 #endif
102 }
103
104
105 // gui_send()
106
107 void gui_send(gui_t * gui, const char format[], ...) {
108
109    va_list arg_list;
110    char string[StringSize];
111
112    ASSERT(gui!=NULL);
113    ASSERT(format!=NULL);
114
115    // format
116
117    va_start(arg_list,format);
118    vsprintf(string,format,arg_list);
119    va_end(arg_list);
120
121    // send
122
123 #ifndef _WIN32
124    io_send(gui->io,"%s",string);
125 #else
126    gui->io.LineOutput(string);
127    my_log("Adapter->GUI: %s\n",string);
128 #endif
129 }
130