sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

dwm-statuscmd-6.2.diff (4795B)


      1 From 2761ad72b4b8a80e434e7eba1a24676119ab666d Mon Sep 17 00:00:00 2001
      2 From: Daniel Bylinka <daniel.bylinka@gmail.com>
      3 Date: Sat, 18 Apr 2020 01:41:03 +0200
      4 Subject: [PATCH] statuscmd: run shell commands based on mouse button and
      5  position when clicking statusbar
      6 
      7 ---
      8  config.def.h |  8 +++++++-
      9  dwm.c        | 47 ++++++++++++++++++++++++++++++++++++++++++++---
     10  2 files changed, 51 insertions(+), 4 deletions(-)
     11 
     12 diff --git a/config.def.h b/config.def.h
     13 index 1c0b587..5cd7efa 100644
     14 --- a/config.def.h
     15 +++ b/config.def.h
     16 @@ -59,6 +59,10 @@ static char dmenumon[2] = "0"; /* component of dmenucmd, manipulated in spawn()
     17  static const char *dmenucmd[] = { "dmenu_run", "-m", dmenumon, "-fn", dmenufont, "-nb", col_gray1, "-nf", col_gray3, "-sb", col_cyan, "-sf", col_gray4, NULL };
     18  static const char *termcmd[]  = { "st", NULL };
     19  
     20 +/* commands spawned when clicking statusbar, the mouse button pressed is exported as BUTTON */
     21 +static char *statuscmds[] = { "notify-send Mouse$BUTTON" };
     22 +static char *statuscmd[] = { "/bin/sh", "-c", NULL, NULL };
     23 +
     24  static Key keys[] = {
     25  	/* modifier                     key        function        argument */
     26  	{ MODKEY,                       XK_p,      spawn,          {.v = dmenucmd } },
     27 @@ -103,7 +107,9 @@ static Button buttons[] = {
     28  	{ ClkLtSymbol,          0,              Button1,        setlayout,      {0} },
     29  	{ ClkLtSymbol,          0,              Button3,        setlayout,      {.v = &layouts[2]} },
     30  	{ ClkWinTitle,          0,              Button2,        zoom,           {0} },
     31 -	{ ClkStatusText,        0,              Button2,        spawn,          {.v = termcmd } },
     32 +	{ ClkStatusText,        0,              Button1,        spawn,          {.v = statuscmd } },
     33 +	{ ClkStatusText,        0,              Button2,        spawn,          {.v = statuscmd } },
     34 +	{ ClkStatusText,        0,              Button3,        spawn,          {.v = statuscmd } },
     35  	{ ClkClientWin,         MODKEY,         Button1,        movemouse,      {0} },
     36  	{ ClkClientWin,         MODKEY,         Button2,        togglefloating, {0} },
     37  	{ ClkClientWin,         MODKEY,         Button3,        resizemouse,    {0} },
     38 diff --git a/dwm.c b/dwm.c
     39 index 4465af1..d35d173 100644
     40 --- a/dwm.c
     41 +++ b/dwm.c
     42 @@ -156,6 +156,7 @@ static void clientmessage(XEvent *e);
     43  static void configure(Client *c);
     44  static void configurenotify(XEvent *e);
     45  static void configurerequest(XEvent *e);
     46 +static void copyvalidchars(char *text, char *rawtext);
     47  static Monitor *createmon(void);
     48  static void destroynotify(XEvent *e);
     49  static void detach(Client *c);
     50 @@ -237,6 +238,9 @@ static void zoom(const Arg *arg);
     51  /* variables */
     52  static const char broken[] = "broken";
     53  static char stext[256];
     54 +static char rawstext[256];
     55 +static int statuscmdn;
     56 +static char lastbutton[] = "-";
     57  static int screen;
     58  static int sw, sh;           /* X display screen geometry width, height */
     59  static int bh, blw = 0;      /* bar geometry */
     60 @@ -421,6 +425,7 @@ buttonpress(XEvent *e)
     61  	Client *c;
     62  	Monitor *m;
     63  	XButtonPressedEvent *ev = &e->xbutton;
     64 +	*lastbutton = '0' + ev->button;
     65  
     66  	click = ClkRootWin;
     67  	/* focus monitor if necessary */
     68 @@ -439,9 +444,26 @@ buttonpress(XEvent *e)
     69  			arg.ui = 1 << i;
     70  		} else if (ev->x < x + blw)
     71  			click = ClkLtSymbol;
     72 -		else if (ev->x > selmon->ww - TEXTW(stext))
     73 +		else if (ev->x > (x = selmon->ww - TEXTW(stext) + lrpad)) {
     74  			click = ClkStatusText;
     75 -		else
     76 +
     77 +			char *text = rawstext;
     78 +			int i = -1;
     79 +			char ch;
     80 +			statuscmdn = 0;
     81 +			while (text[++i]) {
     82 +				if ((unsigned char)text[i] < ' ') {
     83 +					ch = text[i];
     84 +					text[i] = '\0';
     85 +					x += TEXTW(text) - lrpad;
     86 +					text[i] = ch;
     87 +					text += i+1;
     88 +					i = -1;
     89 +					if (x >= ev->x) break;
     90 +					if (ch <= LENGTH(statuscmds)) statuscmdn = ch - 1;
     91 +				}
     92 +			}
     93 +		} else
     94  			click = ClkWinTitle;
     95  	} else if ((c = wintoclient(ev->window))) {
     96  		focus(c);
     97 @@ -627,6 +649,19 @@ configurerequest(XEvent *e)
     98  	XSync(dpy, False);
     99  }
    100  
    101 +void
    102 +copyvalidchars(char *text, char *rawtext)
    103 +{
    104 +	int i = -1, j = 0;
    105 +
    106 +	while(rawtext[++i]) {
    107 +		if ((unsigned char)rawtext[i] >= ' ') {
    108 +			text[j++] = rawtext[i];
    109 +		}
    110 +	}
    111 +	text[j] = '\0';
    112 +}
    113 +
    114  Monitor *
    115  createmon(void)
    116  {
    117 @@ -1641,6 +1676,10 @@ spawn(const Arg *arg)
    118  {
    119  	if (arg->v == dmenucmd)
    120  		dmenumon[0] = '0' + selmon->num;
    121 +	else if (arg->v == statuscmd) {
    122 +		statuscmd[2] = statuscmds[statuscmdn];
    123 +		setenv("BUTTON", lastbutton, 1);
    124 +	}
    125  	if (fork() == 0) {
    126  		if (dpy)
    127  			close(ConnectionNumber(dpy));
    128 @@ -1987,8 +2026,10 @@ updatesizehints(Client *c)
    129  void
    130  updatestatus(void)
    131  {
    132 -	if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
    133 +	if (!gettextprop(root, XA_WM_NAME, rawstext, sizeof(rawstext)))
    134  		strcpy(stext, "dwm-"VERSION);
    135 +	else
    136 +		copyvalidchars(stext, rawstext);
    137  	drawbar(selmon);
    138  }
    139  
    140 -- 
    141 2.26.1
    142