Skip to content

Commit

Permalink
feat: implement window kill command
Browse files Browse the repository at this point in the history
it works for both the active window (no id specified) and to kill a
specific window
  • Loading branch information
migueldeoleiros committed Oct 31, 2024
1 parent 34d36a9 commit e9708bb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ void window_switch_command(char *tokens[], int ntokens, char *response,
struct turtile_context *context);
void window_cycle_command(char *tokens[], int ntokens, char *response,
struct turtile_context *context);
void window_kill_command(char *tokens[], int ntokens, char *response,
struct turtile_context *context);
void workspace_command(char *tokens[], int ntokens, char *response,
struct turtile_context *context);
void workspace_list_command(char *tokens[], int ntokens, char *response,
Expand All @@ -61,6 +63,7 @@ static command_t commands[] = {
{"window", "list", window_list_command},
{"window", "switch", window_switch_command},
{"window", "cycle", window_cycle_command},
{"window", "kill", window_kill_command},
{"window", NULL, window_command},
{"workspace", "list", workspace_list_command},
{"workspace", "switch", workspace_switch_command},
Expand Down Expand Up @@ -227,6 +230,35 @@ void window_cycle_command(char *tokens[], int ntokens, char *response,
next_toplevel->xdg_toplevel->title);
}

void window_kill_command(char *tokens[], int ntokens, char *response,
struct turtile_context *context){
// kill designated toplevel
struct turtile_server *server = context->server;
struct turtile_toplevel *toplevel;

if(ntokens >= 1){
char *new_toplevel_id = tokens[0];

wl_list_for_each(toplevel, &server->focus_toplevels, flink) {
if(strcmp(toplevel->id, new_toplevel_id) == 0){
kill_toplevel(toplevel);
snprintf(response, MAX_MSG_SIZE,
"{\"success\": \"kill: %s\"}",
toplevel->xdg_toplevel->title);
return;
}
}
snprintf(response, MAX_MSG_SIZE,
"{\"error\": \"window %s not found\"}", new_toplevel_id);

} else{
toplevel = get_first_toplevel(server);
kill_toplevel(toplevel);
snprintf(response, MAX_MSG_SIZE,
"{\"success\": \"kill: %s\"}", toplevel->xdg_toplevel->title);
}
}

void workspace_command(char *tokens[], int ntokens, char *response,
struct turtile_context *context){
// TODO: use this function as a help for the other workspace subcommands
Expand Down
21 changes: 21 additions & 0 deletions src/toplevel.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ void focus_toplevel(struct turtile_toplevel *toplevel, struct wlr_surface *surfa
server_redraw_windows(server);
}

void kill_toplevel(struct turtile_toplevel *toplevel) {
struct turtile_server *server = toplevel->server;

struct wl_list workspace_toplevels;
get_workspace_toplevels(server->active_workspace, &workspace_toplevels);

if (wl_list_empty(&workspace_toplevels)){
wlr_xdg_toplevel_send_close(toplevel->xdg_toplevel);
return;
} else if (wl_list_length(&workspace_toplevels) < 2) {
wlr_xdg_toplevel_send_close(toplevel->xdg_toplevel);
return;
}

struct turtile_toplevel *next_toplevel =
wl_container_of(workspace_toplevels.next, next_toplevel, auxlink);
focus_toplevel(next_toplevel, next_toplevel->xdg_toplevel->base->surface);

wlr_xdg_toplevel_send_close(toplevel->xdg_toplevel);
}

struct turtile_toplevel *get_first_toplevel(struct turtile_server *server) {
struct turtile_toplevel *toplevel;
wl_list_for_each(toplevel, &server->focus_toplevels, flink)
Expand Down
3 changes: 3 additions & 0 deletions src/toplevel.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ struct turtile_toplevel {
*/
void focus_toplevel(struct turtile_toplevel *toplevel,
struct wlr_surface *surface);

void kill_toplevel(struct turtile_toplevel *toplevel);

/**
* Retrieves the first toplevel on the active workspace of the given server.
* If no such toplevel is found, NULL is returned.
Expand Down

0 comments on commit e9708bb

Please sign in to comment.