2017-06-05 21:44:29 +10:00
|
|
|
#define _XOPEN_SOURCE 700
|
2017-03-11 15:41:24 +11:00
|
|
|
#define _POSIX_C_SOURCE 199309L
|
2015-11-28 01:53:50 +11:00
|
|
|
#include <stdio.h>
|
2016-09-19 07:41:08 +10:00
|
|
|
#include <stdbool.h>
|
2015-11-28 01:53:50 +11:00
|
|
|
#include <stdlib.h>
|
2015-11-28 02:39:18 +11:00
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <unistd.h>
|
2015-11-29 00:49:02 +11:00
|
|
|
#include <stdint.h>
|
2015-11-28 02:39:18 +11:00
|
|
|
#include <math.h>
|
2015-11-28 06:21:38 +11:00
|
|
|
#include <time.h>
|
2017-06-21 06:03:40 +10:00
|
|
|
#include <sys/wait.h>
|
2015-12-18 02:01:36 +11:00
|
|
|
#include <json-c/json.h>
|
2015-11-28 01:53:50 +11:00
|
|
|
#include "log.h"
|
2015-11-28 02:10:29 +11:00
|
|
|
#include "ipc-client.h"
|
2015-12-15 03:07:31 +11:00
|
|
|
#include "util.h"
|
2016-09-19 07:41:08 +10:00
|
|
|
#include "swaygrab/json.h"
|
2015-11-28 01:53:50 +11:00
|
|
|
|
2016-02-26 19:08:05 +11:00
|
|
|
void sway_terminate(int exit_code) {
|
|
|
|
exit(exit_code);
|
2015-11-28 01:53:50 +11:00
|
|
|
}
|
|
|
|
|
2016-09-19 07:41:08 +10:00
|
|
|
void grab_and_apply_magick(const char *file, const char *payload,
|
2015-11-28 06:21:38 +11:00
|
|
|
int socketfd, int raw) {
|
2016-09-19 07:41:08 +10:00
|
|
|
uint32_t len = strlen(payload);
|
2015-11-28 02:39:18 +11:00
|
|
|
char *pixels = ipc_single_command(socketfd,
|
2016-09-19 07:41:08 +10:00
|
|
|
IPC_SWAY_GET_PIXELS, payload, &len);
|
2015-11-28 02:39:18 +11:00
|
|
|
uint32_t *u32pixels = (uint32_t *)(pixels + 1);
|
|
|
|
uint32_t width = u32pixels[0];
|
|
|
|
uint32_t height = u32pixels[1];
|
2015-11-28 06:21:38 +11:00
|
|
|
len -= 9;
|
2015-11-28 02:39:18 +11:00
|
|
|
pixels += 9;
|
|
|
|
|
|
|
|
if (width == 0 || height == 0) {
|
2016-09-19 07:41:08 +10:00
|
|
|
// indicates geometry was clamped by WLC because it was outside of the output's area
|
|
|
|
json_object *obj = json_tokener_parse(payload);
|
|
|
|
json_object *output;
|
|
|
|
json_object_object_get_ex(obj, "output", &output);
|
|
|
|
const char *name = json_object_get_string(output);
|
|
|
|
json_object_put(obj);
|
|
|
|
sway_abort("Unknown output %s.", name);
|
2015-11-28 02:39:18 +11:00
|
|
|
}
|
|
|
|
|
2015-11-28 06:21:38 +11:00
|
|
|
if (raw) {
|
|
|
|
fwrite(pixels, 1, len, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
free(pixels - 9);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-21 06:03:40 +10:00
|
|
|
char size[10 + 1 + 10 + 2 + 1]; // int32_t are max 10 digits
|
|
|
|
sprintf(size, "%dx%d+0", width, height);
|
2015-11-28 02:39:18 +11:00
|
|
|
|
2017-06-21 06:03:40 +10:00
|
|
|
pid_t child;
|
|
|
|
int fd[2];
|
|
|
|
pipe(fd);
|
|
|
|
|
|
|
|
if ((child = fork()) < 0) {
|
|
|
|
sway_log(L_ERROR, "Swaygrab failed to fork.");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
} else if (child == 0) {
|
|
|
|
close(fd[1]);
|
|
|
|
write(fd[0], pixels, len);
|
|
|
|
free(pixels - 9);
|
|
|
|
waitpid(child, NULL, 0);
|
|
|
|
} else {
|
|
|
|
close(fd[0]);
|
|
|
|
execlp("convert", "-depth", "8", "-size", size, "rgba:-", "-flip", file, NULL);
|
|
|
|
sway_log(L_ERROR, "Swaygrab could not run convert.");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2015-11-28 06:21:38 +11:00
|
|
|
}
|
|
|
|
|
2016-09-19 07:41:08 +10:00
|
|
|
void grab_and_apply_movie_magic(const char *file, const char *payload,
|
2015-11-28 06:21:38 +11:00
|
|
|
int socketfd, int raw, int framerate) {
|
|
|
|
if (raw) {
|
|
|
|
sway_log(L_ERROR, "Raw capture data is not yet supported. Proceeding with ffmpeg normally.");
|
|
|
|
}
|
|
|
|
|
2016-09-19 07:41:08 +10:00
|
|
|
uint32_t len = strlen(payload);
|
2015-11-28 06:21:38 +11:00
|
|
|
char *pixels = ipc_single_command(socketfd,
|
2016-09-19 07:41:08 +10:00
|
|
|
IPC_SWAY_GET_PIXELS, payload, &len);
|
2015-11-28 06:21:38 +11:00
|
|
|
uint32_t *u32pixels = (uint32_t *)(pixels + 1);
|
|
|
|
uint32_t width = u32pixels[0];
|
|
|
|
uint32_t height = u32pixels[1];
|
|
|
|
pixels += 9;
|
|
|
|
|
|
|
|
if (width == 0 || height == 0) {
|
2016-09-19 07:41:08 +10:00
|
|
|
// indicates geometry was clamped by WLC because it was outside of the output's area
|
|
|
|
json_object *obj = json_tokener_parse(payload);
|
|
|
|
json_object *output;
|
|
|
|
json_object_object_get_ex(obj, "output", &output);
|
|
|
|
const char *name = json_object_get_string(output);
|
|
|
|
json_object_put(obj);
|
|
|
|
sway_abort("Unknown output %s.", name);
|
2015-11-28 06:21:38 +11:00
|
|
|
}
|
|
|
|
|
2017-06-18 10:41:09 +10:00
|
|
|
char *ffmpeg_opts = getenv("SWAYGRAB_FFMPEG_OPTS");
|
|
|
|
if(!ffmpeg_opts) {
|
|
|
|
ffmpeg_opts = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *fmt = "ffmpeg %s -f rawvideo -framerate %d "
|
2015-11-28 06:21:38 +11:00
|
|
|
"-video_size %dx%d -pixel_format argb "
|
|
|
|
"-i pipe:0 -r %d -vf vflip %s";
|
|
|
|
char *cmd = malloc(strlen(fmt) - 8 /*args*/
|
2017-06-18 10:41:09 +10:00
|
|
|
+ strlen(ffmpeg_opts) + numlen(width) + numlen(height)
|
|
|
|
+ numlen(framerate) * 2 + strlen(file) + 1);
|
|
|
|
sprintf(cmd, fmt, ffmpeg_opts, framerate, width, height, framerate, file);
|
2015-11-28 06:21:38 +11:00
|
|
|
|
|
|
|
long ns = (long)(1000000000 * (1.0 / framerate));
|
|
|
|
struct timespec start, finish, ts;
|
|
|
|
ts.tv_sec = 0;
|
|
|
|
|
|
|
|
FILE *f = popen(cmd, "w");
|
|
|
|
fwrite(pixels, 1, len, f);
|
|
|
|
free(pixels - 9);
|
|
|
|
int sleep = 0;
|
|
|
|
while (sleep != -1) {
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
2016-09-19 07:41:08 +10:00
|
|
|
len = strlen(payload);
|
2015-11-28 06:21:38 +11:00
|
|
|
pixels = ipc_single_command(socketfd,
|
2016-09-19 07:41:08 +10:00
|
|
|
IPC_SWAY_GET_PIXELS, payload, &len);
|
2015-11-28 06:21:38 +11:00
|
|
|
pixels += 9;
|
|
|
|
len -= 9;
|
|
|
|
|
|
|
|
fwrite(pixels, 1, len, f);
|
|
|
|
|
2015-12-23 15:47:07 +11:00
|
|
|
free(pixels - 9);
|
2015-11-28 06:21:38 +11:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &finish);
|
|
|
|
ts.tv_nsec = ns;
|
|
|
|
double fts = (double)finish.tv_sec + 1.0e-9*finish.tv_nsec;
|
|
|
|
double sts = (double)start.tv_sec + 1.0e-9*start.tv_nsec;
|
|
|
|
long diff = (fts - sts) * 1000000000;
|
|
|
|
ts.tv_nsec = ns - diff;
|
|
|
|
if (ts.tv_nsec < 0) {
|
|
|
|
ts.tv_nsec = 0;
|
|
|
|
}
|
|
|
|
sleep = nanosleep(&ts, NULL);
|
|
|
|
}
|
|
|
|
fflush(f);
|
|
|
|
|
|
|
|
fclose(f);
|
2015-11-28 02:40:28 +11:00
|
|
|
free(cmd);
|
2015-11-28 02:39:18 +11:00
|
|
|
}
|
|
|
|
|
2015-12-18 04:25:25 +11:00
|
|
|
char *default_filename(const char *extension) {
|
|
|
|
int ext_len = strlen(extension);
|
|
|
|
int len = 28 + ext_len; // format: "2015-12-17-180040_swaygrab.ext"
|
|
|
|
char *filename = malloc(len * sizeof(char));
|
|
|
|
time_t t = time(NULL);
|
|
|
|
|
|
|
|
struct tm *lt = localtime(&t);
|
|
|
|
strftime(filename, len, "%Y-%m-%d-%H%M%S_swaygrab.", lt);
|
|
|
|
strncat(filename, extension, ext_len);
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2015-11-28 02:39:18 +11:00
|
|
|
int main(int argc, char **argv) {
|
2015-11-28 06:21:38 +11:00
|
|
|
static int capture = 0, raw = 0;
|
2015-11-28 02:10:29 +11:00
|
|
|
char *socket_path = NULL;
|
2015-12-18 02:01:36 +11:00
|
|
|
char *output = NULL;
|
2015-11-28 06:21:38 +11:00
|
|
|
int framerate = 30;
|
2016-09-19 07:41:08 +10:00
|
|
|
bool grab_focused = false;
|
2015-11-28 02:10:29 +11:00
|
|
|
|
2015-11-28 01:53:50 +11:00
|
|
|
init_log(L_INFO);
|
2015-11-28 02:10:29 +11:00
|
|
|
|
|
|
|
static struct option long_options[] = {
|
2015-11-29 01:18:54 +11:00
|
|
|
{"help", no_argument, NULL, 'h'},
|
2015-11-29 01:35:44 +11:00
|
|
|
{"capture", no_argument, NULL, 'c'},
|
2015-12-18 02:01:36 +11:00
|
|
|
{"output", required_argument, NULL, 'o'},
|
2015-11-28 02:10:29 +11:00
|
|
|
{"version", no_argument, NULL, 'v'},
|
|
|
|
{"socket", required_argument, NULL, 's'},
|
2015-11-29 01:35:44 +11:00
|
|
|
{"raw", no_argument, NULL, 'r'},
|
2015-11-28 06:21:38 +11:00
|
|
|
{"rate", required_argument, NULL, 'R'},
|
2016-09-19 07:41:08 +10:00
|
|
|
{"focused", no_argument, NULL, 'f'},
|
2015-11-28 02:10:29 +11:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2015-11-29 01:09:14 +11:00
|
|
|
const char *usage =
|
2015-12-18 02:01:36 +11:00
|
|
|
"Usage: swaygrab [options] [file]\n"
|
2015-11-29 01:09:14 +11:00
|
|
|
"\n"
|
2015-11-29 01:18:54 +11:00
|
|
|
" -h, --help Show help message and quit.\n"
|
2015-11-29 01:09:14 +11:00
|
|
|
" -c, --capture Capture video.\n"
|
2015-12-18 02:01:36 +11:00
|
|
|
" -o, --output <output> Output source.\n"
|
2015-11-29 01:09:14 +11:00
|
|
|
" -v, --version Show the version number and quit.\n"
|
|
|
|
" -s, --socket <socket> Use the specified socket.\n"
|
|
|
|
" -R, --rate <rate> Specify framerate (default: 30)\n"
|
2016-09-19 07:41:08 +10:00
|
|
|
" -r, --raw Write raw rgba data to stdout.\n"
|
|
|
|
" -f, --focused Grab the focused container.\n";
|
2015-11-29 01:09:14 +11:00
|
|
|
|
2015-11-28 02:10:29 +11:00
|
|
|
int c;
|
|
|
|
while (1) {
|
|
|
|
int option_index = 0;
|
2016-09-19 07:41:08 +10:00
|
|
|
c = getopt_long(argc, argv, "hco:vs:R:rf", long_options, &option_index);
|
2015-11-28 02:10:29 +11:00
|
|
|
if (c == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (c) {
|
2016-09-19 07:41:08 +10:00
|
|
|
case 'f':
|
|
|
|
grab_focused = true;
|
|
|
|
break;
|
2015-11-28 02:10:29 +11:00
|
|
|
case 's': // Socket
|
|
|
|
socket_path = strdup(optarg);
|
|
|
|
break;
|
2015-11-28 06:21:38 +11:00
|
|
|
case 'r':
|
|
|
|
raw = 1;
|
|
|
|
break;
|
2015-12-18 02:01:36 +11:00
|
|
|
case 'o': // output
|
|
|
|
output = strdup(optarg);
|
|
|
|
break;
|
2015-11-28 06:21:38 +11:00
|
|
|
case 'c':
|
|
|
|
capture = 1;
|
|
|
|
break;
|
|
|
|
case 'R': // Frame rate
|
|
|
|
framerate = atoi(optarg);
|
|
|
|
break;
|
2015-11-28 02:10:29 +11:00
|
|
|
case 'v':
|
2017-04-26 19:29:30 +10:00
|
|
|
fprintf(stdout, "sway version " SWAY_VERSION "\n");
|
2015-11-29 00:47:44 +11:00
|
|
|
exit(EXIT_SUCCESS);
|
2015-11-28 02:10:29 +11:00
|
|
|
break;
|
2015-11-29 01:09:14 +11:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s", usage);
|
|
|
|
exit(EXIT_FAILURE);
|
2015-11-28 02:10:29 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!socket_path) {
|
|
|
|
socket_path = get_socketpath();
|
|
|
|
if (!socket_path) {
|
|
|
|
sway_abort("Unable to retrieve socket path");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-18 02:01:36 +11:00
|
|
|
char *file = NULL;
|
2015-11-28 06:21:38 +11:00
|
|
|
if (raw) {
|
2015-12-18 02:01:36 +11:00
|
|
|
if (optind >= argc + 1) {
|
2015-11-28 06:21:38 +11:00
|
|
|
sway_abort("Invalid usage. See `man swaygrab` %d %d", argc, optind);
|
|
|
|
}
|
2015-12-18 04:25:25 +11:00
|
|
|
} else if (optind < argc) {
|
2016-01-18 04:47:36 +11:00
|
|
|
file = strdup(argv[optind]);
|
2015-11-28 02:10:29 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
int socketfd = ipc_open_socket(socket_path);
|
|
|
|
free(socket_path);
|
|
|
|
|
2016-09-19 07:41:08 +10:00
|
|
|
init_json_tree(socketfd);
|
|
|
|
|
|
|
|
struct wlc_geometry *geo;
|
|
|
|
|
|
|
|
if (grab_focused) {
|
|
|
|
output = get_focused_output();
|
|
|
|
json_object *con = get_focused_container();
|
|
|
|
json_object *name;
|
|
|
|
json_object_object_get_ex(con, "name", &name);
|
|
|
|
geo = get_container_geometry(con);
|
|
|
|
free(con);
|
|
|
|
} else {
|
|
|
|
if (!output) {
|
|
|
|
output = get_focused_output();
|
|
|
|
}
|
|
|
|
geo = get_container_geometry(get_output_container(output));
|
|
|
|
// the geometry of the output in the get_tree response is relative to a global (0, 0).
|
|
|
|
// we need it to be relative to itself, so set origin to (0, 0) always.
|
|
|
|
geo->origin.x = 0;
|
|
|
|
geo->origin.y = 0;
|
2015-12-18 02:01:36 +11:00
|
|
|
}
|
|
|
|
|
2016-09-19 07:41:08 +10:00
|
|
|
const char *payload = create_payload(output, geo);
|
|
|
|
|
|
|
|
free(geo);
|
|
|
|
|
2015-12-18 04:25:25 +11:00
|
|
|
if (!file) {
|
|
|
|
if (!capture) {
|
|
|
|
file = default_filename("png");
|
|
|
|
} else {
|
|
|
|
file = default_filename("webm");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-28 02:39:18 +11:00
|
|
|
if (!capture) {
|
2016-09-19 07:41:08 +10:00
|
|
|
grab_and_apply_magick(file, payload, socketfd, raw);
|
2015-11-28 02:39:18 +11:00
|
|
|
} else {
|
2016-09-19 07:41:08 +10:00
|
|
|
grab_and_apply_movie_magic(file, payload, socketfd, raw, framerate);
|
2015-11-28 02:39:18 +11:00
|
|
|
}
|
|
|
|
|
2016-09-19 07:41:08 +10:00
|
|
|
free_json_tree();
|
2015-12-18 02:01:36 +11:00
|
|
|
free(output);
|
2015-12-18 04:25:25 +11:00
|
|
|
free(file);
|
2015-11-28 02:10:29 +11:00
|
|
|
close(socketfd);
|
|
|
|
return 0;
|
2015-11-28 01:53:50 +11:00
|
|
|
}
|