comparison finch/libgnt/gntmain.c @ 15817:0e3a8505ebbe

renamed gaim-text to finch
author Sean Egan <seanegan@gmail.com>
date Sun, 18 Mar 2007 19:38:15 +0000
parents
children 82b6fdd899a9
comparison
equal deleted inserted replaced
15816:317e7613e581 15817:0e3a8505ebbe
1 #define _GNU_SOURCE
2 #if defined(__APPLE__)
3 #define _XOPEN_SOURCE_EXTENDED
4 #endif
5
6 #include "config.h"
7
8 #include <gmodule.h>
9
10 #include <sys/types.h>
11 #include <sys/wait.h>
12
13 #include "gnt.h"
14 #include "gntbox.h"
15 #include "gntcolors.h"
16 #include "gntclipboard.h"
17 #include "gntkeys.h"
18 #include "gntmenu.h"
19 #include "gntstyle.h"
20 #include "gnttree.h"
21 #include "gntutils.h"
22 #include "gntwm.h"
23
24 #include <panel.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <locale.h>
29 #include <unistd.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <errno.h>
34
35 /**
36 * Notes: Interesting functions to look at:
37 * scr_dump, scr_init, scr_restore: for workspaces
38 *
39 * Need to wattrset for colors to use with PDCurses.
40 */
41
42 static GIOChannel *channel = NULL;
43
44 static gboolean ascii_only;
45 static gboolean mouse_enabled;
46
47 static void setup_io(void);
48
49 static gboolean refresh_screen();
50
51 GntWM *wm;
52 static GntClipboard *clipboard;
53
54 #define HOLDING_ESCAPE (escape_stuff.timer != 0)
55
56 static struct {
57 int timer;
58 } escape_stuff;
59
60 static gboolean
61 escape_timeout(gpointer data)
62 {
63 gnt_wm_process_input(wm, "\033");
64 escape_stuff.timer = 0;
65 return FALSE;
66 }
67
68 /**
69 * Mouse support:
70 * - bring a window on top if you click on its taskbar
71 * - click on the top-bar of the active window and drag+drop to move a window
72 * - click on a window to bring it to focus
73 * - allow scrolling in tree/textview on wheel-scroll event
74 * - click to activate button or select a row in tree
75 * wishlist:
76 * - have a little [X] on the windows, and clicking it will close that window.
77 */
78 static gboolean
79 detect_mouse_action(const char *buffer)
80 {
81 int x, y;
82 static enum {
83 MOUSE_NONE,
84 MOUSE_LEFT,
85 MOUSE_RIGHT,
86 MOUSE_MIDDLE
87 } button = MOUSE_NONE;
88 static GntWidget *remember = NULL;
89 static int offset = 0;
90 GntMouseEvent event;
91 GntWidget *widget = NULL;
92 PANEL *p = NULL;
93
94 if (!wm->ordered || buffer[0] != 27)
95 return FALSE;
96
97 buffer++;
98 if (strlen(buffer) < 5)
99 return FALSE;
100
101 x = buffer[3];
102 y = buffer[4];
103 if (x < 0) x += 256;
104 if (y < 0) y += 256;
105 x -= 33;
106 y -= 33;
107
108 while ((p = panel_below(p)) != NULL) {
109 const GntNode *node = panel_userptr(p);
110 GntWidget *wid;
111 if (!node)
112 continue;
113 wid = node->me;
114 if (x >= wid->priv.x && x < wid->priv.x + wid->priv.width) {
115 if (y >= wid->priv.y && y < wid->priv.y + wid->priv.height) {
116 widget = wid;
117 break;
118 }
119 }
120 }
121
122 if (strncmp(buffer, "[M ", 3) == 0) {
123 /* left button down */
124 /* Bring the window you clicked on to front */
125 /* If you click on the topbar, then you can drag to move the window */
126 event = GNT_LEFT_MOUSE_DOWN;
127 } else if (strncmp(buffer, "[M\"", 3) == 0) {
128 /* right button down */
129 event = GNT_RIGHT_MOUSE_DOWN;
130 } else if (strncmp(buffer, "[M!", 3) == 0) {
131 /* middle button down */
132 event = GNT_MIDDLE_MOUSE_DOWN;
133 } else if (strncmp(buffer, "[M`", 3) == 0) {
134 /* wheel up*/
135 event = GNT_MOUSE_SCROLL_UP;
136 } else if (strncmp(buffer, "[Ma", 3) == 0) {
137 /* wheel down */
138 event = GNT_MOUSE_SCROLL_DOWN;
139 } else if (strncmp(buffer, "[M#", 3) == 0) {
140 /* button up */
141 event = GNT_MOUSE_UP;
142 } else
143 return FALSE;
144
145 if (gnt_wm_process_click(wm, event, x, y, widget))
146 return TRUE;
147
148 if (event == GNT_LEFT_MOUSE_DOWN && widget && widget != wm->_list.window &&
149 !GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_TRANSIENT)) {
150 if (widget != wm->ordered->data) {
151 gnt_wm_raise_window(wm, widget);
152 }
153 if (y == widget->priv.y) {
154 offset = x - widget->priv.x;
155 remember = widget;
156 button = MOUSE_LEFT;
157 }
158 } else if (event == GNT_MOUSE_UP) {
159 if (button == MOUSE_NONE && y == getmaxy(stdscr) - 1) {
160 /* Clicked on the taskbar */
161 int n = g_list_length(wm->list);
162 if (n) {
163 int width = getmaxx(stdscr) / n;
164 gnt_bindable_perform_action_named(GNT_BINDABLE(wm), "switch-window-n", x/width, NULL);
165 }
166 } else if (button == MOUSE_LEFT && remember) {
167 x -= offset;
168 if (x < 0) x = 0;
169 if (y < 0) y = 0;
170 gnt_screen_move_widget(remember, x, y);
171 }
172 button = MOUSE_NONE;
173 remember = NULL;
174 offset = 0;
175 }
176
177 gnt_widget_clicked(widget, event, x, y);
178 return TRUE;
179 }
180
181 static gboolean
182 io_invoke_error(GIOChannel *source, GIOCondition cond, gpointer data)
183 {
184 int id = GPOINTER_TO_INT(data);
185 g_source_remove(id);
186 g_io_channel_unref(source);
187
188 channel = NULL;
189 setup_io();
190 return TRUE;
191 }
192
193 static gboolean
194 io_invoke(GIOChannel *source, GIOCondition cond, gpointer null)
195 {
196 char keys[256];
197 int rd = read(STDIN_FILENO, keys + HOLDING_ESCAPE, sizeof(keys) - 1 - HOLDING_ESCAPE);
198 char *k;
199 if (rd < 0)
200 {
201 int ch = getch(); /* This should return ERR, but let's see what it really returns */
202 endwin();
203 printf("ERROR: %s\n", strerror(errno));
204 printf("File descriptor is: %d\n\nGIOChannel is: %p\ngetch() = %d\n", STDIN_FILENO, source, ch);
205 raise(SIGABRT);
206 }
207 else if (rd == 0)
208 {
209 endwin();
210 printf("EOF\n");
211 raise(SIGABRT);
212 }
213
214 rd += HOLDING_ESCAPE;
215 keys[rd] = 0;
216 if (mouse_enabled && detect_mouse_action(keys))
217 return TRUE;
218
219 if (HOLDING_ESCAPE)
220 keys[0] = '\033';
221 k = keys;
222 while (rd) {
223 char back;
224 int p;
225
226 if (k[0] == '\033' && rd == 1) {
227 if (escape_stuff.timer) {
228 gnt_wm_process_input(wm, "\033\033");
229 g_source_remove(escape_stuff.timer);
230 escape_stuff.timer = 0;
231 break;
232 }
233 escape_stuff.timer = g_timeout_add(250, escape_timeout, NULL);
234 break;
235 }
236
237 gnt_keys_refine(k);
238 p = MAX(1, gnt_keys_find_combination(k));
239 back = k[p];
240 k[p] = '\0';
241 gnt_wm_process_input(wm, k); /* XXX: */
242 k[p] = back;
243 rd -= p;
244 k += p;
245 }
246
247 return TRUE;
248 }
249
250 static void
251 setup_io()
252 {
253 int result;
254 channel = g_io_channel_unix_new(STDIN_FILENO);
255 g_io_channel_set_close_on_unref(channel, TRUE);
256
257 #if 0
258 g_io_channel_set_encoding(channel, NULL, NULL);
259 g_io_channel_set_buffered(channel, FALSE);
260 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL );
261 #endif
262
263 result = g_io_add_watch_full(channel, G_PRIORITY_HIGH,
264 (G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI),
265 io_invoke, NULL, NULL);
266
267 g_io_add_watch_full(channel, G_PRIORITY_HIGH,
268 (G_IO_NVAL),
269 io_invoke_error, GINT_TO_POINTER(result), NULL);
270
271 g_io_channel_unref(channel); /* Apparently this caused crashes for some people.
272 But irssi does this, so I am going to assume the
273 crashes were caused by some other stuff. */
274
275 g_printerr("gntmain: setting up IO\n");
276 }
277
278 static gboolean
279 refresh_screen()
280 {
281 gnt_bindable_perform_action_named(GNT_BINDABLE(wm), "refresh-screen", NULL);
282 return FALSE;
283 }
284
285 /* Xerox */
286 static void
287 clean_pid(void)
288 {
289 int status;
290 pid_t pid;
291
292 do {
293 pid = waitpid(-1, &status, WNOHANG);
294 } while (pid != 0 && pid != (pid_t)-1);
295
296 if ((pid == (pid_t) - 1) && (errno != ECHILD)) {
297 char errmsg[BUFSIZ];
298 g_snprintf(errmsg, BUFSIZ, "Warning: waitpid() returned %d", pid);
299 perror(errmsg);
300 }
301 }
302
303 static void
304 sighandler(int sig)
305 {
306 switch (sig) {
307 #ifdef SIGWINCH
308 case SIGWINCH:
309 werase(stdscr);
310 wrefresh(stdscr);
311 g_idle_add(refresh_screen, NULL);
312 signal(SIGWINCH, sighandler);
313 break;
314 #endif
315 case SIGCHLD:
316 clean_pid();
317 signal(SIGCHLD, sighandler);
318 break;
319 }
320 }
321
322 static void
323 init_wm()
324 {
325 const char *name = gnt_style_get(GNT_STYLE_WM);
326 gpointer handle;
327
328 if (name && *name) {
329 handle = g_module_open(name, G_MODULE_BIND_LAZY);
330 if (handle) {
331 gboolean (*init)(GntWM **);
332 if (g_module_symbol(handle, "gntwm_init", (gpointer)&init)) {
333 init(&wm);
334 }
335 }
336 }
337 if (wm == NULL)
338 wm = g_object_new(GNT_TYPE_WM, NULL);
339 }
340
341 void gnt_init()
342 {
343 char *filename;
344 const char *locale;
345
346 if (channel)
347 return;
348
349 locale = setlocale(LC_ALL, "");
350
351 setup_io();
352
353 if (locale && (strstr(locale, "UTF") || strstr(locale, "utf")))
354 ascii_only = FALSE;
355 else
356 ascii_only = TRUE;
357
358 initscr();
359 typeahead(-1);
360 noecho();
361 curs_set(0);
362
363 gnt_init_keys();
364 gnt_init_styles();
365
366 filename = g_build_filename(g_get_home_dir(), ".gntrc", NULL);
367 gnt_style_read_configure_file(filename);
368 g_free(filename);
369
370 gnt_init_colors();
371
372 wbkgdset(stdscr, '\0' | COLOR_PAIR(GNT_COLOR_NORMAL));
373 refresh();
374
375 #ifdef ALL_MOUSE_EVENTS
376 if ((mouse_enabled = gnt_style_get_bool(GNT_STYLE_MOUSE, FALSE)))
377 mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
378 #endif
379
380 wbkgdset(stdscr, '\0' | COLOR_PAIR(GNT_COLOR_NORMAL));
381 werase(stdscr);
382 wrefresh(stdscr);
383
384 #ifdef SIGWINCH
385 signal(SIGWINCH, sighandler);
386 #endif
387 signal(SIGCHLD, sighandler);
388 signal(SIGPIPE, SIG_IGN);
389
390 g_type_init();
391
392 init_wm();
393
394 clipboard = g_object_new(GNT_TYPE_CLIPBOARD, NULL);
395 }
396
397 void gnt_main()
398 {
399 wm->loop = g_main_loop_new(NULL, FALSE);
400 g_main_loop_run(wm->loop);
401 }
402
403 /*********************************
404 * Stuff for 'window management' *
405 *********************************/
406
407 void gnt_screen_occupy(GntWidget *widget)
408 {
409 gnt_wm_new_window(wm, widget);
410 }
411
412 void gnt_screen_release(GntWidget *widget)
413 {
414 gnt_wm_window_close(wm, widget);
415 }
416
417 void gnt_screen_update(GntWidget *widget)
418 {
419 gnt_wm_update_window(wm, widget);
420 }
421
422 gboolean gnt_widget_has_focus(GntWidget *widget)
423 {
424 GntWidget *w;
425 if (!widget)
426 return FALSE;
427
428 if (GNT_IS_MENU(widget))
429 return TRUE;
430
431 w = widget;
432
433 while (widget->parent)
434 widget = widget->parent;
435
436 if (widget == wm->_list.window)
437 return TRUE;
438 if (wm->ordered && wm->ordered->data == widget) {
439 if (GNT_IS_BOX(widget) &&
440 (GNT_BOX(widget)->active == w || widget == w))
441 return TRUE;
442 }
443 return FALSE;
444 }
445
446 void gnt_widget_set_urgent(GntWidget *widget)
447 {
448 while (widget->parent)
449 widget = widget->parent;
450
451 if (wm->ordered && wm->ordered->data == widget)
452 return;
453
454 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_URGENT);
455
456 gnt_wm_update_window(wm, widget);
457 }
458
459 void gnt_quit()
460 {
461 g_hash_table_destroy(wm->nodes); /* XXX: */
462 update_panels();
463 doupdate();
464 gnt_uninit_colors();
465 gnt_uninit_styles();
466 endwin();
467 }
468
469 gboolean gnt_ascii_only()
470 {
471 return ascii_only;
472 }
473
474 void gnt_screen_resize_widget(GntWidget *widget, int width, int height)
475 {
476 gnt_wm_resize_window(wm, widget, width, height);
477 }
478
479 void gnt_screen_move_widget(GntWidget *widget, int x, int y)
480 {
481 gnt_wm_move_window(wm, widget, x, y);
482 }
483
484 void gnt_screen_rename_widget(GntWidget *widget, const char *text)
485 {
486 gnt_box_set_title(GNT_BOX(widget), text);
487 gnt_widget_draw(widget);
488 gnt_wm_update_window(wm, widget);
489 }
490
491 void gnt_register_action(const char *label, void (*callback)())
492 {
493 GntAction *action = g_new0(GntAction, 1);
494 action->label = g_strdup(label);
495 action->callback = callback;
496
497 wm->acts = g_list_append(wm->acts, action);
498 }
499
500 static void
501 reset_menu(GntWidget *widget, gpointer null)
502 {
503 wm->menu = NULL;
504 }
505
506 gboolean gnt_screen_menu_show(gpointer newmenu)
507 {
508 if (wm->menu) {
509 /* For now, if a menu is being displayed, then another menu
510 * can NOT take over. */
511 return FALSE;
512 }
513
514 wm->menu = newmenu;
515 GNT_WIDGET_UNSET_FLAGS(GNT_WIDGET(wm->menu), GNT_WIDGET_INVISIBLE);
516 gnt_widget_draw(GNT_WIDGET(wm->menu));
517
518 g_signal_connect(G_OBJECT(wm->menu), "hide", G_CALLBACK(reset_menu), NULL);
519
520 return TRUE;
521 }
522
523 void gnt_set_clipboard_string(gchar *string)
524 {
525 gnt_clipboard_set_string(clipboard, string);
526 }
527
528 GntClipboard *gnt_get_clipboard()
529 {
530 return clipboard;
531 }
532 gchar *gnt_get_clipboard_string()
533 {
534 return gnt_clipboard_get_string(clipboard);
535 }