diff --git a/src/client.c b/src/client.c index 21e069f..7360ddb 100644 --- a/src/client.c +++ b/src/client.c @@ -42,14 +42,20 @@ XWindowChanges wm_client_to_xwchanges(Client *c) Client* wm_client_find(Wm *wm, Window w) { - Client *c; + PtrArray clients = wm_treenode_find_client_nodes_ptr(&wm->smon->workspaces[wm->smon->selws].tree); + Client *ret = NULL; - for (c = wm->smon->clients; c; c = c->next) { - if (c->window == w) - return c; + for (size_t i = 0; i < clients.size; i++) { + Client *c = ((TreeNode*)clients.ptrs[i])->client; + if (c->window == w) { + ret = c; + goto ret; + } } - return NULL; +ret: + wm_ptrarray_free(&clients); + return ret; } Client* wm_client_create(Wm *wm, Window w) @@ -239,6 +245,7 @@ void wm_client_kill(Wm *wm, Client *c) TreeNode* parent = wm_treenode_remove_client(wm, &c->ws->tree, c); wm_client_free(wm, c); + wm->focused_client = NULL; wm_layout(wm, m); diff --git a/src/wm.c b/src/wm.c index 8f7b1c2..d8aa37a 100644 --- a/src/wm.c +++ b/src/wm.c @@ -57,7 +57,8 @@ void wm_monitors_open_all(Wm *wm, Display *d) printf("wm: Could not open display. Exiting.\n"); exit(1); } - + + wm->wm_mc = count; wm->monitors = malloc(count * sizeof(Monitor)); for (int i = 0; i < count; i++) { @@ -600,20 +601,34 @@ void wm_init(Wm *wm) void wm_exit(Wm *wm) { DEBUG_PRINT("%s\n", __func__); - Client *c; XUngrabKey(wm->display, AnyKey, AnyModifier, wm->root.window); + PtrArray all_clients = wm_ptrarray_new(); - // todo add clients to root client linked list - for (c = wm->smon->clients; c; c = c->next) { - XKillClient(wm->display, c->window); + // TODO: perhaps add created clients to a PtrArray field in workspace. + // would reduce number of calls to wm_treenode_find_client_nodes_ptr + + for (size_t i = 0; i < wm->wm_mc; i++) { + for (size_t j = 0; j < wm->monitors[i].wscount; j++) { + PtrArray clients = wm_treenode_find_client_nodes_ptr( + &wm->monitors[i].workspaces[j].tree); + + for (size_t k = 0; k < clients.size; k++) { + wm_ptrarray_push(&all_clients, clients.ptrs[i]); + } + wm_ptrarray_free(&clients); + } } - for (c = wm->smon->clients; c; c = c->next) { + + for (size_t i = 0; i < all_clients.size; i++) { + Client *c = ((TreeNode*)all_clients.ptrs[i])->client; + XKillClient(wm->display, c->window); wm_client_free(wm, c); } - DEBUG_PRINT("%s1\n", __func__); XCloseDisplay(wm->display); + + wm_ptrarray_free(&all_clients); exit(EXIT_SUCCESS); }