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