1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
/*
* gui-curses-main.c - main loop for Curses GUI
*
* Copyright (C) 2003-2013 Sebastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* WeeChat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include "../../core/weechat.h"
#include "../../core/wee-command.h"
#include "../../core/wee-config.h"
#include "../../core/wee-hook.h"
#include "../../core/wee-log.h"
#include "../../core/wee-string.h"
#include "../../core/wee-utf8.h"
#include "../../core/wee-util.h"
#include "../../core/wee-version.h"
#include "../../plugins/plugin.h"
#include "../gui-main.h"
#include "../gui-bar.h"
#include "../gui-bar-item.h"
#include "../gui-bar-window.h"
#include "../gui-buffer.h"
#include "../gui-chat.h"
#include "../gui-color.h"
#include "../gui-cursor.h"
#include "../gui-filter.h"
#include "../gui-input.h"
#include "../gui-layout.h"
#include "../gui-history.h"
#include "../gui-mouse.h"
#include "../gui-nicklist.h"
#include "../gui-window.h"
#include "gui-curses.h"
int gui_reload_config = 0;
int gui_term_cols = 0;
int gui_term_lines = 0;
/*
* Pre-initializes GUI (called before gui_init).
*/
void
gui_main_pre_init (int *argc, char **argv[])
{
/* make C compiler happy */
(void) argc;
(void) argv;
/* pre-init colors */
gui_color_pre_init ();
/* init some variables for chat area */
gui_chat_init ();
}
/*
* Initializes GUI.
*/
void
gui_main_init ()
{
struct t_gui_buffer *ptr_buffer;
struct t_gui_bar *ptr_bar;
struct t_gui_bar_window *ptr_bar_win;
char title[256];
initscr ();
if (CONFIG_BOOLEAN(config_look_eat_newline_glitch))
gui_term_set_eat_newline_glitch (0);
curs_set (1);
noecho ();
nodelay (stdscr, TRUE);
raw ();
gui_color_init ();
/* build prefixes according to configuration */
gui_chat_prefix_build ();
refresh ();
gui_term_cols = COLS;
gui_term_lines = LINES;
gui_window_read_terminal_size ();
/* init clipboard buffer */
gui_input_clipboard = NULL;
/* get time length */
gui_chat_time_length = gui_chat_get_time_length ();
/* init bar items */
gui_bar_item_init ();
gui_init_ok = 0;
/* create core buffer */
ptr_buffer = gui_buffer_new (NULL, GUI_BUFFER_MAIN,
NULL, NULL, NULL, NULL);
if (ptr_buffer)
{
gui_init_ok = 1;
ptr_buffer->num_displayed = 1;
/* set short name */
if (!ptr_buffer->short_name)
ptr_buffer->short_name = strdup (GUI_BUFFER_MAIN);
/* set title for core buffer */
snprintf (title, sizeof (title), "WeeChat %s %s - %s",
version_get_version (),
WEECHAT_COPYRIGHT_DATE,
WEECHAT_WEBSITE);
gui_buffer_set_title (ptr_buffer, title);
/* create main window (using full space) */
if (gui_window_new (NULL, ptr_buffer, 0, 0,
gui_term_cols, gui_term_lines, 100, 100))
{
gui_current_window = gui_windows;
if (CONFIG_BOOLEAN(config_look_set_title))
gui_window_set_title (version_get_name_version ());
}
/*
* create bar windows for root bars (they were read from config,
* but no window was created, GUI was not initialized)
*/
for (ptr_bar = gui_bars; ptr_bar; ptr_bar = ptr_bar->next_bar)
{
if ((CONFIG_INTEGER(ptr_bar->options[GUI_BAR_OPTION_TYPE]) == GUI_BAR_TYPE_ROOT)
&& (!ptr_bar->bar_window))
{
gui_bar_window_new (ptr_bar, NULL);
}
}
for (ptr_bar_win = gui_windows->bar_windows;
ptr_bar_win; ptr_bar_win = ptr_bar_win->next_bar_window)
{
gui_bar_window_calculate_pos_size (ptr_bar_win, gui_windows);
gui_bar_window_create_win (ptr_bar_win);
}
}
if (CONFIG_BOOLEAN(config_look_mouse))
gui_mouse_enable ();
else
gui_mouse_disable ();
gui_window_set_bracketed_paste_mode (CONFIG_BOOLEAN(config_look_paste_bracketed));
}
/*
* Callback for system signal SIGQUIT: quits WeeChat.
*/
void
gui_main_signal_sigquit ()
{
log_printf (_("Signal %s received, exiting WeeChat..."),
"SIGQUIT");
hook_signal_send ("quit", WEECHAT_HOOK_SIGNAL_STRING, NULL);
weechat_quit = 1;
}
/*
* Callback for system signal SIGTERM: quits WeeChat.
*/
void
gui_main_signal_sigterm ()
{
log_printf (_("Signal %s received, exiting WeeChat..."),
"SIGTERM");
hook_signal_send ("quit", WEECHAT_HOOK_SIGNAL_STRING, NULL);
weechat_quit = 1;
}
/*
* Callback for system signal SIGHUP: reloads WeeChat configuration.
*/
void
gui_main_signal_sighup ()
{
/*
* SIGHUP signal is received when terminal is closed (exit of WeeChat
* without using /quit command), that's why we set only flag to reload
* configuration files later (when terminal is closed, config files are NOT
* reloaded, but they are if signal SIGHUP is sent to WeeChat by user)
*/
gui_reload_config = 1;
}
/*
* Callback for system signal SIGWINCH: refreshes screen.
*/
void
gui_main_signal_sigwinch ()
{
gui_window_ask_refresh (2);
}
/*
* Refreshs for windows, buffers, bars.
*/
void
gui_main_refreshs ()
{
struct t_gui_window *ptr_win;
struct t_gui_buffer *ptr_buffer;
struct t_gui_bar *ptr_bar;
/* refresh color buffer if needed */
if (gui_color_buffer_refresh_needed)
{
gui_color_buffer_display ();
gui_color_buffer_refresh_needed = 0;
}
/* refresh window if needed */
if (gui_window_refresh_needed)
{
gui_window_refresh_screen ((gui_window_refresh_needed > 1) ? 1 : 0);
gui_window_refresh_needed = 0;
}
/* refresh bars if needed */
for (ptr_bar = gui_bars; ptr_bar; ptr_bar = ptr_bar->next_bar)
{
if (ptr_bar->bar_refresh_needed)
{
gui_bar_draw (ptr_bar);
}
}
/* refresh window if needed (if asked during refresh of bars) */
if (gui_window_refresh_needed)
{
gui_window_refresh_screen ((gui_window_refresh_needed > 1) ? 1 : 0);
gui_window_refresh_needed = 0;
}
/* refresh windows if needed */
for (ptr_win = gui_windows; ptr_win; ptr_win = ptr_win->next_window)
{
if (ptr_win->refresh_needed)
{
gui_window_switch_to_buffer (ptr_win, ptr_win->buffer, 0);
gui_window_redraw_buffer (ptr_win->buffer);
ptr_win->refresh_needed = 0;
}
}
/* refresh chat buffers if needed */
for (ptr_buffer = gui_buffers; ptr_buffer;
ptr_buffer = ptr_buffer->next_buffer)
{
if (ptr_buffer->chat_refresh_needed)
{
gui_chat_draw (ptr_buffer,
(ptr_buffer->chat_refresh_needed) > 1 ? 1 : 0);
}
}
/* refresh bars if needed */
for (ptr_bar = gui_bars; ptr_bar; ptr_bar = ptr_bar->next_bar)
{
if (ptr_bar->bar_refresh_needed)
{
gui_bar_draw (ptr_bar);
}
}
/* move cursor (for cursor mode) */
if (gui_cursor_mode)
gui_window_move_cursor ();
}
/*
* Main loop for WeeChat with ncurses GUI.
*/
void
gui_main_loop ()
{
struct t_hook *hook_fd_keyboard;
struct timeval tv_timeout;
fd_set read_fds, write_fds, except_fds;
int max_fd;
int ready;
weechat_quit = 0;
/* catch SIGTERM signal: quit program */
util_catch_signal (SIGTERM, &gui_main_signal_sigterm);
util_catch_signal (SIGQUIT, &gui_main_signal_sigquit);
/* catch SIGHUP signal: reload configuration */
util_catch_signal (SIGHUP, &gui_main_signal_sighup);
/* catch SIGWINCH signal: redraw screen */
util_catch_signal (SIGWINCH, &gui_main_signal_sigwinch);
/* hook stdin (read keyboard) */
hook_fd_keyboard = hook_fd (NULL, STDIN_FILENO, 1, 0, 0,
&gui_key_read_cb, NULL);
gui_window_ask_refresh (1);
while (!weechat_quit)
{
/* reload config, if SIGHUP received */
if (gui_reload_config)
{
gui_reload_config = 0;
log_printf (_("Signal SIGHUP received, reloading configuration "
"files"));
command_reload (NULL, NULL, 0, NULL, NULL);
}
/* execute hook timers */
hook_timer_exec ();
/* auto reset of color pairs */
if (gui_color_pairs_auto_reset)
{
gui_color_reset_pairs ();
gui_color_pairs_auto_reset_last = time (NULL);
gui_color_pairs_auto_reset = 0;
gui_color_pairs_auto_reset_pending = 1;
}
gui_main_refreshs ();
if (gui_window_refresh_needed)
gui_main_refreshs ();
gui_color_pairs_auto_reset_pending = 0;
/* wait for keyboard or network activity */
FD_ZERO (&read_fds);
FD_ZERO (&write_fds);
FD_ZERO (&except_fds);
max_fd = hook_fd_set (&read_fds, &write_fds, &except_fds);
hook_timer_time_to_next (&tv_timeout);
ready = select (max_fd + 1, &read_fds, &write_fds, &except_fds,
&tv_timeout);
if (ready > 0)
{
hook_fd_exec (&read_fds, &write_fds, &except_fds);
}
}
/* remove keyboard hook */
unhook (hook_fd_keyboard);
}
/*
* Ends GUI.
*
* Argument "clean_exit" is 0 when WeeChat is crashing (we don't clean objects
* because WeeChat can crash again during this cleanup...).
*/
void
gui_main_end (int clean_exit)
{
if (clean_exit)
{
/*
* final refreshs, to see messages just before exiting
* (if we are upgrading, don't refresh anything!)
*/
if (!weechat_upgrading)
{
gui_main_refreshs ();
if (gui_window_refresh_needed)
gui_main_refreshs ();
}
/* disable bracketed paste mode */
gui_window_set_bracketed_paste_mode (0);
/* disable mouse */
gui_mouse_disable ();
/* remove bar items and bars */
gui_bar_item_end ();
gui_bar_free_all ();
/* remove filters */
gui_filter_free_all ();
/* free clipboard buffer */
if (gui_input_clipboard)
free (gui_input_clipboard);
/* delete layouts */
gui_layout_remove_all ();
/* delete all windows */
while (gui_windows)
{
gui_window_free (gui_windows);
}
gui_window_tree_free (&gui_windows_tree);
/* delete all buffers */
while (gui_buffers)
{
gui_buffer_close (gui_buffers);
}
gui_init_ok = 0;
/* delete global history */
gui_history_global_free ();
/* reset title */
if (CONFIG_BOOLEAN(config_look_set_title))
gui_window_set_title (NULL);
/* end color */
gui_color_end ();
/* free some variables used for chat area */
gui_chat_end ();
}
/* end of Curses output */
refresh ();
endwin ();
}
|