From eca13f2896a2bd2a937026ec6fba65124c277eef Mon Sep 17 00:00:00 2001 From: speedie Date: Fri, 21 Apr 2023 09:49:38 +0200 Subject: [PATCH] fix lots of issues/bad code --- libs/arg.c | 21 +++++++++++++++++++++ libs/arg.h | 1 + libs/client.c | 2 +- libs/define.c | 4 ++++ libs/draw.c | 23 +++++++++++++++-------- libs/img.c | 8 +++++--- libs/key.c | 1 + libs/match.c | 2 -- libs/mouse.c | 16 +++++++++++----- mouse.h | 1 + spmenu.c | 7 ++++++- 11 files changed, 66 insertions(+), 20 deletions(-) diff --git a/libs/arg.c b/libs/arg.c index e0967ba..84e9072 100644 --- a/libs/arg.c +++ b/libs/arg.c @@ -337,6 +337,7 @@ clearins(const Arg *arg) if (hidemode) strcpy(modetext, ""); + calcoffsets(); drawmenu(); } @@ -511,3 +512,23 @@ setprofile(const Arg *arg) // this just runs an external shell script to set the profile exit(system("command -v spmenu_profile > /dev/null && spmenu_profile --spmenu-set-profile > /dev/null")); } + +void +spawn(const Arg *arg) +{ + struct sigaction sa; + + if (fork() == 0) { + if (dpy) + close(ConnectionNumber(dpy)); + + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sa.sa_handler = SIG_DFL; + sigaction(SIGCHLD, &sa, NULL); + + setsid(); + execvp(((char **)arg->v)[1], ((char **)arg->v)+1); + fprintf(stderr, "spmenu: failed to execute command '%s'", ((char **)arg->v)[0]); + } +} diff --git a/libs/arg.h b/libs/arg.h index e7572a7..3b425ea 100644 --- a/libs/arg.h +++ b/libs/arg.h @@ -38,3 +38,4 @@ static void setimggaps(const Arg *arg); static void setlines(const Arg *arg); static void setcolumns(const Arg *arg); static void setprofile(const Arg *arg); +static void spawn(const Arg *arg); diff --git a/libs/client.c b/libs/client.c index cc7ca64..964d6ba 100644 --- a/libs/client.c +++ b/libs/client.c @@ -144,6 +144,6 @@ resizeclient(void) // no window/invalid window or menu height we had before is the same as the current window height if (!win || omh == mh) return; - XMoveResizeWindow(dpy, win, x, y, mw - 2 * sp - 2 * borderwidth, mh); + XMoveResizeWindow(dpy, win, x, y, mw - 2 * sp - borderwidth, mh); drw_resize(drw, mw - 2 * sp - borderwidth * 2, mh); } diff --git a/libs/define.c b/libs/define.c index f2ea003..388162b 100644 --- a/libs/define.c +++ b/libs/define.c @@ -21,3 +21,7 @@ #define ALTR Mod3Mask #define SUPER Mod4Mask #define SUPERR Mod5Mask + +// for running stuff +#define shell "/bin/sh" +#define cmd( cmd ) {.v = (const char*[]){ shell, "-c", cmd, NULL } }, diff --git a/libs/draw.c b/libs/draw.c index f4b0ce3..98694e3 100644 --- a/libs/draw.c +++ b/libs/draw.c @@ -191,12 +191,8 @@ drawitem(int x, int y, int w) if (!hidematchcount) numberWidth = pango_numbers ? TEXTWM(numbers) : TEXTW(numbers); if (!hidecaps) capsWidth = pango_caps ? TEXTWM(capstext) : TEXTW(capstext); - // mode indicator is always going to be at the right - if (hidemode) { - numberWidth += 2 * sp + borderwidth; - } else { - modeWidth += 2 * sp + borderwidth; - } + if (!strcmp(capstext, "")) + capsWidth = 0; #if USEIMAGE int ox = 0; // original x position @@ -237,9 +233,18 @@ drawitem(int x, int y, int w) x = drawlarrow(x, y, w); for (item = curr; item != next; item = item->right) // draw items - x = drawitemtext(item, x, y, MIN(pango_item ? TEXTWM(item->text) : TEXTW(item->text), mw - x - rarrowWidth - numberWidth - modeWidth - capsWidth - menumarginh)); + x = drawitemtext(item, x, y, MIN(pango_item ? TEXTWM(item->text) : TEXTW(item->text), + mw - x - + rarrowWidth - + numberWidth - + modeWidth - + capsWidth - + menumarginh - + 2 * sp - + 2 * borderwidth + )); - w = rarrowWidth + numberWidth + modeWidth + menumarginh; + w = rarrowWidth + numberWidth + modeWidth + capsWidth + menumarginh + 2 * sp + 2 * borderwidth; x = drawrarrow(mw - w, y, w); } @@ -437,6 +442,8 @@ drawmenu(void) x += menumarginh; y += menumarginv; + calcoffsets(); + // why have an empty line? if ((hideprompt && hideinput && hidemode && hidematchcount #if USEIMAGE diff --git a/libs/img.c b/libs/img.c index 519e9e9..ae8c0a8 100644 --- a/libs/img.c +++ b/libs/img.c @@ -25,6 +25,7 @@ setimagesize(int width, int height) return; } + calcoffsets(); drawmenu(); } @@ -429,9 +430,9 @@ resizetoimageheight(int imageheight) return; } - //XResizeWindow(dpy, win, mw - 2 * sp - 2 * borderwidth, mh); - XMoveResizeWindow(dpy, win, x, y, mw - 2 * sp - 2 * borderwidth, mh); - drw_resize(drw, mw - 2 * sp - 2 * borderwidth, mh); + //XResizeWindow(dpy, win, mw - 2 * sp - borderwidth, mh); + XMoveResizeWindow(dpy, win, x, y, mw - 2 * sp - borderwidth, mh); + drw_resize(drw, mw - 2 * sp - borderwidth, mh); if (olines != lines) { struct item *item; @@ -444,6 +445,7 @@ resizetoimageheight(int imageheight) jumptoindex(i); } + calcoffsets(); drawmenu(); } #endif diff --git a/libs/key.c b/libs/key.c index 9c958dd..5bcaa96 100644 --- a/libs/key.c +++ b/libs/key.c @@ -52,6 +52,7 @@ keypress(XEvent *e) allowkeys = !allowkeys; } + calcoffsets(); drawmenu(); } } diff --git a/libs/match.c b/libs/match.c index 93c28cc..d5673df 100644 --- a/libs/match.c +++ b/libs/match.c @@ -81,8 +81,6 @@ fuzzymatch(void) calcoffsets(); } } - - calcoffsets(); } void diff --git a/libs/mouse.c b/libs/mouse.c index 4e10cdc..6937f4f 100644 --- a/libs/mouse.c +++ b/libs/mouse.c @@ -34,18 +34,23 @@ motionevent(XButtonEvent *ev) int larrowWidth = 0; int rarrowWidth = 0; + int numberWidth = 0; + int modeWidth = 0; + int capsWidth = 0; if (!hidelarrow) larrowWidth = pango_leftarrow ? TEXTWM(leftarrow) : TEXTW(leftarrow); if (!hiderarrow) rarrowWidth = pango_rightarrow ? TEXTWM(rightarrow) : TEXTW(rightarrow); + if (!hidematchcount) numberWidth = pango_numbers ? TEXTWM(numbers) : TEXTW(numbers); + if (!hidemode) modeWidth = pango_mode ? TEXTWM(modetext) : TEXTW(modetext); + if (!hidecaps) capsWidth = pango_caps ? TEXTWM(capstext) : TEXTW(capstext); xy = lines > 0 ? bh : inputw + promptw + larrowWidth; xy += menumarginv; ev_xy = lines > 0 ? ev->y : ev->x; for (item = curr; item && item != next; item = item->right) { - int wh = lines > 0 ? bh : textw_clamp(item->text, mw - xy - rarrowWidth - menumarginh); + int wh = lines > 0 ? bh : textw_clamp(item->text, mw - xy - rarrowWidth - menumarginh - 2 * sp - 2 * borderwidth - numberWidth - modeWidth - capsWidth); if (ev_xy >= xy && ev_xy < (xy + wh)) { sel = item; - calcoffsets(); drawmenu(); break; } @@ -66,6 +71,7 @@ buttonpress(XEvent *e) x = xpad = plw; } + // margin x += menumarginh; int larrowWidth = 0; @@ -93,11 +99,11 @@ buttonpress(XEvent *e) // check if we clicked on the prompt or the input if (ev->x < x + promptw + powerlineprompt ? plw : 0) { click = clickprompt; - } else if ((ev->x > mw - capsWidth - 2 * sp - 2 * borderwidth) && !hidecaps) { + } else if ((ev->x > mw - capsWidth - 2 * sp - 2 * borderwidth - menumarginh) && !hidecaps && capsWidth) { click = clickcaps; - } else if (ev->x > mw - modeWidth - capsWidth - 2 * sp - 2 * borderwidth) { + } else if (ev->x > mw - modeWidth - capsWidth - 2 * sp - 2 * borderwidth - menumarginh) { click = clickmode; - } else if (ev->x > mw - modeWidth - numberWidth - capsWidth - 2 * sp - 2 * borderwidth) { + } else if (ev->x > mw - modeWidth - numberWidth - capsWidth - 2 * sp - 2 * borderwidth - menumarginh) { click = clicknumber; } else { // actually we clicked on the input w = (lines > 0 || !matches) ? mw - x : inputw; diff --git a/mouse.h b/mouse.h index d0caeed..69e1776 100644 --- a/mouse.h +++ b/mouse.h @@ -28,5 +28,6 @@ static Mouse buttons[] = { { clickprompt, 0, Button1, clear, {0} }, { clickmode, 0, Button1, switchmode, {0} }, { clicknumber, 0, Button1, viewhist, {0} }, + { clickcaps, 0, Button1, spawn, cmd("notify-send cuck") }, { clickselitem, 0, Button1, NULL, {0} }, }; diff --git a/spmenu.c b/spmenu.c index c6e76e8..34e547d 100644 --- a/spmenu.c +++ b/spmenu.c @@ -293,13 +293,18 @@ calcoffsets(void) int modeWidth = 0; int larrowWidth = 0; int rarrowWidth = 0; + int capsWidth = 0; if (!hidematchcount) numberWidth = pango_numbers ? TEXTWM(numbers) : TEXTW(numbers); if (!hidemode) modeWidth = pango_mode ? TEXTWM(modetext) : TEXTW(modetext); if (!hidelarrow) larrowWidth = pango_leftarrow ? TEXTWM(leftarrow) : TEXTW(leftarrow); if (!hiderarrow) rarrowWidth = pango_rightarrow ? TEXTWM(rightarrow) : TEXTW(rightarrow); + if (!hidecaps) capsWidth = pango_caps ? TEXTWM(capstext) : TEXTW(capstext); - n = mw - (promptw + inputw + larrowWidth + rarrowWidth + modeWidth + numberWidth); + if (!strcmp(capstext, "")) + capsWidth = 0; + + n = mw - (promptw + inputw + larrowWidth + rarrowWidth + modeWidth + numberWidth + capsWidth + 2 * borderwidth); } // calculate which items will begin the next page