00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <ev.h>
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 #include <string.h>
00018 #include <unistd.h>
00019 #include <iconv.h>
00020 #include <signal.h>
00021
00022 #include <xcb/xcb.h>
00023 #include <xcb/xcb_aux.h>
00024 #include <xcb/xcb_event.h>
00025 #include <xcb/xcb_keysyms.h>
00026
00027 #include <X11/keysym.h>
00028
00029 #include "i3.h"
00030 #include "util.h"
00031 #include "xcb.h"
00032 #include "log.h"
00033 #include "config.h"
00034 #include "randr.h"
00035
00036 static xcb_gcontext_t pixmap_gc;
00037 static xcb_pixmap_t pixmap;
00038 static int raised_signal;
00039
00040 static char *crash_text[] = {
00041 "i3 just crashed.",
00042 "To debug this problem, either attach gdb now",
00043 "or press 'e' to exit and get a core-dump.",
00044 "If you want to keep your session,",
00045 "press 'r' to restart i3 in-place."
00046 };
00047 static int crash_text_longest = 1;
00048
00049
00050
00051
00052
00053 static int sig_draw_window(xcb_connection_t *conn, xcb_window_t win, int width, int height, int font_height) {
00054
00055 xcb_rectangle_t border = { 0, 0, width, height},
00056 inner = { 2, 2, width - 4, height - 4};
00057 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FF0000"));
00058 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &border);
00059 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
00060 xcb_poly_fill_rectangle(conn, pixmap, pixmap_gc, 1, &inner);
00061
00062
00063 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#FFFFFF"));
00064
00065 for (int i = 0; i < sizeof(crash_text) / sizeof(char*); i++) {
00066 int text_len = strlen(crash_text[i]);
00067 char *full_text = convert_utf8_to_ucs2(crash_text[i], &text_len);
00068 xcb_image_text_16(conn, text_len, pixmap, pixmap_gc, 8 ,
00069 3 + (i + 1) * font_height ,
00070 (xcb_char2b_t*)full_text);
00071 free(full_text);
00072 }
00073
00074
00075 xcb_copy_area(conn, pixmap, win, pixmap_gc, 0, 0, 0, 0, width, height);
00076 xcb_flush(conn);
00077
00078 return 1;
00079 }
00080
00081
00082
00083
00084
00085 static int sig_handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
00086 uint16_t state = event->state;
00087
00088
00089
00090
00091 state &= ~xcb_numlock_mask;
00092
00093 xcb_keysym_t sym = xcb_key_press_lookup_keysym(keysyms, event, state);
00094
00095 if (sym == 'e') {
00096 DLOG("User issued exit-command, raising error again.\n");
00097 raise(raised_signal);
00098 exit(1);
00099 }
00100
00101 if (sym == 'r')
00102 i3_restart();
00103
00104 return 1;
00105 }
00106
00107
00108
00109
00110
00111 static xcb_window_t open_input_window(xcb_connection_t *conn, Rect screen_rect, uint32_t width, uint32_t height) {
00112 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
00113 xcb_window_t win = xcb_generate_id(conn);
00114
00115 uint32_t mask = 0;
00116 uint32_t values[2];
00117
00118 mask |= XCB_CW_BACK_PIXEL;
00119 values[0] = 0;
00120
00121 mask |= XCB_CW_OVERRIDE_REDIRECT;
00122 values[1] = 1;
00123
00124
00125 uint32_t x = screen_rect.x + ((screen_rect.width / 2) - (width / 2)),
00126 y = screen_rect.y + ((screen_rect.height / 2) - (height / 2));
00127
00128 xcb_create_window(conn,
00129 XCB_COPY_FROM_PARENT,
00130 win,
00131 root,
00132 x, y, width, height,
00133 0,
00134 XCB_WINDOW_CLASS_INPUT_OUTPUT,
00135 XCB_WINDOW_CLASS_COPY_FROM_PARENT,
00136 mask,
00137 values);
00138
00139
00140 xcb_map_window(conn, win);
00141
00142 return win;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 void handle_signal(int sig, siginfo_t *info, void *data) {
00152 DLOG("i3 crashed. SIG: %d\n", sig);
00153
00154 struct sigaction action;
00155 action.sa_handler = SIG_DFL;
00156 sigaction(sig, &action, NULL);
00157 raised_signal = sig;
00158
00159 xcb_connection_t *conn = global_conn;
00160
00161
00162 xcb_event_handlers_t sig_evenths;
00163 memset(&sig_evenths, 0, sizeof(xcb_event_handlers_t));
00164 xcb_event_handlers_init(conn, &sig_evenths);
00165 xcb_event_set_key_press_handler(&sig_evenths, sig_handle_key_press, NULL);
00166
00167 i3Font *font = load_font(conn, config.font);
00168
00169
00170 int crash_text_num = sizeof(crash_text) / sizeof(char*);
00171 int height = 13 + (crash_text_num * font->height);
00172
00173
00174 int text_len = strlen(crash_text[crash_text_longest]);
00175 char *longest_text = convert_utf8_to_ucs2(crash_text[crash_text_longest], &text_len);
00176 int font_width = predict_text_width(conn, config.font, longest_text, text_len);
00177 int width = font_width + 20;
00178
00179
00180 Output *screen;
00181 xcb_window_t win;
00182 TAILQ_FOREACH(screen, &outputs, outputs) {
00183 if (!screen->active)
00184 continue;
00185 win = open_input_window(conn, screen->rect, width, height);
00186
00187
00188 pixmap = xcb_generate_id(conn);
00189 pixmap_gc = xcb_generate_id(conn);
00190 xcb_create_pixmap(conn, root_depth, pixmap, win, width, height);
00191 xcb_create_gc(conn, pixmap_gc, pixmap, 0, 0);
00192
00193
00194 xcb_change_gc_single(conn, pixmap_gc, XCB_GC_FONT, font->id);
00195
00196
00197 xcb_grab_keyboard(conn, false, win, XCB_CURRENT_TIME, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
00198
00199
00200 xcb_grab_pointer(conn, false, win, XCB_NONE, XCB_GRAB_MODE_ASYNC,
00201 XCB_GRAB_MODE_ASYNC, win, XCB_NONE, XCB_CURRENT_TIME);
00202
00203 sig_draw_window(conn, win, width, height, font->height);
00204 xcb_flush(conn);
00205 }
00206
00207 xcb_event_wait_for_event_loop(&sig_evenths);
00208 }
00209
00210
00211
00212
00213
00214 void setup_signal_handler() {
00215 struct sigaction action;
00216
00217 action.sa_sigaction = handle_signal;
00218 action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
00219 sigemptyset(&action.sa_mask);
00220
00221 if (sigaction(SIGSEGV, &action, NULL) == -1 ||
00222 sigaction(SIGFPE, &action, NULL) == -1)
00223 ELOG("Could not setup signal handler");
00224 }