From 8377ec92a99ce6be438900a91dd936bdbcab621a Mon Sep 17 00:00:00 2001 From: speedie Date: Thu, 16 Mar 2023 10:09:51 +0100 Subject: [PATCH] some cleanup, start using // comments more --- libs/define.c | 23 ++++++++++ libs/draw.c | 38 ++++++++-------- libs/key_struct.c | 7 +++ spmenu.c | 107 ++++++++++++++++++++++------------------------ 4 files changed, 100 insertions(+), 75 deletions(-) create mode 100644 libs/define.c create mode 100644 libs/key_struct.c diff --git a/libs/define.c b/libs/define.c new file mode 100644 index 0000000..879ab4c --- /dev/null +++ b/libs/define.c @@ -0,0 +1,23 @@ +// declare macros +#define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask)) +#define BUTTONMASK (ButtonPressMask|ButtonReleaseMask) +#define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \ + && MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org))) +#define LENGTH(X) (sizeof X / sizeof X[0]) +#define TEXTW(X) (drw_font_getwidth(drw, (X), False) + lrpad) +#define TEXTWM(X) (drw_font_getwidth(drw, (X), True) + lrpad) + +// number +#define NUMBERSMAXDIGITS 100 +#define NUMBERSBUFSIZE (NUMBERSMAXDIGITS * 2) + 1 + +/* user friendly names for all the modifiers */ +#define CONTROL ControlMask +#define SHIFT ShiftMask +#define ALT Mod1Mask +#define ALTR Mod3Mask +#define SUPER Mod4Mask +#define SUPERR Mod5Mask + +/* alpha */ +#define opaque 0xffU diff --git a/libs/draw.c b/libs/draw.c index 7d66b32..4e88af1 100644 --- a/libs/draw.c +++ b/libs/draw.c @@ -1,5 +1,5 @@ void -drawhighlights(struct item *item, int x, int y, int maxw) +drawhighlights(struct item *item, int x, int y, int w) { char restorechar, tokens[sizeof text], *highlight, *token; int indentx, highlightlen; @@ -10,6 +10,7 @@ drawhighlights(struct item *item, int x, int y, int maxw) for (token = strtok(tokens, " "); token; token = strtok(NULL, " ")) { highlight = fstrstr(itemtext, token); + while (highlight) { highlightlen = highlight - itemtext; restorechar = *highlight; @@ -20,8 +21,8 @@ drawhighlights(struct item *item, int x, int y, int maxw) restorechar = highlight[strlen(token)]; highlight[strlen(token)] = '\0'; - if (indentx - (lrpad / 2) - 1 < maxw) - drw_text(drw, x + indentx - (lrpad / 2) - 1, y, MIN(maxw - indentx, TEXTW(highlight) - lrpad), bh, 0, highlight, 0, pango_highlight ? True : False); + if (indentx - (lrpad / 2) - 1 < w) + drw_text(drw, x + indentx - (lrpad / 2) - 1, y, MIN(w - indentx, TEXTW(highlight) - lrpad), bh, 0, highlight, 0, pango_highlight ? True : False); highlight[strlen(token)] = restorechar; @@ -60,25 +61,24 @@ drawitem(struct item *item, int x, int y, int w) memcpy(scm, scheme[SchemeItemNormPri], sizeof(scm)); } - /* set scheme */ - drw_setscheme(drw, scm); + drw_setscheme(drw, scm); // set scheme /* parse item text */ for (wr = 0, rd = 0; item->text[rd]; rd++) { if (item->text[rd] == '' && item->text[rd + 1] == '[') { size_t alen = strspn(item->text + rd + 2, "0123456789;"); - if (item->text[rd + alen + 2] == 'm') { /* character is 'm' which is the last character in the sequence */ - buffer[wr] = '\0'; /* clear out character */ + if (item->text[rd + alen + 2] == 'm') { // last character in sequence is always 'm' + buffer[wr] = '\0'; apply_fribidi(buffer); rw = MIN(w, TEXTW(buffer) - lrpad); drw_text(drw, x, y, rw + lp, bh, lp, isrtl ? fribidi_text : buffer, 0, pango_item ? True : False); x += rw + lp; - orw += rw; + orw += rw; // width of all colored text, we add this to the full width later ib = 1; - lp = 0; /* no padding */ + lp = 0; char *ep = item->text + rd + 1; @@ -123,7 +123,7 @@ drawitem(struct item *item, int x, int y, int w) rd += alen + 2; wr = 0; - drw_setscheme(drw, scm); + drw_setscheme(drw, scm); // set scheme continue; } @@ -136,11 +136,11 @@ drawitem(struct item *item, int x, int y, int w) w -= orw; - /* draw any text that doesn't use sgr sequences */ + // now draw any non-colored text apply_fribidi(buffer); int r = drw_text(drw, x, y, w, bh, lp, isrtl ? fribidi_text : buffer, 0, pango_item ? True : False); + if (!hidehighlight && !ib) drawhighlights(item, x, y, w); - if (!hidehighlight && !ib) drawhighlights(item, x, y, w - rw); return r; } @@ -200,7 +200,6 @@ drawmenu(void) drw_rect(drw, x + curpos, 2 + (bh - fh) / 2, 2, fh - 4, 1, 0); } - /* get match count */ if (!hidematchcount) { recalculatenumbers(); numberWidth = TEXTW(numbers); @@ -244,22 +243,22 @@ drawmenu(void) ); } + // horizontal list } else if (matches) { - /* draw horizontal list */ x += inputw; w = larrowWidth; - if (curr->left && !hidelarrow) { + if (curr->left && !hidelarrow) { // draw left arrow drw_setscheme(drw, scheme[SchemeLArrow]); drw_text(drw, x, 0, w, bh, lrpad / 2, leftarrow, 0, pango_leftarrow ? True : False); } x += w; - for (item = curr; item != next; item = item->right) + for (item = curr; item != next; item = item->right) // draw items x = drawitem(item, x, 0, MIN(pango_item ? TEXTWM(item->text) : TEXTW(item->text), mw - x - rarrowWidth - numberWidth - modeWidth)); - if (next && !hiderarrow) { + if (next && !hiderarrow) { // draw right arrow w = rarrowWidth; drw_setscheme(drw, scheme[SchemeRArrow]); @@ -267,15 +266,16 @@ drawmenu(void) } } - if (!hidematchcount) { + if (!hidematchcount) { // draw match count drw_setscheme(drw, scheme[SchemeNumber]); drw_text(drw, mw - numberWidth - modeWidth, 0, numberWidth, bh, lrpad / 2, numbers, 0, pango_numbers ? True : False); } - if (!hidemode) { + if (!hidemode) { // draw mode indicator drw_setscheme(drw, scheme[SchemeMode]); drw_text(drw, mw - modeWidth, 0, modeWidth, bh, lrpad / 2, modetext, 0, pango_mode ? True : False); } + // map the drawing drw_map(drw, win, 0, 0, mw, mh); } diff --git a/libs/key_struct.c b/libs/key_struct.c new file mode 100644 index 0000000..dbd2bda --- /dev/null +++ b/libs/key_struct.c @@ -0,0 +1,7 @@ +typedef struct { + unsigned int mode; + unsigned int mod; + KeySym keysym; + void (*func)(const Arg *); + const Arg arg; +} Key; diff --git a/spmenu.c b/spmenu.c index 1a18135..98bcee4 100644 --- a/spmenu.c +++ b/spmenu.c @@ -10,7 +10,8 @@ * After making changes, run 'make clean install' to install and 'make' to attempt a compilation. * `make man` will generate a man page from 'docs/docs.md', which is a Markdown document. Run this before commiting. * - * See LICENSE file for copyright and license details. */ + * See LICENSE file for copyright and license details. + */ #include #include #include @@ -21,52 +22,55 @@ #include #include -/* check if we should enable right to left language support */ +// check if we should enable right to left language support #ifndef RTL #define USERTL 0 #else #define USERTL 1 #endif -/* check if we should enable pango support */ +// check if we should enable pango or use xft #ifndef PANGO #define USEPANGO 0 #else #define USEPANGO 1 #endif -/* check if we should enable image support */ +// check if we should enable image support #ifndef IMAGE #define USEIMAGE 0 #else #define USEIMAGE 1 #endif -/* check if we should enable multimonitor support using xinerama */ +// check if we should enable multimonitor support using libXinerama #ifdef XINERAMA #define USEXINERAMA 1 #else #define USEXINERAMA 0 #endif -/* include right to left language library */ +// include fribidi used for right to left language support #if USERTL #include #endif -/* include libraries used for image support */ +// include libraries used for image support #if USEIMAGE #include #include #include #include +// openssl is used to generate a checksum, used for caching +// TODO: remove this dependency by doing it some other way #endif -/* include xinerama */ +// include xinerama used for multi monitor support #if USEXINERAMA #include #endif +// include X11 headers #include #include #include @@ -74,38 +78,17 @@ #include #include +// include pango used for markup #if USEPANGO #include #endif +// include macros and other defines +#include "libs/define.c" + +// various headers #include "libs/sl/draw.h" #include "libs/sl/main.h" - -/* macros */ -#define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask)) -#define BUTTONMASK (ButtonPressMask|ButtonReleaseMask) -#define INTERSECT(x,y,w,h,r) (MAX(0, MIN((x)+(w),(r).x_org+(r).width) - MAX((x),(r).x_org)) \ - && MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org))) -#define LENGTH(X) (sizeof X / sizeof X[0]) -#define TEXTW(X) (drw_font_getwidth(drw, (X), False) + lrpad) -#define TEXTWM(X) (drw_font_getwidth(drw, (X), True) + lrpad) - -/* number */ -#define NUMBERSMAXDIGITS 100 -#define NUMBERSBUFSIZE (NUMBERSMAXDIGITS * 2) + 1 - -/* user friendly names for all the modifiers */ -#define CONTROL ControlMask -#define SHIFT ShiftMask -#define ALT Mod1Mask -#define ALTR Mod3Mask -#define SUPER Mod4Mask -#define SUPERR Mod5Mask - -/* alpha */ -#define opaque 0xffU - -/* include headers */ #include "libs/schemes.h" #include "libs/arg.h" #include "libs/mode.h" @@ -114,30 +97,24 @@ #include "libs/mouse.h" #include "libs/sort.h" +// misc +#include "libs/key_struct.c" + +// text static char text[BUFSIZ] = ""; - -struct item { - char *text; - char *image; - char *ex; - struct item *left, *right; - int hp; - double distance; -}; - -typedef struct { - unsigned int mode; - unsigned int mod; - KeySym keysym; - void (*func)(const Arg *); - const Arg arg; -} Key; - static char numbers[NUMBERSBUFSIZE] = ""; + +// high priority static int hplength = 0; static char **hpitems = NULL; + +// embed static char *embed; + +// keybinds static int numlockmask = 0; + +// height of each item, menu width, menu height static int bh, mw, mh; static int reallines = 0; static int reqlineheight; /* required menu height */ @@ -155,16 +132,28 @@ static struct item *matches, *matchend; static struct item *prev, *curr, *next, *sel; static int screen; -/* image globals */ +// item struct +struct item { + char *text; + #if USEIMAGE + char *image; + #endif + char *ex; + struct item *left, *right; + int hp; + double distance; +}; + +// image globals #if USEIMAGE static int flip = 0; static int rotation = 0; static int needredraw = 1; -#endif static int longestedge = 0; static int imagew = 0; static int imageh = 0; static int imageg = 0; +#endif /* set an integer if to 1 if we have right to left language support enabled * doing it this way, because we can reduce the amount of #if and #else lines used. @@ -175,16 +164,18 @@ static int isrtl = 1; static int isrtl = 0; #endif +// X11 properties static Atom clip, utf8, types, dock; static Display *dpy; static Window root, parentwin, win; static XIC xic; static int useargb = 0; + +// colors static Visual *visual; static int depth; static Colormap cmap; - static Drw *drw; static Clr *scheme[SchemeLast]; static Clr textclrs[256]; @@ -811,7 +802,9 @@ main(int argc, char *argv[]) readargs(argc, argv); + #if USEIMAGE longestedge = MAX(imagewidth, imageheight); + #endif if (mode) { curMode = 1; @@ -864,12 +857,14 @@ main(int argc, char *argv[]) grabkeyboard(); } - /* set values which we can restore later */ + // set default values + #if USEIMAGE if (!imagew || !imageh || !imageg) { imagew = imagewidth; imageh = imageheight; imagegaps = imagegaps; } + #endif setup(); eventloop();