a75afd24c37f4b74f01bed8cdbd187037225e910
[silc.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 "irssi-version.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                 gui_expandos_init();
160                 gui_printtext_init();
161                 gui_readline_init();
162                 lastlog_init();
163                 mainwindows_init();
164                 mainwindow_activity_init();
165                 mainwindows_layout_init();
166                 gui_windows_init();
167                 statusbar_init();
168                 term_refresh_thaw();
169
170                 /* don't check settings with dummy mode */
171                 settings_check();
172         }
173
174         module_register("core", "fe-text");
175
176 #ifdef HAVE_STATIC_PERL
177         perl_core_init();
178         fe_perl_init();
179 #endif
180
181         dirty_check();
182
183         fe_common_core_finish_init();
184         signal_emit("irssi init finished", 0);
185 }
186
187 static void textui_deinit(void)
188 {
189         signal(SIGINT, SIG_DFL);
190
191         term_refresh_freeze();
192         while (modules != NULL)
193                 module_unload(modules->data);
194
195 #ifdef HAVE_STATIC_PERL
196         perl_core_deinit();
197         fe_perl_deinit();
198 #endif
199
200         dirty_check(); /* one last time to print any quit messages */
201         signal_remove("gui exit", (SIGNAL_FUNC) sig_exit);
202
203         if (dummy)
204                 term_dummy_deinit();
205         else {
206                 lastlog_deinit();
207                 statusbar_deinit();
208                 gui_printtext_deinit();
209                 gui_readline_deinit();
210                 gui_windows_deinit();
211                 mainwindows_layout_deinit();
212                 mainwindow_activity_deinit();
213                 mainwindows_deinit();
214                 gui_expandos_deinit();
215                 textbuffer_commands_deinit();
216                 textbuffer_view_deinit();
217                 textbuffer_deinit();
218
219                 term_refresh_thaw();
220                 term_deinit();
221         }
222
223         theme_unregister();
224
225         fe_silc_deinit();
226         fe_common_core_deinit();
227         silc_deinit();
228         core_deinit();
229 }
230
231 static void check_files(void)
232 {
233         struct stat statbuf;
234
235         if (stat(get_irssi_dir(), &statbuf) != 0) {
236                 /* ~/.irssi doesn't exist, first time running irssi */
237                 display_firsttimer = TRUE;
238         }
239 }
240
241 #ifdef WIN32
242 static void winsock_init(void)
243 {
244         WORD wVersionRequested;
245         WSADATA wsaData;
246
247         wVersionRequested = MAKEWORD(2, 2);
248
249         if (WSAStartup(wVersionRequested, &wsaData) != 0) {
250                 printf("Error initializing winsock\n");
251                 exit(1);
252         }
253 }
254 #endif
255
256 #ifdef USE_GC
257 #ifdef HAVE_GC_H
258 #  include <gc.h>
259 #else
260 #  include <gc/gc.h>
261 #endif
262
263 GMemVTable gc_mem_table = {
264         GC_malloc,
265         GC_realloc,
266         GC_free,
267
268         NULL, NULL, NULL
269 };
270 #endif
271
272 int main(int argc, char **argv)
273 {
274         static int version = 0;
275         static GOptionEntry options[] = {
276                 { "version", 'v', 0, G_OPTION_ARG_NONE, &version, "Display version", NULL },
277                 { NULL }
278         };
279
280 #ifdef USE_GC
281         g_mem_set_vtable(&gc_mem_table);
282 #endif
283
284         core_register_options();
285         fe_common_core_register_options();
286
287         srand(time(NULL));
288
289         dummy = FALSE;
290         quitting = FALSE;
291         core_preinit(argv[0]);
292
293         check_files();
294
295 #ifdef WIN32
296         winsock_init();
297 #endif
298 #ifdef HAVE_SOCKS
299         SOCKSinit(argv[0]);
300 #endif
301
302         /* setlocale() must be called at the beginning before any calls that
303            affect it, especially regexps seem to break if they're generated
304            before t his call.
305
306            locales aren't actually used for anything else than autodetection
307            of UTF-8 currently..
308
309            furthermore to get the users's charset with g_get_charset() properly
310            you have to call setlocale(LC_ALL, "") */
311         setlocale(LC_ALL, "");
312
313         textui_init();
314
315         args_register(options);
316         args_execute(argc, argv);
317
318         if (version) {
319                 printf(PACKAGE" " IRSSI_VERSION" (%d %04d)\n",
320                        IRSSI_VERSION_DATE, IRSSI_VERSION_TIME);
321                 return 0;
322         }
323
324         if (!dummy && !term_init()) {
325                 fprintf(stderr, "Can't initialize screen handling, quitting.\n");
326                 fprintf(stderr, "You can still use the dummy mode with -d parameter\n");
327                 return 1;
328         }
329
330         textui_finish_init();
331         main_loop = g_main_new(TRUE);
332
333         /* Does the same as g_main_run(main_loop), except we
334            can call our dirty-checker after each iteration */
335         while (!quitting) {
336 #ifdef USE_GC
337                 GC_collect_a_little();
338 #endif
339                 if (!dummy) term_refresh_freeze();
340                 g_main_iteration(TRUE);
341                 if (!dummy) term_refresh_thaw();
342
343                 if (reload_config) {
344                         /* SIGHUP received, do /RELOAD */
345                         reload_config = FALSE;
346                         signal_emit("command reload", 1, "");
347                 }
348
349                 dirty_check();
350         }
351
352         g_main_destroy(main_loop);
353         textui_deinit();
354
355         session_upgrade(); /* if we /UPGRADEd, start the new process */
356         return 0;
357 }