Added SILC Thread Queue API
[crypto.git] / apps / irssi / src / fe-text / term-dummy.c
1 /*
2  term-dummy.c : irssi
3
4     Copyright (C) 2001 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22 #include "signals.h"
23
24 #include "fe-windows.h"
25
26 static int newline;
27
28 static GIOChannel *stdin_channel;
29 static int readtag;
30 static GString *input;
31
32 static void sig_gui_printtext(WINDOW_REC *window, void *fgcolor,
33                               void *bgcolor, void *pflags,
34                               char *str, void *level)
35 {
36         if (newline) {
37                 newline = FALSE;
38                 printf("\r");
39         }
40
41         printf("%s", str);
42 }
43
44 static void sig_gui_printtext_finished(WINDOW_REC *window)
45 {
46         printf("\n");
47         newline = TRUE;
48 }
49
50 static void sig_window_created(WINDOW_REC *window)
51 {
52         window->width = 80;
53         window->height = 25;
54 }
55
56 static void readline(void)
57 {
58         unsigned char buffer[128];
59         char *p;
60         int ret, i;
61
62         ret = read(0, buffer, sizeof(buffer));
63         if (ret == 0 || (ret == -1 && errno != EINTR)) {
64                 /* lost terminal */
65                 signal_emit("command quit", 1, "Lost terminal");
66                 return;
67         }
68
69         for (i = 0; i < ret; i++)
70                 g_string_append_c(input, buffer[i]);
71
72         p = strchr(input->str, '\n');
73         if (p != NULL) {
74                 *p = '\0';
75                 signal_emit("send command", 3, input->str,
76                             active_win->active_server, active_win->active);
77                 *p = '\n';
78                 g_string_erase(input, 0, (int) (p-input->str)+1);
79         }
80 }
81
82 void term_dummy_init(void)
83 {
84         newline = TRUE;
85         input = g_string_new(NULL);
86
87         signal_add("gui print text", (SIGNAL_FUNC) sig_gui_printtext);
88         signal_add("gui print text finished", (SIGNAL_FUNC) sig_gui_printtext_finished);
89         signal_add("window created", (SIGNAL_FUNC) sig_window_created);
90
91         stdin_channel = g_io_channel_unix_new(0);
92         readtag = g_input_add_full(stdin_channel,
93                                    G_PRIORITY_HIGH, G_INPUT_READ,
94                                    (GInputFunction) readline, NULL);
95         g_io_channel_unref(stdin_channel);
96 }
97
98 void term_dummy_deinit(void)
99 {
100         signal_remove("gui print text", (SIGNAL_FUNC) sig_gui_printtext);
101         signal_remove("gui print text finished", (SIGNAL_FUNC) sig_gui_printtext_finished);
102         signal_remove("window created", (SIGNAL_FUNC) sig_window_created);
103
104         g_source_remove(readtag);
105         g_string_free(input, TRUE);
106 }