Skip to content

Commit

Permalink
feat: implement window move to workspace command
Browse files Browse the repository at this point in the history
- Add getters for both toplevel and workspace
  • Loading branch information
migueldeoleiros committed Oct 31, 2024
1 parent e9708bb commit 09168e7
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
59 changes: 55 additions & 4 deletions src/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,19 @@ void window_command(char *tokens[], int ntokens, char *response,
void window_list_command(char *tokens[], int ntokens, char *response,
struct turtile_context *context);
void window_switch_command(char *tokens[], int ntokens, char *response,
struct turtile_context *context);
struct turtile_context *context);
void window_cycle_command(char *tokens[], int ntokens, char *response,
struct turtile_context *context);
struct turtile_context *context);
void window_kill_command(char *tokens[], int ntokens, char *response,
struct turtile_context *context);
void window_move_to_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,
struct turtile_context *context);
struct turtile_context *context);
void workspace_switch_command(char *tokens[], int ntokens, char *response,
struct turtile_context *context);
struct turtile_context *conntext);
typedef struct {
char *cmd_name;
char *subcmd_name;
Expand All @@ -64,6 +66,7 @@ static command_t commands[] = {
{"window", "switch", window_switch_command},
{"window", "cycle", window_cycle_command},
{"window", "kill", window_kill_command},
{"window", "move-to", window_move_to_command},
{"window", NULL, window_command},
{"workspace", "list", workspace_list_command},
{"workspace", "switch", workspace_switch_command},
Expand Down Expand Up @@ -259,6 +262,54 @@ void window_kill_command(char *tokens[], int ntokens, char *response,
}
}

void window_move_to_command(char *tokens[], int ntokens, char *response,
struct turtile_context *context){
struct turtile_server *server = context->server;

if (ntokens >= 1) {
char *target_workspace_name = tokens[0];
struct turtile_workspace *target_workspace =
get_workspace(server, target_workspace_name);

if (!target_workspace) {
snprintf(response, MAX_MSG_SIZE,
"{\"error\": \"workspace not found\"}");
return;
}

struct turtile_toplevel *toplevel_to_move;
if (ntokens >= 2) {
char *toplevel_id = tokens[1];

toplevel_to_move = get_toplevel(server, toplevel_id);

if (!toplevel_to_move) {
snprintf(response, MAX_MSG_SIZE,
"{\"error\": \"window %s not found\"}", toplevel_id);
return;
}
} else {
toplevel_to_move = get_first_toplevel(server);
if (!toplevel_to_move) {
snprintf(response, MAX_MSG_SIZE,
"{\"error\": \"no focused window to move\"}");
return;
}
}

toplevel_to_move->workspace = target_workspace;
server_redraw_windows(server);

snprintf(response, MAX_MSG_SIZE,
"{\"success\": \"moved window %s to workspace %s\"}",
toplevel_to_move->xdg_toplevel->title, target_workspace->name);

} else {
snprintf(response, MAX_MSG_SIZE,
"{\"error\": \"missing argument: workspace name\"}");
}
}

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
10 changes: 10 additions & 0 deletions src/toplevel.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ void kill_toplevel(struct turtile_toplevel *toplevel) {
wlr_xdg_toplevel_send_close(toplevel->xdg_toplevel);
}

struct turtile_toplevel *get_toplevel(struct turtile_server *server, char *id) {
struct turtile_toplevel *toplevel;
wl_list_for_each(toplevel, &server->toplevels, link) {
if (strcmp(toplevel->id, id) == 0) {
return toplevel;
}
}
return NULL;
}

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
2 changes: 2 additions & 0 deletions src/toplevel.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ void focus_toplevel(struct turtile_toplevel *toplevel,

void kill_toplevel(struct turtile_toplevel *toplevel);

struct turtile_toplevel *get_toplevel(struct turtile_server *server, char *id);

/**
* Retrieves the first toplevel on the active workspace of the given server.
* If no such toplevel is found, NULL is returned.
Expand Down
11 changes: 11 additions & 0 deletions src/workspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ struct turtile_workspace* create_workspace(struct turtile_server *server,
return new_workspace;
}

struct turtile_workspace *get_workspace(struct turtile_server *server,
char *name) {
struct turtile_workspace *workspace;
wl_list_for_each(workspace, &server->workspaces, link) {
if (strcmp(workspace->name, name) == 0) {
return workspace;
}
}
return NULL;
}

void switch_workspace(struct turtile_workspace *workspace){
if(workspace == NULL){
return;
Expand Down
3 changes: 3 additions & 0 deletions src/workspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ struct turtile_workspace {
*/
struct turtile_workspace* create_workspace(struct turtile_server *server,
char *name);

struct turtile_workspace *get_workspace(struct turtile_server *server,
char *name);
/**
* Switches the active workspace to the specified workspace.
*
Expand Down

0 comments on commit 09168e7

Please sign in to comment.