Skip to content

Commit

Permalink
WIP: statuscmd patch for clickable status bar.
Browse files Browse the repository at this point in the history
  • Loading branch information
chhajedji committed Feb 18, 2021
1 parent 050cb96 commit 1035392
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
15 changes: 14 additions & 1 deletion config.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,14 @@ static const Layout layouts[] = {
/* commands */
static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn() */
static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL };
static const char *termcmd[] = { "st", NULL };

#if STATUSCMD
/* commands spawned when clicking statusbar, the mouse button pressed is exported as BUTTON */
static char *statuscmds[] = { "notify-send 1.$BUTTON", "notify-send 2.$BUTTON", "notify-send 3.$BUTTON", "notify-send 4.$BUTTON", "notify-send 5.$BUTTON", "notify-send 6.$BUTTON", "notify-send 7.$BUTTON", "notify-send 8.$BUTTON" };
static char *statuscmd[] = { "/bin/sh", "-c", NULL, NULL };
#else
static const char *termcmd[] = { "konsole", NULL };
#endif

static Key keys[] = {
/* modifier key function argument */
Expand Down Expand Up @@ -238,7 +245,13 @@ static Button buttons[] = {
{ ClkLtSymbol, 0, Button1, setlayout, {0} },
{ ClkLtSymbol, 0, Button3, setlayout, {.v = &layouts[2]} },
{ ClkWinTitle, 0, Button2, zoom, {0} },
#if STATUSCMD
{ ClkStatusText, 0, Button1, spawn, {.v = statuscmd } },
{ ClkStatusText, 0, Button2, spawn, {.v = statuscmd } },
{ ClkStatusText, 0, Button3, spawn, {.v = statuscmd } },
#else
{ ClkStatusText, 0, Button2, spawn, {.v = termcmd } },
#endif
{ ClkClientWin, MODKEY, Button1, movemouse, {0} },
{ ClkClientWin, MODKEY, Button2, togglefloating, {0} },
{ ClkClientWin, MODKEY, Button3, resizemouse, {0} },
Expand Down
62 changes: 62 additions & 0 deletions dwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ static void clientmessage(XEvent *e);
static void configure(Client *c);
static void configurenotify(XEvent *e);
static void configurerequest(XEvent *e);
#if STATUSCMD
static void copyvalidchars(char *text, char *rawtext);
#endif
static Monitor *createmon(void);
static void destroynotify(XEvent *e);
static void detach(Client *c);
Expand Down Expand Up @@ -303,6 +306,11 @@ static void centeredfloatingmaster(Monitor *m);
static Systray *systray = NULL;
static const char broken[] = "broken";
static char stext[256];
#if STATUSCMD
static char rawstext[256];
static int statuscmdn;
static char lastbutton[] = "-";
#endif
static int screen;
static int sw, sh; /* X display screen geometry width, height */
static int bh, blw = 0; /* bar geometry */
Expand Down Expand Up @@ -559,6 +567,9 @@ buttonpress(XEvent *e)
Client *c;
Monitor *m;
XButtonPressedEvent *ev = &e->xbutton;
#if STATUSCMD
*lastbutton = '0' + ev->button;
#endif

click = ClkRootWin;
/* focus monitor if necessary */
Expand All @@ -582,9 +593,32 @@ buttonpress(XEvent *e)
arg.ui = 1 << i;
} else if (ev->x < x + blw)
click = ClkLtSymbol;
#if !STATUSCMD
else if (ev->x > selmon->ww - TEXTW(stext) - getsystraywidth())
click = ClkStatusText;
else
#else
else if (ev->x > selmon->ww - TEXTW(stext) - getsystraywidth() + lrpad) {

This comment has been minimized.

Copy link
@bakkeby

bakkeby Feb 18, 2021

If you look closely at the patch for this particular line then you can spot that it uses a little trick here.

-		else if (ev->x > selmon->ww - TEXTW(stext))
+		else if (ev->x > (x = selmon->ww - TEXTW(stext) + lrpad)) {

Up until now the variable x has been used to keep track of where we are on the bar with regards to our calculations, but the statuscmd patch here explicitly sets that to the start of the status text.

In your version you just check if ev->x is greater than the start of the status text, which means that x, which is used to determine how far on the status you have clicked, remains pointing to the end of the layout symbol or something like that.

I would recommend against adding the setting of x as part of the if statement though as makes it harder to read (and can cause conflicts with other patches such as awesomebar).

Within the if statement I'd instead just add this line;

x = selmon->ww - TEXTW(stext) - getsystraywidth() + lrpad;

This comment has been minimized.

Copy link
@chhajedji

chhajedji Feb 18, 2021

Author Owner

Wow! Thanks @bakkeby for this noticing this minute difference. I updated the value of x and now I can see clicks at different places triggering different commands.

-               else if (ev->x > selmon->ww - TEXTW(stext) - getsystraywidth() + lrpad) {
+               else if (ev->x > (x = selmon->ww - TEXTW(stext) - getsystraywidth() + lrpad)) {

I updated the value of x in the else if check only as value of x needed to be updated when if is false.
Again, thank you very much.

click = ClkStatusText;

char *text = rawstext;
int i = -1;
char ch;
statuscmdn = 0;
while (text[++i]) {
if ((unsigned char)text[i] < ' ') {
ch = text[i];
text[i] = '\0';
x += TEXTW(text) - lrpad;
text[i] = ch;
text += i+1;
i = -1;
if (x >= ev->x) break;
if (ch <= LENGTH(statuscmds)) statuscmdn = ch - 1;
}
}
} else
#endif
click = ClkWinTitle;
} else if ((c = wintoclient(ev->window))) {
focus(c);
Expand Down Expand Up @@ -823,6 +857,21 @@ configurerequest(XEvent *e)
XSync(dpy, False);
}

#if STATUSCMD
void
copyvalidchars(char *text, char *rawtext)
{
int i = -1, j = 0;

while(rawtext[++i]) {
if ((unsigned char)rawtext[i] >= ' ') {
text[j++] = rawtext[i];
}
}
text[j] = '\0';
}
#endif

Monitor *
createmon(void)
{
Expand Down Expand Up @@ -2061,6 +2110,12 @@ spawn(const Arg *arg)
{
if (arg->v == dmenucmd)
dmenumon[0] = '0' + selmon->num;
#if STATUSCMD
else if (arg->v == statuscmd) {
statuscmd[2] = statuscmds[statuscmdn];
setenv("BUTTON", lastbutton, 1);
}
#endif
if (fork() == 0) {
if (dpy)
close(ConnectionNumber(dpy));
Expand Down Expand Up @@ -2516,8 +2571,15 @@ updatesizehints(Client *c)
void
updatestatus(void)
{
#if STATUSCMD
if (!gettextprop(root, XA_WM_NAME, rawstext, sizeof(rawstext)))
strcpy(stext, "dwm-"VERSION);
else
copyvalidchars(stext, rawstext);
#else
if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
strcpy(stext, "dwm-"VERSION);
#endif
drawbar(selmon);
updatesystray();
}
Expand Down
1 change: 1 addition & 0 deletions include_patches.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
#define ATTACH_ASIDE (1)
#define STATUSCOLOR (0)
#define FULL_SCREEN (0)
#define STATUSCMD (1)

0 comments on commit 1035392

Please sign in to comment.