2015-11-27 15:53:50 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-11-27 16:39:18 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <unistd.h>
|
2015-11-28 14:49:02 +01:00
|
|
|
#include <stdint.h>
|
2015-11-27 16:39:18 +01:00
|
|
|
#include <math.h>
|
2015-11-27 20:21:38 +01:00
|
|
|
#include <time.h>
|
2015-11-27 15:53:50 +01:00
|
|
|
#include "log.h"
|
2015-11-27 16:10:29 +01:00
|
|
|
#include "ipc-client.h"
|
2015-11-27 15:53:50 +01:00
|
|
|
|
|
|
|
void sway_terminate(void) {
|
2015-11-28 14:47:44 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2015-11-27 15:53:50 +01:00
|
|
|
}
|
|
|
|
|
2015-11-27 16:39:18 +01:00
|
|
|
int numlen(int n) {
|
|
|
|
if (n >= 1000000) return 7;
|
|
|
|
if (n >= 100000) return 6;
|
|
|
|
if (n >= 10000) return 5;
|
|
|
|
if (n >= 1000) return 4;
|
|
|
|
if (n >= 100) return 3;
|
|
|
|
if (n >= 10) return 2;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-11-27 20:21:38 +01:00
|
|
|
void grab_and_apply_magick(const char *file, const char *output,
|
|
|
|
int socketfd, int raw) {
|
2015-11-27 16:39:18 +01:00
|
|
|
uint32_t len = strlen(output);
|
|
|
|
char *pixels = ipc_single_command(socketfd,
|
|
|
|
IPC_SWAY_GET_PIXELS, output, &len);
|
|
|
|
uint32_t *u32pixels = (uint32_t *)(pixels + 1);
|
|
|
|
uint32_t width = u32pixels[0];
|
|
|
|
uint32_t height = u32pixels[1];
|
2015-11-27 20:21:38 +01:00
|
|
|
len -= 9;
|
2015-11-27 16:39:18 +01:00
|
|
|
pixels += 9;
|
|
|
|
|
|
|
|
if (width == 0 || height == 0) {
|
|
|
|
sway_abort("Unknown output %s.", output);
|
|
|
|
}
|
|
|
|
|
2015-11-27 20:21:38 +01:00
|
|
|
if (raw) {
|
|
|
|
fwrite(pixels, 1, len, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
free(pixels - 9);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-27 16:39:18 +01:00
|
|
|
const char *fmt = "convert -depth 8 -size %dx%d+0 rgba:- -flip %s";
|
|
|
|
char *cmd = malloc(strlen(fmt) - 6 /*args*/
|
|
|
|
+ numlen(width) + numlen(height) + strlen(file) + 1);
|
|
|
|
sprintf(cmd, fmt, width, height, file);
|
|
|
|
|
|
|
|
FILE *f = popen(cmd, "w");
|
|
|
|
fwrite(pixels, 1, len, f);
|
|
|
|
fflush(f);
|
|
|
|
fclose(f);
|
2015-11-27 20:21:38 +01:00
|
|
|
free(pixels - 9);
|
|
|
|
free(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void grab_and_apply_movie_magic(const char *file, const char *output,
|
|
|
|
int socketfd, int raw, int framerate) {
|
|
|
|
if (raw) {
|
|
|
|
sway_log(L_ERROR, "Raw capture data is not yet supported. Proceeding with ffmpeg normally.");
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t len = strlen(output);
|
|
|
|
char *pixels = ipc_single_command(socketfd,
|
|
|
|
IPC_SWAY_GET_PIXELS, output, &len);
|
|
|
|
uint32_t *u32pixels = (uint32_t *)(pixels + 1);
|
|
|
|
uint32_t width = u32pixels[0];
|
|
|
|
uint32_t height = u32pixels[1];
|
|
|
|
pixels += 9;
|
|
|
|
|
|
|
|
if (width == 0 || height == 0) {
|
|
|
|
sway_abort("Unknown output %s.", output);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *fmt = "ffmpeg -f rawvideo -framerate %d "
|
|
|
|
"-video_size %dx%d -pixel_format argb "
|
|
|
|
"-i pipe:0 -r %d -vf vflip %s";
|
|
|
|
char *cmd = malloc(strlen(fmt) - 8 /*args*/
|
|
|
|
+ numlen(width) + numlen(height) + numlen(framerate) * 2
|
|
|
|
+ strlen(file) + 1);
|
|
|
|
sprintf(cmd, fmt, framerate, width, height, framerate, file);
|
|
|
|
|
|
|
|
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);
|
|
|
|
len = strlen(output);
|
|
|
|
pixels = ipc_single_command(socketfd,
|
|
|
|
IPC_SWAY_GET_PIXELS, output, &len);
|
|
|
|
pixels += 9;
|
|
|
|
len -= 9;
|
|
|
|
|
|
|
|
fwrite(pixels, 1, len, f);
|
|
|
|
|
|
|
|
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-27 16:40:28 +01:00
|
|
|
free(cmd);
|
2015-11-27 16:39:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2015-11-27 20:21:38 +01:00
|
|
|
static int capture = 0, raw = 0;
|
2015-11-27 16:10:29 +01:00
|
|
|
char *socket_path = NULL;
|
2015-11-27 20:21:38 +01:00
|
|
|
int framerate = 30;
|
2015-11-27 16:10:29 +01:00
|
|
|
|
2015-11-27 15:53:50 +01:00
|
|
|
init_log(L_INFO);
|
2015-11-27 16:10:29 +01:00
|
|
|
|
|
|
|
static struct option long_options[] = {
|
2015-11-28 15:18:54 +01:00
|
|
|
{"help", no_argument, NULL, 'h'},
|
2015-11-28 15:35:44 +01:00
|
|
|
{"capture", no_argument, NULL, 'c'},
|
2015-11-27 16:10:29 +01:00
|
|
|
{"version", no_argument, NULL, 'v'},
|
|
|
|
{"socket", required_argument, NULL, 's'},
|
2015-11-28 15:35:44 +01:00
|
|
|
{"raw", no_argument, NULL, 'r'},
|
2015-11-27 20:21:38 +01:00
|
|
|
{"rate", required_argument, NULL, 'R'},
|
2015-11-27 16:10:29 +01:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2015-11-28 15:09:14 +01:00
|
|
|
const char *usage =
|
|
|
|
"Usage: swaygrab [options] <output> [file]\n"
|
|
|
|
"\n"
|
2015-11-28 15:18:54 +01:00
|
|
|
" -h, --help Show help message and quit.\n"
|
2015-11-28 15:09:14 +01:00
|
|
|
" -c, --capture Capture video.\n"
|
|
|
|
" -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"
|
|
|
|
" -r, --raw Write raw rgba data to stdout.\n";
|
|
|
|
|
2015-11-27 16:10:29 +01:00
|
|
|
int c;
|
|
|
|
while (1) {
|
|
|
|
int option_index = 0;
|
2015-11-28 15:18:54 +01:00
|
|
|
c = getopt_long(argc, argv, "hcvs:r", long_options, &option_index);
|
2015-11-27 16:10:29 +01:00
|
|
|
if (c == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (c) {
|
|
|
|
case 's': // Socket
|
|
|
|
socket_path = strdup(optarg);
|
|
|
|
break;
|
2015-11-27 20:21:38 +01:00
|
|
|
case 'r':
|
|
|
|
raw = 1;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
capture = 1;
|
|
|
|
break;
|
|
|
|
case 'R': // Frame rate
|
|
|
|
framerate = atoi(optarg);
|
|
|
|
break;
|
2015-11-27 16:10:29 +01:00
|
|
|
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
|
2015-11-28 14:47:44 +01:00
|
|
|
exit(EXIT_SUCCESS);
|
2015-11-27 16:10:29 +01:00
|
|
|
break;
|
2015-11-28 15:09:14 +01:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "%s", usage);
|
|
|
|
exit(EXIT_FAILURE);
|
2015-11-27 16:10:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!socket_path) {
|
|
|
|
socket_path = get_socketpath();
|
|
|
|
if (!socket_path) {
|
|
|
|
sway_abort("Unable to retrieve socket path");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-27 22:01:06 +01:00
|
|
|
char *file = NULL, *output = NULL;
|
2015-11-27 20:21:38 +01:00
|
|
|
if (raw) {
|
|
|
|
if (optind >= argc) {
|
|
|
|
sway_abort("Invalid usage. See `man swaygrab` %d %d", argc, optind);
|
|
|
|
}
|
|
|
|
output = argv[optind];
|
|
|
|
} else {
|
|
|
|
if (optind >= argc - 1) {
|
|
|
|
sway_abort("Invalid usage. See `man swaygrab`");
|
|
|
|
}
|
|
|
|
file = argv[optind + 1];
|
|
|
|
output = argv[optind];
|
2015-11-27 16:10:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int socketfd = ipc_open_socket(socket_path);
|
|
|
|
free(socket_path);
|
|
|
|
|
2015-11-27 16:39:18 +01:00
|
|
|
if (!capture) {
|
2015-11-27 20:21:38 +01:00
|
|
|
grab_and_apply_magick(file, output, socketfd, raw);
|
2015-11-27 16:39:18 +01:00
|
|
|
} else {
|
2015-11-27 20:21:38 +01:00
|
|
|
grab_and_apply_movie_magic(file, output, socketfd, raw, framerate);
|
2015-11-27 16:39:18 +01:00
|
|
|
}
|
|
|
|
|
2015-11-27 16:10:29 +01:00
|
|
|
close(socketfd);
|
|
|
|
return 0;
|
2015-11-27 15:53:50 +01:00
|
|
|
}
|