]> git.lizzy.rs Git - bspwm.git/blobdiff - bspwm.c
Add new contributor
[bspwm.git] / bspwm.c
diff --git a/bspwm.c b/bspwm.c
index fafc80116f48842e57520258dc3467993cf5d693..249a7d8d35c477be920e68424690091ada8b6b6e 100644 (file)
--- a/bspwm.c
+++ b/bspwm.c
@@ -12,7 +12,7 @@
 #include <xcb/xcb.h>
 #include <xcb/xcb_event.h>
 #include <xcb/xcb_ewmh.h>
-#include <xcb/xinerama.h>
+#include <xcb/randr.h>
 #include "types.h"
 #include "settings.h"
 #include "messages.h"
@@ -30,42 +30,125 @@ void quit(void)
     running = false;
 }
 
+void cleanup(void)
+{
+    while (mon_head != NULL)
+        remove_monitor(mon_head);
+    while (rule_head != NULL)
+        remove_rule(rule_head);
+    free(frozen_pointer);
+}
+
 void register_events(void)
 {
     uint32_t values[] = {ROOT_EVENT_MASK};
-    xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, screen->root, XCB_CW_EVENT_MASK, values));
+    xcb_generic_error_t *e = xcb_request_check(dpy, xcb_change_window_attributes_checked(dpy, root, XCB_CW_EVENT_MASK, values));
     if (e != NULL) {
         xcb_disconnect(dpy);
-        err("another wm is already running\n");
+        err("Another window manager is already running.\n");
     }
 }
 
-void grab_buttons(void)
+bool import_monitors(void)
 {
-    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);
+    PUTS("import monitors");
+    xcb_randr_get_screen_resources_current_reply_t *sres = xcb_randr_get_screen_resources_current_reply(dpy, xcb_randr_get_screen_resources_current(dpy, root), NULL);
+    if (sres == NULL)
+        return false;
+
+    int len = xcb_randr_get_screen_resources_current_outputs_length(sres);
+    xcb_randr_output_t *outputs = xcb_randr_get_screen_resources_current_outputs(sres);
+
+    xcb_randr_get_output_info_cookie_t cookies[len];
+    for (int i = 0; i < len; i++)
+        cookies[i] = xcb_randr_get_output_info(dpy, outputs[i], XCB_CURRENT_TIME);
+
+    for (monitor_t *m = mon_head; m != NULL; m = m->next)
+        m->wired = false;
+
+    monitor_t *mm = NULL;
+    unsigned int num = 0;
+
+    for (int i = 0; i < len; i++) {
+        xcb_randr_get_output_info_reply_t *info = xcb_randr_get_output_info_reply(dpy, cookies[i], NULL);
+        if (info != NULL && info->crtc != XCB_NONE) {
+
+            xcb_randr_get_crtc_info_reply_t *cir = xcb_randr_get_crtc_info_reply(dpy, xcb_randr_get_crtc_info(dpy, info->crtc, XCB_CURRENT_TIME), NULL);
+            if (cir != NULL) {
+                xcb_rectangle_t rect = (xcb_rectangle_t) {cir->x, cir->y, cir->width, cir->height};
+                mm = get_monitor_by_id(outputs[i]);
+                if (mm != NULL) {
+                    mm->rectangle = rect;
+                    for (desktop_t *d = mm->desk_head; d != NULL; d = d->next)
+                        for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
+                            fit_monitor(mm, n->client);
+                    arrange(mm, mm->desk);
+                    mm->wired = true;
+                    PRINTF("update monitor %s (0x%X)\n", mm->name, mm->id);
+                } else {
+                    mm = add_monitor(&rect);
+                    char *name = (char *)xcb_randr_get_output_info_name(info);
+                    size_t name_len = MIN(sizeof(mm->name), (size_t)xcb_randr_get_output_info_name_length(info));
+                    strncpy(mm->name, name, name_len);
+                    mm->name[name_len] = '\0';
+                    mm->id = outputs[i];
+                    add_desktop(mm, make_desktop(NULL));
+                    PRINTF("add monitor %s (0x%X)\n", mm->name, mm->id);
+                }
+                num++;
+            }
+            free(cir);
+        }
+        free(info);
+    }
+
+    monitor_t *m = mon_head;
+    while (m != NULL) {
+        monitor_t *next = m->next;
+        if (!m->wired) {
+            PRINTF("remove monitor %s (0x%X)\n", m->name, m->id);
+            merge_monitors(m, mm);
+            remove_monitor(m);
+        }
+        m = next;
+    }
+
+    free(sres);
+    update_motion_recorder();
+    return (num_monitors > 0);
 }
 
-void ungrab_buttons(void)
+void init(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);
+    num_monitors = num_desktops = num_clients = 0;
+    monitor_uid = desktop_uid = client_uid = rule_uid = 0;
+    mon = last_mon = mon_head = mon_tail = NULL;
+    rule_head = rule_tail = NULL;
+    status_fifo = NULL;
+    randr_base = 0;
+    visible = true;
+    exit_status = 0;
 }
 
 void setup(void)
 {
+    init();
     ewmh_init();
     screen = xcb_setup_roots_iterator(xcb_get_setup(dpy)).data;
-    if (!screen)
-        err("can't acquire screen\n");
+    if (screen == NULL)
+        err("Can't acquire the default screen.\n");
+    root = screen->root;
     register_events();
 
     screen_width = screen->width_in_pixels;
     screen_height = screen->height_in_pixels;
     root_depth = screen->root_depth;
 
+    uint32_t mask = XCB_CW_EVENT_MASK;
+    uint32_t values[] = {XCB_EVENT_MASK_POINTER_MOTION};
+    motion_recorder = xcb_generate_id(dpy);
+    xcb_create_window(dpy, XCB_COPY_FROM_PARENT, motion_recorder, root, 0, 0, screen_width, screen_height, 0, XCB_WINDOW_CLASS_INPUT_ONLY, XCB_COPY_FROM_PARENT, mask, values);
+
     xcb_atom_t net_atoms[] = {ewmh->_NET_SUPPORTED,
                               ewmh->_NET_DESKTOP_NAMES,
                               ewmh->_NET_NUMBER_OF_DESKTOPS,
@@ -75,6 +158,7 @@ void setup(void)
                               ewmh->_NET_WM_DESKTOP,
                               ewmh->_NET_WM_STATE,
                               ewmh->_NET_WM_STATE_FULLSCREEN,
+                              ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
                               ewmh->_NET_WM_WINDOW_TYPE,
                               ewmh->_NET_WM_WINDOW_TYPE_DOCK,
                               ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
@@ -84,71 +168,70 @@ void setup(void)
 
     xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
 
-    monitor_uid = desktop_uid = client_uid = 0;
-    mon = last_mon = mon_head = mon_tail = NULL;
-
-    bool xinerama_is_active = false;
+    xcb_intern_atom_reply_t *iar = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen("_COMPTON_SHADOW"), "_COMPTON_SHADOW"), NULL);
 
-    if (xcb_get_extension_data(dpy, &xcb_xinerama_id)->present) {
-        xcb_xinerama_is_active_reply_t *xia = xcb_xinerama_is_active_reply(dpy, xcb_xinerama_is_active(dpy), NULL);
-        if (xia != NULL) {
-            xinerama_is_active = xia->state;
-            free(xia);
-        }
+    if (iar != NULL) {
+        compton_shadow = iar->atom;
+        free(iar);
     }
 
-    if (xinerama_is_active) {
-        xcb_xinerama_query_screens_reply_t *xsq = xcb_xinerama_query_screens_reply(dpy, xcb_xinerama_query_screens(dpy), NULL);
-        xcb_xinerama_screen_info_t *xsi = xcb_xinerama_query_screens_screen_info(xsq);
-        int n = xcb_xinerama_query_screens_screen_info_length(xsq);
-        PRINTF("number of monitors: %d\n", n);
-        for (int i = 0; i < n; i++) {
-            xcb_xinerama_screen_info_t info = xsi[i];
-            xcb_rectangle_t rect = (xcb_rectangle_t) {info.x_org, info.y_org, info.width, info.height};
-            add_monitor(&rect);
-        }
-        free(xsq);
+    const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
+    if (qep->present && import_monitors()) {
+        randr = true;
+        randr_base = qep->first_event;
+        xcb_randr_select_input(dpy, root, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
     } else {
-        warn("Xinerama is inactive");
+        randr = false;
+        warn("Couldn't retrieve monitors via RandR.\n");
         xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
-        add_monitor(&rect);
+        monitor_t *m = add_monitor(&rect);
+        add_desktop(m, make_desktop(NULL));
     }
 
-    for (monitor_t *m = mon_head; m != NULL; m = m->next)
-        add_desktop(m, NULL);
-
     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);
-    split_mode = MODE_AUTOMATIC;
 }
 
 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;
+    status_prefix = 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, "hvs:p:")) != -1) {
+        switch (opt) {
+            case 'h':
+                printf("bspwm [-h|-v|-s PANEL_FIFO|-p PANEL_PREFIX]\n");
+                exit(EXIT_SUCCESS);
+                break;
+            case 'v':
+                printf("%s\n", VERSION);
+                exit(EXIT_SUCCESS);
+                break;
+            case 's':
+                fifo_path = optarg;
+                break;
+            case 'p':
+                status_prefix = optarg;
+                break;
+        }
+    }
 
     running = true;
-
     dpy = xcb_connect(NULL, &default_screen);
 
     if (xcb_connection_has_error(dpy))
-        err("can't open display\n");
+        err("Can't open the default display.\n");
 
     setup();
 
@@ -164,16 +247,26 @@ int main(int argc, char *argv[])
     sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
 
     if (sock_fd == -1)
-        err("couldn't create socket\n");
+        err("Couldn't create the socket.\n");
 
-    bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address));
-    listen(sock_fd, SOMAXCONN);
+    if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1)
+        err("Couldn't bind a name to the socket.\n");
+
+    if (listen(sock_fd, SOMAXCONN) == -1)
+        err("Couldn't listen to the socket.\n");
 
     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();
 
     while (running) {
@@ -184,7 +277,7 @@ int main(int argc, char *argv[])
         FD_SET(sock_fd, &descriptors);
         FD_SET(dpy_fd, &descriptors);
 
-        if (select(sel, &descriptors, NULL, NULL, NULL)) {
+        if (select(sel, &descriptors, NULL, NULL, NULL) > 0) {
 
             if (FD_ISSET(sock_fd, &descriptors)) {
                 ret_fd = accept(sock_fd, NULL, 0);
@@ -209,15 +302,18 @@ int main(int argc, char *argv[])
 
         }
 
-        if (xcb_connection_has_error(dpy)) {
-            err("connection has errors\n");
-        }
+        if (xcb_connection_has_error(dpy))
+            err("The server has closed the connection.\n");
     }
 
+    cleanup();
     close(sock_fd);
+    if (status_fifo != NULL)
+        fclose(status_fifo);
     xcb_ewmh_connection_wipe(ewmh);
+    xcb_destroy_window(dpy, motion_recorder);
     free(ewmh);
     xcb_flush(dpy);
     xcb_disconnect(dpy);
-    return 0;
+    return exit_status;
 }