mirror of
				https://github.com/swaywm/sway.git
				synced 2025-11-03 09:01:43 -05:00 
			
		
		
		
	Implement modes
This commit is contained in:
		
							parent
							
								
									741424c4e7
								
							
						
					
					
						commit
						8efee109ad
					
				
					 5 changed files with 74 additions and 2 deletions
				
			
		| 
						 | 
					@ -13,5 +13,6 @@ struct sockaddr_un *ipc_user_sockaddr(void);
 | 
				
			||||||
void ipc_event_workspace(swayc_t *old, swayc_t *new, const char *change);
 | 
					void ipc_event_workspace(swayc_t *old, swayc_t *new, const char *change);
 | 
				
			||||||
void ipc_event_window(swayc_t *window, const char *change);
 | 
					void ipc_event_window(swayc_t *window, const char *change);
 | 
				
			||||||
void ipc_event_barconfig_update(struct bar_config *bar);
 | 
					void ipc_event_barconfig_update(struct bar_config *bar);
 | 
				
			||||||
 | 
					void ipc_event_mode(const char *mode);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -100,6 +100,7 @@ static struct cmd_handler handlers[] = {
 | 
				
			||||||
	{ "exec_always", cmd_exec_always },
 | 
						{ "exec_always", cmd_exec_always },
 | 
				
			||||||
	{ "include", cmd_include },
 | 
						{ "include", cmd_include },
 | 
				
			||||||
	{ "input", cmd_input },
 | 
						{ "input", cmd_input },
 | 
				
			||||||
 | 
						{ "mode", cmd_mode },
 | 
				
			||||||
	{ "output", cmd_output },
 | 
						{ "output", cmd_output },
 | 
				
			||||||
	{ "seat", cmd_seat },
 | 
						{ "seat", cmd_seat },
 | 
				
			||||||
	{ "workspace", cmd_workspace },
 | 
						{ "workspace", cmd_workspace },
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										59
									
								
								sway/commands/mode.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								sway/commands/mode.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,59 @@
 | 
				
			||||||
 | 
					#define _XOPEN_SOURCE 500
 | 
				
			||||||
 | 
					#include <stdbool.h>
 | 
				
			||||||
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					#include <strings.h>
 | 
				
			||||||
 | 
					#include "sway/commands.h"
 | 
				
			||||||
 | 
					#include "sway/config.h"
 | 
				
			||||||
 | 
					#include "sway/ipc-server.h"
 | 
				
			||||||
 | 
					#include "list.h"
 | 
				
			||||||
 | 
					#include "log.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					struct cmd_results *cmd_mode(int argc, char **argv) {
 | 
				
			||||||
 | 
						struct cmd_results *error = NULL;
 | 
				
			||||||
 | 
						if ((error = checkarg(argc, "mode", EXPECTED_AT_LEAST, 1))) {
 | 
				
			||||||
 | 
							return error;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						const char *mode_name = argv[0];
 | 
				
			||||||
 | 
						bool new_mode = (argc == 2 && strcmp(argv[1], "{") == 0);
 | 
				
			||||||
 | 
						if (new_mode && !config->reading) {
 | 
				
			||||||
 | 
							return cmd_results_new(CMD_FAILURE,
 | 
				
			||||||
 | 
									"mode", "Can only be used in config file.");
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						struct sway_mode *mode = NULL;
 | 
				
			||||||
 | 
						// Find mode
 | 
				
			||||||
 | 
						for (int i = 0; i < config->modes->length; ++i) {
 | 
				
			||||||
 | 
							struct sway_mode *test = config->modes->items[i];
 | 
				
			||||||
 | 
							if (strcasecmp(test->name, mode_name) == 0) {
 | 
				
			||||||
 | 
								mode = test;
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// Create mode if it doesn't exist
 | 
				
			||||||
 | 
						if (!mode && new_mode) {
 | 
				
			||||||
 | 
							mode = calloc(1, sizeof(struct sway_mode));
 | 
				
			||||||
 | 
							if (!mode) {
 | 
				
			||||||
 | 
								return cmd_results_new(CMD_FAILURE,
 | 
				
			||||||
 | 
										"mode", "Unable to allocate mode");
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							mode->name = strdup(mode_name);
 | 
				
			||||||
 | 
							mode->keysym_bindings = create_list();
 | 
				
			||||||
 | 
							mode->keycode_bindings = create_list();
 | 
				
			||||||
 | 
							list_add(config->modes, mode);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if (!mode) {
 | 
				
			||||||
 | 
							error = cmd_results_new(CMD_INVALID,
 | 
				
			||||||
 | 
									"mode", "Unknown mode `%s'", mode_name);
 | 
				
			||||||
 | 
							return error;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if ((config->reading && new_mode) || (!config->reading && !new_mode)) {
 | 
				
			||||||
 | 
							wlr_log(L_DEBUG, "Switching to mode `%s'",mode->name);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// Set current mode
 | 
				
			||||||
 | 
						config->current_mode = mode;
 | 
				
			||||||
 | 
						if (!new_mode) {
 | 
				
			||||||
 | 
							// trigger IPC mode event
 | 
				
			||||||
 | 
							ipc_event_mode(config->current_mode->name);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return cmd_results_new(new_mode ? CMD_BLOCK_MODE : CMD_SUCCESS, NULL, NULL);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -290,7 +290,7 @@ void ipc_event_window(swayc_t *window, const char *change) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const char *json_string = json_object_to_json_string(obj);
 | 
						const char *json_string = json_object_to_json_string(obj);
 | 
				
			||||||
	ipc_send_event(json_string, IPC_EVENT_WINDOW);
 | 
						ipc_send_event(json_string, IPC_EVENT_WINDOW);
 | 
				
			||||||
	json_object_put(obj); // free
 | 
						json_object_put(obj);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void ipc_event_barconfig_update(struct bar_config *bar) {
 | 
					void ipc_event_barconfig_update(struct bar_config *bar) {
 | 
				
			||||||
| 
						 | 
					@ -299,7 +299,17 @@ void ipc_event_barconfig_update(struct bar_config *bar) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const char *json_string = json_object_to_json_string(json);
 | 
						const char *json_string = json_object_to_json_string(json);
 | 
				
			||||||
	ipc_send_event(json_string, IPC_EVENT_BARCONFIG_UPDATE);
 | 
						ipc_send_event(json_string, IPC_EVENT_BARCONFIG_UPDATE);
 | 
				
			||||||
	json_object_put(json); // free
 | 
						json_object_put(json);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void ipc_event_mode(const char *mode) {
 | 
				
			||||||
 | 
						wlr_log(L_DEBUG, "Sending mode::%s event", mode);
 | 
				
			||||||
 | 
						json_object *obj = json_object_new_object();
 | 
				
			||||||
 | 
						json_object_object_add(obj, "change", json_object_new_string(mode));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						const char *json_string = json_object_to_json_string(obj);
 | 
				
			||||||
 | 
						ipc_send_event(json_string, IPC_EVENT_MODE);
 | 
				
			||||||
 | 
						json_object_put(obj);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data) {
 | 
					int ipc_client_handle_writable(int client_fd, uint32_t mask, void *data) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -16,6 +16,7 @@ sway_sources = files(
 | 
				
			||||||
	'commands/include.c',
 | 
						'commands/include.c',
 | 
				
			||||||
	'commands/input.c',
 | 
						'commands/input.c',
 | 
				
			||||||
	'commands/layout.c',
 | 
						'commands/layout.c',
 | 
				
			||||||
 | 
						'commands/mode.c',
 | 
				
			||||||
	'commands/seat.c',
 | 
						'commands/seat.c',
 | 
				
			||||||
	'commands/seat/attach.c',
 | 
						'commands/seat/attach.c',
 | 
				
			||||||
	'commands/seat/fallback.c',
 | 
						'commands/seat/fallback.c',
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue