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 load_configuration(conn, override_configpath, false);
00287 if (only_check_config) {
00288 LOG("Done checking configuration file. Exiting.\n");
00289 exit(0);
00290 }
00291
00292
00293
00294
00295
00296
00297 expand_table_cols(TAILQ_FIRST(workspaces));
00298 expand_table_rows(TAILQ_FIRST(workspaces));
00299
00300
00301 #define REQUEST_ATOM(name) atom_cookies[name] = xcb_intern_atom(conn, 0, strlen(#name), #name);
00302
00303 REQUEST_ATOM(_NET_SUPPORTED);
00304 REQUEST_ATOM(_NET_WM_STATE_FULLSCREEN);
00305 REQUEST_ATOM(_NET_SUPPORTING_WM_CHECK);
00306 REQUEST_ATOM(_NET_WM_NAME);
00307 REQUEST_ATOM(_NET_WM_STATE);
00308 REQUEST_ATOM(_NET_WM_WINDOW_TYPE);
00309 REQUEST_ATOM(_NET_WM_DESKTOP);
00310 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
00311 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
00312 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
00313 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
00314 REQUEST_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
00315 REQUEST_ATOM(_NET_WM_STRUT_PARTIAL);
00316 REQUEST_ATOM(WM_PROTOCOLS);
00317 REQUEST_ATOM(WM_DELETE_WINDOW);
00318 REQUEST_ATOM(UTF8_STRING);
00319 REQUEST_ATOM(WM_STATE);
00320 REQUEST_ATOM(WM_CLIENT_LEADER);
00321 REQUEST_ATOM(_NET_CURRENT_DESKTOP);
00322 REQUEST_ATOM(_NET_ACTIVE_WINDOW);
00323 REQUEST_ATOM(_NET_WORKAREA);
00324
00325
00326 int major, minor, error;
00327
00328 major = XkbMajorVersion;
00329 minor = XkbMinorVersion;
00330
00331 int errBase;
00332
00333 if ((xkbdpy = XkbOpenDisplay(getenv("DISPLAY"), &xkb_event_base, &errBase, &major, &minor, &error)) == NULL) {
00334 ELOG("ERROR: XkbOpenDisplay() failed, disabling XKB support\n");
00335 xkb_supported = false;
00336 }
00337
00338 if (xkb_supported) {
00339 if (fcntl(ConnectionNumber(xkbdpy), F_SETFD, FD_CLOEXEC) == -1) {
00340 fprintf(stderr, "Could not set FD_CLOEXEC on xkbdpy\n");
00341 return 1;
00342 }
00343
00344 int i1;
00345 if (!XkbQueryExtension(xkbdpy,&i1,&xkb_event_base,&errBase,&major,&minor)) {
00346 fprintf(stderr, "XKB not supported by X-server\n");
00347 return 1;
00348 }
00349
00350
00351 if (!XkbSelectEvents(xkbdpy, XkbUseCoreKbd,
00352 XkbMapNotifyMask | XkbStateNotifyMask,
00353 XkbMapNotifyMask | XkbStateNotifyMask)) {
00354 fprintf(stderr, "Could not set XKB event mask\n");
00355 return 1;
00356 }
00357 }
00358
00359
00360 struct ev_loop *loop = ev_loop_new(0);
00361 if (loop == NULL)
00362 die("Could not initialize libev. Bad LIBEV_FLAGS?\n");
00363
00364 struct ev_io *xcb_watcher = scalloc(sizeof(struct ev_io));
00365 struct ev_io *xkb = scalloc(sizeof(struct ev_io));
00366 struct ev_check *xcb_check = scalloc(sizeof(struct ev_check));
00367 struct ev_prepare *xcb_prepare = scalloc(sizeof(struct ev_prepare));
00368
00369 ev_io_init(xcb_watcher, xcb_got_event, xcb_get_file_descriptor(conn), EV_READ);
00370 ev_io_start(loop, xcb_watcher);
00371
00372 if (xkb_supported) {
00373 ev_io_init(xkb, xkb_got_event, ConnectionNumber(xkbdpy), EV_READ);
00374 ev_io_start(loop, xkb);
00375
00376
00377 XFlush(xkbdpy);
00378 }
00379
00380 ev_check_init(xcb_check, xcb_check_cb);
00381 ev_check_start(loop, xcb_check);
00382
00383 ev_prepare_init(xcb_prepare, xcb_prepare_cb);
00384 ev_prepare_start(loop, xcb_prepare);
00385
00386
00387 xcb_grab_server(conn);
00388
00389 xcb_event_handlers_init(conn, &evenths);
00390
00391
00392 for (i = 2; i < 128; ++i)
00393 xcb_event_set_handler(&evenths, i, handle_event, 0);
00394
00395 for (i = 0; i < 256; ++i)
00396 xcb_event_set_error_handler(&evenths, i, (xcb_generic_error_handler_t)handle_event, 0);
00397
00398
00399 xcb_event_set_expose_handler(&evenths, handle_expose_event, NULL);
00400
00401
00402 xcb_event_set_key_press_handler(&evenths, handle_key_press, NULL);
00403
00404
00405 xcb_event_set_enter_notify_handler(&evenths, handle_enter_notify, NULL);
00406
00407
00408 xcb_event_set_button_press_handler(&evenths, handle_button_press, NULL);
00409
00410
00411 xcb_event_set_map_request_handler(&evenths, handle_map_request, &prophs);
00412
00413
00414
00415 xcb_event_set_unmap_notify_handler(&evenths, handle_unmap_notify_event, NULL);
00416
00417
00418 xcb_event_set_destroy_notify_handler(&evenths, handle_destroy_notify_event, NULL);
00419
00420
00421
00422 xcb_event_set_configure_notify_handler(&evenths, handle_configure_event, NULL);
00423
00424
00425 xcb_event_set_configure_request_handler(&evenths, handle_configure_request, NULL);
00426
00427
00428
00429 xcb_event_set_motion_notify_handler(&evenths, handle_motion_notify, NULL);
00430
00431
00432 xcb_event_set_mapping_notify_handler(&evenths, handle_mapping_notify, NULL);
00433
00434
00435
00436 xcb_event_set_client_message_handler(&evenths, handle_client_message, NULL);
00437
00438
00439 xcb_property_handlers_init(&prophs, &evenths);
00440
00441
00442 xcb_property_set_handler(&prophs, WM_NORMAL_HINTS, UINT_MAX, handle_normal_hints, NULL);
00443
00444
00445 xcb_screen_t *root_screen = xcb_aux_get_screen(conn, screens);
00446 root = root_screen->root;
00447 root_depth = root_screen->root_depth;
00448
00449 uint32_t mask = XCB_CW_EVENT_MASK;
00450 uint32_t values[] = { XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
00451 XCB_EVENT_MASK_STRUCTURE_NOTIFY |
00452
00453
00454 XCB_EVENT_MASK_POINTER_MOTION |
00455 XCB_EVENT_MASK_PROPERTY_CHANGE |
00456 XCB_EVENT_MASK_ENTER_WINDOW };
00457 xcb_void_cookie_t cookie;
00458 cookie = xcb_change_window_attributes_checked(conn, root, mask, values);
00459 check_error(conn, cookie, "Another window manager seems to be running");
00460
00461
00462 #define GET_ATOM(name) { \
00463 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(conn, atom_cookies[name], NULL); \
00464 if (!reply) { \
00465 ELOG("Could not get atom " #name "\n"); \
00466 exit(-1); \
00467 } \
00468 atoms[name] = reply->atom; \
00469 free(reply); \
00470 }
00471
00472 GET_ATOM(_NET_SUPPORTED);
00473 GET_ATOM(_NET_WM_STATE_FULLSCREEN);
00474 GET_ATOM(_NET_SUPPORTING_WM_CHECK);
00475 GET_ATOM(_NET_WM_NAME);
00476 GET_ATOM(_NET_WM_STATE);
00477 GET_ATOM(_NET_WM_WINDOW_TYPE);
00478 GET_ATOM(_NET_WM_DESKTOP);
00479 GET_ATOM(_NET_WM_WINDOW_TYPE_DOCK);
00480 GET_ATOM(_NET_WM_WINDOW_TYPE_DIALOG);
00481 GET_ATOM(_NET_WM_WINDOW_TYPE_UTILITY);
00482 GET_ATOM(_NET_WM_WINDOW_TYPE_TOOLBAR);
00483 GET_ATOM(_NET_WM_WINDOW_TYPE_SPLASH);
00484 GET_ATOM(_NET_WM_STRUT_PARTIAL);
00485 GET_ATOM(WM_PROTOCOLS);
00486 GET_ATOM(WM_DELETE_WINDOW);
00487 GET_ATOM(UTF8_STRING);
00488 GET_ATOM(WM_STATE);
00489 GET_ATOM(WM_CLIENT_LEADER);
00490 GET_ATOM(_NET_CURRENT_DESKTOP);
00491 GET_ATOM(_NET_ACTIVE_WINDOW);
00492 GET_ATOM(_NET_WORKAREA);
00493
00494 xcb_property_set_handler(&prophs, atoms[_NET_WM_WINDOW_TYPE], UINT_MAX, handle_window_type, NULL);
00495
00496
00497
00498 xcb_property_set_handler(&prophs, atoms[_NET_WM_NAME], 128, handle_windowname_change, NULL);
00499
00500
00501 xcb_property_set_handler(&prophs, WM_TRANSIENT_FOR, UINT_MAX, handle_transient_for, NULL);
00502
00503
00504 xcb_watch_wm_name(&prophs, 128, handle_windowname_change_legacy, NULL);
00505
00506
00507 xcb_property_set_handler(&prophs, WM_CLASS, 128, handle_windowclass_change, NULL);
00508
00509
00510 xcb_property_set_handler(&prophs, atoms[WM_CLIENT_LEADER], UINT_MAX, handle_clientleader_change, NULL);
00511
00512
00513 xcb_property_set_handler(&prophs, WM_HINTS, UINT_MAX, handle_hints, NULL);
00514
00515
00516 check_error(conn, xcb_change_property_checked(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTED],
00517 ATOM, 32, 7, atoms), "Could not set _NET_SUPPORTED");
00518
00519 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_SUPPORTING_WM_CHECK], WINDOW, 32, 1, &root);
00520 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, atoms[_NET_WM_NAME], atoms[UTF8_STRING], 8, strlen("i3"), "i3");
00521
00522 keysyms = xcb_key_symbols_alloc(conn);
00523
00524 xcb_get_numlock_mask(conn);
00525
00526 translate_keysyms();
00527 grab_all_keys(conn, false);
00528
00529 int randr_base;
00530 if (force_xinerama) {
00531 initialize_xinerama(conn);
00532 } else {
00533 DLOG("Checking for XRandR...\n");
00534 initialize_randr(conn, &randr_base);
00535
00536 xcb_event_set_handler(&evenths,
00537 randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY,
00538 handle_screen_change,
00539 NULL);
00540 }
00541
00542 xcb_flush(conn);
00543
00544
00545 xcb_query_pointer_reply_t *reply;
00546 if ((reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL)) == NULL) {
00547 ELOG("Could not get pointer position\n");
00548 return 1;
00549 }
00550
00551 Output *screen = get_output_containing(reply->root_x, reply->root_y);
00552 if (screen == NULL) {
00553 ELOG("ERROR: No screen at %d x %d, starting on the first screen\n",
00554 reply->root_x, reply->root_y);
00555 screen = get_first_output();
00556 }
00557
00558 DLOG("Starting on %p\n", screen->current_workspace);
00559 c_ws = screen->current_workspace;
00560
00561 manage_existing_windows(conn, &prophs, root);
00562
00563
00564 if (config.ipc_socket_path != NULL) {
00565 int ipc_socket = ipc_create_socket(config.ipc_socket_path);
00566 if (ipc_socket == -1) {
00567 ELOG("Could not create the IPC socket, IPC disabled\n");
00568 } else {
00569 struct ev_io *ipc_io = scalloc(sizeof(struct ev_io));
00570 ev_io_init(ipc_io, ipc_new_client, ipc_socket, EV_READ);
00571 ev_io_start(loop, ipc_io);
00572 }
00573 }
00574
00575
00576 xcb_check_cb(NULL, NULL, 0);
00577
00578 setup_signal_handler();
00579
00580
00581
00582 signal(SIGPIPE, SIG_IGN);
00583
00584
00585 xcb_ungrab_server(conn);
00586
00587
00588 struct Autostart *exec;
00589 if (autostart) {
00590 TAILQ_FOREACH(exec, &autostarts, autostarts) {
00591 LOG("auto-starting %s\n", exec->command);
00592 start_application(exec->command);
00593 }
00594 }
00595
00596 ev_loop(loop, 0);
00597
00598
00599 return 0;
00600 }