00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #include <string.h>
00014 #include <sys/types.h>
00015 #include <unistd.h>
00016 #include <stdbool.h>
00017 #include <assert.h>
00018 #include <limits.h>
00019 #include <locale.h>
00020 #include <fcntl.h>
00021 #include <getopt.h>
00022
00023 #include <X11/XKBlib.h>
00024 #include <X11/extensions/XKB.h>
00025
00026 #include <xcb/xcb.h>
00027 #include <xcb/xcb_atom.h>
00028 #include <xcb/xcb_aux.h>
00029 #include <xcb/xcb_event.h>
00030 #include <xcb/xcb_property.h>
00031 #include <xcb/xcb_keysyms.h>
00032 #include <xcb/xcb_icccm.h>
00033
00034 #include <ev.h>
00035
00036 #include "config.h"
00037 #include "data.h"
00038 #include "debug.h"
00039 #include "handlers.h"
00040 #include "click.h"
00041 #include "i3.h"
00042 #include "layout.h"
00043 #include "queue.h"
00044 #include "table.h"
00045 #include "util.h"
00046 #include "xcb.h"
00047 #include "randr.h"
00048 #include "xinerama.h"
00049 #include "manage.h"
00050 #include "ipc.h"
00051 #include "log.h"
00052 #include "sighandler.h"
00053
00054 static int xkb_event_base;
00055
00056 int xkb_current_group;
00057
00058 xcb_connection_t *global_conn;
00059
00060
00061 char **start_argv;
00062
00063
00064 Display *xkbdpy;
00065
00066 xcb_key_symbols_t *keysyms;
00067
00068
00069 struct bindings_head *bindings;
00070
00071
00072 struct autostarts_head autostarts = TAILQ_HEAD_INITIALIZER(autostarts);
00073
00074
00075 struct assignments_head assignments = TAILQ_HEAD_INITIALIZER(assignments);
00076
00077
00078 struct stack_wins_head stack_wins = SLIST_HEAD_INITIALIZER(stack_wins);
00079
00080
00081
00082 xcb_event_handlers_t evenths;
00083 xcb_atom_t atoms[NUM_ATOMS];
00084
00085 xcb_window_t root;
00086 int num_screens = 0;
00087
00088
00089 uint8_t root_depth;
00090
00091
00092 bool xkb_supported = true;
00093
00094
00095
00096
00097
00098
00099 static void xcb_got_event(EV_P_ struct ev_io *w, int revents) {
00100
00101 }
00102
00103
00104
00105
00106
00107 static void xcb_prepare_cb(EV_P_ ev_prepare *w, int revents) {
00108 xcb_flush(evenths.c);
00109 }
00110
00111
00112
00113
00114
00115
00116 static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
00117 xcb_generic_event_t *event;
00118
00119 while ((event = xcb_poll_for_event(evenths.c)) != NULL) {
00120 xcb_event_handle(&evenths, event);
00121 free(event);
00122 }
00123 }
00124
00125
00126
00127
00128
00129
00130 static void xkb_got_event(EV_P_ struct ev_io *w, int revents) {
00131 DLOG("Handling XKB event\n");
00132 XkbEvent ev;
00133
00134
00135
00136
00137 bool mapping_changed = false;
00138 while (XPending(xkbdpy)) {
00139 XNextEvent(xkbdpy, (XEvent*)&ev);
00140
00141
00142 if (ev.type != xkb_event_base)
00143 continue;
00144
00145 if (ev.any.xkb_type == XkbMapNotify) {
00146 mapping_changed = true;
00147 continue;
00148 }
00149
00150 if (ev.any.xkb_type != XkbStateNotify) {
00151 ELOG("Unknown XKB event received (type %d)\n", ev.any.xkb_type);
00152 continue;
00153 }
00154
00155
00156
00157
00158
00159 if (xkb_current_group == ev.state.group)
00160 continue;
00161
00162 xkb_current_group = ev.state.group;
00163
00164 if (ev.state.group == XkbGroup2Index) {
00165 DLOG("Mode_switch enabled\n");
00166 grab_all_keys(global_conn, true);
00167 }
00168
00169 if (ev.state.group == XkbGroup1Index) {
00170 DLOG("Mode_switch disabled\n");
00171 ungrab_all_keys(global_conn);
00172 grab_all_keys(global_conn, false);
00173 }
00174 }
00175
00176 if (!mapping_changed)
00177 return;
00178
00179 DLOG("Keyboard mapping changed, updating keybindings\n");
00180 xcb_key_symbols_free(keysyms);
00181 keysyms = xcb_key_symbols_alloc(global_conn);
00182
00183 xcb_get_numlock_mask(global_conn);
00184
00185 ungrab_all_keys(global_conn);
00186 DLOG("Re-grabbing...\n");
00187 translate_keysyms();
00188 grab_all_keys(global_conn, (xkb_current_group == XkbGroup2Index));
00189 DLOG("Done\n");
00190 }
00191
00192
00193 int main(int argc, char *argv[], char *env[]) {
00194 int i, screens, opt;
00195 char *override_configpath = NULL;
00196 bool autostart = true;
00197 bool only_check_config = false;
00198 bool force_xinerama = false;
00199 xcb_connection_t *conn;
00200 xcb_property_handlers_t prophs;
00201 xcb_intern_atom_cookie_t atom_cookies[NUM_ATOMS];
00202 static struct option long_options[] = {
00203 {"no-autostart", no_argument, 0, 'a'},
00204 {"config", required_argument, 0, 'c'},
00205 {"version", no_argument, 0, 'v'},
00206 {"help", no_argument, 0, 'h'},
00207 {"force-xinerama", no_argument, 0, 0},
00208 {0, 0, 0, 0}
00209 };
00210 int option_index = 0;
00211
00212 setlocale(LC_ALL, "");
00213
00214
00215 if (!isatty(fileno(stdout)))
00216 setbuf(stdout, NULL);
00217
00218 start_argv = argv;
00219
00220 while ((opt = getopt_long(argc, argv, "c:Cvahld:V", long_options, &option_index)) != -1) {
00221 switch (opt) {
00222 case 'a':
00223 LOG("Autostart disabled using -a\n");
00224 autostart = false;
00225 break;
00226 case 'c':
00227 override_configpath = sstrdup(optarg);
00228 break;
00229 case 'C':
00230 LOG("Checking configuration file only (-C)\n");
00231 only_check_config = true;
00232 break;
00233 case 'v':
00234 printf("i3 version " I3_VERSION " © 2009-2010 Michael Stapelberg and contributors\n");
00235 exit(EXIT_SUCCESS);
00236 case 'V':
00237 set_verbosity(true);
00238 break;
00239 case 'd':
00240 LOG("Enabling debug loglevel %s\n", optarg);
00241 add_loglevel(optarg);
00242 break;
00243 case 'l':
00244
00245 break;
00246 case 0:
00247 if (strcmp(long_options[option_index].name, "force-xinerama") == 0) {
00248 force_xinerama = true;
00249 ELOG("Using Xinerama instead of RandR. This option should be "
00250 "avoided at all cost because it does not refresh the list "
00251 "of screens, so you cannot configure displays at runtime. "
00252 "Please check if your driver really does not support RandR "
00253 "and disable this option as soon as you can.\n");
00254 break;
00255 }
00256
00257 default:
00258 fprintf(stderr, "Usage: %s [-c configfile] [-d loglevel] [-a] [-v] [-V] [-C]\n", argv[0]);
00259 fprintf(stderr, "\n");
00260 fprintf(stderr, "-a: disable autostart\n");
00261 fprintf(stderr, "-v: display version and exit\n");
00262 fprintf(stderr, "-V: enable verbose mode\n");
00263 fprintf(stderr, "-d <loglevel>: enable debug loglevel <loglevel>\n");
00264 fprintf(stderr, "-c <configfile>: use the provided configfile instead\n");
00265 fprintf(stderr, "-C: check configuration file and exit\n");
00266 fprintf(stderr, "--force-xinerama: Use Xinerama instead of RandR. This "
00267 "option should only be used if you are stuck with the "
00268 "nvidia closed source driver which does not support RandR.\n");
00269 exit(EXIT_FAILURE);
00270 }
00271 }
00272
00273 LOG("i3 version " I3_VERSION " starting\n");
00274
00275
00276 init_table();
00277
00278 memset(&evenths, 0, sizeof(xcb_event_handlers_t));
00279 memset(&prophs, 0, sizeof(xcb_property_handlers_t));
00280
00281 conn = global_conn = xcb_connect(NULL, &screens);
00282
00283 if (xcb_connection_has_error(conn))
00284 die("Cannot open display\n");
00285
00286
00287 xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
00288 root = root_screen->root;
00289 root_depth = root_screen->root_depth;
00290
00291 load_configuration(conn, override_configpath, false);
00292 if (only_check_config) {
00293 LOG("Done checking configuration file. Exiting.\n");
00294 exit(0);
00295 }
00296
00297
00298
00299
00300
00301
00302 expand_table_cols(TAILQ_FIRST(workspaces));
00303 expand_table_rows(TAILQ_FIRST(workspaces));
00304
00305
00306 #define REQUEST_ATOM(name) atom_cookies[name] = xcb_intern_atom(conn, 0, strlen(#name), #name);
00307
00308 REQUEST_ATOM(_NET_SUPPORTED);
00309 REQUEST_ATOM(_NET_WM_STATE_FULLSCREEN);
00310 REQUEST_ATOM(_NET_SUPPORTING_WM_CHECK);
00311 REQUEST_ATOM(_NET_WM_NAME);
00312 REQUEST_ATOM(_NET_WM_STATE);
00313 REQUEST_ATOM(_NET_WM_WINDOW_TYPE);
00314 REQUEST_ATOM(_NET_WM_DESKTOP);
00315 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
00316 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
00317 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
00318 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
00319 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
00320 REQUEST_ATOM(_NET_WM_STRUT_PARTIAL);
00321 REQUEST_ATOM(WM_PROTOCOLS);
00322 REQUEST_ATOM(WM_DELETE_WINDOW);
00323 REQUEST_ATOM(UTF8_STRING);
00324 REQUEST_ATOM(WM_STATE);
00325 REQUEST_ATOM(WM_CLIENT_LEADER);
00326 REQUEST_ATOM(_NET_CURRENT_DESKTOP);
00327 REQUEST_ATOM(_NET_ACTIVE_WINDOW);
00328 REQUEST_ATOM(_NET_WORKAREA);
00329
00330
00331 int major, minor, error;
00332
00333 major = XkbMajorVersion;
00334 minor = XkbMinorVersion;
00335
00336 int errBase;
00337
00338 if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &xkb_event_base, &errBase, &major, &minor, &error)) == NULL) {
00339 ELOG("ERROR: XkbOpenDisplay() failed, disabling XKB support\n");
00340 xkb_supported = false;
00341 }
00342
00343 if (xkb_supported) {
00344 if (fcntl(ConnectionNumber(xkbdpy), F_SETFD, FD_CLOEXEC) == -1) {
00345 fprintf(stderr, "Could not set FD_CLOEXEC on xkbdpy\n");
00346 return 1;
00347 }
00348
00349 int i1;
00350 if (!XkbQueryExtension(xkbdpy,&i1,&xkb_event_base,&errBase,&major,&minor)) {
00351 fprintf(stderr, "XKB not supported by X-server\n");
00352 return 1;
00353 }
00354
00355
00356 if (!XkbSelectEvents(xkbdpy, XkbUseCoreKbd,
00357 XkbMapNotifyMask | XkbStateNotifyMask,
00358 XkbMapNotifyMask | XkbStateNotifyMask)) {
00359 fprintf(stderr, "Could not set XKB event mask\n");
00360 return 1;
00361 }
00362 }
00363
00364
00365 struct ev_loop *loop = ev_loop_new(0);
00366 if (loop == NULL)
00367 die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
00368
00369 struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
00370 struct ev_io *xkb = scalloc(sizeof(struct ev_io));
00371 struct ev_check *xcb_check = scalloc(sizeof(struct ev_check));
00372 struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare));
00373
00374 ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
00375 ev_io_start(loop, xcb_watcher);
00376
00377 if (xkb_supported) {
00378 ev_io_init(xkb, xkb_got_event, ConnectionNumber(xkbdpy), EV_READ);
00379 ev_io_start(loop, xkb);
00380
00381
00382 XFlush(xkbdpy);
00383 }
00384
00385 ev_check_init(xcb_check, xcb_check_cb);
00386 ev_check_start(loop, xcb_check);
00387
00388 ev_prepare_init(xcb_prepare, xcb_prepare_cb);
00389 ev_prepare_start(loop, xcb_prepare);
00390
00391
00392 xcb_grab_server(conn);
00393
00394 xcb_event_handlers_init(conn, &evenths);
00395
00396
00397 for (i = 2; i < 128; ++i)
00398 xcb_event_set_handler(&evenths, i, handle_event, 0);
00399
00400 for (i = 0; i < 256; ++i)
00401 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
00402
00403
00404 xcb_event_set_expose_handler(&evenths, handle_expose_event, NULL);
00405
00406
00407 xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
00408
00409
00410 xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, NULL);
00411
00412
00413 xcb_event_set_button_press_handler(&evenths, handle_button_press, NULL);
00414
00415
00416 xcb_event_set_map_request_handler(&evenths, handle_map_request, &prophs);
00417
00418
00419
00420 xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, NULL);
00421
00422
00423 xcb_event_set_destroy_notify_handler(&evenths, handle_destroy_notify_event, NULL);
00424
00425
00426
00427 xcb_event_set_configure_notify_handler(&evenths, handle_configure_event, NULL);
00428
00429
00430 xcb_event_set_configure_request_handler(&evenths, handle_configure_request, NULL);
00431
00432
00433
00434 xcb_event_set_motion_notify_handler(&evenths, handle_motion_notify, NULL);
00435
00436
00437 xcb_event_set_mapping_notify_handler(&evenths, handle_mapping_notify, NULL);
00438
00439
00440
00441 xcb_event_set_client_message_handler(&evenths, handle_client_message, NULL);
00442
00443
00444 xcb_property_handlers_init(&prophs, &evenths);
00445
00446
00447 xcb_property_set_handler(&prophs, WM_NORMAL_HINTS, UINT_MAX, handle_normal_hints, NULL);
00448
00449
00450 uint32_t mask = XCB_CW_EVENT_MASK;
00451 uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
00452 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
00453
00454
00455 XCB_EVENT_MASK_POINTER_MOTION |
00456 XCB_EVENT_MASK_PROPERTY_CHANGE |
00457 XCB_EVENT_MASK_ENTER_WINDOW };
00458 xcb_void_cookie_t cookie;
00459 cookie = xcb_change_window_attributes_checked(conn, root, mask, values);
00460 check_error(conn, cookie, "Another window manager seems to be running");
00461
00462
00463 #define GET_ATOM(name) { \
00464 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, atom_cookies[name], NULL); \
00465 if (!reply) { \
00466 ELOG("Could not get atom " #name "\n"); \
00467 exit(-1); \
00468 } \
00469 atoms[name] = reply->atom; \
00470 free(reply); \
00471 }
00472
00473 GET_ATOM(_NET_SUPPORTED);
00474 GET_ATOM(_NET_WM_STATE_FULLSCREEN);
00475 GET_ATOM(_NET_SUPPORTING_WM_CHECK);
00476 GET_ATOM(_NET_WM_NAME);
00477 GET_ATOM(_NET_WM_STATE);
00478 GET_ATOM(_NET_WM_WINDOW_TYPE);
00479 GET_ATOM(_NET_WM_DESKTOP);
00480 GET_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
00481 GET_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
00482 GET_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
00483 GET_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
00484 GET_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
00485 GET_ATOM(_NET_WM_STRUT_PARTIAL);
00486 GET_ATOM(WM_PROTOCOLS);
00487 GET_ATOM(WM_DELETE_WINDOW);
00488 GET_ATOM(UTF8_STRING);
00489 GET_ATOM(WM_STATE);
00490 GET_ATOM(WM_CLIENT_LEADER);
00491 GET_ATOM(_NET_CURRENT_DESKTOP);
00492 GET_ATOM(_NET_ACTIVE_WINDOW);
00493 GET_ATOM(_NET_WORKAREA);
00494
00495 xcb_property_set_handler(&prophs, atoms[_NET_WM_WINDOW_TYPE], UINT_MAX, handle_window_type, NULL);
00496
00497
00498
00499 xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL);
00500
00501
00502 xcb_property_set_handler(&prophs, WM_TRANSIENT_FOR, UINT_MAX, handle_transient_for, NULL);
00503
00504
00505 xcb_watch_wm_name(&prophs, 128, handle_windowname_change_legacy, NULL);
00506
00507
00508 xcb_property_set_handler(&prophs, WM_CLASS, 128, handle_windowclass_change, NULL);
00509
00510
00511 xcb_property_set_handler(&prophs, atoms[WM_CLIENT_LEADER], UINT_MAX, handle_clientleader_change, NULL);
00512
00513
00514 xcb_property_set_handler(&prophs, WM_HINTS, UINT_MAX, handle_hints, NULL);
00515
00516
00517 check_error(conn, xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTED],
00518 ATOM, 32, 7, atoms), "Could not set _NET_SUPPORTED");
00519
00520 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTING_WM_CHECK], WINDOW, 32, 1, &root);
00521 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_WM_NAME], atoms[UTF8_STRING], 8, strlen("i3"), "i3");
00522
00523 keysyms = xcb_key_symbols_alloc(conn);
00524
00525 xcb_get_numlock_mask(conn);
00526
00527 translate_keysyms();
00528 grab_all_keys(conn, false);
00529
00530 int randr_base = -1;
00531 if (force_xinerama) {
00532 initialize_xinerama(conn);
00533 } else {
00534 DLOG("Checking for XRandR...\n");
00535 initialize_randr(conn, &randr_base);
00536
00537 if (randr_base != -1)
00538 xcb_event_set_handler(&evenths,
00539 randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY,
00540 handle_screen_change,
00541 NULL);
00542 }
00543
00544 xcb_flush(conn);
00545
00546
00547 xcb_query_pointer_reply_t *reply;
00548 if ((reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL)) == NULL) {
00549 ELOG("Could not get pointer position\n");
00550 return 1;
00551 }
00552
00553 Output *screen = get_output_containing(reply->root_x, reply->root_y);
00554 if (screen == NULL) {
00555 ELOG("ERROR: No screen at %d x %d, starting on the first screen\n",
00556 reply->root_x, reply->root_y);
00557 screen = get_first_output();
00558 }
00559
00560 DLOG("Starting on %p\n", screen->current_workspace);
00561 c_ws = screen->current_workspace;
00562
00563 manage_existing_windows(conn, &prophs, root);
00564
00565
00566 if (config.ipc_socket_path != NULL) {
00567 int ipc_socket = ipc_create_socket(config.ipc_socket_path);
00568 if (ipc_socket == -1) {
00569 ELOG("Could not create the IPC socket, IPC disabled\n");
00570 } else {
00571 struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
00572 ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
00573 ev_io_start(loop, ipc_io);
00574 }
00575 }
00576
00577
00578 xcb_check_cb(NULL, NULL, 0);
00579
00580 setup_signal_handler();
00581
00582
00583
00584 signal(SIGPIPE, SIG_IGN);
00585
00586
00587 xcb_ungrab_server(conn);
00588
00589
00590 struct Autostart *exec;
00591 if (autostart) {
00592 TAILQ_FOREACH(exec, &autostarts, autostarts) {
00593 LOG("auto-starting %s\n", exec->command);
00594 start_application(exec->command);
00595 }
00596 }
00597
00598 ev_loop(loop, 0);
00599
00600
00601 return 0;
00602 }