Update to 24f58c58bb8d22c0e8e6c5ce43c536c47b719bc6
[gnt.git] / gntmain.c
blob62918057148c6d06aba27237da92e787d83f73b2
1 /**
2 * GNT - The GLib Ncurses Toolkit
4 * GNT is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #define _GNU_SOURCE
24 #if (defined(__APPLE__) || defined(__unix__)) && !defined(__FreeBSD__)
25 #define _XOPEN_SOURCE_EXTENDED
26 #endif
28 #include "config.h"
30 #include <gmodule.h>
32 #include <sys/types.h>
33 #include <sys/wait.h>
35 #include "gnt.h"
36 #include "gntbox.h"
37 #include "gntbutton.h"
38 #include "gntcolors.h"
39 #include "gntclipboard.h"
40 #include "gntkeys.h"
41 #include "gntlabel.h"
42 #include "gntmenu.h"
43 #include "gntstyle.h"
44 #include "gnttree.h"
45 #include "gntutils.h"
46 #include "gntwindow.h"
47 #include "gntwm.h"
49 #include <panel.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <locale.h>
54 #include <unistd.h>
55 #include <signal.h>
56 #include <string.h>
57 #include <ctype.h>
58 #include <errno.h>
60 /**
61 * Notes: Interesting functions to look at:
62 * scr_dump, scr_init, scr_restore: for workspaces
64 * Need to wattrset for colors to use with PDCurses.
67 static GIOChannel *channel = NULL;
68 static int channel_read_callback;
70 static gboolean ascii_only;
71 static gboolean mouse_enabled;
73 static void setup_io(void);
75 static gboolean refresh_screen(void);
77 static GntWM *wm;
78 static GntClipboard *clipboard;
80 #define HOLDING_ESCAPE (escape_stuff.timer != 0)
82 static struct {
83 int timer;
84 } escape_stuff;
86 static gboolean
87 escape_timeout(gpointer data)
89 gnt_wm_process_input(wm, "\033");
90 escape_stuff.timer = 0;
91 return FALSE;
94 /**
95 * Mouse support:
96 * - bring a window on top if you click on its taskbar
97 * - click on the top-bar of the active window and drag+drop to move a window
98 * - click on a window to bring it to focus
99 * - allow scrolling in tree/textview on wheel-scroll event
100 * - click to activate button or select a row in tree
101 * wishlist:
102 * - have a little [X] on the windows, and clicking it will close that window.
104 static gboolean
105 detect_mouse_action(const char *buffer)
107 int x, y;
108 static enum {
109 MOUSE_NONE,
110 MOUSE_LEFT,
111 MOUSE_RIGHT,
112 MOUSE_MIDDLE
113 } button = MOUSE_NONE;
114 static GntWidget *remember = NULL;
115 static int offset = 0;
116 GntMouseEvent event;
117 GntWidget *widget = NULL;
118 PANEL *p = NULL;
120 if (!wm->cws->ordered || buffer[0] != 27)
121 return FALSE;
123 buffer++;
124 if (strlen(buffer) < 5)
125 return FALSE;
127 x = buffer[3];
128 y = buffer[4];
129 if (x < 0) x += 256;
130 if (y < 0) y += 256;
131 x -= 33;
132 y -= 33;
134 while ((p = panel_below(p)) != NULL) {
135 const GntNode *node = panel_userptr(p);
136 GntWidget *wid;
137 if (!node)
138 continue;
139 wid = node->me;
140 if (x >= wid->priv.x && x < wid->priv.x + wid->priv.width) {
141 if (y >= wid->priv.y && y < wid->priv.y + wid->priv.height) {
142 widget = wid;
143 break;
148 if (strncmp(buffer, "[M ", 3) == 0) {
149 /* left button down */
150 /* Bring the window you clicked on to front */
151 /* If you click on the topbar, then you can drag to move the window */
152 event = GNT_LEFT_MOUSE_DOWN;
153 } else if (strncmp(buffer, "[M\"", 3) == 0) {
154 /* right button down */
155 event = GNT_RIGHT_MOUSE_DOWN;
156 } else if (strncmp(buffer, "[M!", 3) == 0) {
157 /* middle button down */
158 event = GNT_MIDDLE_MOUSE_DOWN;
159 } else if (strncmp(buffer, "[M`", 3) == 0) {
160 /* wheel up*/
161 event = GNT_MOUSE_SCROLL_UP;
162 } else if (strncmp(buffer, "[Ma", 3) == 0) {
163 /* wheel down */
164 event = GNT_MOUSE_SCROLL_DOWN;
165 } else if (strncmp(buffer, "[M#", 3) == 0) {
166 /* button up */
167 event = GNT_MOUSE_UP;
168 } else
169 return FALSE;
171 if (widget && gnt_wm_process_click(wm, event, x, y, widget))
172 return TRUE;
174 if (event == GNT_LEFT_MOUSE_DOWN && widget && widget != wm->_list.window &&
175 !GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_TRANSIENT)) {
176 if (widget != wm->cws->ordered->data) {
177 gnt_wm_raise_window(wm, widget);
179 if (y == widget->priv.y) {
180 offset = x - widget->priv.x;
181 remember = widget;
182 button = MOUSE_LEFT;
184 } else if (event == GNT_MOUSE_UP) {
185 if (button == MOUSE_NONE && y == getmaxy(stdscr) - 1) {
186 /* Clicked on the taskbar */
187 int n = g_list_length(wm->cws->list);
188 if (n) {
189 int width = getmaxx(stdscr) / n;
190 gnt_bindable_perform_action_named(GNT_BINDABLE(wm), "switch-window-n", x/width, NULL);
192 } else if (button == MOUSE_LEFT && remember) {
193 x -= offset;
194 if (x < 0) x = 0;
195 if (y < 0) y = 0;
196 gnt_screen_move_widget(remember, x, y);
198 button = MOUSE_NONE;
199 remember = NULL;
200 offset = 0;
203 if (widget)
204 gnt_widget_clicked(widget, event, x, y);
205 return TRUE;
208 static gboolean
209 io_invoke_error(GIOChannel *source, GIOCondition cond, gpointer data)
211 int id = GPOINTER_TO_INT(data);
212 g_source_remove(id);
213 g_io_channel_unref(source);
215 channel = NULL;
216 setup_io();
217 return TRUE;
220 static gboolean
221 io_invoke(GIOChannel *source, GIOCondition cond, gpointer null)
223 char keys[256];
224 gssize rd;
225 char *k;
226 char *cvrt = NULL;
228 if (wm->mode == GNT_KP_MODE_WAIT_ON_CHILD)
229 return FALSE;
231 rd = read(STDIN_FILENO, keys + HOLDING_ESCAPE, sizeof(keys) - 1 - HOLDING_ESCAPE);
232 if (rd < 0)
234 int ch = getch(); /* This should return ERR, but let's see what it really returns */
235 endwin();
236 printf("ERROR: %s\n", strerror(errno));
237 printf("File descriptor is: %d\n\nGIOChannel is: %p\ngetch() = %d\n", STDIN_FILENO, source, ch);
238 raise(SIGABRT);
240 else if (rd == 0)
242 endwin();
243 printf("EOF\n");
244 raise(SIGABRT);
247 rd += HOLDING_ESCAPE;
248 if (HOLDING_ESCAPE)
249 keys[0] = '\033';
250 keys[rd] = 0;
251 gnt_wm_set_event_stack(wm, TRUE);
253 cvrt = g_locale_to_utf8(keys, rd, (gsize*)&rd, NULL, NULL);
254 k = cvrt ? cvrt : keys;
255 if (mouse_enabled && detect_mouse_action(k))
256 goto end;
258 #if 0
259 /* I am not sure what's happening here. If this actually does something,
260 * then this needs to go in gnt_keys_refine. */
261 if (*k < 0) { /* Alt not sending ESC* */
262 *(k + 1) = 128 - *k;
263 *k = 27;
264 *(k + 2) = 0;
265 rd++;
267 #endif
269 while (rd) {
270 char back;
271 int p;
273 if (k[0] == '\033' && rd == 1) {
274 if (escape_stuff.timer) {
275 gnt_wm_process_input(wm, "\033\033");
276 g_source_remove(escape_stuff.timer);
277 escape_stuff.timer = 0;
278 break;
280 escape_stuff.timer = g_timeout_add(250, escape_timeout, NULL);
281 break;
284 gnt_keys_refine(k);
285 p = MAX(1, gnt_keys_find_combination(k));
286 back = k[p];
287 k[p] = '\0';
288 gnt_wm_process_input(wm, k); /* XXX: */
289 k[p] = back;
290 rd -= p;
291 k += p;
293 end:
294 if (wm)
295 gnt_wm_set_event_stack(wm, FALSE);
296 g_free(cvrt);
297 return TRUE;
300 static void
301 setup_io()
303 int result;
304 channel = g_io_channel_unix_new(STDIN_FILENO);
305 g_io_channel_set_close_on_unref(channel, TRUE);
307 #if 0
308 g_io_channel_set_encoding(channel, NULL, NULL);
309 g_io_channel_set_buffered(channel, FALSE);
310 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL );
311 #endif
313 channel_read_callback = result = g_io_add_watch_full(channel, G_PRIORITY_HIGH,
314 (G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI),
315 io_invoke, NULL, NULL);
317 g_io_add_watch_full(channel, G_PRIORITY_HIGH,
318 (G_IO_NVAL),
319 io_invoke_error, GINT_TO_POINTER(result), NULL);
321 g_io_channel_unref(channel); /* Apparently this caused crashes for some people.
322 But irssi does this, so I am going to assume the
323 crashes were caused by some other stuff. */
325 g_printerr("gntmain: setting up IO (%d)\n", channel_read_callback);
328 static gboolean
329 refresh_screen(void)
331 gnt_bindable_perform_action_named(GNT_BINDABLE(wm), "refresh-screen", NULL);
332 return FALSE;
335 /* Xerox */
336 static void
337 clean_pid(void)
339 int status;
340 pid_t pid;
342 do {
343 pid = waitpid(-1, &status, WNOHANG);
344 } while (pid != 0 && pid != (pid_t)-1);
346 if ((pid == (pid_t) - 1) && (errno != ECHILD)) {
347 char errmsg[BUFSIZ];
348 g_snprintf(errmsg, BUFSIZ, "Warning: waitpid() returned %d", pid);
349 perror(errmsg);
353 static void
354 exit_confirmed(gpointer null)
356 gnt_bindable_perform_action_named(GNT_BINDABLE(wm), "wm-quit", NULL);
359 static void
360 exit_win_close(GntWidget *w, GntWidget **win)
362 *win = NULL;
365 static void
366 ask_before_exit(void)
368 static GntWidget *win = NULL;
369 GntWidget *bbox, *button;
371 if (wm->menu) {
372 do {
373 gnt_widget_hide(GNT_WIDGET(wm->menu));
374 if (wm->menu)
375 wm->menu = wm->menu->parentmenu;
376 } while (wm->menu);
379 if (win)
380 goto raise;
382 win = gnt_vwindow_new(FALSE);
383 gnt_box_add_widget(GNT_BOX(win), gnt_label_new("Are you sure you want to quit?"));
384 gnt_box_set_title(GNT_BOX(win), "Quit?");
385 gnt_box_set_alignment(GNT_BOX(win), GNT_ALIGN_MID);
386 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(exit_win_close), &win);
388 bbox = gnt_hbox_new(FALSE);
389 gnt_box_add_widget(GNT_BOX(win), bbox);
391 button = gnt_button_new("Quit");
392 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(exit_confirmed), NULL);
393 gnt_box_add_widget(GNT_BOX(bbox), button);
395 button = gnt_button_new("Cancel");
396 g_signal_connect_swapped(G_OBJECT(button), "activate", G_CALLBACK(gnt_widget_destroy), win);
397 gnt_box_add_widget(GNT_BOX(bbox), button);
399 gnt_widget_show(win);
400 raise:
401 gnt_wm_raise_window(wm, win);
404 #ifdef SIGWINCH
405 static void (*org_winch_handler)(int);
406 #endif
408 static void
409 sighandler(int sig)
411 switch (sig) {
412 #ifdef SIGWINCH
413 case SIGWINCH:
414 erase();
415 g_idle_add((GSourceFunc)refresh_screen, NULL);
416 if (org_winch_handler)
417 org_winch_handler(sig);
418 signal(SIGWINCH, sighandler);
419 break;
420 #endif
421 case SIGCHLD:
422 clean_pid();
423 signal(SIGCHLD, sighandler);
424 break;
425 case SIGINT:
426 ask_before_exit();
427 signal(SIGINT, sighandler);
428 break;
432 static void
433 init_wm(void)
435 const char *name = gnt_style_get(GNT_STYLE_WM);
436 gpointer handle;
438 if (name && *name) {
439 handle = g_module_open(name, G_MODULE_BIND_LAZY);
440 if (handle) {
441 gboolean (*init)(GntWM **);
442 if (g_module_symbol(handle, "gntwm_init", (gpointer)&init)) {
443 init(&wm);
447 if (wm == NULL)
448 wm = g_object_new(GNT_TYPE_WM, NULL);
451 void gnt_init()
453 char *filename;
454 const char *locale;
456 if (channel)
457 return;
459 locale = setlocale(LC_ALL, "");
461 setup_io();
463 #ifdef NO_WIDECHAR
464 ascii_only = TRUE;
465 #else
466 if (locale && (strstr(locale, "UTF") || strstr(locale, "utf")))
467 ascii_only = FALSE;
468 else
469 ascii_only = TRUE;
470 #endif
472 initscr();
473 typeahead(-1);
474 noecho();
475 curs_set(0);
477 gnt_init_keys();
478 gnt_init_styles();
480 filename = g_build_filename(g_get_home_dir(), ".gntrc", NULL);
481 gnt_style_read_configure_file(filename);
482 g_free(filename);
484 gnt_init_colors();
486 wbkgdset(stdscr, '\0' | gnt_color_pair(GNT_COLOR_NORMAL));
487 refresh();
489 #ifdef ALL_MOUSE_EVENTS
490 if ((mouse_enabled = gnt_style_get_bool(GNT_STYLE_MOUSE, FALSE)))
491 mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
492 #endif
494 wbkgdset(stdscr, '\0' | gnt_color_pair(GNT_COLOR_NORMAL));
495 werase(stdscr);
496 wrefresh(stdscr);
498 #ifdef SIGWINCH
499 org_winch_handler = signal(SIGWINCH, sighandler);
500 #endif
501 signal(SIGCHLD, sighandler);
502 signal(SIGINT, sighandler);
503 signal(SIGPIPE, SIG_IGN);
505 g_type_init();
507 init_wm();
509 clipboard = g_object_new(GNT_TYPE_CLIPBOARD, NULL);
512 void gnt_main()
514 wm->loop = g_main_loop_new(NULL, FALSE);
515 g_main_loop_run(wm->loop);
518 /*********************************
519 * Stuff for 'window management' *
520 *********************************/
522 void gnt_window_present(GntWidget *window)
524 if (wm->event_stack)
525 gnt_wm_raise_window(wm, window);
526 else
527 gnt_widget_set_urgent(window);
530 void gnt_screen_occupy(GntWidget *widget)
532 gnt_wm_new_window(wm, widget);
535 void gnt_screen_release(GntWidget *widget)
537 if (wm)
538 gnt_wm_window_close(wm, widget);
541 void gnt_screen_update(GntWidget *widget)
543 gnt_wm_update_window(wm, widget);
546 gboolean gnt_widget_has_focus(GntWidget *widget)
548 GntWidget *w;
549 if (!widget)
550 return FALSE;
552 if (GNT_IS_MENU(widget))
553 return TRUE;
555 w = widget;
557 while (widget->parent)
558 widget = widget->parent;
560 if (widget == wm->_list.window)
561 return TRUE;
562 if (wm->cws->ordered && wm->cws->ordered->data == widget) {
563 if (GNT_IS_BOX(widget) &&
564 (GNT_BOX(widget)->active == w || widget == w))
565 return TRUE;
567 return FALSE;
570 void gnt_widget_set_urgent(GntWidget *widget)
572 while (widget->parent)
573 widget = widget->parent;
575 if (wm->cws->ordered && wm->cws->ordered->data == widget)
576 return;
578 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_URGENT);
580 gnt_wm_update_window(wm, widget);
583 void gnt_quit()
585 g_object_unref(G_OBJECT(wm));
586 wm = NULL;
588 update_panels();
589 doupdate();
590 gnt_uninit_colors();
591 gnt_uninit_styles();
592 endwin();
595 gboolean gnt_ascii_only()
597 return ascii_only;
600 void gnt_screen_resize_widget(GntWidget *widget, int width, int height)
602 gnt_wm_resize_window(wm, widget, width, height);
605 void gnt_screen_move_widget(GntWidget *widget, int x, int y)
607 gnt_wm_move_window(wm, widget, x, y);
610 void gnt_screen_rename_widget(GntWidget *widget, const char *text)
612 gnt_box_set_title(GNT_BOX(widget), text);
613 gnt_widget_draw(widget);
614 gnt_wm_update_window(wm, widget);
617 void gnt_register_action(const char *label, void (*callback)(void))
619 GntAction *action = g_new0(GntAction, 1);
620 action->label = g_strdup(label);
621 action->callback = callback;
623 wm->acts = g_list_append(wm->acts, action);
626 static void
627 reset_menu(GntWidget *widget, gpointer null)
629 wm->menu = NULL;
632 gboolean gnt_screen_menu_show(gpointer newmenu)
634 if (wm->menu) {
635 /* For now, if a menu is being displayed, then another menu
636 * can NOT take over. */
637 return FALSE;
640 wm->menu = newmenu;
641 GNT_WIDGET_UNSET_FLAGS(GNT_WIDGET(wm->menu), GNT_WIDGET_INVISIBLE);
642 gnt_widget_draw(GNT_WIDGET(wm->menu));
644 g_signal_connect(G_OBJECT(wm->menu), "hide", G_CALLBACK(reset_menu), NULL);
645 g_signal_connect(G_OBJECT(wm->menu), "destroy", G_CALLBACK(reset_menu), NULL);
647 return TRUE;
650 void gnt_set_clipboard_string(const gchar *string)
652 gnt_clipboard_set_string(clipboard, string);
655 GntClipboard *gnt_get_clipboard()
657 return clipboard;
660 gchar *gnt_get_clipboard_string()
662 return gnt_clipboard_get_string(clipboard);
665 #if GLIB_CHECK_VERSION(2,4,0)
666 typedef struct
668 void (*callback)(int status, gpointer data);
669 gpointer data;
670 } ChildProcess;
672 static void
673 reap_child(GPid pid, gint status, gpointer data)
675 ChildProcess *cp = data;
676 if (cp->callback) {
677 cp->callback(status, cp->data);
679 g_free(cp);
680 clean_pid();
681 wm->mode = GNT_KP_MODE_NORMAL;
682 endwin();
683 setup_io();
684 refresh();
685 refresh_screen();
687 #endif
689 gboolean gnt_giveup_console(const char *wd, char **argv, char **envp,
690 gint *stin, gint *stout, gint *sterr,
691 void (*callback)(int status, gpointer data), gpointer data)
693 #if GLIB_CHECK_VERSION(2,4,0)
694 GPid pid = 0;
695 ChildProcess *cp = NULL;
697 if (!g_spawn_async_with_pipes(wd, argv, envp,
698 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
699 (GSpawnChildSetupFunc)endwin, NULL,
700 &pid, stin, stout, sterr, NULL))
701 return FALSE;
703 cp = g_new0(ChildProcess, 1);
704 cp->callback = callback;
705 cp->data = data;
706 g_source_remove(channel_read_callback);
707 wm->mode = GNT_KP_MODE_WAIT_ON_CHILD;
708 g_child_watch_add(pid, reap_child, cp);
710 return TRUE;
711 #else
712 return FALSE;
713 #endif
716 gboolean gnt_is_refugee()
718 #if GLIB_CHECK_VERSION(2,4,0)
719 return (wm && wm->mode == GNT_KP_MODE_WAIT_ON_CHILD);
720 #else
721 return FALSE;
722 #endif