move more stuff to libs/

This commit is contained in:
speedie 2023-03-08 17:20:32 +01:00
parent 91fccf0007
commit 345a8467e5
7 changed files with 214 additions and 196 deletions

View file

@ -1,3 +1,9 @@
void
prepare_window_size(void)
{
sp = menupaddingh;
vp = (menuposition == 1) ? menupaddingv : - menupaddingv;
}
void void
create_window(int x, int y, int w, int h) create_window(int x, int y, int w, int h)

View file

@ -1,3 +1,4 @@
static void create_window(int x, int y, int w, int h); static void create_window(int x, int y, int w, int h);
static void prepare_window_size(void);
static void set_window(void); static void set_window(void);
static void set_prop(void); static void set_prop(void);

58
libs/key.c Normal file
View file

@ -0,0 +1,58 @@
void
updatenumlockmask(void)
{
unsigned int i, j;
XModifierKeymap *modmap;
numlockmask = 0;
modmap = XGetModifierMapping(dpy);
for (i = 0; i < 8; i++)
for (j = 0; j < modmap->max_keypermod; j++)
if (modmap->modifiermap[i * modmap->max_keypermod + j]
== XKeysymToKeycode(dpy, XK_Num_Lock))
numlockmask = (1 << i);
XFreeModifiermap(modmap);
}
void
keypress(XEvent *e)
{
updatenumlockmask();
{
unsigned int i;
KeySym keysym;
XKeyEvent *ev;
char buf[64];
KeySym ksym = NoSymbol;
Status status;
int len = 0;
ev = &e->xkey;
len = XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status);
keysym = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 0);
for (i = 0; i < LENGTH(keys); i++) {
if (keysym == keys[i].keysym && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state) && keys[i].func) {
if ((keys[i].mode && curMode) || keys[i].mode == -1) {
keys[i].func(&(keys[i].arg));
return;
} else if (!keys[i].mode && !curMode) {
keys[i].func(&(keys[i].arg));
} else {
continue;
}
}
}
if (!iscntrl(*buf) && type && curMode ) {
if (allowkeys) {
insert(buf, len);
} else {
allowkeys = !allowkeys;
}
drawmenu();
}
}
}

2
libs/key.h Normal file
View file

@ -0,0 +1,2 @@
static void updatenumlockmask(void);
static void keypress(XEvent *e);

116
libs/mouse.c Normal file
View file

@ -0,0 +1,116 @@
void
buttonpress(XEvent *e)
{
struct item *item;
XButtonPressedEvent *ev = &e->xbutton;
int x = 0, y = 0, h = bh, w, item_num = 0;
if (ev->window != win)
return;
/* right-click: exit */
if (ev->button == Button3)
exit(1);
if (prompt && *prompt)
x += promptw;
/* input field */
w = (lines > 0 || !matches) ? mw - x : inputw;
/* left-click on input: clear input,
* NOTE: if there is no left-arrow the space for < is reserved so
* add that to the input width */
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;
}
/* 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;
if (lines > 0) {
/* vertical list: (ctrl)left-click on item */
w = mw - x;
for (item = curr; item != next; item = item->right) {
if (item_num++ == lines){
item_num = 1;
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();
}
return;
}
}
} else if (matches) {
/* left-click on left arrow */
x += inputw;
w = TEXTW(leftarrow);
if (prev && curr->left) {
if (ev->x >= x && ev->x <= x + w) {
sel = curr = prev;
calcoffsets();
drawmenu();
return;
}
}
/* horizontal list: (ctrl)left-click on item */
for (item = curr; item != next; item = item->right) {
x += w;
w = MIN(TEXTW(item->text), mw - x - TEXTW(rightarrow));
if (ev->x >= x && ev->x <= x + w) {
puts(item->text);
if (!(ev->state & ControlMask))
exit(0);
sel = item;
if (sel) {
drawmenu();
}
return;
}
}
/* left-click on right arrow */
w = TEXTW(rightarrow);
x = mw - w;
if (next && ev->x >= x && ev->x <= x + w) {
sel = curr = next;
calcoffsets();
drawmenu();
return;
}
}
}

1
libs/mouse.h Normal file
View file

@ -0,0 +1 @@
static void buttonpress(XEvent *e);

218
spmenu.c
View file

@ -106,11 +106,13 @@
/* alpha */ /* alpha */
#define opaque 0xffU #define opaque 0xffU
/* include enums */ /* include headers */
#include "libs/schemes.h" #include "libs/schemes.h"
#include "libs/arg.h" #include "libs/arg.h"
#include "libs/mode.h" #include "libs/mode.h"
#include "libs/xrdb.h" #include "libs/xrdb.h"
#include "libs/key.h"
#include "libs/mouse.h"
static char text[BUFSIZ] = ""; static char text[BUFSIZ] = "";
@ -198,6 +200,7 @@ static void movewordedge(int dir);
static void insert(const char *str, ssize_t n); static void insert(const char *str, ssize_t n);
static void cleanup(void); static void cleanup(void);
static void navigatehistfile(int dir); static void navigatehistfile(int dir);
static int max_textw(void);
/* user configuration */ /* user configuration */
#include "options.h" /* Include main header */ #include "options.h" /* Include main header */
@ -210,11 +213,6 @@ static char * cistrstr(const char *s, const char *sub);
static int (*fstrncmp)(const char *, const char *, size_t) = strncasecmp; static int (*fstrncmp)(const char *, const char *, size_t) = strncasecmp;
static char *(*fstrstr)(const char *, const char *) = cistrstr; static char *(*fstrstr)(const char *, const char *) = cistrstr;
#include "libs/xrdb.c"
#include "libs/mode.c"
#include "libs/client.h"
#include "libs/client.c"
#if USEIMAGE #if USEIMAGE
static int longestedge = 0; /* longest edge */ static int longestedge = 0; /* longest edge */
@ -225,11 +223,17 @@ static int longestedge = 0; /* longest edge */
#include "libs/rtl.h" #include "libs/rtl.h"
#include "libs/rtl.c" #include "libs/rtl.c"
#endif #endif
#include "libs/key.c"
#include "libs/mouse.c"
#include "libs/draw.c" #include "libs/draw.c"
#include "libs/schemes.c" #include "libs/schemes.c"
#include "libs/arg.c" #include "libs/arg.c"
#include "libs/argv.h" #include "libs/argv.h"
#include "libs/argv.c" #include "libs/argv.c"
#include "libs/xrdb.c"
#include "libs/mode.c"
#include "libs/client.h"
#include "libs/client.c"
void void
appenditem(struct item *item, struct item **list, struct item **last) appenditem(struct item *item, struct item **list, struct item **last)
@ -656,182 +660,6 @@ navigatehistfile(int dir)
match(); match();
} }
void
updatenumlockmask(void)
{
unsigned int i, j;
XModifierKeymap *modmap;
numlockmask = 0;
modmap = XGetModifierMapping(dpy);
for (i = 0; i < 8; i++)
for (j = 0; j < modmap->max_keypermod; j++)
if (modmap->modifiermap[i * modmap->max_keypermod + j]
== XKeysymToKeycode(dpy, XK_Num_Lock))
numlockmask = (1 << i);
XFreeModifiermap(modmap);
}
void
keypress(XEvent *e)
{
updatenumlockmask();
{
unsigned int i;
KeySym keysym;
XKeyEvent *ev;
char buf[64];
KeySym ksym = NoSymbol;
Status status;
int len = 0;
ev = &e->xkey;
len = XmbLookupString(xic, ev, buf, sizeof buf, &ksym, &status);
keysym = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 0);
for (i = 0; i < LENGTH(keys); i++) {
if (keysym == keys[i].keysym && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state) && keys[i].func) {
if ((keys[i].mode && curMode) || keys[i].mode == -1) {
keys[i].func(&(keys[i].arg));
return;
} else if (!keys[i].mode && !curMode) {
keys[i].func(&(keys[i].arg));
} else {
continue;
}
}
}
if (!iscntrl(*buf) && type && curMode ) {
if (allowkeys) {
insert(buf, len);
} else {
allowkeys = !allowkeys;
}
drawmenu();
}
}
}
void
buttonpress(XEvent *e)
{
struct item *item;
XButtonPressedEvent *ev = &e->xbutton;
int x = 0, y = 0, h = bh, w, item_num = 0;
if (ev->window != win)
return;
/* right-click: exit */
if (ev->button == Button3)
exit(1);
if (prompt && *prompt)
x += promptw;
/* input field */
w = (lines > 0 || !matches) ? mw - x : inputw;
/* left-click on input: clear input,
* NOTE: if there is no left-arrow the space for < is reserved so
* add that to the input width */
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;
}
/* 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;
if (lines > 0) {
/* vertical list: (ctrl)left-click on item */
w = mw - x;
for (item = curr; item != next; item = item->right) {
if (item_num++ == lines){
item_num = 1;
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();
}
return;
}
}
} else if (matches) {
/* left-click on left arrow */
x += inputw;
w = TEXTW(leftarrow);
if (prev && curr->left) {
if (ev->x >= x && ev->x <= x + w) {
sel = curr = prev;
calcoffsets();
drawmenu();
return;
}
}
/* horizontal list: (ctrl)left-click on item */
for (item = curr; item != next; item = item->right) {
x += w;
w = MIN(TEXTW(item->text), mw - x - TEXTW(rightarrow));
if (ev->x >= x && ev->x <= x + w) {
puts(item->text);
if (!(ev->state & ControlMask))
exit(0);
sel = item;
if (sel) {
drawmenu();
}
return;
}
}
/* left-click on right arrow */
w = TEXTW(rightarrow);
x = mw - w;
if (next && ev->x >= x && ev->x <= x + w) {
sel = curr = next;
calcoffsets();
drawmenu();
return;
}
}
}
void void
pastesel(void) pastesel(void)
{ {
@ -1024,19 +852,19 @@ run(void)
void void
setup(void) setup(void)
{ {
int x, y, i, j; int x, y, i, j, di;
unsigned int du; unsigned int du;
unsigned int tmp, minstrlen = 0, curstrlen = 0; unsigned int tmp, minstrlen = 0, curstrlen = 0;
int numwidthchecks = 100; int numwidthchecks = 100;
struct item *item; struct item *item;
XIM xim; XIM xim;
Window w, dw, *dws; Window w, dw, *dws;
XWindowAttributes wa;
#if USEXINERAMA #if USEXINERAMA
XineramaScreenInfo *info; XineramaScreenInfo *info;
Window pw; Window pw;
int a, di, n, area = 0; int a, n, area = 0;
#endif #endif
XWindowAttributes wa;
/* init appearance */ /* init appearance */
init_appearance(); init_appearance();
@ -1079,6 +907,7 @@ setup(void)
} }
} }
} }
#if USEXINERAMA #if USEXINERAMA
i = 0; i = 0;
if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) { if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) {
@ -1108,10 +937,12 @@ setup(void)
if (centered) { if (centered) {
mw = MIN(MAX(max_textw() + promptw, minwidth), info[i].width); mw = MIN(MAX(max_textw() + promptw, minwidth), info[i].width);
x = info[i].x_org + ((info[i].width - mw) / 2); x = info[i].x_org + ((info[i].width - mw) / 2);
//y = info[i].y_org + 0;
y = info[i].y_org + ((info[i].height - mh) / 2); y = info[i].y_org + ((info[i].height - mh) / 2);
} else { } else {
x = info[i].x_org + dmx; x = info[i].x_org + dmx;
y = info[i].y_org + (menuposition ? 0 : info[i].height - mh - dmy); y = info[i].y_org + (menuposition ? 0 : info[i].height - mh - dmy);
//y = info[i].y_org + 0;
mw = (dmw>0 ? dmw : info[i].width); mw = (dmw>0 ? dmw : info[i].width);
} }
@ -1192,15 +1023,19 @@ main(int argc, char *argv[])
if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
fputs("warning: no locale support\n", stderr); fputs("warning: no locale support\n", stderr);
if (!(dpy = XOpenDisplay(NULL))) if (!(dpy = XOpenDisplay(NULL)))
die("cannot open display"); die("spmenu: cannot open display");
screen = DefaultScreen(dpy); screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen); root = RootWindow(dpy, screen);
if (!embed || !(parentwin = strtol(embed, NULL, 0))) if (!embed || !(parentwin = strtol(embed, NULL, 0)))
parentwin = root; parentwin = root;
if (!XGetWindowAttributes(dpy, parentwin, &wa)) if (!XGetWindowAttributes(dpy, parentwin, &wa))
die("could not get embedding window attributes: 0x%lx", die("spmenu: could not get embedding window attributes: 0x%lx", parentwin);
parentwin);
xinitvisual(); xinitvisual();
drw = drw_create(dpy, screen, root, wa.width, wa.height, visual, depth, cmap); drw = drw_create(dpy, screen, root, wa.width, wa.height, visual, depth, cmap);
@ -1208,13 +1043,12 @@ main(int argc, char *argv[])
die("no fonts could be loaded."); die("no fonts could be loaded.");
lrpad = drw->font->h; lrpad = drw->font->h;
sp = menupaddingh; prepare_window_size();
vp = (menuposition == 1) ? menupaddingv : - menupaddingv;
#ifdef __OpenBSD__ #ifdef __OpenBSD__
if (pledge("stdio rpath", NULL) == -1) if (pledge("stdio rpath", NULL) == -1)
die("pledge"); die("pledge");
#endif #endif
loadhistory(); loadhistory();