]> git.sur5r.net Git - i3/i3/blob - src/sighandler.c
Merge branch 'master' into next
[i3/i3] / src / sighandler.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
6  * © 2009-2010 Jan-Erik Rediger
7  *
8  * sighandler.c: Interactive crash dialog upon SIGSEGV/SIGABRT/SIGFPE (offers
9  *               to restart inplace).
10  *
11  */
12 #include "all.h"
13
14 #include <ev.h>
15 #include <iconv.h>
16 #include <signal.h>
17
18 #include <xcb/xcb_event.h>
19
20 #include <X11/keysym.h>
21
22 static xcb_gcontext_t pixmap_gc;
23 static xcb_pixmap_t pixmap;
24 static int raised_signal;
25
26 static char *crash_text[] = {
27     "i3 just crashed.",
28     "To debug this problem, either attach gdb now",
29     "or press",
30     "- 'e' to exit and get a core-dump,",
31     "- 'r' to restart i3 in-place or",
32     "- 'f' to forget the current layout and restart"
33 };
34 static int crash_text_longest = 5;
35
36 /*
37  * Draw the window containing the info text
38  *
39  */
40 static int sig_draw_window(xcb_window_t win, int width, int height, int font_height) {
41     /* re-draw the background */
42     xcb_rectangle_t border = { 0, 0, width, height},
43                     inner = { 2, 2, width - 4, height - 4};
44     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel("#FF0000") });
45     xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
46     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel("#000000") });
47     xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner);
48
49     /* restore font color */
50     set_font_colors(pixmap_gc, get_colorpixel("#FFFFFF"), get_colorpixel("#000000"));
51
52     for (int i = 0; i < sizeof(crash_text) / sizeof(char*); i++) {
53         draw_text(crash_text[i], strlen(crash_text[i]), false, pixmap, pixmap_gc,
54                 8, 5 + i * font_height, width - 16);
55     }
56
57     /* Copy the contents of the pixmap to the real window */
58     xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, width, height);
59     xcb_flush(conn);
60
61     return 1;
62 }
63
64 /*
65  * Handles keypresses of 'e' or 'r' to exit or restart i3
66  *
67  */
68 static int sig_handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
69     uint16_t state = event->state;
70
71     /* Apparantly, after activating numlock once, the numlock modifier
72      * stays turned on (use xev(1) to verify). So, to resolve useful
73      * keysyms, we remove the numlock flag from the event state */
74     state &= ~xcb_numlock_mask;
75
76     xcb_keysym_t sym = xcb_key_press_lookup_keysym(keysyms, event, state);
77
78     if (sym == 'e') {
79         DLOG("User issued exit-command, raising error again.\n");
80         raise(raised_signal);
81         exit(1);
82     }
83
84     if (sym == 'r')
85         i3_restart(false);
86
87     if (sym == 'f')
88         i3_restart(true);
89
90     return 1;
91 }
92
93 /*
94  * Opens the window we use for input/output and maps it
95  *
96  */
97 static xcb_window_t open_input_window(xcb_connection_t *conn, Rect screen_rect, uint32_t width, uint32_t height) {
98     xcb_window_t win = xcb_generate_id(conn);
99
100     uint32_t mask = 0;
101     uint32_t values[2];
102
103     mask |= XCB_CW_BACK_PIXEL;
104     values[0] = 0;
105
106     mask |= XCB_CW_OVERRIDE_REDIRECT;
107     values[1] = 1;
108
109     /* center each popup on the specified screen */
110     uint32_t x = screen_rect.x + ((screen_rect.width / 2) - (width / 2)),
111              y = screen_rect.y + ((screen_rect.height / 2) - (height / 2));
112
113     xcb_create_window(conn,
114                       XCB_COPY_FROM_PARENT,
115                       win, /* the window id */
116                       root, /* parent == root */
117                       x, y, width, height, /* dimensions */
118                       0, /* border = 0, we draw our own */
119                       XCB_WINDOW_CLASS_INPUT_OUTPUT,
120                       XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
121                       mask,
122                       values);
123
124     /* Map the window (= make it visible) */
125     xcb_map_window(conn, win);
126
127     return win;
128 }
129
130 /*
131  * Handle signals
132  * It creates a window asking the user to restart in-place
133  * or exit to generate a core dump
134  *
135  */
136 void handle_signal(int sig, siginfo_t *info, void *data) {
137     DLOG("i3 crashed. SIG: %d\n", sig);
138
139     struct sigaction action;
140     action.sa_handler = SIG_DFL;
141     sigaction(sig, &action, NULL);
142     raised_signal = sig;
143
144     /* width and height of the popup window, so that the text fits in */
145     int crash_text_num = sizeof(crash_text) / sizeof(char*);
146     int height = 13 + (crash_text_num * config.font.height);
147
148     /* calculate width for longest text */
149     size_t text_len = strlen(crash_text[crash_text_longest]);
150     xcb_char2b_t *longest_text = convert_utf8_to_ucs2(crash_text[crash_text_longest], &text_len);
151     int font_width = predict_text_width((char *)longest_text, text_len, true);
152     int width = font_width + 20;
153
154     /* Open a popup window on each virtual screen */
155     Output *screen;
156     xcb_window_t win;
157     TAILQ_FOREACH(screen, &outputs, outputs) {
158         if (!screen->active)
159             continue;
160         win = open_input_window(conn, screen->rect, width, height);
161
162         /* Create pixmap */
163         pixmap = xcb_generate_id(conn);
164         pixmap_gc = xcb_generate_id(conn);
165         xcb_create_pixmap(conn, root_depth, pixmap, win, width, height);
166         xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
167
168         /* Grab the keyboard to get all input */
169         xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
170
171         /* Grab the cursor inside the popup */
172         xcb_grab_pointer(conn, false, win, XCB_NONE, XCB_GRAB_MODE_ASYNC,
173                          XCB_GRAB_MODE_ASYNC, win, XCB_NONE, XCB_CURRENT_TIME);
174
175         sig_draw_window(win, width, height, config.font.height);
176         xcb_flush(conn);
177     }
178
179     xcb_generic_event_t *event;
180     /* Yay, more own eventhandlers… */
181     while ((event = xcb_wait_for_event(conn))) {
182         /* Strip off the highest bit (set if the event is generated) */
183         int type = (event->response_type & 0x7F);
184         if (type == XCB_KEY_PRESS) {
185             sig_handle_key_press(NULL, conn, (xcb_key_press_event_t*)event);
186         }
187         free(event);
188     }
189 }
190
191 /*
192  * Setup signal handlers to safely handle SIGSEGV and SIGFPE
193  *
194  */
195 void setup_signal_handler() {
196     struct sigaction action;
197
198     action.sa_sigaction = handle_signal;
199     action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
200     sigemptyset(&action.sa_mask);
201
202     /* Catch all signals with default action "Core", see signal(7) */
203     if (sigaction(SIGQUIT, &action, NULL) == -1 ||
204         sigaction(SIGILL, &action, NULL) == -1 ||
205         sigaction(SIGABRT, &action, NULL) == -1 ||
206         sigaction(SIGFPE, &action, NULL) == -1 ||
207         sigaction(SIGSEGV, &action, NULL) == -1)
208         ELOG("Could not setup signal handler");
209 }