Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 28 additions & 18 deletions sowm.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include "sowm.h"

static client *list = {0}, *ws_list[10] = {0}, *cur;
static int ws = 1, sw, sh, wx, wy, numlock = 0;
static unsigned int ww, wh;
static int ws = 1, sw, sh, wx, wy;
static unsigned int ww, wh, clean_mask;

static Display *d;
static XButtonEvent mouse;
Expand Down Expand Up @@ -66,10 +66,11 @@ void notify_motion(XEvent *e) {

void key_press(XEvent *e) {
KeySym keysym = XkbKeycodeToKeysym(d, e->xkey.keycode, 0, 0);
unsigned mod = clean_mask & e->xkey.state;

for (unsigned int i=0; i < sizeof(keys)/sizeof(*keys); ++i)
if (keys[i].keysym == keysym &&
mod_clean(keys[i].mod) == mod_clean(e->xkey.state))
keys[i].mod == mod)
keys[i].function(keys[i].arg);
}

Expand Down Expand Up @@ -239,32 +240,41 @@ void run(const Arg arg) {
execvp((char*)arg.com[0], (char**)arg.com);
}

void input_grab(Window root) {
unsigned int i, j, modifiers[] = {0, LockMask, numlock, numlock|LockMask};
// Taken from DWM. Many thanks. https://git.suckless.org/dwm
static unsigned int numlockmask(void) {
unsigned int nlm = 0;
XModifierKeymap *modmap = XGetModifierMapping(d);
KeyCode code;
KeyCode code = XKeysymToKeycode(d, XK_Num_Lock);

for (i = 0; i < 8; i++)
for (int k = 0; k < modmap->max_keypermod; k++)
if (modmap->modifiermap[i * modmap->max_keypermod + k]
== XKeysymToKeycode(d, 0xff7f))
numlock = (1 << i);
for (unsigned int i = 0; i < 8; i++)
for (int m = modmap->max_keypermod, k = 0; k < m; k++)
if (modmap->modifiermap[i * m + k] == code)
nlm = 1<<i;
XFreeModifiermap(modmap);

XUngrabKey(d, AnyKey, AnyModifier, root);

for (i = 0; i < sizeof(keys)/sizeof(*keys); i++)
clean_mask = ~(nlm|LockMask) &
(ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask);
return nlm;
}

void input_grab(Window root) {
unsigned int nlm = numlockmask();
unsigned int modifiers[] = {0, LockMask, nlm, nlm|LockMask};
KeyCode code;

for (size_t i = 0; i < sizeof(keys)/sizeof(*keys); i++)
if ((code = XKeysymToKeycode(d, keys[i].keysym)))
for (j = 0; j < sizeof(modifiers)/sizeof(*modifiers); j++)
for (size_t j = 0; j < sizeof(modifiers)/sizeof(*modifiers); j++)
XGrabKey(d, code, keys[i].mod | modifiers[j], root,
True, GrabModeAsync, GrabModeAsync);

for (i = 1; i < 4; i += 2)
for (j = 0; j < sizeof(modifiers)/sizeof(*modifiers); j++)
XGrabButton(d, i, MOD | modifiers[j], root, True,
for (unsigned int b = 1; b < 4; b += 2)
for (size_t j = 0; j < sizeof(modifiers)/sizeof(*modifiers); j++)
XGrabButton(d, b, MOD | modifiers[j], root, True,
ButtonPressMask|ButtonReleaseMask|PointerMotionMask,
GrabModeAsync, GrabModeAsync, 0, 0);

XFreeModifiermap(modmap);
}

int main(void) {
Expand Down
4 changes: 0 additions & 4 deletions sowm.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
XGetGeometry(d, W, &(Window){0}, gx, gy, gw, gh, \
&(unsigned int){0}, &(unsigned int){0})

// Taken from DWM. Many thanks. https://git.suckless.org/dwm
#define mod_clean(mask) (mask & ~(numlock|LockMask) & \
(ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))

typedef struct {
const char** com;
const int i;
Expand Down