]> git.sur5r.net Git - i3/i3/blob - src/sighandler.c
Merge branch 'fix-fullscreen-enternotify'
[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     xcb_change_gc(conn, pixmap_gc, XCB_GC_FOREGROUND, (uint32_t[]){ get_colorpixel("#FFFFFF") });
51
52     for (int i = 0; i < sizeof(crash_text) / sizeof(char*); i++) {
53         int text_len = strlen(crash_text[i]);
54         char *full_text = convert_utf8_to_ucs2(crash_text[i], &text_len);
55         xcb_image_text_16(conn, text_len, pixmap, pixmap_gc, 8 /* X */,
56                           3 + (i + 1) * font_height /* Y = baseline of font */,
57                           (xcb_char2b_t*)full_text);
58         free(full_text);
59     }
60
61     /* Copy the contents of the pixmap to the real window */
62     xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, width, height);
63     xcb_flush(conn);
64
65     return 1;
66 }
67
68 /*
69  * Handles keypresses of 'e' or 'r' to exit or restart i3
70  *
71  */
72 static int sig_handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
73     uint16_t state = event->state;
74
75     /* Apparantly, after activating numlock once, the numlock modifier
76      * stays turned on (use xev(1) to verify). So, to resolve useful
77      * keysyms, we remove the numlock flag from the event state */
78     state &= ~xcb_numlock_mask;
79
80     xcb_keysym_t sym = xcb_key_press_lookup_keysym(keysyms, event, state);
81
82     if (sym == 'e') {
83         DLOG("User issued exit-command, raising error again.\n");
84         raise(raised_signal);
85         exit(1);
86     }
87
88     if (sym == 'r')
89         i3_restart(false);
90
91     if (sym == 'f')
92         i3_restart(true);
93
94     return 1;
95 }
96
97 /*
98  * Opens the window we use for input/output and maps it
99  *
100  */
101 static xcb_window_t open_input_window(xcb_connection_t *conn, Rect screen_rect, uint32_t width, uint32_t height) {
102     xcb_window_t win = xcb_generate_id(conn);
103
104     uint32_t mask = 0;
105     uint32_t values[2];
106
107     mask |= XCB_CW_BACK_PIXEL;
108     values[0] = 0;
109
110     mask |= XCB_CW_OVERRIDE_REDIRECT;
111     values[1] = 1;
112
113     /* center each popup on the specified screen */
114     uint32_t x = screen_rect.x + ((screen_rect.width / 2) - (width / 2)),
115              y = screen_rect.y + ((screen_rect.height / 2) - (height / 2));
116
117     xcb_create_window(conn,
118                       XCB_COPY_FROM_PARENT,
119                       win, /* the window id */
120                       root, /* parent == root */
121                       x, y, width, height, /* dimensions */
122                       0, /* border = 0, we draw our own */
123                       XCB_WINDOW_CLASS_INPUT_OUTPUT,
124                       XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
125                       mask,
126                       values);
127
128     /* Map the window (= make it visible) */
129     xcb_map_window(conn, win);
130
131     return win;
132 }
133
134 /*
135  * Handle signals
136  * It creates a window asking the user to restart in-place
137  * or exit to generate a core dump
138  *
139  */
140 void handle_signal(int sig, siginfo_t *info, void *data) {
141     DLOG("i3 crashed. SIG: %d\n", sig);
142
143     struct sigaction action;
144     action.sa_handler = SIG_DFL;
145     sigaction(sig, &action, NULL);
146     raised_signal = sig;
147
148     /* width and height of the popup window, so that the text fits in */
149     int crash_text_num = sizeof(crash_text) / sizeof(char*);
150     int height = 13 + (crash_text_num * config.font.height);
151
152     /* calculate width for longest text */
153     int text_len = strlen(crash_text[crash_text_longest]);
154     char *longest_text = convert_utf8_to_ucs2(crash_text[crash_text_longest], &text_len);
155     int font_width = predict_text_width(longest_text, text_len);
156     int width = font_width + 20;
157
158     /* Open a popup window on each virtual screen */
159     Output *screen;
160     xcb_window_t win;
161     TAILQ_FOREACH(screen, &outputs, outputs) {
162         if (!screen->active)
163             continue;
164         win = open_input_window(conn, screen->rect, width, height);
165
166         /* Create pixmap */
167         pixmap = xcb_generate_id(conn);
168         pixmap_gc = xcb_generate_id(conn);
169         xcb_create_pixmap(conn, root_depth, pixmap, win, width, height);
170         xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
171
172         /* Create graphics context */
173         xcb_change_gc(conn, pixmap_gc, XCB_GC_FONT, (uint32_t[]){ config.font.id });
174
175         /* Grab the keyboard to get all input */
176         xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
177
178         /* Grab the cursor inside the popup */
179         xcb_grab_pointer(conn, false, win, XCB_NONE, XCB_GRAB_MODE_ASYNC,
180                          XCB_GRAB_MODE_ASYNC, win, XCB_NONE, XCB_CURRENT_TIME);
181
182         sig_draw_window(win, width, height, config.font.height);
183         xcb_flush(conn);
184     }
185
186     xcb_generic_event_t *event;
187     /* Yay, more own eventhandlers… */
188     while ((event = xcb_wait_for_event(conn))) {
189         /* Strip off the highest bit (set if the event is generated) */
190         int type = (event->response_type & 0x7F);
191         if (type == XCB_KEY_PRESS) {
192             sig_handle_key_press(NULL, conn, (xcb_key_press_event_t*)event);
193         }
194         free(event);
195     }
196 }
197
198 /*
199  * Setup signal handlers to safely handle SIGSEGV and SIGFPE
200  *
201  */
202 void setup_signal_handler() {
203     struct sigaction action;
204
205     action.sa_sigaction = handle_signal;
206     action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
207     sigemptyset(&action.sa_mask);
208
209     if (sigaction(SIGSEGV, &action, NULL) == -1 ||
210         sigaction(SIGABRT, &action, NULL) == -1 ||
211         sigaction(SIGFPE, &action, NULL) == -1)
212         ELOG("Could not setup signal handler");
213 }