X-Git-Url: https://git.subgeniuskitty.com/xmenu/.git/blobdiff_plain/f8d55995dffb94166a38f510ea47bb0218e7bfbf..d3c213125090d49408c78d2c4a9200ec1575c187:/xmenu.c diff --git a/xmenu.c b/xmenu.c index 6e189cc..d557f6d 100644 --- a/xmenu.c +++ b/xmenu.c @@ -96,7 +96,6 @@ static Atom wmdelete; static Atom netatom[NetLast]; /* flags */ -static int fflag = 0; /* whether glyphs should align based on the first font */ static int iflag = 0; /* whether to disable icons */ static int mflag = 0; /* whether the user specified a monitor with -p */ static int pflag = 0; /* whether the user specified a position with -p */ @@ -118,11 +117,8 @@ main(int argc, char *argv[]) XClassHint classh; int ch; - while ((ch = getopt(argc, argv, "fip:w")) != -1) { + while ((ch = getopt(argc, argv, "ip:w")) != -1) { switch (ch) { - case 'f': - fflag = 1; - break; case 'i': iflag = 1; break; @@ -146,7 +142,7 @@ main(int argc, char *argv[]) /* open connection to server and set X variables */ if ((dpy = XOpenDisplay(NULL)) == NULL) - errx(1, "cannot open display"); + errx(1, "could not open display"); screen = DefaultScreen(dpy); visual = DefaultVisual(dpy, screen); rootwin = RootWindow(dpy, screen); @@ -260,14 +256,18 @@ parsefonts(const char *s) i = 0; while (isspace(*p)) p++; - while (*p != '\0' && *p != ',') { + while (i < sizeof buf && *p != '\0' && *p != ',') buf[i++] = *p++; - } + if (i >= sizeof buf) + errx(1, "font name too long"); if (*p == ',') p++; buf[i] = '\0'; + if (nfont == 0) + if ((dc.pattern = FcNameParse((FcChar8 *)buf)) == NULL) + errx(1, "the first font in the cache must be loaded from a font string"); if ((dc.fonts[nfont++] = XftFontOpenName(dpy, screen, buf)) == NULL) - errx(1, "cannot load font"); + errx(1, "could not load font"); } } @@ -276,7 +276,7 @@ static void ealloccolor(const char *s, XftColor *color) { if(!XftColorAllocName(dpy, visual, colormap, s, color)) - errx(1, "cannot allocate color: %s", s); + errx(1, "could not allocate color: %s", s); } /* query monitor information and cursor position */ @@ -300,7 +300,7 @@ initmonitor(void) if ((info = XineramaQueryScreens(dpy, &nmons)) != NULL) { int selmon = 0; - if (!mflag || (mflag && (config.monitor < 0 || config.monitor >= nmons))) { + if (!mflag || config.monitor < 0 || config.monitor >= nmons) { for (i = 0; i < nmons; i++) { if (BETWEEN(cursx, info[i].x_org, info[i].x_org + info[i].width) && BETWEEN(cursy, info[i].y_org, info[i].y_org + info[i].height)) { @@ -316,6 +316,8 @@ initmonitor(void) mon.y = info[selmon].y_org; mon.w = info[selmon].width; mon.h = info[selmon].height; + + XFree(info); } if (!pflag) { @@ -331,9 +333,9 @@ initmonitor(void) static void initresources(void) { - char *xrm; long n; char *type; + char *xrm; XrmDatabase xdb; XrmValue xval; @@ -514,7 +516,7 @@ buildmenutree(unsigned level, const char *label, const char *output, char *file) menu = menu->parent, i++) ; if (menu == NULL) - errx(1, "reached NULL menu"); + errx(1, "improper indentation detected"); /* find last item in the new menu */ for (item = menu->list; item->next != NULL; item = item->next) @@ -643,12 +645,49 @@ getnextutf8char(const char *s, const char **next_ret) static XftFont * getfontucode(FcChar32 ucode) { + FcCharSet *fccharset = NULL; + FcPattern *fcpattern = NULL; + FcPattern *match = NULL; + XftFont *retfont = NULL; + XftResult result; size_t i; for (i = 0; i < dc.nfonts; i++) if (XftCharExists(dpy, dc.fonts[i], ucode) == FcTrue) return dc.fonts[i]; - return NULL; + + /* create a charset containing our code point */ + fccharset = FcCharSetCreate(); + FcCharSetAddChar(fccharset, ucode); + + /* create a pattern akin to the dc.pattern but containing our charset */ + if (fccharset) { + fcpattern = FcPatternDuplicate(dc.pattern); + FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset); + } + + /* find pattern matching fcpattern */ + if (fcpattern) { + FcConfigSubstitute(NULL, fcpattern, FcMatchPattern); + FcDefaultSubstitute(fcpattern); + match = XftFontMatch(dpy, screen, fcpattern, &result); + } + + /* if found a pattern, open its font */ + if (match) { + retfont = XftFontOpenPattern(dpy, match); + if (retfont && XftCharExists(dpy, retfont, ucode) == FcTrue) { + if ((dc.fonts = realloc(dc.fonts, dc.nfonts+1)) == NULL) + err(1, "realloc"); + dc.fonts[dc.nfonts] = retfont; + return dc.fonts[dc.nfonts++]; + } else { + XftFontClose(dpy, retfont); + } + } + + /* in case no fount was found, return the first one */ + return dc.fonts[0]; } /* draw text into XftDraw, return width of text glyphs */ @@ -656,9 +695,6 @@ static int drawtext(XftDraw *draw, XftColor *color, int x, int y, unsigned h, const char *text) { int textwidth = 0; - int texty; - - texty = y + (h - (dc.fonts[0]->ascent + dc.fonts[0]->descent))/2 + dc.fonts[0]->ascent; while (*text) { XftFont *currfont; @@ -668,17 +704,16 @@ drawtext(XftDraw *draw, XftColor *color, int x, int y, unsigned h, const char *t size_t len; ucode = getnextutf8char(text, &next); - if ((currfont = getfontucode(ucode)) == NULL) - currfont = dc.fonts[0]; + currfont = getfontucode(ucode); len = next - text; - XftTextExtentsUtf8(dpy, currfont, (XftChar8 *)text, len, &ext); textwidth += ext.xOff; if (draw) { - if (!fflag) - texty = y + (h - (currfont->ascent + currfont->descent))/2 + currfont->ascent; + int texty; + + texty = y + (h - (currfont->ascent + currfont->descent))/2 + currfont->ascent; XftDrawStringUtf8(draw, color, currfont, x, texty, (XftChar8 *)text, len); x += ext.xOff; } @@ -831,7 +866,7 @@ grabpointer(void) return; nanosleep(&ts, NULL); } - errx(1, "cannot grab keyboard"); + errx(1, "could not grab pointer"); } /* try to grab keyboard, we may have to wait for another process to ungrab */ @@ -847,7 +882,7 @@ grabkeyboard(void) return; nanosleep(&ts, NULL); } - errx(1, "cannot grab keyboard"); + errx(1, "could not grab keyboard"); } /* load and scale icon */ @@ -855,13 +890,55 @@ static Imlib_Image loadicon(const char *file) { Imlib_Image icon; + Imlib_Load_Error errcode; + const char *errstr; int width; int height; int imgsize; - icon = imlib_load_image(file); - if (icon == NULL) - errx(1, "cannot load icon %s", file); + icon = imlib_load_image_with_error_return(file, &errcode); + if (*file == '\0') { + warnx("could not load icon (file name is blank)"); + return NULL; + } else if (icon == NULL) { + switch (errcode) { + case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST: + errstr = "file does not exist"; + break; + case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY: + errstr = "file is directory"; + break; + case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ: + case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE: + errstr = "permission denied"; + break; + case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT: + errstr = "unknown file format"; + break; + case IMLIB_LOAD_ERROR_PATH_TOO_LONG: + errstr = "path too long"; + break; + case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT: + case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY: + case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE: + errstr = "improper path"; + break; + case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS: + errstr = "too many symbolic links"; + break; + case IMLIB_LOAD_ERROR_OUT_OF_MEMORY: + errstr = "out of memory"; + break; + case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS: + errstr = "out of file descriptors"; + break; + default: + errstr = "unknown error"; + break; + } + warnx("could not load icon (%s): %s", errstr, file); + return NULL; + } imlib_context_set_image(icon); @@ -938,15 +1015,21 @@ drawitems(struct Menu *menu) Convex, CoordModeOrigin); } - /* draw icon */ - if (item->file != NULL && !iflag) { + /* try to load icon */ + if (item->file && !iflag) { item->icon = loadicon(item->file); + free(item->file); + } + /* draw icon if properly loaded */ + if (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_context_set_image(item->icon); + imlib_free_image(); } } } @@ -1238,13 +1321,6 @@ cleanmenu(struct Menu *menu) if (tmp->label != tmp->output) free(tmp->label); free(tmp->output); - if (tmp->file != NULL) { - free(tmp->file); - if (tmp->icon != NULL) { - imlib_context_set_image(tmp->icon); - imlib_free_image(); - } - } item = item->next; free(tmp); } @@ -1280,6 +1356,6 @@ cleanup(void) static void usage(void) { - (void)fprintf(stderr, "usage: xmenu [-fiw] [-p position] [title]\n"); + (void)fprintf(stderr, "usage: xmenu [-iw] [-p position] [title]\n"); exit(1); }