~ubuntu-branches/ubuntu/intrepid/irssi/intrepid-updates

« back to all changes in this revision

Viewing changes to src/fe-text/irssi.c

  • Committer: Bazaar Package Importer
  • Author(s): David Pashley
  • Date: 2005-12-10 21:25:51 UTC
  • Revision ID: james.westby@ubuntu.com-20051210212551-5qwm108g7inyu2f2
Tags: upstream-0.8.10
ImportĀ upstreamĀ versionĀ 0.8.10

Show diffs side-by-side

added added

removed removed

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