summaryrefslogtreecommitdiffstats
path: root/swaygrab/main.c
blob: bd64bd93c387512cff239b29839f95db60875172 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include "log.h"
#include "ipc-client.h"

void sway_terminate(void) {
	exit(1);
}

int main(int argc, const char **argv) {
	int capture;
	char *socket_path = NULL;

	init_log(L_INFO);

	static struct option long_options[] = {
		{"capture", no_argument, &capture, 'c'},
		{"version", no_argument, NULL, 'v'},
		{"socket", required_argument, NULL, 's'},
		{0, 0, 0, 0}
	};

	int c;
	while (1) {
		int option_index = 0;
		c = getopt_long(argc, argv, "cvs:", long_options, &option_index);
		if (c == -1) {
			break;
		}
		switch (c) {
		case 0: // Flag
			break;
		case 's': // Socket
			socket_path = strdup(optarg);
			break;
		case 'v':
#if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE
			fprintf(stdout, "sway version %s (%s, branch \"%s\")\n", SWAY_GIT_VERSION, SWAY_VERSION_DATE, SWAY_GIT_BRANCH);
#else
			fprintf(stdout, "version not detected\n");
#endif
			exit(0);
			break;
		}
	}

	if (!socket_path) {
		socket_path = get_socketpath();
		if (!socket_path) {
			sway_abort("Unable to retrieve socket path");
		}
	}

	if (optind >= argc) {
		sway_abort("Expected output file on command line. See `man swaygrab`");
	}

	char *out = argv[optind];
	int socketfd = ipc_open_socket(socket_path);
	free(socket_path);

	close(socketfd);
	free(out);
	return 0;
}