spmenu/libs/x11/mouse.c

139 lines
4.9 KiB
C
Raw Normal View History

2023-05-14 00:21:16 +02:00
/* See LICENSE file for copyright and license details. */
void buttonpress_x11(XEvent *e) {
2023-05-08 23:00:45 +02:00
struct item *item;
XButtonPressedEvent *ev = &e->xbutton;
int x = 0, y = 0, h = bh, w, item_num = 0;
unsigned int i, click;
2023-06-19 23:16:03 +02:00
int yp = 0;
2023-03-08 17:20:32 +01:00
2023-04-21 09:49:38 +02:00
// margin
x += menumarginh;
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);
if (!strcmp(capstext, ""))
capsWidth = 0;
2023-06-19 23:16:03 +02:00
if ((hideprompt && hideinput && hidemode && hidematchcount && hidecaps) && lines) {
yp = 1;
} else if (lines && ev->y < h + menumarginv && ev->y > menumarginv) {
yp = 1;
2023-06-19 23:26:23 +02:00
} else if (!lines) {
yp = 1;
2023-06-19 23:16:03 +02:00
}
2023-05-08 23:00:45 +02:00
if (ev->window != win) return; // if incorrect or wrong window, return
2023-03-08 17:20:32 +01:00
click = ClickWindow; // clicking anywhere, we use this and override it if we clicked on something specific
// check click position and override the value of click
2023-06-19 23:16:03 +02:00
if (yp && ev->x < x + promptw + powerlineprompt ? plw : 0) { // prompt
click = ClickPrompt;
2023-06-19 23:16:03 +02:00
} else if (yp && (ev->x > mw - capsWidth - 2 * sp - 2 * borderwidth - menumarginh) && !hidecaps && capsWidth) { // caps lock indicator
click = ClickCaps;
2023-06-19 23:16:03 +02:00
} else if (yp && ev->x > mw - modeWidth - capsWidth - 2 * sp - 2 * borderwidth - menumarginh) { // mode indicator
click = ClickMode;
2023-06-19 23:16:03 +02:00
} else if (yp && ev->x > mw - modeWidth - numberWidth - capsWidth - 2 * sp - 2 * borderwidth - menumarginh) { // match count
click = ClickNumber;
2023-06-19 23:16:03 +02:00
} else if (yp && !hideinput) { // input
w = (lines > 0 || !matches) ? mw - x : inputw;
if ((lines <= 0 && ev->x >= 0 && ev->x <= x + w + promptw +
2023-05-08 23:00:45 +02:00
((!prev || !curr->left) ? larrowWidth : 0)) ||
(lines > 0 && ev->y >= y && ev->y <= y + h)) {
click = ClickInput;
}
}
2023-03-08 17:20:32 +01:00
2023-06-19 23:16:03 +02:00
#if USEIMAGE
if (!hideimage && longestedge != 0) {
x += MAX((imagegaps * 2) + imagewidth, indentitems ? promptw : 0);
}
#endif
// item click
2023-05-08 23:00:45 +02:00
if (lines > 0) {
w = mw - x;
2023-06-19 23:16:03 +02:00
ev->y -= menumarginv;
2023-06-20 01:27:46 +02:00
if (hideprompt && hideinput && hidemode && hidematchcount && hidecaps) {
ev->y += h;
}
2023-05-08 23:00:45 +02:00
for (item = curr; item != next; item = item->right) {
if (item_num++ == lines) {
item_num = 1;
x += w / columns;
y = 0;
}
2023-05-08 23:00:45 +02:00
y += h;
// ClickSelItem, called function doesn't matter
2023-06-19 23:16:03 +02:00
if (ev->y >= y && ev->y <= (y + h) && ev->x >= x + (powerlineitems ? plw : 0) && ev->x <= (x + w / columns) + (powerlineitems ? plw : 0)) {
2023-04-04 02:28:05 +02:00
for (i = 0; i < LENGTH(buttons); i++) {
2023-04-25 17:50:17 +02:00
if (ignoreglobalmouse) break;
if (buttons[i].click == ClickSelItem && buttons[i].button == ev->button) {
2023-04-04 02:28:05 +02:00
puts(item->text);
exit(0);
} else if (buttons[i].click == ClickItem) {
click = ClickItem;
2023-04-04 02:28:05 +02:00
}
}
for (i = 0; i < LENGTH(cbuttons); i++) {
2023-04-25 17:50:17 +02:00
if (ignoreconfmouse) break;
if (cbuttons[i].click == ClickSelItem && cbuttons[i].button == ev->button) {
puts(item->text);
exit(0);
} else if (cbuttons[i].click == ClickItem) {
click = ClickItem;
}
2023-04-04 02:28:05 +02:00
}
2023-05-08 23:00:45 +02:00
}
}
} else if (matches) { // a single line, meaning it could be arrows too, so we check that here
x += inputw;
w = larrowWidth;
2023-05-08 23:00:45 +02:00
if (prev && curr->left) {
if (ev->x >= x && ev->x <= x + w) {
click = ClickLArrow;
2023-05-08 23:00:45 +02:00
}
}
2023-05-08 23:00:45 +02:00
// right arrow
w = rarrowWidth;
x = mw - w;
if (next && ev->x >= x && ev->x <= x + w) {
click = ClickRArrow;
2023-05-08 23:00:45 +02:00
}
}
// go through mouse button array and run function
for (i = 0; i < LENGTH(buttons); i++) {
if (ignoreglobalmouse) break;
if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button)
buttons[i].func(&buttons[i].arg);
}
// go through mouse config array and run function
for (i = 0; i < LENGTH(cbuttons); i++) {
if (ignoreconfmouse) break;
if (click == cbuttons[i].click && cbuttons[i].func && cbuttons[i].button == ev->button)
cbuttons[i].func(&cbuttons[i].arg);
}
2023-03-08 17:20:32 +01:00
}