Update.
[gnt.git] / gntmain.c
blobae2cedbb277762f9274863570ba5aede934e44e8
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__)
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();
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 int 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 gnt_wm_set_event_stack(wm, FALSE);
295 g_free(cvrt);
296 return TRUE;
299 static void
300 setup_io()
302 int result;
303 channel = g_io_channel_unix_new(STDIN_FILENO);
304 g_io_channel_set_close_on_unref(channel, TRUE);
306 #if 0
307 g_io_channel_set_encoding(channel, NULL, NULL);
308 g_io_channel_set_buffered(channel, FALSE);
309 g_io_channel_set_flags(channel, G_IO_FLAG_NONBLOCK, NULL );
310 #endif
312 channel_read_callback = result = g_io_add_watch_full(channel, G_PRIORITY_HIGH,
313 (G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI),
314 io_invoke, NULL, NULL);
316 g_io_add_watch_full(channel, G_PRIORITY_HIGH,
317 (G_IO_NVAL),
318 io_invoke_error, GINT_TO_POINTER(result), NULL);
320 g_io_channel_unref(channel); /* Apparently this caused crashes for some people.
321 But irssi does this, so I am going to assume the
322 crashes were caused by some other stuff. */
324 g_printerr("gntmain: setting up IO (%d)\n", channel_read_callback);
327 static gboolean
328 refresh_screen()
330 gnt_bindable_perform_action_named(GNT_BINDABLE(wm), "refresh-screen", NULL);
331 return FALSE;
334 /* Xerox */
335 static void
336 clean_pid(void)
338 int status;
339 pid_t pid;
341 do {
342 pid = waitpid(-1, &status, WNOHANG);
343 } while (pid != 0 && pid != (pid_t)-1);
345 if ((pid == (pid_t) - 1) && (errno != ECHILD)) {
346 char errmsg[BUFSIZ];
347 g_snprintf(errmsg, BUFSIZ, "Warning: waitpid() returned %d", pid);
348 perror(errmsg);
352 static void
353 exit_confirmed(gpointer null)
355 gnt_bindable_perform_action_named(GNT_BINDABLE(wm), "wm-quit", NULL);
358 static void
359 exit_win_close(GntWidget *w, GntWidget **win)
361 *win = NULL;
364 static void
365 ask_before_exit()
367 static GntWidget *win = NULL;
368 GntWidget *bbox, *button;
370 if (wm->menu) {
371 do {
372 gnt_widget_hide(GNT_WIDGET(wm->menu));
373 if (wm->menu)
374 wm->menu = wm->menu->parentmenu;
375 } while (wm->menu);
378 if (win)
379 goto raise;
381 win = gnt_vwindow_new(FALSE);
382 gnt_box_add_widget(GNT_BOX(win), gnt_label_new("Are you sure you want to quit?"));
383 gnt_box_set_title(GNT_BOX(win), "Quit?");
384 gnt_box_set_alignment(GNT_BOX(win), GNT_ALIGN_MID);
385 g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(exit_win_close), &win);
387 bbox = gnt_hbox_new(FALSE);
388 gnt_box_add_widget(GNT_BOX(win), bbox);
390 button = gnt_button_new("Quit");
391 g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(exit_confirmed), NULL);
392 gnt_box_add_widget(GNT_BOX(bbox), button);
394 button = gnt_button_new("Cancel");
395 g_signal_connect_swapped(G_OBJECT(button), "activate", G_CALLBACK(gnt_widget_destroy), win);
396 gnt_box_add_widget(GNT_BOX(bbox), button);
398 gnt_widget_show(win);
399 raise:
400 gnt_wm_raise_window(wm, win);
403 #ifdef SIGWINCH
404 static void (*org_winch_handler)(int);
405 #endif
407 static void
408 sighandler(int sig)
410 switch (sig) {
411 #ifdef SIGWINCH
412 case SIGWINCH:
413 erase();
414 g_idle_add(refresh_screen, NULL);
415 if (org_winch_handler)
416 org_winch_handler(sig);
417 signal(SIGWINCH, sighandler);
418 break;
419 #endif
420 case SIGCHLD:
421 clean_pid();
422 signal(SIGCHLD, sighandler);
423 break;
424 case SIGINT:
425 ask_before_exit();
426 signal(SIGINT, sighandler);
427 break;
431 static void
432 init_wm()
434 const char *name = gnt_style_get(GNT_STYLE_WM);
435 gpointer handle;
437 if (name && *name) {
438 handle = g_module_open(name, G_MODULE_BIND_LAZY);
439 if (handle) {
440 gboolean (*init)(GntWM **);
441 if (g_module_symbol(handle, "gntwm_init", (gpointer)&init)) {
442 init(&wm);
446 if (wm == NULL)
447 wm = g_object_new(GNT_TYPE_WM, NULL);
450 void gnt_init()
452 char *filename;
453 const char *locale;
455 if (channel)
456 return;
458 locale = setlocale(LC_ALL, "");
460 setup_io();
462 #ifdef NO_WIDECHAR
463 ascii_only = TRUE;
464 #else
465 if (locale && (strstr(locale, "UTF") || strstr(locale, "utf")))
466 ascii_only = FALSE;
467 else
468 ascii_only = TRUE;
469 #endif
471 initscr();
472 typeahead(-1);
473 noecho();
474 curs_set(0);
476 gnt_init_keys();
477 gnt_init_styles();
479 filename = g_build_filename(g_get_home_dir(), ".gntrc", NULL);
480 gnt_style_read_configure_file(filename);
481 g_free(filename);
483 gnt_init_colors();
485 wbkgdset(stdscr, '\0' | gnt_color_pair(GNT_COLOR_NORMAL));
486 refresh();
488 #ifdef ALL_MOUSE_EVENTS
489 if ((mouse_enabled = gnt_style_get_bool(GNT_STYLE_MOUSE, FALSE)))
490 mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
491 #endif
493 wbkgdset(stdscr, '\0' | gnt_color_pair(GNT_COLOR_NORMAL));
494 werase(stdscr);
495 wrefresh(stdscr);
497 #ifdef SIGWINCH
498 org_winch_handler = signal(SIGWINCH, sighandler);
499 #endif
500 signal(SIGCHLD, sighandler);
501 signal(SIGINT, sighandler);
502 signal(SIGPIPE, SIG_IGN);
504 g_type_init();
506 init_wm();
508 clipboard = g_object_new(GNT_TYPE_CLIPBOARD, NULL);
511 void gnt_main()
513 wm->loop = g_main_loop_new(NULL, FALSE);
514 g_main_loop_run(wm->loop);
517 /*********************************
518 * Stuff for 'window management' *
519 *********************************/
521 void gnt_window_present(GntWidget *window)
523 if (wm->event_stack)
524 gnt_wm_raise_window(wm, window);
525 else
526 gnt_widget_set_urgent(window);
529 void gnt_screen_occupy(GntWidget *widget)
531 gnt_wm_new_window(wm, widget);
534 void gnt_screen_release(GntWidget *widget)
536 if (wm)
537 gnt_wm_window_close(wm, widget);
540 void gnt_screen_update(GntWidget *widget)
542 gnt_wm_update_window(wm, widget);
545 gboolean gnt_widget_has_focus(GntWidget *widget)
547 GntWidget *w;
548 if (!widget)
549 return FALSE;
551 if (GNT_IS_MENU(widget))
552 return TRUE;
554 w = widget;
556 while (widget->parent)
557 widget = widget->parent;
559 if (widget == wm->_list.window)
560 return TRUE;
561 if (wm->cws->ordered && wm->cws->ordered->data == widget) {
562 if (GNT_IS_BOX(widget) &&
563 (GNT_BOX(widget)->active == w || widget == w))
564 return TRUE;
566 return FALSE;
569 void gnt_widget_set_urgent(GntWidget *widget)
571 while (widget->parent)
572 widget = widget->parent;
574 if (wm->cws->ordered && wm->cws->ordered->data == widget)
575 return;
577 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_URGENT);
579 gnt_wm_update_window(wm, widget);
582 void gnt_quit()
584 g_object_unref(G_OBJECT(wm));
585 wm = NULL;
587 update_panels();
588 doupdate();
589 gnt_uninit_colors();
590 gnt_uninit_styles();
591 endwin();
594 gboolean gnt_ascii_only()
596 return ascii_only;
599 void gnt_screen_resize_widget(GntWidget *widget, int width, int height)
601 gnt_wm_resize_window(wm, widget, width, height);
604 void gnt_screen_move_widget(GntWidget *widget, int x, int y)
606 gnt_wm_move_window(wm, widget, x, y);
609 void gnt_screen_rename_widget(GntWidget *widget, const char *text)
611 gnt_box_set_title(GNT_BOX(widget), text);
612 gnt_widget_draw(widget);
613 gnt_wm_update_window(wm, widget);
616 void gnt_register_action(const char *label, void (*callback)())
618 GntAction *action = g_new0(GntAction, 1);
619 action->label = g_strdup(label);
620 action->callback = callback;
622 wm->acts = g_list_append(wm->acts, action);
625 static void
626 reset_menu(GntWidget *widget, gpointer null)
628 wm->menu = NULL;
631 gboolean gnt_screen_menu_show(gpointer newmenu)
633 if (wm->menu) {
634 /* For now, if a menu is being displayed, then another menu
635 * can NOT take over. */
636 return FALSE;
639 wm->menu = newmenu;
640 GNT_WIDGET_UNSET_FLAGS(GNT_WIDGET(wm->menu), GNT_WIDGET_INVISIBLE);
641 gnt_widget_draw(GNT_WIDGET(wm->menu));
643 g_signal_connect(G_OBJECT(wm->menu), "hide", G_CALLBACK(reset_menu), NULL);
644 g_signal_connect(G_OBJECT(wm->menu), "destroy", G_CALLBACK(reset_menu), NULL);
646 return TRUE;
649 void gnt_set_clipboard_string(const gchar *string)
651 gnt_clipboard_set_string(clipboard, string);
654 GntClipboard *gnt_get_clipboard()
656 return clipboard;
659 gchar *gnt_get_clipboard_string()
661 return gnt_clipboard_get_string(clipboard);
664 #if GLIB_CHECK_VERSION(2,4,0)
665 typedef struct
667 void (*callback)(int status, gpointer data);
668 gpointer data;
669 } ChildProcess;
671 static void
672 reap_child(GPid pid, gint status, gpointer data)
674 ChildProcess *cp = data;
675 if (cp->callback) {
676 cp->callback(status, cp->data);
678 g_free(cp);
679 clean_pid();
680 wm->mode = GNT_KP_MODE_NORMAL;
681 clear();
682 setup_io();
683 refresh_screen();
685 #endif
687 gboolean gnt_giveup_console(const char *wd, char **argv, char **envp,
688 gint *stin, gint *stout, gint *sterr,
689 void (*callback)(int status, gpointer data), gpointer data)
691 #if GLIB_CHECK_VERSION(2,4,0)
692 GPid pid = 0;
693 ChildProcess *cp = NULL;
695 if (!g_spawn_async_with_pipes(wd, argv, envp,
696 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
697 (GSpawnChildSetupFunc)endwin, NULL,
698 &pid, stin, stout, sterr, NULL))
699 return FALSE;
701 cp = g_new0(ChildProcess, 1);
702 cp->callback = callback;
703 cp->data = data;
704 g_source_remove(channel_read_callback);
705 wm->mode = GNT_KP_MODE_WAIT_ON_CHILD;
706 g_child_watch_add(pid, reap_child, cp);
708 return TRUE;
709 #else
710 return FALSE;
711 #endif
714 gboolean gnt_is_refugee()
716 #if GLIB_CHECK_VERSION(2,4,0)
717 return (wm && wm->mode == GNT_KP_MODE_WAIT_ON_CHILD);
718 #else
719 return FALSE;
720 #endif