X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=bspwm.c;h=ea3b0ecd468d583fba5437e32d1a45d89e298d60;hb=62f32bc655936ce7a4345bc836ccdf1c987cc743;hp=0f70809fe2f79508863099b09d7fb89e7ca9d390;hpb=159b1585995681060e3b13a94d2101cc6fa8caeb;p=bspwm.git diff --git a/bspwm.c b/bspwm.c index 0f70809..ea3b0ec 100644 --- a/bspwm.c +++ b/bspwm.c @@ -36,40 +36,35 @@ void register_events(void) xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, screen->root, XCB_CW_EVENT_MASK, values)); if (e != NULL) { xcb_disconnect(dpy); - err("another WM is already running\n"); + err("another wm is already running\n"); } } -void grab_buttons(void) +void handle_buttons(bool grab) { - xcb_grab_button(dpy, false, screen->root, XCB_EVENT_MASK_BUTTON_PRESS, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE, XCB_BUTTON_INDEX_1, button_modifier); - xcb_grab_button(dpy, false, screen->root, XCB_EVENT_MASK_BUTTON_PRESS, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE, XCB_BUTTON_INDEX_2, button_modifier); - xcb_grab_button(dpy, false, screen->root, XCB_EVENT_MASK_BUTTON_PRESS, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE, XCB_BUTTON_INDEX_3, button_modifier); + uint8_t buts[] = {XCB_BUTTON_INDEX_1, XCB_BUTTON_INDEX_2, XCB_BUTTON_INDEX_3}; + uint16_t mods[] = {button_modifier, button_modifier | numlock_modifier, button_modifier | capslock_modifier, button_modifier | numlock_modifier | capslock_modifier}; + + for (unsigned int i = 0; i < LENGTH(buts); i++) { + uint8_t b = buts[i]; + for (unsigned int j = 0; j < LENGTH(mods); j++) { + uint16_t m = mods[j]; + if (grab) + xcb_grab_button(dpy, false, screen->root, XCB_EVENT_MASK_BUTTON_PRESS, XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC, XCB_NONE, XCB_NONE, b, m); + else + xcb_ungrab_button(dpy, b, screen->root, m); + } + } } -void ungrab_buttons(void) +void grab_buttons(void) { - xcb_ungrab_button(dpy, XCB_BUTTON_INDEX_1, screen->root, button_modifier); - xcb_ungrab_button(dpy, XCB_BUTTON_INDEX_2, screen->root, button_modifier); - xcb_ungrab_button(dpy, XCB_BUTTON_INDEX_3, screen->root, button_modifier); + handle_buttons(true); } -void adopt_orphans(void) +void ungrab_buttons(void) { - xcb_query_tree_reply_t *qtr = xcb_query_tree_reply(dpy, xcb_query_tree(dpy, screen->root), NULL); - if (qtr == NULL) - return; - int len = xcb_query_tree_children_length(qtr); - xcb_window_t *wins = xcb_query_tree_children(qtr); - for (int i = 0; i < len; i++) { - xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, wins[i]), NULL); - if (wa != NULL) { - if (wa->map_state == XCB_MAP_STATE_VIEWABLE) - manage_window(wins[i]); - free(wa); - } - } - free(qtr); + handle_buttons(false); } void setup(void) @@ -77,7 +72,8 @@ void setup(void) ewmh_init(); screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data; if (!screen) - err("error: cannot aquire screen\n"); + err("can't acquire screen\n"); + register_events(); screen_width = screen->width_in_pixels; screen_height = screen->height_in_pixels; @@ -136,43 +132,52 @@ void setup(void) ewmh_update_number_of_desktops(); ewmh_update_desktop_names(); + ewmh_update_current_desktop(); rule_head = make_rule(); frozen_pointer = make_pointer_state(); get_pointer_position(&pointer_position); + last_entered = XCB_NONE; split_mode = MODE_AUTOMATIC; + visible = true; + exit_status = 0; } int main(int argc, char *argv[]) { - if (argc == 2 && strcmp(argv[1], "-v") == 0) { - printf("%s\n", VERSION); - exit(EXIT_SUCCESS); - } - fd_set descriptors; char socket_path[MAXLEN]; + char *fifo_path = NULL; int sock_fd, ret_fd, dpy_fd, sel, n; struct sockaddr_un sock_address; size_t rsplen = 0; char msg[BUFSIZ] = {0}; char rsp[BUFSIZ] = {0}; - xcb_generic_event_t *event; + char opt; + + while ((opt = getopt(argc, argv, "vs:")) != -1) { + switch (opt) { + case 'v': + printf("%s\n", VERSION); + exit(EXIT_SUCCESS); + break; + case 's': + fifo_path = optarg; + break; + } + } running = true; - dpy = xcb_connect(NULL, &default_screen); if (xcb_connection_has_error(dpy)) - err("error: cannot open display\n"); + err("can't open display\n"); setup(); - register_events(); dpy_fd = xcb_get_file_descriptor(dpy); char *sp = getenv(SOCKET_ENV_VAR); - strncpy(socket_path, (sp == NULL ? DEFAULT_SOCKET_PATH : sp), sizeof(socket_path)); sock_address.sun_family = AF_UNIX; @@ -182,18 +187,24 @@ int main(int argc, char *argv[]) sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); if (sock_fd == -1) - err("error: could not create socket\n"); + err("couldn't create socket\n"); bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)); listen(sock_fd, SOMAXCONN); - sel = MAX(sock_fd, dpy_fd) + 1; + if (fifo_path != NULL) { + int fifo_fd = open(fifo_path, O_RDWR | O_NONBLOCK); + if (fifo_fd != -1) + status_fifo = fdopen(fifo_fd, "w"); + else + warn("couldn't open status fifo\n"); + } + load_settings(); run_autostart(); grab_buttons(); ewmh_update_wm_name(); - adopt_orphans(); while (running) { @@ -234,9 +245,11 @@ int main(int argc, char *argv[]) } close(sock_fd); + if (status_fifo != NULL) + fclose(status_fifo); xcb_ewmh_connection_wipe(ewmh); free(ewmh); xcb_flush(dpy); xcb_disconnect(dpy); - return 0; + return exit_status; }