Added SILC Thread Queue API
[runtime.git] / apps / irssi / src / fe-text / silc.c
1 /*
2  irssi.c : irssi
3
4     Copyright (C) 1999-2000, 2007 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 "module-formats.h"
23 #include "modules-load.h"
24 #include "args.h"
25 #include "signals.h"
26 #include "levels.h"
27 #include "core.h"
28 #include "settings.h"
29 #include "session.h"
30
31 #include "printtext.h"
32 #include "fe-common-core.h"
33 #include "fe-common-silc.h"
34 #include "themes.h"
35
36 #include "term.h"
37 #include "gui-entry.h"
38 #include "mainwindows.h"
39 #include "gui-printtext.h"
40 #include "gui-readline.h"
41 #include "statusbar.h"
42 #include "gui-windows.h"
43 #include "textbuffer-reformat.h"
44
45 #include <signal.h>
46 #include <locale.h>
47
48 #ifdef HAVE_STATIC_PERL
49 void perl_core_init(void);
50 void perl_core_deinit(void);
51
52 void fe_perl_init(void);
53 void fe_perl_deinit(void);
54 #endif
55
56 void silc_init(void);
57 void silc_deinit(void);
58
59 void gui_expandos_init(void);
60 void gui_expandos_deinit(void);
61
62 void textbuffer_commands_init(void);
63 void textbuffer_commands_deinit(void);
64
65 void lastlog_init(void);
66 void lastlog_deinit(void);
67
68 void mainwindow_activity_init(void);
69 void mainwindow_activity_deinit(void);
70
71 void mainwindows_layout_init(void);
72 void mainwindows_layout_deinit(void);
73
74 void term_dummy_init(void);
75 void term_dummy_deinit(void);
76
77 static int dirty, full_redraw, dummy;
78
79 static GMainLoop *main_loop;
80 int quitting;
81
82 static int display_firsttimer = FALSE;
83
84 static void sig_exit(void)
85 {
86   quitting = TRUE;
87 }
88
89 /* redraw irssi's screen.. */
90 void irssi_redraw(void)
91 {
92         dirty = TRUE;
93         full_redraw = TRUE;
94 }
95
96 void irssi_set_dirty(void)
97 {
98         dirty = TRUE;
99 }
100
101 static void dirty_check(void)
102 {
103         if (!dirty || dummy)
104                 return;
105
106         term_resize_dirty();
107
108         if (full_redraw) {
109                 full_redraw = FALSE;
110
111                 /* first clear the screen so curses will be
112                    forced to redraw the screen */
113                 term_clear();
114                 term_refresh(NULL);
115
116                 mainwindows_redraw();
117                 statusbar_redraw(NULL, TRUE);
118         }
119
120         mainwindows_redraw_dirty();
121         statusbar_redraw_dirty();
122         term_refresh(NULL);
123
124         dirty = FALSE;
125 }
126
127 static void textui_init(void)
128 {
129 #ifdef SIGTRAP
130         struct sigaction act;
131
132         sigemptyset(&act.sa_mask);
133         act.sa_flags = 0;
134         act.sa_handler = SIG_IGN;
135         sigaction(SIGTRAP, &act, NULL);
136 #endif
137
138         irssi_gui = IRSSI_GUI_TEXT;
139         core_init();
140         silc_init();
141         fe_common_core_init();
142         fe_silc_init();
143
144         theme_register(gui_text_formats);
145         signal_add_last("gui exit", (SIGNAL_FUNC) sig_exit);
146 }
147
148 static void textui_finish_init(void)
149 {
150         quitting = FALSE;
151
152         if (dummy)
153                 term_dummy_init();
154         else {
155                 term_refresh_freeze();
156                 textbuffer_init();
157                 textbuffer_view_init();
158                 textbuffer_commands_init();
159                 textbuffer_reformat_init();
160                 gui_expandos_init();
161                 gui_printtext_init();
162                 gui_readline_init();
163                 lastlog_init();
164                 mainwindows_init();
165                 mainwindow_activity_init();
166                 mainwindows_layout_init();
167                 gui_windows_init();
168                 statusbar_init();
169                 term_refresh_thaw();
170
171                 /* don't check settings with dummy mode */
172                 settings_check();
173         }
174
175         module_register("core", "fe-text");
176
177 #ifdef HAVE_STATIC_PERL
178         perl_core_init();
179         fe_perl_init();
180 #endif
181
182         dirty_check();
183
184         fe_common_core_finish_init();
185         signal_emit("irssi init finished", 0);
186 }
187
188 static void textui_deinit(void)
189 {
190         signal(SIGINT, SIG_DFL);
191
192         term_refresh_freeze();
193         while (modules != NULL)
194                 module_unload(modules->data);
195
196 #ifdef HAVE_STATIC_PERL
197         perl_core_deinit();
198         fe_perl_deinit();
199 #endif
200
201         dirty_check(); /* one last time to print any quit messages */
202         signal_remove("gui exit", (SIGNAL_FUNC) sig_exit);
203
204         if (dummy)
205                 term_dummy_deinit();
206         else {
207                 lastlog_deinit();
208                 statusbar_deinit();
209                 gui_printtext_deinit();
210                 gui_readline_deinit();
211                 gui_windows_deinit();
212                 mainwindows_layout_deinit();
213                 mainwindow_activity_deinit();
214                 mainwindows_deinit();
215                 gui_expandos_deinit();
216                 textbuffer_reformat_deinit();
217                 textbuffer_commands_deinit();
218                 textbuffer_view_deinit();
219                 textbuffer_deinit();
220
221                 term_refresh_thaw();
222                 term_deinit();
223         }
224
225         theme_unregister();
226
227         fe_silc_deinit();
228         fe_common_core_deinit();
229         silc_deinit();
230         core_deinit();
231 }
232
233 static void check_oldcrap(void)
234 {
235         FILE *f;
236         char *path, str[256];
237         int found;
238
239         /* check that default.theme is up-to-date */
240         path = g_strdup_printf("%s/default.theme", get_irssi_dir());
241         f = fopen(path, "r+");
242         if (f == NULL) {
243                 g_free(path);
244                 return;
245         }
246         found = FALSE;
247         while (!found && fgets(str, sizeof(str), f) != NULL)
248                 found = strstr(str, "abstracts = ") != NULL;
249         fclose(f);
250
251         if (found) {
252                 g_free(path);
253                 return;
254         }
255
256         printf("\nYou seem to have old default.theme in "IRSSI_DIR_SHORT"/ directory.\n");
257         printf("Themeing system has changed a bit since last irssi release,\n");
258         printf("you should either delete your old default.theme or manually\n");
259         printf("merge it with the new default.theme.\n\n");
260         printf("Do you want to delete the old theme now? (Y/n)\n");
261
262         str[0] = '\0';
263         fgets(str, sizeof(str), stdin);
264         if (i_toupper(str[0]) == 'Y' || str[0] == '\n' || str[0] == '\0')
265                 remove(path);
266         g_free(path);
267 }
268
269 static void check_files(void)
270 {
271         struct stat statbuf;
272
273         if (stat(get_irssi_dir(), &statbuf) != 0) {
274                 /* ~/.irssi doesn't exist, first time running irssi */
275                 display_firsttimer = TRUE;
276         } else {
277                 check_oldcrap();
278         }
279 }
280
281 #ifdef WIN32
282 static void winsock_init(void)
283 {
284         WORD wVersionRequested;
285         WSADATA wsaData;
286
287         wVersionRequested = MAKEWORD(2, 2);
288
289         if (WSAStartup(wVersionRequested, &wsaData) != 0) {
290                 printf("Error initializing winsock\n");
291                 exit(1);
292         }
293 }
294 #endif
295
296 #ifdef USE_GC
297 #ifdef HAVE_GC_H
298 #  include <gc.h>
299 #else
300 #  include <gc/gc.h>
301 #endif
302
303 GMemVTable gc_mem_table = {
304         GC_malloc,
305         GC_realloc,
306         GC_free,
307
308         NULL, NULL, NULL
309 };
310 #endif
311
312 int main(int argc, char **argv)
313 {
314         static struct poptOption options[] = {
315 #if 0 /* --dummy is not available in SILC Client */
316                 { "dummy", 'd', POPT_ARG_NONE, &dummy, 0, "Use the dummy terminal mode", NULL },
317 #endif
318                 { NULL, '\0', 0, NULL }
319         };
320
321 #ifdef USE_GC
322         g_mem_set_vtable(&gc_mem_table);
323 #endif
324
325         srand(time(NULL));
326
327         dummy = FALSE;
328         quitting = FALSE;
329         core_init_paths(argc, argv);
330
331         check_files();
332
333 #ifdef WIN32
334         winsock_init();
335 #endif
336 #ifdef HAVE_SOCKS
337         SOCKSinit(argv[0]);
338 #endif
339 #ifdef ENABLE_NLS
340         /* initialize the i18n stuff */
341         bindtextdomain(PACKAGE, LOCALEDIR);
342         textdomain(PACKAGE);
343 #endif
344
345         /* setlocale() must be called at the beginning before any calls that
346            affect it, especially regexps seem to break if they're generated
347            before t his call.
348
349            locales aren't actually used for anything else than autodetection
350            of UTF-8 currently..
351
352            furthermore to get the users's charset with g_get_charset() properly
353            you have to call setlocale(LC_ALL, "") */
354         setlocale(LC_ALL, "");
355
356         textui_init();
357         args_register(options);
358         args_execute(argc, argv);
359
360         if (!dummy && !term_init()) {
361                 fprintf(stderr, "Can't initialize screen handling, quitting.\n");
362                 fprintf(stderr, "You can still use the dummy mode with -d parameter\n");
363                 return 1;
364         }
365
366         textui_finish_init();
367         main_loop = g_main_new(TRUE);
368
369         /* Does the same as g_main_run(main_loop), except we
370            can call our dirty-checker after each iteration */
371         while (!quitting) {
372 #ifdef USE_GC
373                 GC_collect_a_little();
374 #endif
375                 if (!dummy) term_refresh_freeze();
376                 g_main_iteration(TRUE);
377                 if (!dummy) term_refresh_thaw();
378
379                 if (reload_config) {
380                         /* SIGHUP received, do /RELOAD */
381                         reload_config = FALSE;
382                         signal_emit("command reload", 1, "");
383                 }
384
385                 dirty_check();
386         }
387
388         g_main_destroy(main_loop);
389         textui_deinit();
390
391         session_upgrade(); /* if we /UPGRADEd, start the new process */
392         return 0;
393 }