add Mouse array, allowing mouse binds to be easily changed similar to

the Key binds
This commit is contained in:
speedie 2023-04-02 16:10:13 +02:00
parent 3f01a71e0b
commit f1313697dc
7 changed files with 114 additions and 80 deletions

View file

@ -140,7 +140,6 @@ free to add it to this list.</p>
issues!</p>
<h3 id="general">General</h3>
<ul>
<li>Key: Mouse bind array (Likely mouse.h).</li>
<li>Config file: Add configuration file using (probably) libconfig,
allowing keybinds to be configured without recompiling spmenu.</li>
<li>Image support: Stop using OpenSSL for caching images, mostly because

View file

@ -119,7 +119,6 @@ Pull requests would be greatly appreciated for any of these issues!
### General
- Key: Mouse bind array (Likely mouse.h).
- Config file: Add configuration file using (probably) libconfig, allowing
keybinds to be configured without recompiling spmenu.
- Image support: Stop using OpenSSL for caching images, mostly because MD5()

View file

@ -2,7 +2,7 @@
*
* Types
*
* MODIFIER1 is what you defined below, default is Super)
* MODIFIER1 is what you defined below, default is Alt)
*
* SHIFT is unless changed going to be your Shift key.
* CONTROL is unless changed going to be your Control key.

View file

@ -23,12 +23,14 @@ motionevent(XButtonEvent *ev)
int itemCount = 0;
// walk through all items
for (item = items; item && item->text; item++)
for (item = items; item && item->text; item++) {
itemCount++;
}
// to prevent slowdown, arbritary limit of 50 items
if (itemCount > 50)
if (itemCount > 50) {
return;
}
int larrowWidth = 0;
int rarrowWidth = 0;
@ -56,62 +58,51 @@ buttonpress(XEvent *e)
struct item *item;
XButtonPressedEvent *ev = &e->xbutton;
int x = 0, y = 0, h = bh, w, item_num = 0;
unsigned int i, click;
int xpad = 0;
if (!hidepowerline) {
x = xpad = plw;
}
int larrowWidth = 0;
int rarrowWidth = 0;
int numberWidth = 0;
int modeWidth = 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 incorrect or wrong window, return
if (ev->window != win)
return;
// right-click: exit
if (ev->button == Button3)
exit(1);
// clicking anywhere, we use this as a base for a click
click = clickwindow;
if (prompt && *prompt)
x += promptw + plw;
// input field
// check if we clicked on the prompt or the input
if (ev->x < x + promptw + plw) {
click = clickprompt;
} else if (ev->x > mw - modeWidth) {
click = clickmode;
} else if (ev->x > mw - modeWidth - numberWidth) {
click = clicknumber;
} else { // actually we clicked on the input
w = (lines > 0 || !matches) ? mw - x : inputw;
// left-click on input: clear input
if (ev->button == Button1 &&
((lines <= 0 && ev->x >= 0 && ev->x <= x + w +
((!prev || !curr->left) ? TEXTW(leftarrow) : 0)) ||
(lines > 0 && ev->y >= y && ev->y <= y + h))) {
insert(NULL, -cursor);
drawmenu();
return;
if ((lines <= 0 && ev->x >= 0 && ev->x <= x + w + promptw +
((!prev || !curr->left) ? larrowWidth : 0)) ||
(lines > 0 && ev->y >= y && ev->y <= y + h)) {
click = clickinput;
}
// middle-mouse click: paste selection
if (ev->button == Button2) {
XConvertSelection(dpy, (ev->state & ShiftMask) ? clip : XA_PRIMARY,
utf8, utf8, win, CurrentTime);
drawmenu();
return;
}
// scroll up
if (ev->button == Button4 && prev) {
sel = curr = prev;
calcoffsets();
drawmenu();
return;
}
// scroll down
if (ev->button == Button5 && next) {
sel = curr = next;
calcoffsets();
drawmenu();
return;
}
if (ev->button != Button1)
return;
if (ev->state & ~ControlMask)
return;
// item click
if (lines > 0) {
// vertical list: (ctrl)left-click on item
// vertical list
w = mw - x;
for (item = curr; item != next; item = item->right) {
if (item_num++ == lines) {
@ -119,54 +110,53 @@ buttonpress(XEvent *e)
x += w / columns;
y = 0;
}
y += h;
if (ev->y >= y && ev->y <= (y + h) &&
ev->x >= x && ev->x <= (x + w / columns)) {
puts(item->text);
if (!(ev->state & ControlMask))
exit(0);
sel = item;
if (sel) {
drawmenu();
}
if (ev->y >= y && ev->y <= (y + h) && ev->x >= x && ev->x <= (x + w / columns)) {
// TODO: make clickitem a thing
if (ev->button != Button1)
return;
puts(item->text);
exit(0);
}
}
} else if (matches) {
// left-click on left arrow
x += inputw;
w = TEXTW(leftarrow);
w = larrowWidth;
if (prev && curr->left) {
if (ev->x >= x && ev->x <= x + w) {
sel = curr = prev;
calcoffsets();
drawmenu();
return;
click = clicklarrow;
}
}
// horizontal list: (ctrl)left-click on item
// item click
// horizontal list
for (item = curr; item != next; item = item->right) {
x += w;
w = MIN(TEXTW(item->text), mw - x - TEXTW(rightarrow));
w = MIN(TEXTW(item->text), mw - x - rarrowWidth);
if (ev->x >= x && ev->x <= x + w) {
puts(item->text);
if (!(ev->state & ControlMask))
exit(0);
sel = item;
if (sel) {
drawmenu();
}
// TODO: make clickitem a thing
if (ev->button != Button1)
return;
puts(item->text);
exit(0);
}
}
// left-click on right arrow
w = TEXTW(rightarrow);
w = rarrowWidth;
x = mw - w;
if (next && ev->x >= x && ev->x <= x + w) {
sel = curr = next;
calcoffsets();
drawmenu();
return;
click = clickrarrow;
}
}
// go through mouse button array
for (i = 0; i < LENGTH(buttons); i++)
if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
&& CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
buttons[i].func(&buttons[i].arg);
}

View file

@ -1,3 +1,22 @@
typedef struct {
unsigned int click;
unsigned int mask;
unsigned int button;
void (*func)(const Arg *arg);
const Arg arg;
} Mouse;
// clicks
enum {
clickwindow,
clickprompt,
clickinput,
clicklarrow,
clickrarrow,
clicknumber,
clickmode,
};
static unsigned int textw_clamp(const char *str, unsigned int n);
static void motionevent(XButtonEvent *ev);
static void buttonpress(XEvent *e);

26
mouse.h Normal file
View file

@ -0,0 +1,26 @@
/* Mouse bindings
*
* This header contains mouse binds.
* Change them or remove them if you prefer. You can also add more if you want.
*
* clickwindow: - spmenu window/client
* clickinput: - Input box
* clickprompt: - Prompt
* clicklarrow: - Left arrow
* clickrarrow: - Right arrow
* clicknumber: - Match count
* clickmode: - Mode indicator
*
* Button1 - Left click
* Button2 - Middle click
* Button3 - Right click
* Button4 - Scroll up
* Button5 - Scroll down
*
* Note that clicking on an item will select it, as of now it's hardcoded.
*/
static Mouse buttons[] = {
{ clickinput, 0, Button1, clear, {0} },
{ clickprompt, 0, Button1, clear, {0} },
};

View file

@ -199,6 +199,7 @@ static size_t nextrune(int inc);
// user configuration
#include "options.h"
#include "keybinds.h"
#include "mouse.h"
// xresources/color arrays
#include "libs/xresources.h"