mirror of
				https://github.com/swaywm/sway.git
				synced 2025-11-03 09:01:43 -05:00 
			
		
		
		
	Merge pull request #349 from mikkeloscar/swaygrab-auto-output
swaygrab: make focused output default
This commit is contained in:
		
						commit
						ef44b3cf59
					
				
					 2 changed files with 47 additions and 8 deletions
				
			
		| 
						 | 
					@ -1,9 +1,14 @@
 | 
				
			||||||
 | 
					include_directories(
 | 
				
			||||||
 | 
						${JSONC_INCLUDE_DIRS}
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
add_executable(swaygrab
 | 
					add_executable(swaygrab
 | 
				
			||||||
	main.c
 | 
						main.c
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
target_link_libraries(swaygrab
 | 
					target_link_libraries(swaygrab
 | 
				
			||||||
	sway-common
 | 
						sway-common
 | 
				
			||||||
 | 
						${JSONC_LIBRARIES}
 | 
				
			||||||
	rt
 | 
						rt
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,6 +6,7 @@
 | 
				
			||||||
#include <stdint.h>
 | 
					#include <stdint.h>
 | 
				
			||||||
#include <math.h>
 | 
					#include <math.h>
 | 
				
			||||||
#include <time.h>
 | 
					#include <time.h>
 | 
				
			||||||
 | 
					#include <json-c/json.h>
 | 
				
			||||||
#include "log.h"
 | 
					#include "log.h"
 | 
				
			||||||
#include "ipc-client.h"
 | 
					#include "ipc-client.h"
 | 
				
			||||||
#include "util.h"
 | 
					#include "util.h"
 | 
				
			||||||
| 
						 | 
					@ -110,9 +111,34 @@ void grab_and_apply_movie_magic(const char *file, const char *output,
 | 
				
			||||||
	free(cmd);
 | 
						free(cmd);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					char *get_focused_output(int socketfd) {
 | 
				
			||||||
 | 
						uint32_t len = 0;
 | 
				
			||||||
 | 
						char *res = ipc_single_command(socketfd, IPC_GET_WORKSPACES, NULL, &len);
 | 
				
			||||||
 | 
						json_object *workspaces = json_tokener_parse(res);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						int length = json_object_array_length(workspaces);
 | 
				
			||||||
 | 
						json_object *workspace, *focused, *json_output;
 | 
				
			||||||
 | 
						char *output = NULL;
 | 
				
			||||||
 | 
						int i;
 | 
				
			||||||
 | 
						for (i = 0; i < length; ++i) {
 | 
				
			||||||
 | 
							workspace = json_object_array_get_idx(workspaces, i);
 | 
				
			||||||
 | 
							json_object_object_get_ex(workspace, "focused", &focused);
 | 
				
			||||||
 | 
							if (json_object_get_boolean(focused) == TRUE) {
 | 
				
			||||||
 | 
								json_object_object_get_ex(workspace, "output", &json_output);
 | 
				
			||||||
 | 
								output = strdup(json_object_get_string(json_output));
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						json_object_put(workspaces);
 | 
				
			||||||
 | 
						free(res);
 | 
				
			||||||
 | 
						return output;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int main(int argc, char **argv) {
 | 
					int main(int argc, char **argv) {
 | 
				
			||||||
	static int capture = 0, raw = 0;
 | 
						static int capture = 0, raw = 0;
 | 
				
			||||||
	char *socket_path = NULL;
 | 
						char *socket_path = NULL;
 | 
				
			||||||
 | 
						char *output = NULL;
 | 
				
			||||||
	int framerate = 30;
 | 
						int framerate = 30;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	init_log(L_INFO);
 | 
						init_log(L_INFO);
 | 
				
			||||||
| 
						 | 
					@ -120,6 +146,7 @@ int main(int argc, char **argv) {
 | 
				
			||||||
	static struct option long_options[] = {
 | 
						static struct option long_options[] = {
 | 
				
			||||||
		{"help", no_argument, NULL, 'h'},
 | 
							{"help", no_argument, NULL, 'h'},
 | 
				
			||||||
		{"capture", no_argument, NULL, 'c'},
 | 
							{"capture", no_argument, NULL, 'c'},
 | 
				
			||||||
 | 
							{"output", required_argument, NULL, 'o'},
 | 
				
			||||||
		{"version", no_argument, NULL, 'v'},
 | 
							{"version", no_argument, NULL, 'v'},
 | 
				
			||||||
		{"socket", required_argument, NULL, 's'},
 | 
							{"socket", required_argument, NULL, 's'},
 | 
				
			||||||
		{"raw", no_argument, NULL, 'r'},
 | 
							{"raw", no_argument, NULL, 'r'},
 | 
				
			||||||
| 
						 | 
					@ -128,10 +155,11 @@ int main(int argc, char **argv) {
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const char *usage =
 | 
						const char *usage =
 | 
				
			||||||
		"Usage: swaygrab [options] <output> [file]\n"
 | 
							"Usage: swaygrab [options] [file]\n"
 | 
				
			||||||
		"\n"
 | 
							"\n"
 | 
				
			||||||
		"  -h, --help             Show help message and quit.\n"
 | 
							"  -h, --help             Show help message and quit.\n"
 | 
				
			||||||
		"  -c, --capture          Capture video.\n"
 | 
							"  -c, --capture          Capture video.\n"
 | 
				
			||||||
 | 
							"  -o, --output <output>  Output source.\n"
 | 
				
			||||||
		"  -v, --version          Show the version number and quit.\n"
 | 
							"  -v, --version          Show the version number and quit.\n"
 | 
				
			||||||
		"  -s, --socket <socket>  Use the specified socket.\n"
 | 
							"  -s, --socket <socket>  Use the specified socket.\n"
 | 
				
			||||||
		"  -R, --rate <rate>      Specify framerate (default: 30)\n"
 | 
							"  -R, --rate <rate>      Specify framerate (default: 30)\n"
 | 
				
			||||||
| 
						 | 
					@ -140,7 +168,7 @@ int main(int argc, char **argv) {
 | 
				
			||||||
	int c;
 | 
						int c;
 | 
				
			||||||
	while (1) {
 | 
						while (1) {
 | 
				
			||||||
		int option_index = 0;
 | 
							int option_index = 0;
 | 
				
			||||||
		c = getopt_long(argc, argv, "hcvs:r", long_options, &option_index);
 | 
							c = getopt_long(argc, argv, "hco:vs:r", long_options, &option_index);
 | 
				
			||||||
		if (c == -1) {
 | 
							if (c == -1) {
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
| 
						 | 
					@ -151,6 +179,9 @@ int main(int argc, char **argv) {
 | 
				
			||||||
		case 'r':
 | 
							case 'r':
 | 
				
			||||||
			raw = 1;
 | 
								raw = 1;
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
 | 
							case 'o': // output
 | 
				
			||||||
 | 
								output = strdup(optarg);
 | 
				
			||||||
 | 
								break;
 | 
				
			||||||
		case 'c':
 | 
							case 'c':
 | 
				
			||||||
			capture = 1;
 | 
								capture = 1;
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
| 
						 | 
					@ -178,29 +209,32 @@ int main(int argc, char **argv) {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	char *file = NULL, *output = NULL;
 | 
						char *file = NULL;
 | 
				
			||||||
	if (raw) {
 | 
						if (raw) {
 | 
				
			||||||
		if (optind >= argc) {
 | 
							if (optind >= argc + 1) {
 | 
				
			||||||
			sway_abort("Invalid usage. See `man swaygrab` %d %d", argc, optind);
 | 
								sway_abort("Invalid usage. See `man swaygrab` %d %d", argc, optind);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		output = argv[optind];
 | 
					 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		if (optind >= argc - 1) {
 | 
							if (optind >= argc) {
 | 
				
			||||||
			sway_abort("Invalid usage. See `man swaygrab`");
 | 
								sway_abort("Invalid usage. See `man swaygrab`");
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		file = argv[optind + 1];
 | 
							file = argv[optind];
 | 
				
			||||||
		output = argv[optind];
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	int socketfd = ipc_open_socket(socket_path);
 | 
						int socketfd = ipc_open_socket(socket_path);
 | 
				
			||||||
	free(socket_path);
 | 
						free(socket_path);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!output) {
 | 
				
			||||||
 | 
							output = get_focused_output(socketfd);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (!capture) {
 | 
						if (!capture) {
 | 
				
			||||||
		grab_and_apply_magick(file, output, socketfd, raw);
 | 
							grab_and_apply_magick(file, output, socketfd, raw);
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		grab_and_apply_movie_magic(file, output, socketfd, raw, framerate);
 | 
							grab_and_apply_movie_magic(file, output, socketfd, raw, framerate);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						free(output);
 | 
				
			||||||
	close(socketfd);
 | 
						close(socketfd);
 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue