mirror of
				https://github.com/swaywm/sway.git
				synced 2025-11-03 09:01:43 -05:00 
			
		
		
		
	Merge pull request #169 from taiyu-len/master
handling of commands during config
This commit is contained in:
		
						commit
						c2cd56c939
					
				
					 3 changed files with 52 additions and 40 deletions
				
			
		| 
						 | 
					@ -6,8 +6,14 @@
 | 
				
			||||||
struct cmd_handler {
 | 
					struct cmd_handler {
 | 
				
			||||||
	char *command;
 | 
						char *command;
 | 
				
			||||||
	bool (*handle)(struct sway_config *config, int argc, char **argv);
 | 
						bool (*handle)(struct sway_config *config, int argc, char **argv);
 | 
				
			||||||
 | 
						enum {
 | 
				
			||||||
 | 
							CMD_COMPOSITOR_READY,
 | 
				
			||||||
 | 
							CMD_KEYBIND,
 | 
				
			||||||
 | 
							CMD_ANYTIME
 | 
				
			||||||
 | 
						} config_type;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct cmd_handler *find_handler(char *line);
 | 
				
			||||||
bool handle_command(struct sway_config *config, char *command);
 | 
					bool handle_command(struct sway_config *config, char *command);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void remove_view_from_scratchpad();
 | 
					void remove_view_from_scratchpad();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -860,31 +860,31 @@ static bool cmd_ws_auto_back_and_forth(struct sway_config *config, int argc, cha
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Keep alphabetized */
 | 
					/* Keep alphabetized */
 | 
				
			||||||
static struct cmd_handler handlers[] = {
 | 
					static struct cmd_handler handlers[] = {
 | 
				
			||||||
	{ "bindsym", cmd_bindsym },
 | 
						{ "bindsym", cmd_bindsym, CMD_ANYTIME },
 | 
				
			||||||
	{ "default_orientation", cmd_orientation },
 | 
						{ "default_orientation", cmd_orientation, CMD_ANYTIME},
 | 
				
			||||||
	{ "exec", cmd_exec },
 | 
						{ "exec", cmd_exec, CMD_COMPOSITOR_READY },
 | 
				
			||||||
	{ "exec_always", cmd_exec_always },
 | 
						{ "exec_always", cmd_exec_always, CMD_COMPOSITOR_READY },
 | 
				
			||||||
	{ "exit", cmd_exit },
 | 
						{ "exit", cmd_exit, CMD_KEYBIND },
 | 
				
			||||||
	{ "floating", cmd_floating },
 | 
						{ "floating", cmd_floating, CMD_KEYBIND },
 | 
				
			||||||
	{ "floating_modifier", cmd_floating_mod },
 | 
						{ "floating_modifier", cmd_floating_mod, CMD_ANYTIME },
 | 
				
			||||||
	{ "focus", cmd_focus },
 | 
						{ "focus", cmd_focus, CMD_KEYBIND },
 | 
				
			||||||
	{ "focus_follows_mouse", cmd_focus_follows_mouse },
 | 
						{ "focus_follows_mouse", cmd_focus_follows_mouse, CMD_ANYTIME },
 | 
				
			||||||
	{ "fullscreen", cmd_fullscreen },
 | 
						{ "fullscreen", cmd_fullscreen, CMD_KEYBIND },
 | 
				
			||||||
	{ "gaps", cmd_gaps },
 | 
						{ "gaps", cmd_gaps, CMD_ANYTIME },
 | 
				
			||||||
	{ "kill", cmd_kill },
 | 
						{ "kill", cmd_kill, CMD_KEYBIND },
 | 
				
			||||||
	{ "layout", cmd_layout },
 | 
						{ "layout", cmd_layout, CMD_KEYBIND },
 | 
				
			||||||
	{ "log_colors", cmd_log_colors },
 | 
						{ "log_colors", cmd_log_colors, CMD_ANYTIME },
 | 
				
			||||||
	{ "move", cmd_move},
 | 
						{ "move", cmd_move, CMD_KEYBIND },
 | 
				
			||||||
	{ "output", cmd_output},
 | 
						{ "output", cmd_output, CMD_ANYTIME },
 | 
				
			||||||
	{ "reload", cmd_reload },
 | 
						{ "reload", cmd_reload, CMD_KEYBIND },
 | 
				
			||||||
	{ "resize", cmd_resize },
 | 
						{ "resize", cmd_resize, CMD_KEYBIND },
 | 
				
			||||||
	{ "scratchpad", cmd_scratchpad },
 | 
						{ "scratchpad", cmd_scratchpad, CMD_KEYBIND },
 | 
				
			||||||
	{ "set", cmd_set },
 | 
						{ "set", cmd_set, CMD_ANYTIME },
 | 
				
			||||||
	{ "split", cmd_split },
 | 
						{ "split", cmd_split, CMD_KEYBIND },
 | 
				
			||||||
	{ "splith", cmd_splith },
 | 
						{ "splith", cmd_splith, CMD_KEYBIND },
 | 
				
			||||||
	{ "splitv", cmd_splitv },
 | 
						{ "splitv", cmd_splitv, CMD_KEYBIND },
 | 
				
			||||||
	{ "workspace", cmd_workspace },
 | 
						{ "workspace", cmd_workspace, CMD_COMPOSITOR_READY },
 | 
				
			||||||
	{ "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth }
 | 
						{ "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth, CMD_ANYTIME },
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static char **split_directive(char *line, int *argc) {
 | 
					static char **split_directive(char *line, int *argc) {
 | 
				
			||||||
| 
						 | 
					@ -945,9 +945,11 @@ static int handler_compare(const void *_a, const void *_b) {
 | 
				
			||||||
	return strcasecmp(a->command, b->command);
 | 
						return strcasecmp(a->command, b->command);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static struct cmd_handler *find_handler(struct cmd_handler handlers[], int l, char *line) {
 | 
					struct cmd_handler *find_handler(char *line) {
 | 
				
			||||||
	struct cmd_handler d = { .command=line };
 | 
						struct cmd_handler d = { .command=line };
 | 
				
			||||||
	struct cmd_handler *res = bsearch(&d, handlers, l, sizeof(struct cmd_handler), handler_compare);
 | 
						struct cmd_handler *res = bsearch(&d, handlers,
 | 
				
			||||||
 | 
								sizeof(handlers) / sizeof(struct cmd_handler),
 | 
				
			||||||
 | 
								sizeof(struct cmd_handler), handler_compare);
 | 
				
			||||||
	return res;
 | 
						return res;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -964,7 +966,7 @@ bool handle_command(struct sway_config *config, char *exec) {
 | 
				
			||||||
		strncpy(cmd, exec, index);
 | 
							strncpy(cmd, exec, index);
 | 
				
			||||||
		cmd[index] = '\0';
 | 
							cmd[index] = '\0';
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	struct cmd_handler *handler = find_handler(handlers, sizeof(handlers) / sizeof(struct cmd_handler), cmd);
 | 
						struct cmd_handler *handler = find_handler(cmd);
 | 
				
			||||||
	if (handler == NULL) {
 | 
						if (handler == NULL) {
 | 
				
			||||||
		sway_log(L_ERROR, "Unknown command '%s'", cmd);
 | 
							sway_log(L_ERROR, "Unknown command '%s'", cmd);
 | 
				
			||||||
		exec_success = false; // TODO: return error, probably
 | 
							exec_success = false; // TODO: return error, probably
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -218,19 +218,23 @@ bool read_config(FILE *file, bool is_active) {
 | 
				
			||||||
		// Any command which would require wlc to be initialized
 | 
							// Any command which would require wlc to be initialized
 | 
				
			||||||
		// should be queued for later execution
 | 
							// should be queued for later execution
 | 
				
			||||||
		list_t *args = split_string(line, " ");
 | 
							list_t *args = split_string(line, " ");
 | 
				
			||||||
		if (!is_active && (
 | 
							struct cmd_handler *handler;
 | 
				
			||||||
			strcmp("exec", args->items[0]) == 0 ||
 | 
							if ((handler = find_handler(args->items[0]))) {
 | 
				
			||||||
			strcmp("exec_always", args->items[0]) == 0 )) {
 | 
								if (handler->config_type == CMD_KEYBIND) {
 | 
				
			||||||
			sway_log(L_DEBUG, "Deferring command %s", line);
 | 
									sway_log(L_ERROR, "Invalid command during config ``%s''", line);
 | 
				
			||||||
 | 
								} else if (handler->config_type == CMD_COMPOSITOR_READY && !is_active) {
 | 
				
			||||||
 | 
									sway_log(L_DEBUG, "Deferring command ``%s''", line);
 | 
				
			||||||
				char *cmd = malloc(strlen(line) + 1);
 | 
									char *cmd = malloc(strlen(line) + 1);
 | 
				
			||||||
				strcpy(cmd, line);
 | 
									strcpy(cmd, line);
 | 
				
			||||||
				list_add(temp_config->cmd_queue, cmd);
 | 
									list_add(temp_config->cmd_queue, cmd);
 | 
				
			||||||
			} else if (!temp_depth && !handle_command(temp_config, line)) {
 | 
								} else if (!temp_depth && !handle_command(temp_config, line)) {
 | 
				
			||||||
			sway_log(L_DEBUG, "Config load failed for line %s", line);
 | 
									sway_log(L_DEBUG, "Config load failed for line ``%s''", line);
 | 
				
			||||||
				success = false;
 | 
									success = false;
 | 
				
			||||||
				temp_config->failed = true;
 | 
									temp_config->failed = true;
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								sway_log(L_ERROR, "Invalid command ``%s''", line);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		free_flat_list(args);
 | 
							free_flat_list(args);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
_continue:
 | 
					_continue:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue