Fixing #7
[xmenu] / xmenu.c
diff --git a/xmenu.c b/xmenu.c
index af0f801..56a3dd4 100644 (file)
--- a/xmenu.c
+++ b/xmenu.c
@@ -1,7 +1,9 @@
 #include <err.h>
 #include <err.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <limits.h>
 #include <time.h>
 #include <unistd.h>
 #include <X11/Xlib.h>
 #include <time.h>
 #include <unistd.h>
 #include <X11/Xlib.h>
@@ -17,6 +19,9 @@
  * Function declarations
  */
 
  * Function declarations
  */
 
+/* argument parser */
+static void parseposition(const char *optarg);
+
 /* initializers, and their helper routines */
 static void ealloccolor(const char *s, XftColor *color);
 static void initresources(void);
 /* initializers, and their helper routines */
 static void ealloccolor(const char *s, XftColor *color);
 static void initresources(void);
@@ -78,8 +83,9 @@ static Atom wmdelete;
 static Atom netatom[NetLast];
 
 /* flags */
 static Atom netatom[NetLast];
 
 /* flags */
-static int wflag = 0;   /* whether to let the window manager control XMenu */
 static int iflag = 0;   /* whether to disable icons */
 static int iflag = 0;   /* whether to disable icons */
+static int pflag = 0;   /* whether the user specified a position */
+static int wflag = 0;   /* whether to let the window manager control XMenu */
 
 /* include config variable */
 #include "config.h"
 
 /* include config variable */
 #include "config.h"
@@ -97,11 +103,15 @@ main(int argc, char *argv[])
        XClassHint classh;
        int ch;
 
        XClassHint classh;
        int ch;
 
-       while ((ch = getopt(argc, argv, "iw")) != -1) {
+       while ((ch = getopt(argc, argv, "ip:w")) != -1) {
                switch (ch) {
                case 'i':
                        iflag = 1;
                        break;
                switch (ch) {
                case 'i':
                        iflag = 1;
                        break;
+               case 'p':
+                       pflag = 1;
+                       parseposition(optarg);
+                       break;
                case 'w':
                        wflag = 1;
                        break;
                case 'w':
                        wflag = 1;
                        break;
@@ -168,6 +178,30 @@ main(int argc, char *argv[])
        return 0;
 }
 
        return 0;
 }
 
+/* parse position string from -p, put results on config.x and config.y */
+static void
+parseposition(const char *optarg)
+{
+       long n;
+       const char *s = optarg;
+       char *endp;
+
+       n = strtol(s, &endp, 10);
+       if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s || *endp != 'x')
+               goto error;
+       config.posx = n;
+       s = endp+1;
+       n = strtol(s, &endp, 10);
+       if (errno == ERANGE || n > INT_MAX || n < 0 || endp == s || *endp != '\0')
+               goto error;
+       config.posy = n;
+
+       return;
+
+error:
+       errx(1, "improper position: %s", optarg);
+}
+
 /* get color from color string */
 static void
 ealloccolor(const char *s, XftColor *color)
 /* get color from color string */
 static void
 ealloccolor(const char *s, XftColor *color)
@@ -253,7 +287,8 @@ initconfig(void)
        int di;      /* dummy variable */
        unsigned du; /* dummy variable */
 
        int di;      /* dummy variable */
        unsigned du; /* dummy variable */
 
-       XQueryPointer(dpy, rootwin, &dw, &dw, &config.cursx, &config.cursy, &di, &di, &du);
+       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;
@@ -511,8 +546,29 @@ setupitems(struct Menu *menu)
                menu->w = MAX(menu->w, itemwidth);
 
                /* create icon */
                menu->w = MAX(menu->w, itemwidth);
 
                /* create icon */
-               if (item->file != NULL && !iflag)
+               if (item->file != NULL && !iflag) {
                        item->icon = loadicon(item->file);
                        item->icon = loadicon(item->file);
+
+                       item->sel = XCreatePixmap(dpy, menu->win,
+                                                 config.iconsize, config.iconsize,
+                                                 DefaultDepth(dpy, screen));
+                       XSetForeground(dpy, dc.gc, dc.selected[ColorBG].pixel);
+                       XFillRectangle(dpy, item->sel, dc.gc, 0, 0,
+                                      config.iconsize, config.iconsize);
+                       imlib_context_set_drawable(item->sel);
+                       imlib_context_set_image(item->icon);
+                       imlib_render_image_on_drawable(0, 0);
+
+                       item->unsel = XCreatePixmap(dpy, menu->win,
+                                                   config.iconsize, config.iconsize,
+                                                   DefaultDepth(dpy, screen));
+                       XSetForeground(dpy, dc.gc, dc.normal[ColorBG].pixel);
+                       XFillRectangle(dpy, item->unsel, dc.gc, 0, 0,
+                                      config.iconsize, config.iconsize);
+                       imlib_context_set_drawable(item->unsel);
+                       imlib_context_set_image(item->icon);
+                       imlib_render_image_on_drawable(0, 0);
+               }
        }
 }
 
        }
 }
 
@@ -525,13 +581,13 @@ 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 (config.screenw - config.cursx >= menu->w)
-                       menu->x = config.cursx;
-               else if (config.cursx > width)
-                       menu->x = config.cursx - width;
+               if (pflag || config.screenw - config.posx >= menu->w)
+                       menu->x = config.posx;
+               else if (config.posx > width)
+                       menu->x = config.posx - width;
 
 
-               if (config.screenh - config.cursy >= height)
-                       menu->y = config.cursy;
+               if (pflag || config.screenh - config.posy >= height)
+                       menu->y = config.posy;
                else if (config.screenh > height)
                        menu->y = config.screenh - height;
        } else {                    /* else, calculate in respect to parent menu */
                else if (config.screenh > height)
                        menu->y = config.screenh - height;
        } else {                    /* else, calculate in respect to parent menu */
@@ -721,7 +777,7 @@ drawitem(struct Menu *menu, struct Item *item, XftColor *color)
        y = item->y + (item->h + dc.font->ascent) / 2;
        XSetForeground(dpy, dc.gc, color[ColorFG].pixel);
        XftDrawStringUtf8(menu->draw, &color[ColorFG], dc.font,
        y = item->y + (item->h + dc.font->ascent) / 2;
        XSetForeground(dpy, dc.gc, color[ColorFG].pixel);
        XftDrawStringUtf8(menu->draw, &color[ColorFG], dc.font,
-                      x, y, (XftChar8 *)item->label, item->labellen);
+                         x, y, (XftChar8 *)item->label, item->labellen);
 
        /* draw triangle, if item contains a submenu */
        if (item->submenu != NULL) {
 
        /* draw triangle, if item contains a submenu */
        if (item->submenu != NULL) {
@@ -743,9 +799,12 @@ drawitem(struct Menu *menu, struct Item *item, XftColor *color)
        if (item->icon != NULL) {
                x = config.horzpadding;
                y = item->y + config.iconpadding;
        if (item->icon != NULL) {
                x = config.horzpadding;
                y = item->y + config.iconpadding;
-               imlib_context_set_drawable(menu->pixmap);
-               imlib_context_set_image(item->icon);
-               imlib_render_image_on_drawable(x, y);
+               if (color == dc.selected)
+                       XCopyArea(dpy, item->sel, menu->pixmap, dc.gc, 0, 0,
+                                 menu->w, menu->h, x, y);
+               else
+                       XCopyArea(dpy, item->unsel, menu->pixmap, dc.gc, 0, 0,
+                                 menu->w, menu->h, x, y);
        }
 }
 
        }
 }
 
@@ -1020,6 +1079,6 @@ cleanup(void)
 static void
 usage(void)
 {
 static void
 usage(void)
 {
-       (void)fprintf(stderr, "usage: xmenu [-iw] [title]\n");
+       (void)fprintf(stderr, "usage: xmenu [-iw] [-p position] [title]\n");
        exit(1);
 }
        exit(1);
 }