version 1.4w10UCIb22
[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).GetBuffer(string)) {
72        my_log("GUI->Adapter: %s\n", string);
73        return true;
74    } else {
75         string[0]='\0';
76         return false;
77    }
78 #endif
79 }
80
81 // gui_get()
82
83 void gui_get(gui_t * gui, char string[], int size) {
84     bool data_available;
85 #ifdef _WIN32
86     (gui->io).LineInput(string);
87     my_log("GUI->Adapter: %s\n", string);
88 #else
89     if (!io_get_line(gui->io,string,size)) { // EOF
90         my_log("POLYGLOT *** EOF from GUI ***\n");
91         quit();
92     }
93 #endif
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    gui->io.LineOutput(string);
119    my_log("Adapter->GUI: %s\n",string);
120 #endif
121 }
122