add function and keybind to exit wm, improve main loop

This commit is contained in:
Akos Horvath 2023-03-26 18:20:28 +02:00
parent a80fcebf04
commit 98ff93e7c0
2 changed files with 117 additions and 51 deletions

164
wm.c
View File

@ -323,7 +323,6 @@ void wm_mainloop(Wm *wm)
char *buffer;
int len;
while (1) {
// accepted_socket = accept(wm->socket_fd, &s, &t);
// if (accepted_socket == -1) {
@ -347,54 +346,55 @@ void wm_mainloop(Wm *wm)
// fprintf(stderr, "errno: %d\n", errno);
// }
// }
while (XPending(wm->display)) {
XEvent e;
XNextEvent(wm->display, &e);
while (wm->running) {
XEvent e;
XNextEvent(wm->display, &e);
switch (e.type) {
case CreateNotify:
wm_create_handler(wm, e.xcreatewindow);
break;
case DestroyNotify:
wm_destroy_handler(wm, e.xdestroywindow);
break;
case ReparentNotify:
wm_reparent_handler(e.xreparent);
break;
case ConfigureRequest:
wm_configure_handler(wm, e.xconfigurerequest);
break;
case KeyRelease:
wm_keyrelease_handler(e.xkey);
break;
case KeyPress:
wm_keypress_handler(wm, e.xkey);
break;
case MotionNotify:
wm_motion_handler(wm, e.xmotion);
break;
case FocusIn:
break;
case FocusOut:
break;
case EnterNotify:
DEBUG_PRINT("pointer entered window: %d\n",
(int)e.xcrossing.window);
if (wm->cfg_focus_on_motion)
wm_client_focus(wm, wm_client_find(wm, e.xcrossing.window));
break;
case LeaveNotify:
DEBUG_PRINT("pointer left window: %d\n",
(int)e.xcrossing.window);
break;
case ConfigureNotify:
DEBUG_PRINT("ConfigureNotify: %d\n", (int)e.xconfigure.window)
break;
case MapRequest:
DEBUG_PRINT("MapRequest: %d\n", (int)e.xmaprequest.window)
wm_maprequest_handler(wm, e.xmaprequest);
break;
}
switch (e.type) {
case CreateNotify:
wm_create_handler(wm, e.xcreatewindow);
break;
case DestroyNotify:
wm_destroy_handler(wm, e.xdestroywindow);
break;
case ReparentNotify:
wm_reparent_handler(e.xreparent);
break;
case ConfigureRequest:
wm_configure_handler(wm, e.xconfigurerequest);
break;
case KeyRelease:
wm_keyrelease_handler(e.xkey);
break;
case KeyPress:
wm_keypress_handler(wm, e.xkey);
break;
case MotionNotify:
wm_motion_handler(wm, e.xmotion);
break;
case FocusIn:
break;
case FocusOut:
break;
case EnterNotify:
DEBUG_PRINT("pointer entered window: %d\n",
(int)e.xcrossing.window);
if (wm->cfg_focus_on_motion)
wm_client_focus(wm, wm_client_find(wm, e.xcrossing.window));
break;
case LeaveNotify:
DEBUG_PRINT("pointer left window: %d\n",
(int)e.xcrossing.window);
break;
case ConfigureNotify:
DEBUG_PRINT("ConfigureNotify: %d\n", (int)e.xconfigure.window)
break;
case MapRequest:
DEBUG_PRINT("MapRequest: %d\n", (int)e.xmaprequest.window)
wm_maprequest_handler(wm, e.xmaprequest);
break;
default:
DEBUG_PRINT("default: %d\n", e.type);
}
}
}
@ -411,7 +411,10 @@ void wm_init(Wm *wm)
d = wm_connect_display();
wm->display = d;
wm_monitors_open_all(wm, d);
char *display_string = DisplayString(wm->display);
setenv("DISPLAY", display_string, 1);
// TODO: only testing
// wm_socket_init();
@ -471,7 +474,29 @@ void wm_init(Wm *wm)
wm->dock = -1;
wm->running = true;
wm_mainloop(wm);
wm_exit(wm);
}
void wm_exit(Wm *wm)
{
DEBUG_PRINT("%s\n", __func__);
Client *c;
XUngrabKey(wm->display, AnyKey, AnyModifier, wm->root.window);
// todo add clients to root client linked list
for (c = wm->smon->clients; c; c = c->next) {
XKillClient(wm->display, c->window);
}
for (c = wm->smon->clients; c; c = c->next) {
wm_client_free(wm, c);
}
DEBUG_PRINT("%s1\n", __func__);
XCloseDisplay(wm->display);
exit(EXIT_SUCCESS);
}
void wm_init_cfg_def(Wm *wm)
@ -490,10 +515,32 @@ void wm_grab_keys(Wm *wm)
RETURN_IF_NULL(wm->cfg_keybinds)
Keybind k;
// from dwm
{
unsigned int i, j;
XModifierKeymap *modmap;
unsigned int numlockmask = 0;
modmap = XGetModifierMapping(wm->display);
for (i = 0; i < 8; i++)
for (j = 0; j < modmap->max_keypermod; j++)
if (modmap->modifiermap[i * modmap->max_keypermod + j]
== XKeysymToKeycode(wm->display, XK_Num_Lock))
numlockmask = (1 << i);
XFreeModifiermap(modmap);
}
// from dwm
unsigned int modifiers[] = { 0, LockMask, 0|LockMask };
XUngrabKey(wm->display, AnyKey, AnyModifier, wm->root.window);
for (int i = 0; i < wm->cfg_kb_count; i++) {
k = wm->cfg_keybinds[i];
XGrabKey(wm->display, XKeysymToKeycode(wm->display, k.keysym), k.mask,
wm->root.window, True, GrabModeAsync, GrabModeAsync);
for (int j = 0; j < 3; j++)
XGrabKey(wm->display, XKeysymToKeycode(wm->display, k.keysym),
k.mask | modifiers[j], wm->root.window, True, GrabModeAsync,
GrabModeAsync);
}
}
@ -521,6 +568,11 @@ void wm_kb_spawn(Wm *wm, Arg *args)
}
}
void wm_kb_exit(Wm* wm, Arg *args)
{
wm->running = false;
}
void wm_kb_kill(Wm *wm, Arg *args)
{
Client *c;
@ -553,12 +605,22 @@ void wm_keybinds_init_def(Wm *wm)
char *st[] = {"st", NULL};
char **sth = malloc(sizeof(st));
memcpy(sth, st, sizeof(st));
char *dmenu[] = {"i3-dmenu-desktop", NULL};
char **dmenuh = malloc(sizeof(dmenu));
memcpy(dmenuh, dmenu, sizeof(dmenu));
int size;
Keybind k[] = {
(Keybind) {Mod4Mask, XK_Return, *wm_kb_spawn,
(Arg) {.sl = sth, .count = 2}},
(Keybind) {Mod4Mask | ShiftMask, XK_q, *wm_kb_exit,
(Arg) {0}},
(Keybind) {Mod4Mask, XK_d, *wm_kb_spawn,
(Arg) {.sl = dmenuh, .count = 2}},
(Keybind) {Mod4Mask, XK_c, *wm_kb_kill,
(Arg) {.c = NULL}},

4
wm.h
View File

@ -117,6 +117,7 @@ struct Wm {
Display *display;
Monitor *monitors;
Monitor *smon;
bool running;
int wm_mc;
bool other_wm;
Client root;
@ -149,6 +150,8 @@ void wm_spawn(Wm* wm, char **str);
void wm_switch_ws(Wm* wm, int ws);
void wm_mainloop(Wm* wm);
void wm_init(Wm *wm);
void wm_exit(Wm *wm);
void wm_init_cfg_def(Wm *wm);
void wm_grab_keys(Wm *wm);
@ -156,6 +159,7 @@ void wm_kb_spawn(Wm *wm, Arg *args);
void wm_kb_kill(Wm *wm, Arg *args);
void wm_kb_switch_ws(Wm *wm, Arg *args);
void wm_kb_focus_dir(Wm *wm, Arg *args);
void wm_kb_exit(Wm* wm, Arg *args);
void wm_keybinds_init_def(Wm *wm);