mirror of
				https://github.com/swaywm/sway.git
				synced 2025-10-29 05:40:18 -04:00 
			
		
		
		
	Merge pull request #7 from Luminarys/master
Added in reload and exec_always handling.
This commit is contained in:
		
						commit
						c9bce5dd22
					
				
					 9 changed files with 122 additions and 70 deletions
				
			
		|  | @ -39,7 +39,7 @@ int cmd_bindsym(struct sway_config *config, int argc, char **argv) { | ||||||
| 	binding->keys = create_list(); | 	binding->keys = create_list(); | ||||||
| 	binding->modifiers = 0; | 	binding->modifiers = 0; | ||||||
| 	binding->command = join_args(argv + 1, argc - 1); | 	binding->command = join_args(argv + 1, argc - 1); | ||||||
| 	 | 
 | ||||||
| 	list_t *split = split_string(argv[0], "+"); | 	list_t *split = split_string(argv[0], "+"); | ||||||
| 	int i; | 	int i; | ||||||
| 	for (i = 0; i < split->length; ++i) { | 	for (i = 0; i < split->length; ++i) { | ||||||
|  | @ -78,6 +78,28 @@ int cmd_exec(struct sway_config *config, int argc, char **argv) { | ||||||
| 		sway_log(L_ERROR, "Invalid exec command (expected at least 1 argument, got %d)", argc); | 		sway_log(L_ERROR, "Invalid exec command (expected at least 1 argument, got %d)", argc); | ||||||
| 		return 1; | 		return 1; | ||||||
| 	} | 	} | ||||||
|  | 
 | ||||||
|  | 	if (config->reloading) { | ||||||
|  | 		sway_log(L_DEBUG, "Ignoring exec %s due to reload", join_args(argv, argc)); | ||||||
|  | 		return 0; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if (fork() == 0) { | ||||||
|  | 		char *args = join_args(argv, argc); | ||||||
|  | 		sway_log(L_DEBUG, "Executing %s", args); | ||||||
|  | 		execl("/bin/sh", "sh", "-c", args, (char *)NULL); | ||||||
|  | 		free(args); | ||||||
|  | 		exit(0); | ||||||
|  | 	} | ||||||
|  | 	return 0; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | int cmd_exec_always(struct sway_config *config, int argc, char **argv) { | ||||||
|  | 	if (argc < 1) { | ||||||
|  | 		sway_log(L_ERROR, "Invalid exec_always command (expected at least 1 argument, got %d)", argc); | ||||||
|  | 		return 1; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	if (fork() == 0) { | 	if (fork() == 0) { | ||||||
| 		char *args = join_args(argv, argc); | 		char *args = join_args(argv, argc); | ||||||
| 		sway_log(L_DEBUG, "Executing %s", args); | 		sway_log(L_DEBUG, "Executing %s", args); | ||||||
|  | @ -152,6 +174,18 @@ int cmd_layout(struct sway_config *config, int argc, char **argv) { | ||||||
| 	return 0; | 	return 0; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | int cmd_reload(struct sway_config *config, int argc, char **argv) { | ||||||
|  | 	if (argc != 0) { | ||||||
|  | 		sway_log(L_ERROR, "Invalid reload command (expected 0 arguments, got %d)", argc); | ||||||
|  | 		return 1; | ||||||
|  | 	} | ||||||
|  | 	if (!load_config()) { | ||||||
|  | 		return 1;	 | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return 0; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| int cmd_set(struct sway_config *config, int argc, char **argv) { | int cmd_set(struct sway_config *config, int argc, char **argv) { | ||||||
| 	if (argc != 2) { | 	if (argc != 2) { | ||||||
| 		sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc); | 		sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc); | ||||||
|  | @ -232,12 +266,14 @@ int cmd_fullscreen(struct sway_config *config, int argc, char **argv) { | ||||||
| struct cmd_handler handlers[] = { | struct cmd_handler handlers[] = { | ||||||
| 	{ "bindsym", cmd_bindsym }, | 	{ "bindsym", cmd_bindsym }, | ||||||
| 	{ "exec", cmd_exec }, | 	{ "exec", cmd_exec }, | ||||||
|  | 	{ "exec_always", cmd_exec_always }, | ||||||
| 	{ "exit", cmd_exit }, | 	{ "exit", cmd_exit }, | ||||||
| 	{ "focus", cmd_focus }, | 	{ "focus", cmd_focus }, | ||||||
| 	{ "focus_follows_mouse", cmd_focus_follows_mouse }, | 	{ "focus_follows_mouse", cmd_focus_follows_mouse }, | ||||||
| 	{ "fullscreen", cmd_fullscreen }, | 	{ "fullscreen", cmd_fullscreen }, | ||||||
| 	{ "layout", cmd_layout }, | 	{ "layout", cmd_layout }, | ||||||
| 	{ "log_colors", cmd_log_colors }, | 	{ "log_colors", cmd_log_colors }, | ||||||
|  | 	{ "reload", cmd_reload }, | ||||||
| 	{ "set", cmd_set }, | 	{ "set", cmd_set }, | ||||||
| 	{ "splith", cmd_splith }, | 	{ "splith", cmd_splith }, | ||||||
| 	{ "splitv", cmd_splitv } | 	{ "splitv", cmd_splitv } | ||||||
|  |  | ||||||
|  | @ -4,8 +4,8 @@ | ||||||
| #include "config.h" | #include "config.h" | ||||||
| 
 | 
 | ||||||
| struct cmd_handler { | struct cmd_handler { | ||||||
|     char *command; | 	char *command; | ||||||
|     int (*handle)(struct sway_config *config, int argc, char **argv); | 	int (*handle)(struct sway_config *config, int argc, char **argv); | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| int handle_command(struct sway_config *config, char *command); | int handle_command(struct sway_config *config, char *command); | ||||||
|  |  | ||||||
|  | @ -8,6 +8,25 @@ | ||||||
| #include "commands.h" | #include "commands.h" | ||||||
| #include "config.h" | #include "config.h" | ||||||
| 
 | 
 | ||||||
|  | bool load_config() { | ||||||
|  | 	// TODO: Allow use of more config file locations
 | ||||||
|  | 	const char *name = "/.sway/config"; | ||||||
|  | 	const char *home = getenv("HOME"); | ||||||
|  | 	char *temp = malloc(strlen(home) + strlen(name) + 1); | ||||||
|  | 	strcpy(temp, home); | ||||||
|  | 	strcat(temp, name); | ||||||
|  | 	FILE *f = fopen(temp, "r"); | ||||||
|  | 	if (!f) { | ||||||
|  | 		fprintf(stderr, "Unable to open %s for reading", temp); | ||||||
|  | 		free(temp); | ||||||
|  | 		return false; | ||||||
|  | 	} | ||||||
|  | 	free(temp); | ||||||
|  | 	config = read_config(f, false); | ||||||
|  | 	fclose(f); | ||||||
|  | 	return true; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| void config_defaults(struct sway_config *config) { | void config_defaults(struct sway_config *config) { | ||||||
| 	config->symbols = create_list(); | 	config->symbols = create_list(); | ||||||
| 	config->modes = create_list(); | 	config->modes = create_list(); | ||||||
|  | @ -18,12 +37,17 @@ void config_defaults(struct sway_config *config) { | ||||||
| 	// Flags
 | 	// Flags
 | ||||||
| 	config->focus_follows_mouse = true; | 	config->focus_follows_mouse = true; | ||||||
| 	config->mouse_warping = true; | 	config->mouse_warping = true; | ||||||
|  | 	config->reloading = false; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| struct sway_config *read_config(FILE *file) { | struct sway_config *read_config(FILE *file, bool is_active) { | ||||||
| 	struct sway_config *config = malloc(sizeof(struct sway_config)); | 	struct sway_config *config = malloc(sizeof(struct sway_config)); | ||||||
| 	config_defaults(config); | 	config_defaults(config); | ||||||
| 
 | 
 | ||||||
|  | 	if (is_active) { | ||||||
|  | 		config->reloading = true; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	bool success = true; | 	bool success = true; | ||||||
| 
 | 
 | ||||||
| 	int temp_depth = 0; // Temporary: skip all config sections with depth
 | 	int temp_depth = 0; // Temporary: skip all config sections with depth
 | ||||||
|  | @ -44,7 +68,7 @@ struct sway_config *read_config(FILE *file) { | ||||||
| 		if (!temp_depth && handle_command(config, line) != 0) { | 		if (!temp_depth && handle_command(config, line) != 0) { | ||||||
| 			success = false; | 			success = false; | ||||||
| 		} | 		} | ||||||
| 		 | 
 | ||||||
| _continue: | _continue: | ||||||
| 		if (line && line[strlen(line) - 1] == '{') { | 		if (line && line[strlen(line) - 1] == '{') { | ||||||
| 			temp_depth++; | 			temp_depth++; | ||||||
|  | @ -56,6 +80,10 @@ _continue: | ||||||
| 		exit(1); | 		exit(1); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	if (is_active) { | ||||||
|  | 		config->reloading = true; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	return config; | 	return config; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -6,32 +6,35 @@ | ||||||
| #include "list.h" | #include "list.h" | ||||||
| 
 | 
 | ||||||
| struct sway_variable { | struct sway_variable { | ||||||
|     char *name; | 	char *name; | ||||||
|     char *value; | 	char *value; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| struct sway_binding { | struct sway_binding { | ||||||
|     list_t *keys; | 	list_t *keys; | ||||||
|     uint32_t modifiers; | 	uint32_t modifiers; | ||||||
|     char *command; | 	char *command; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| struct sway_mode { | struct sway_mode { | ||||||
|     char *name; | 	char *name; | ||||||
|     list_t *bindings; | 	list_t *bindings; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| struct sway_config { | struct sway_config { | ||||||
|     list_t *symbols; | 	list_t *symbols; | ||||||
|     list_t *modes; | 	list_t *modes; | ||||||
|     struct sway_mode *current_mode; | 	struct sway_mode *current_mode; | ||||||
| 
 | 
 | ||||||
|     // Flags
 | 	// Flags
 | ||||||
|     bool focus_follows_mouse; | 	bool focus_follows_mouse; | ||||||
|     bool mouse_warping; | 	bool mouse_warping; | ||||||
|  | 	 | ||||||
|  | 	bool reloading; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| struct sway_config *read_config(FILE *file); | bool load_config(); | ||||||
|  | struct sway_config *read_config(FILE *file, bool is_active); | ||||||
| char *do_var_replacement(struct sway_config *config, char *str); | char *do_var_replacement(struct sway_config *config, char *str); | ||||||
| 
 | 
 | ||||||
| extern struct sway_config *config; | extern struct sway_config *config; | ||||||
|  |  | ||||||
|  | @ -5,38 +5,38 @@ | ||||||
| #include "list.h" | #include "list.h" | ||||||
| 
 | 
 | ||||||
| struct sway_container { | struct sway_container { | ||||||
|     wlc_handle handle; | 	wlc_handle handle; | ||||||
| 
 | 
 | ||||||
|     enum { | 	enum { | ||||||
|         C_ROOT, | 		C_ROOT, | ||||||
|         C_OUTPUT, | 		C_OUTPUT, | ||||||
|         C_WORKSPACE, | 		C_WORKSPACE, | ||||||
|         C_CONTAINER, | 		C_CONTAINER, | ||||||
|         C_VIEW | 		C_VIEW | ||||||
|     } type; | 	} type; | ||||||
| 
 | 
 | ||||||
|     enum { | 	enum { | ||||||
|         L_NONE, | 		L_NONE, | ||||||
|         L_HORIZ, | 		L_HORIZ, | ||||||
|         L_VERT, | 		L_VERT, | ||||||
|         L_STACKED, | 		L_STACKED, | ||||||
|         L_TABBED, | 		L_TABBED, | ||||||
|         L_FLOATING | 		L_FLOATING | ||||||
|     } layout; | 	} layout; | ||||||
| 
 | 
 | ||||||
|     // Not including borders or margins
 | 	// Not including borders or margins
 | ||||||
|     int width, height; | 	int width, height; | ||||||
| 
 | 
 | ||||||
|     int x, y; | 	int x, y; | ||||||
| 
 | 
 | ||||||
|     int weight; | 	int weight; | ||||||
| 
 | 
 | ||||||
|     char *name; | 	char *name; | ||||||
| 
 | 
 | ||||||
|     list_t *children; | 	list_t *children; | ||||||
| 
 | 
 | ||||||
|     struct sway_container *parent; | 	struct sway_container *parent; | ||||||
|     struct sway_container *focused; | 	struct sway_container *focused; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| typedef struct sway_container swayc_t; | typedef struct sway_container swayc_t; | ||||||
|  |  | ||||||
|  | @ -2,9 +2,9 @@ | ||||||
| #define _SWAY_LIST_H | #define _SWAY_LIST_H | ||||||
| 
 | 
 | ||||||
| typedef struct { | typedef struct { | ||||||
|     int capacity; | 	int capacity; | ||||||
|     int length; | 	int length; | ||||||
|     void **items; | 	void **items; | ||||||
| } list_t; | } list_t; | ||||||
| 
 | 
 | ||||||
| list_t *create_list(); | list_t *create_list(); | ||||||
|  |  | ||||||
|  | @ -18,7 +18,7 @@ void init_log(int verbosity) { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void sway_log_colors(int mode) { | void sway_log_colors(int mode) { | ||||||
|     colored = (mode == 1) ? 1 : 0; | 	colored = (mode == 1) ? 1 : 0; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void sway_abort(char *format, ...) { | void sway_abort(char *format, ...) { | ||||||
|  |  | ||||||
|  | @ -2,10 +2,10 @@ | ||||||
| #define _SWAY_LOG_H | #define _SWAY_LOG_H | ||||||
| 
 | 
 | ||||||
| typedef enum { | typedef enum { | ||||||
|     L_SILENT = 0, | 	L_SILENT = 0, | ||||||
|     L_ERROR = 1, | 	L_ERROR = 1, | ||||||
|     L_INFO = 2, | 	L_INFO = 2, | ||||||
|     L_DEBUG = 3, | 	L_DEBUG = 3, | ||||||
| } log_importance_t; | } log_importance_t; | ||||||
| 
 | 
 | ||||||
| void init_log(int verbosity); | void init_log(int verbosity); | ||||||
|  |  | ||||||
							
								
								
									
										23
									
								
								sway/main.c
									
										
									
									
									
								
							
							
						
						
									
										23
									
								
								sway/main.c
									
										
									
									
									
								
							|  | @ -9,24 +9,6 @@ | ||||||
| 
 | 
 | ||||||
| struct sway_config *config; | struct sway_config *config; | ||||||
| 
 | 
 | ||||||
| void load_config() { |  | ||||||
| 	// TODO: Allow use of more config file locations
 |  | ||||||
| 	const char *name = "/.sway/config"; |  | ||||||
| 	const char *home = getenv("HOME"); |  | ||||||
| 	char *temp = malloc(strlen(home) + strlen(name) + 1); |  | ||||||
| 	strcpy(temp, home); |  | ||||||
| 	strcat(temp, name); |  | ||||||
| 	FILE *f = fopen(temp, "r"); |  | ||||||
| 	if (!f) { |  | ||||||
| 		fprintf(stderr, "Unable to open %s for reading", temp); |  | ||||||
| 		free(temp); |  | ||||||
| 		exit(1); |  | ||||||
| 	} |  | ||||||
| 	free(temp); |  | ||||||
| 	config = read_config(f); |  | ||||||
| 	fclose(f); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| int main(int argc, char **argv) { | int main(int argc, char **argv) { | ||||||
| 	init_log(L_DEBUG); // TODO: Control this with command line arg
 | 	init_log(L_DEBUG); // TODO: Control this with command line arg
 | ||||||
| 	init_layout(); | 	init_layout(); | ||||||
|  | @ -52,6 +34,7 @@ int main(int argc, char **argv) { | ||||||
| 			.motion = handle_pointer_motion, | 			.motion = handle_pointer_motion, | ||||||
| 			.button = handle_pointer_button | 			.button = handle_pointer_button | ||||||
| 		} | 		} | ||||||
|  | 
 | ||||||
| 	}; | 	}; | ||||||
| 
 | 
 | ||||||
| 	setenv("WLC_DIM", "0", 0); | 	setenv("WLC_DIM", "0", 0); | ||||||
|  | @ -60,7 +43,9 @@ int main(int argc, char **argv) { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	setenv("DISPLAY", ":1", 1); | 	setenv("DISPLAY", ":1", 1); | ||||||
| 	load_config(); | 	if (load_config()) { | ||||||
|  | 		exit(1); | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
| 	wlc_run(); | 	wlc_run(); | ||||||
| 	return 0; | 	return 0; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Drew DeVault
						Drew DeVault