mirror of
				https://github.com/swaywm/sway.git
				synced 2025-10-29 05:40:18 -04:00 
			
		
		
		
	Move IPC client into common, refactor IPC
This commit is contained in:
		
							parent
							
								
									5ae359279b
								
							
						
					
					
						commit
						27f03c705d
					
				
					 8 changed files with 105 additions and 79 deletions
				
			
		|  | @ -7,80 +7,14 @@ | |||
| #include <sys/socket.h> | ||||
| #include <unistd.h> | ||||
| #include "stringop.h" | ||||
| #include "ipc.h" | ||||
| #include "ipc-client.h" | ||||
| #include "readline.h" | ||||
| #include "log.h" | ||||
| 
 | ||||
| static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; | ||||
| static const size_t ipc_header_size = sizeof(ipc_magic)+8; | ||||
| 
 | ||||
| void sway_terminate(void) { | ||||
| 	exit(1); | ||||
| } | ||||
| 
 | ||||
| char *get_socketpath(void) { | ||||
| 	FILE *fp = popen("sway --get-socketpath", "r"); | ||||
| 	if (!fp) { | ||||
| 		return NULL; | ||||
| 	} | ||||
| 	char *line = read_line(fp); | ||||
| 	pclose(fp); | ||||
| 	return line; | ||||
| } | ||||
| 
 | ||||
| char *do_ipc(const char *socket_path, uint32_t type, const char *payload, uint32_t len) { | ||||
| 	struct sockaddr_un addr; | ||||
| 	int socketfd; | ||||
| 	if ((socketfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { | ||||
| 		sway_abort("Unable to open Unix socket"); | ||||
| 	} | ||||
| 	addr.sun_family = AF_UNIX; | ||||
| 	strcpy(addr.sun_path, socket_path); | ||||
| 	int l = sizeof(addr.sun_family) + strlen(addr.sun_path); | ||||
| 	if (connect(socketfd, (struct sockaddr *)&addr, l) == -1) { | ||||
| 		sway_abort("Unable to connect to %s", socket_path); | ||||
| 	} | ||||
| 
 | ||||
| 	char data[ipc_header_size]; | ||||
| 	uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic)); | ||||
| 	memcpy(data, ipc_magic, sizeof(ipc_magic)); | ||||
| 	data32[0] = len; | ||||
| 	data32[1] = type; | ||||
| 
 | ||||
| 	if (write(socketfd, data, ipc_header_size) == -1) { | ||||
| 		sway_abort("Unable to send IPC header"); | ||||
| 	} | ||||
| 
 | ||||
| 	if (write(socketfd, payload, len) == -1) { | ||||
| 		sway_abort("Unable to send IPC payload"); | ||||
| 	} | ||||
| 
 | ||||
| 	size_t total = 0; | ||||
| 	while (total < ipc_header_size) { | ||||
| 		ssize_t received = recv(socketfd, data + total, ipc_header_size - total, 0); | ||||
| 		if (received < 0) { | ||||
| 			sway_abort("Unable to receive IPC response"); | ||||
| 		} | ||||
| 		total += received; | ||||
| 	} | ||||
| 
 | ||||
| 	total = 0; | ||||
| 	len = data32[0]; | ||||
| 	char *response = malloc(len + 1); | ||||
| 	while (total < len) { | ||||
| 		ssize_t received = recv(socketfd, response + total, len - total, 0); | ||||
| 		if (received < 0) { | ||||
| 			sway_abort("Unable to receive IPC response"); | ||||
| 		} | ||||
| 		total += received; | ||||
| 	} | ||||
| 	response[len] = '\0'; | ||||
| 
 | ||||
| 	close(socketfd); | ||||
| 
 | ||||
| 	return response; | ||||
| } | ||||
| 
 | ||||
| int main(int argc, char **argv) { | ||||
| 	static int quiet = 0; | ||||
| 	char *socket_path = NULL; | ||||
|  | @ -159,7 +93,7 @@ int main(int argc, char **argv) { | |||
| 		command = join_args(argv + optind, argc - optind); | ||||
| 	} | ||||
| 
 | ||||
| 	char *resp = do_ipc(socket_path, type, command, strlen(command)); | ||||
| 	char *resp = ipc_single_command(socket_path, type, command, strlen(command)); | ||||
| 	if (!quiet) { | ||||
| 		printf("%s", resp); | ||||
| 	} | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Drew DeVault
						Drew DeVault