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