]> git.lizzy.rs Git - bspwm.git/blobdiff - bspwm.c
Enhance external rules example scripts
[bspwm.git] / bspwm.c
diff --git a/bspwm.c b/bspwm.c
index 10ed3085f563ce6e50f549aa77ed1e40335c0a2f..680ea6b5aa2418f2bc9d67c3ef891a8f090c4f76 100644 (file)
--- a/bspwm.c
+++ b/bspwm.c
@@ -1,37 +1,60 @@
-#include <stdlib.h>
+/* * Copyright (c) 2012-2013 Bastien Dejean
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ *  * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation and/or
+ * other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
-#include <signal.h>
-#include <fcntl.h>
-#include <ctype.h>
+#include <sys/select.h>
+#ifdef __OpenBSD__
+#include <sys/types.h>
+#endif
+#include <sys/wait.h>
 #include <sys/socket.h>
 #include <sys/un.h>
-#include <sys/select.h>
-#include <xcb/xcb.h>
-#include <xcb/xcb_event.h>
-#include <xcb/xcb_ewmh.h>
-#include <xcb/randr.h>
+#include <signal.h>
+#include <unistd.h>
 #include "types.h"
+#include "desktop.h"
+#include "monitor.h"
 #include "settings.h"
 #include "messages.h"
-#include "rules.h"
+#include "subscribe.h"
 #include "events.h"
 #include "common.h"
-#include "helpers.h"
 #include "window.h"
-#include "bspwm.h"
-#include "tree.h"
+#include "history.h"
+#include "stack.h"
 #include "ewmh.h"
+#include "rule.h"
+#include "bspwm.h"
 
 int main(int argc, char *argv[])
 {
     fd_set descriptors;
     char socket_path[MAXLEN];
-    char *fifo_path = NULL;
     config_path[0] = '\0';
-    status_prefix = NULL;
-    int sock_fd, ret_fd, dpy_fd, sel, n;
+    int sock_fd, cli_fd, dpy_fd, max_fd, n;
     struct sockaddr_un sock_address;
     size_t rsp_len = 0;
     char msg[BUFSIZ] = {0};
@@ -39,10 +62,10 @@ int main(int argc, char *argv[])
     xcb_generic_event_t *event;
     char opt;
 
-    while ((opt = getopt(argc, argv, "hvc:s:p:")) != -1) {
+    while ((opt = getopt(argc, argv, "hvc:")) != -1) {
         switch (opt) {
             case 'h':
-                printf(WM_NAME " [-h|-v|-c CONFIG_PATH|-s PANEL_FIFO|-p PANEL_PREFIX]\n");
+                printf(WM_NAME " [-h|-v|-c CONFIG_PATH]\n");
                 exit(EXIT_SUCCESS);
                 break;
             case 'v':
@@ -50,13 +73,7 @@ int main(int argc, char *argv[])
                 exit(EXIT_SUCCESS);
                 break;
             case 'c':
-                strncpy(config_path, optarg, sizeof(config_path));
-                break;
-            case 's':
-                fifo_path = optarg;
-                break;
-            case 'p':
-                status_prefix = optarg;
+                snprintf(config_path, sizeof(config_path), "%s", optarg);
                 break;
         }
     }
@@ -79,10 +96,10 @@ int main(int argc, char *argv[])
     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));
+    snprintf(socket_path, sizeof(socket_path), "%s", (sp == NULL ? DEFAULT_SOCKET_PATH : sp));
 
     sock_address.sun_family = AF_UNIX;
-    strncpy(sock_address.sun_path, socket_path, sizeof(sock_address.sun_path));
+    snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s", socket_path);
     unlink(socket_path);
 
     sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
@@ -96,16 +113,8 @@ int main(int argc, char *argv[])
     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");
-    }
-
+    signal(SIGCHLD, sig_handler);
+    signal(SIGPIPE, SIG_IGN);
     load_settings();
     run_config();
     running = true;
@@ -117,23 +126,41 @@ int main(int argc, char *argv[])
         FD_ZERO(&descriptors);
         FD_SET(sock_fd, &descriptors);
         FD_SET(dpy_fd, &descriptors);
+        max_fd = MAX(sock_fd, dpy_fd);
+        for (pending_rule_t *pr = pending_rule_head; pr != NULL; pr = pr->next) {
+            FD_SET(pr->fd, &descriptors);
+            if (pr->fd > max_fd)
+                max_fd = pr->fd;
+        }
+
+        if (select(max_fd + 1, &descriptors, NULL, NULL, NULL) > 0) {
 
-        if (select(sel, &descriptors, NULL, NULL, NULL) > 0) {
+            pending_rule_t *pr = pending_rule_head;
+            while (pr != NULL) {
+                pending_rule_t *next = pr->next;
+                if (FD_ISSET(pr->fd, &descriptors)) {
+                    manage_window(pr->win, pr->csq, pr->fd);
+                    remove_pending_rule(pr);
+                }
+                pr = next;
+            }
 
             if (FD_ISSET(sock_fd, &descriptors)) {
-                ret_fd = accept(sock_fd, NULL, 0);
-                if (ret_fd > 0 && (n = recv(ret_fd, msg, sizeof(msg), 0)) > 0) {
+                cli_fd = accept(sock_fd, NULL, 0);
+                if (cli_fd > 0 && (n = recv(cli_fd, msg, sizeof(msg), 0)) > 0) {
                     msg[n] = '\0';
                     if (handle_message(msg, n, rsp)) {
                         rsp_len = strlen(rsp);
-                        if (rsp[rsp_len - 1] == '\n')
-                            rsp[--rsp_len] = '\0';
                     } else {
                         rsp[0] = MESSAGE_FAILURE;
                         rsp_len = 1;
                     }
-                    send(ret_fd, rsp, rsp_len, 0);
-                    close(ret_fd);
+                    if (rsp_len == 1 && rsp[0] == MESSAGE_SUBSCRIBE) {
+                        add_subscriber(cli_fd);
+                    } else {
+                        send(cli_fd, rsp, rsp_len, 0);
+                        close(cli_fd);
+                    }
                     rsp[0] = '\0';
                 }
             }
@@ -144,7 +171,6 @@ int main(int argc, char *argv[])
                     free(event);
                 }
             }
-
         }
 
         if (xcb_connection_has_error(dpy))
@@ -153,8 +179,6 @@ int main(int argc, char *argv[])
 
     cleanup();
     close(sock_fd);
-    if (status_fifo != NULL)
-        fclose(status_fifo);
     xcb_ewmh_connection_wipe(ewmh);
     xcb_destroy_window(dpy, motion_recorder);
     free(ewmh);
@@ -166,13 +190,15 @@ int main(int argc, char *argv[])
 void init(void)
 {
     num_monitors = num_desktops = num_clients = 0;
-    monitor_uid = desktop_uid = rule_uid = 0;
-    mon = last_mon = mon_head = mon_tail = pri_mon = NULL;
-    rule_head = rule_tail = NULL;
-    status_fifo = NULL;
+    monitor_uid = desktop_uid = 0;
+    mon = mon_head = mon_tail = pri_mon = NULL;
+    history_head = history_tail = history_needle = NULL;
+    stack_head = stack_tail = NULL;
+    subscribe_head = subscribe_tail = NULL;
+    pending_rule_head = pending_rule_tail = NULL;
     last_motion_time = last_motion_x = last_motion_y = 0;
+    visible = auto_raise = sticky_still = record_history = true;
     randr_base = 0;
-    visible = auto_raise = true;
     exit_status = 0;
 }
 
@@ -202,18 +228,20 @@ void setup(void)
                               ewmh->_NET_CURRENT_DESKTOP,
                               ewmh->_NET_CLIENT_LIST,
                               ewmh->_NET_ACTIVE_WINDOW,
+                              ewmh->_NET_CLOSE_WINDOW,
                               ewmh->_NET_WM_DESKTOP,
                               ewmh->_NET_WM_STATE,
                               ewmh->_NET_WM_STATE_FULLSCREEN,
+                              ewmh->_NET_WM_STATE_STICKY,
                               ewmh->_NET_WM_STATE_DEMANDS_ATTENTION,
                               ewmh->_NET_WM_WINDOW_TYPE,
                               ewmh->_NET_WM_WINDOW_TYPE_DOCK,
+                              ewmh->_NET_WM_WINDOW_TYPE_DESKTOP,
                               ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION,
                               ewmh->_NET_WM_WINDOW_TYPE_DIALOG,
                               ewmh->_NET_WM_WINDOW_TYPE_UTILITY,
                               ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR};
 
-    xcb_ewmh_set_wm_name(ewmh, root, strlen(WM_NAME), WM_NAME);
     xcb_ewmh_set_supported(ewmh, default_screen, LENGTH(net_atoms), net_atoms);
     ewmh_set_supporting(motion_recorder);
 
@@ -222,6 +250,7 @@ void setup(void)
     GETATOM(WM_DELETE_WINDOW)
     GETATOM(WM_TAKE_FOCUS)
     GETATOM(_BSPWM_FLOATING_WINDOW)
+    GETATOM(_NET_WM_WINDOW_OPACITY)
 #undef GETATOM
 
     const xcb_query_extension_reply_t *qep = xcb_get_extension_data(dpy, &xcb_randr_id);
@@ -233,7 +262,7 @@ void setup(void)
         randr = false;
         warn("Couldn't retrieve monitors via RandR.\n");
         xcb_rectangle_t rect = (xcb_rectangle_t) {0, 0, screen_width, screen_height};
-        monitor_t *m = add_monitor(&rect);
+        monitor_t *m = add_monitor(rect);
         add_desktop(m, make_desktop(NULL));
     }
 
@@ -257,93 +286,6 @@ void register_events(void)
     }
 }
 
-bool import_monitors(void)
-{
-    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];
-                    PRINTF("add monitor %s (0x%X)\n", mm->name, mm->id);
-                }
-                num++;
-            }
-            free(cir);
-        }
-        free(info);
-    }
-
-    /* initially focus the primary monitor and add the first desktop to it */
-    xcb_randr_get_output_primary_reply_t *gpo = xcb_randr_get_output_primary_reply(dpy, xcb_randr_get_output_primary(dpy, root), NULL);
-    if (gpo != NULL) {
-        pri_mon = get_monitor_by_id(gpo->output);
-        if (!running && pri_mon != NULL) {
-            if (mon != pri_mon)
-                mon = pri_mon;
-            add_desktop(pri_mon, make_desktop(NULL));
-            ewmh_update_current_desktop();
-        }
-    }
-    free(gpo);
-
-    /* add one desktop to each new monitor */
-    for (monitor_t *m = mon_head; m != NULL; m = m->next)
-        if (m->desk == NULL && (running || pri_mon == NULL || m != pri_mon))
-            add_desktop(m, make_desktop(NULL));
-
-    /* merge and remove disconnected monitors */
-    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 quit(void)
 {
     running = false;
@@ -353,31 +295,30 @@ void cleanup(void)
 {
     while (mon_head != NULL)
         remove_monitor(mon_head);
-    while (rule_head != NULL)
-        remove_rule(rule_head);
+    while (stack_head != NULL)
+        remove_stack(stack_head);
+    while (subscribe_head != NULL)
+        remove_subscriber(subscribe_head);
+    while (pending_rule_head != NULL)
+        remove_pending_rule(pending_rule_head);
+    empty_history();
     free(frozen_pointer);
 }
 
 void put_status(void)
 {
-    if (status_fifo == NULL)
-        return;
-    if (status_prefix != NULL)
-        fprintf(status_fifo, "%s", status_prefix);
-    bool urgent = false;
-    for (monitor_t *m = mon_head; m != NULL; m = m->next) {
-        fprintf(status_fifo, "%c%s:", (mon == m ? 'M' : 'm'), m->name);
-        for (desktop_t *d = m->desk_head; d != NULL; d = d->next, urgent = false) {
-            for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n, d->root))
-                urgent |= n->client->urgent;
-            char c = (urgent ? 'u' : (d->root == NULL ? 'f' : 'o'));
-            if (m->desk == d)
-                c = toupper(c);
-            fprintf(status_fifo, "%c%s:", c, d->name);
-        }
+    subscriber_list_t *sb = subscribe_head;
+    while (sb != NULL) {
+        subscriber_list_t *next = sb->next;
+        feed_subscriber(sb);
+        sb = next;
     }
-    if (mon != NULL && mon->desk != NULL)
-        fprintf(status_fifo, "L%s", (mon->desk->layout == LAYOUT_TILED ? "tiled" : "monocle"));
-    fprintf(status_fifo, "\n");
-    fflush(status_fifo);
+}
+
+void sig_handler(int sig)
+{
+    signal(sig, sig_handler);
+    if (sig == SIGCHLD)
+        while (waitpid(-1, 0, WNOHANG) > 0)
+            ;
 }