Adding Xinerama support
authorphillbush <phillbush@cock.li>
Wed, 29 Jul 2020 20:55:20 +0000 (17:55 -0300)
committerphillbush <phillbush@cock.li>
Wed, 29 Jul 2020 20:55:20 +0000 (17:55 -0300)
xmenu.1
xmenu.c
xmenu.h

diff --git a/xmenu.1 b/xmenu.1
index d87ba4b..a85d4cf 100644 (file)
--- a/xmenu.1
+++ b/xmenu.1
@@ -30,8 +30,34 @@ Set the position to spawn xmenu.
 Without this option, xmenu spawns next to the cursor.
 .I position
 is a string of the form
 Without this option, xmenu spawns next to the cursor.
 .I position
 is a string of the form
-.BR INTxINT ,
+.BR INTxINT[:MONITOR] ,
 where the first INT is the x position and the second INT is the y position.
 where the first INT is the x position and the second INT is the y position.
+The monitor part between brackets is optional.
+.B MONITOR
+can be a number from 0 to the number of monitors minus 1;
+or it can be a string like
+.B current
+or
+.BR cursor .
+If present, the monitor specifies that the position is relative to the upper left corner
+of that monitor.
+If
+.B monitor
+is
+.B current
+or
+.BR cursor ,
+the monitor to be used is that where the cursor is in.
+For example,
+.B -p 0x0:cursor
+specifies that
+.B xmenu
+must spawn at the position 0x0 of the monitor where the cursor is in.
+And
+.B -p 100x500:0
+specifies that
+.B xmenu
+must spawn at the position 100x500 of the monitor 0.
 .TP
 .B -w
 Asks the window manager to draw a border around the menus.
 .TP
 .B -w
 Asks the window manager to draw a border around the menus.
diff --git a/xmenu.c b/xmenu.c
index a80876d..4a4ffca 100644 (file)
--- a/xmenu.c
+++ b/xmenu.c
@@ -13,6 +13,7 @@
 #include <X11/Xresource.h>
 #include <X11/XKBlib.h>
 #include <X11/Xft/Xft.h>
 #include <X11/Xresource.h>
 #include <X11/XKBlib.h>
 #include <X11/Xft/Xft.h>
+#include <X11/extensions/Xinerama.h>
 #include <Imlib2.h>
 #include "xmenu.h"
 
 #include <Imlib2.h>
 #include "xmenu.h"
 
  */
 
 /* argument parser */
  */
 
 /* argument parser */
-static void parseposition(const char *optarg);
+static void parseposition(char *optarg);
 
 /* initializers, and their helper routines */
 static void parsefonts(const char *s);
 static void ealloccolor(const char *s, XftColor *color);
 
 /* initializers, and their helper routines */
 static void parsefonts(const char *s);
 static void ealloccolor(const char *s, XftColor *color);
+static void initmonitor(void);
 static void initresources(void);
 static void initdc(void);
 static void initconfig(void);
 static void initresources(void);
 static void initdc(void);
 static void initconfig(void);
@@ -87,13 +89,15 @@ static Visual *visual;
 static Window rootwin;
 static Colormap colormap;
 static struct DC dc;
 static Window rootwin;
 static Colormap colormap;
 static struct DC dc;
+static struct Monitor mon;
 static Atom utf8string;
 static Atom wmdelete;
 static Atom netatom[NetLast];
 
 /* flags */
 static int iflag = 0;   /* whether to disable icons */
 static Atom utf8string;
 static Atom wmdelete;
 static Atom netatom[NetLast];
 
 /* flags */
 static int iflag = 0;   /* whether to disable icons */
-static int pflag = 0;   /* whether the user specified a position */
+static int mflag = 0;   /* whether the user specified a monitor with -p */
+static int pflag = 0;   /* whether the user specified a position with -p */
 static int wflag = 0;   /* whether to let the window manager control XMenu */
 
 /* include config variable */
 static int wflag = 0;   /* whether to let the window manager control XMenu */
 
 /* include config variable */
@@ -153,6 +157,7 @@ main(int argc, char *argv[])
        }
 
        /* initializers */
        }
 
        /* initializers */
+       initmonitor();
        initresources();
        initdc();
        initconfig();
        initresources();
        initdc();
        initconfig();
@@ -187,12 +192,13 @@ main(int argc, char *argv[])
        return 0;
 }
 
        return 0;
 }
 
-/* parse position string from -p, put results on config.x and config.y */
+/* parse position string from -p,
+ * put results on config.posx, config.posy, and config.monitor */
 static void
 static void
-parseposition(const char *optarg)
+parseposition(char *optarg)
 {
        long n;
 {
        long n;
-       const char *s = optarg;
+       char *s = optarg;
        char *endp;
 
        n = strtol(s, &endp, 10);
        char *endp;
 
        n = strtol(s, &endp, 10);
@@ -201,9 +207,24 @@ parseposition(const char *optarg)
        config.posx = n;
        s = endp+1;
        n = strtol(s, &endp, 10);
        config.posx = n;
        s = endp+1;
        n = strtol(s, &endp, 10);
-       if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s || *endp != '\0')
+       if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s)
                goto error;
        config.posy = n;
                goto error;
        config.posy = n;
+       if (*endp == ':') {
+               s = endp+1;
+               mflag = 1;
+               if (strncasecmp(s, "CUR", 3) == 0) {
+                       config.monitor = -1;
+                       endp = s+3;
+               } else {
+                       n = strtol(s, &endp, 10);
+                       if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s || *endp != '\0')
+                               goto error;
+                       config.monitor = n;
+               }
+       } else if (*endp != '\0') {
+               goto error;
+       }
 
        return;
 
 
        return;
 
@@ -253,6 +274,54 @@ ealloccolor(const char *s, XftColor *color)
                errx(1, "cannot allocate color: %s", s);
 }
 
                errx(1, "cannot allocate color: %s", s);
 }
 
+/* query monitor information and cursor position */
+static void
+initmonitor(void)
+{
+       XineramaScreenInfo *info = NULL;
+       Window dw;          /* dummy variable */
+       int di;             /* dummy variable */
+       unsigned du;        /* dummy variable */
+       int cursx, cursy;   /* cursor position */
+       int nmons;
+       int i;
+
+       XQueryPointer(dpy, rootwin, &dw, &dw, &cursx, &cursy, &di, &di, &du);
+
+       mon.x = mon.y = 0;
+       mon.w = DisplayWidth(dpy, screen);
+       mon.h = DisplayHeight(dpy, screen);
+
+       if ((info = XineramaQueryScreens(dpy, &nmons)) != NULL) {
+               int selmon = 0;
+
+               if (!mflag || (mflag && (config.monitor < 0 || config.monitor >= nmons))) {
+                       for (i = 0; i < nmons; i++) {
+                               if (cursx >= info[i].x_org && cursx <= info[i].x_org + info[i].width &&
+                                   cursy >= info[i].y_org && cursy <= info[i].y_org + info[i].height) {
+                                       selmon = i;
+                                       break;
+                               }
+                       }
+               } else {
+                       selmon = config.monitor;
+               }
+
+               mon.x = info[selmon].x_org;
+               mon.y = info[selmon].y_org;
+               mon.w = info[selmon].width;
+               mon.h = info[selmon].height;
+       }
+
+       if (!pflag) {
+               config.posx = cursx;
+               config.posy = cursy;
+       } else if (mflag) {
+               config.posx += mon.x;
+               config.posy += mon.y;
+       }
+}
+
 /* read xrdb for configuration options */
 static void
 initresources(void)
 /* read xrdb for configuration options */
 static void
 initresources(void)
@@ -325,12 +394,6 @@ initdc(void)
 static void
 initconfig(void)
 {
 static void
 initconfig(void)
 {
-       Window dw;   /* dummy variable */
-       int di;      /* dummy variable */
-       unsigned du; /* dummy variable */
-
-       if (!pflag)  /* if the user haven't specified a position, use cursor position*/
-               XQueryPointer(dpy, rootwin, &dw, &dw, &config.posx, &config.posy, &di, &di, &du);
        config.screenw = DisplayWidth(dpy, screen);
        config.screenh = DisplayHeight(dpy, screen);
        config.iconsize = config.height_pixels - config.iconpadding * 2;
        config.screenw = DisplayWidth(dpy, screen);
        config.screenh = DisplayHeight(dpy, screen);
        config.iconsize = config.height_pixels - config.iconpadding * 2;
@@ -557,7 +620,7 @@ getnextutf8char(const char *s, const char **next_ret)
        s++;
        for (i = 1; i < usize; i++) {
                *next_ret = s+1;
        s++;
        for (i = 1; i < usize; i++) {
                *next_ret = s+1;
-               /* if byte is EOS or is not a continuation byte, return unknown */
+               /* if byte is nul or is not a continuation byte, return unknown */
                if (*s == '\0' || ((unsigned char)*s & utfmask[0]) != utfbyte[0])
                        return unknown;
                /* 6 is the number of relevant bits in the continuation byte */
                if (*s == '\0' || ((unsigned char)*s & utfmask[0]) != utfbyte[0])
                        return unknown;
                /* 6 is the number of relevant bits in the continuation byte */
@@ -671,27 +734,31 @@ setupmenupos(struct Menu *menu)
        width = menu->w + config.border_pixels * 2;
        height = menu->h + config.border_pixels * 2;
        if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
        width = menu->w + config.border_pixels * 2;
        height = menu->h + config.border_pixels * 2;
        if (menu->parent == NULL) { /* if root menu, calculate in respect to cursor */
-               if (pflag || config.screenw - config.posx >= menu->w)
+               if (pflag || (config.posx > mon.x && mon.x + mon.w - config.posx >= width))
                        menu->x = config.posx;
                else if (config.posx > width)
                        menu->x = config.posx - width;
 
                        menu->x = config.posx;
                else if (config.posx > width)
                        menu->x = config.posx - width;
 
-               if (pflag || config.screenh - config.posy >= height)
+               if (pflag || (config.posy > mon.y && mon.y + mon.h - config.posy >= height))
                        menu->y = config.posy;
                else if (config.screenh > height)
                        menu->y = config.posy;
                else if (config.screenh > height)
-                       menu->y = config.screenh - height;
+                       menu->y = mon.y + mon.h - height;
        } else {                    /* else, calculate in respect to parent menu */
        } else {                    /* else, calculate in respect to parent menu */
-               if (config.screenw - (menu->parent->x + menu->parent->w + config.border_pixels + config.gap_pixels) >= width)
-                       menu->x = menu->parent->x + menu->parent->w + config.border_pixels + config.gap_pixels;
+               int parentwidth;
+
+               parentwidth = menu->parent->x + menu->parent->w + config.border_pixels + config.gap_pixels;
+
+               if (mon.x + mon.w - parentwidth >= width)
+                       menu->x = parentwidth;
                else if (menu->parent->x > menu->w + config.border_pixels + config.gap_pixels)
                        menu->x = menu->parent->x - menu->w - config.border_pixels - config.gap_pixels;
 
                else if (menu->parent->x > menu->w + config.border_pixels + config.gap_pixels)
                        menu->x = menu->parent->x - menu->w - config.border_pixels - config.gap_pixels;
 
-               if (config.screenh - (menu->caller->y + menu->parent->y) > height)
+               if (mon.y + mon.h - (menu->caller->y + menu->parent->y) > height)
                        menu->y = menu->caller->y + menu->parent->y;
                        menu->y = menu->caller->y + menu->parent->y;
-               else if (config.screenh - menu->parent->y > height)
+               else if (mon.y + mon.h - menu->parent->y > height)
                        menu->y = menu->parent->y;
                        menu->y = menu->parent->y;
-               else if (config.screenh > height)
-                       menu->y = config.screenh - height;
+               else if (mon.y + mon.h > height)
+                       menu->y = mon.y + mon.h - height;
        }
 }
 
        }
 }
 
@@ -871,8 +938,8 @@ drawitems(struct Menu *menu)
                        if (item->file != NULL && !iflag) {
                                item->icon = loadicon(item->file);
 
                        if (item->file != NULL && !iflag) {
                                item->icon = loadicon(item->file);
 
-                               imlib_context_set_drawable(item->sel);
                                imlib_context_set_image(item->icon);
                                imlib_context_set_image(item->icon);
+                               imlib_context_set_drawable(item->sel);
                                imlib_render_image_on_drawable(config.horzpadding, config.iconpadding);
                                imlib_context_set_drawable(item->unsel);
                                imlib_render_image_on_drawable(config.horzpadding, config.iconpadding);
                                imlib_render_image_on_drawable(config.horzpadding, config.iconpadding);
                                imlib_context_set_drawable(item->unsel);
                                imlib_render_image_on_drawable(config.horzpadding, config.iconpadding);
diff --git a/xmenu.h b/xmenu.h
index 2405a8b..04a4ddf 100644 (file)
--- a/xmenu.h
+++ b/xmenu.h
@@ -36,10 +36,13 @@ struct Config {
        int iconpadding;
        int horzpadding;
 
        int iconpadding;
        int horzpadding;
 
-       /* the values below are computed by xmenu */
+       /* the values below are set by options */
+       int monitor;
+       int posx, posy;         /* rootmenu position */
+
+       /* the value below is computed by xmenu */
        int iconsize;
        int iconsize;
-       int posx, posy;           /* cursor position */
-       int screenw, screenh;       /* screen width and height */
+       int screenw, screenh;   /* screen width and height */
 };
 
 /* draw context structure */
 };
 
 /* draw context structure */
@@ -70,6 +73,11 @@ struct Item {
        Imlib_Image icon;
 };
 
        Imlib_Image icon;
 };
 
+/* monitor and cursor geometry structure */
+struct Monitor {
+       int x, y, w, h;         /* monitor geometry */
+};
+
 /* menu structure */
 struct Menu {
        struct Menu *parent;    /* parent menu */
 /* menu structure */
 struct Menu {
        struct Menu *parent;    /* parent menu */