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