aboutsummaryrefslogtreecommitdiffstats
path: root/swaygrab
diff options
context:
space:
mode:
authorLibravatar Mikkel Oscar Lyderik <mikkeloscar@gmail.com>2015-12-17 18:25:25 +0100
committerLibravatar Mikkel Oscar Lyderik <mikkeloscar@gmail.com>2015-12-17 18:37:09 +0100
commitc97555d9f085138814b49e57c353d3b88e6b6fb8 (patch)
treefd566c6f7e23764d2124be44c2e2c98f38582fde /swaygrab
parentMerge pull request #350 from mikkeloscar/swaygrab-auto-output (diff)
downloadsway-c97555d9f085138814b49e57c353d3b88e6b6fb8.tar.gz
sway-c97555d9f085138814b49e57c353d3b88e6b6fb8.tar.zst
sway-c97555d9f085138814b49e57c353d3b88e6b6fb8.zip
swaygrab: add default output filename.
With this it's possible to run `swaygrab` without a filename argument. With no filename supplied it will use a default name based on the current time. The default file will get the extension `png` for screenshots and `webm` for video capture.
Diffstat (limited to 'swaygrab')
-rw-r--r--swaygrab/main.c27
1 files changed, 23 insertions, 4 deletions
diff --git a/swaygrab/main.c b/swaygrab/main.c
index 671b8737..1120f0d5 100644
--- a/swaygrab/main.c
+++ b/swaygrab/main.c
@@ -135,6 +135,19 @@ char *get_focused_output(int socketfd) {
135 return output; 135 return output;
136} 136}
137 137
138char *default_filename(const char *extension) {
139 int ext_len = strlen(extension);
140 int len = 28 + ext_len; // format: "2015-12-17-180040_swaygrab.ext"
141 char *filename = malloc(len * sizeof(char));
142 time_t t = time(NULL);
143
144 struct tm *lt = localtime(&t);
145 strftime(filename, len, "%Y-%m-%d-%H%M%S_swaygrab.", lt);
146 strncat(filename, extension, ext_len);
147
148 return filename;
149}
150
138int main(int argc, char **argv) { 151int main(int argc, char **argv) {
139 static int capture = 0, raw = 0; 152 static int capture = 0, raw = 0;
140 char *socket_path = NULL; 153 char *socket_path = NULL;
@@ -214,10 +227,7 @@ int main(int argc, char **argv) {
214 if (optind >= argc + 1) { 227 if (optind >= argc + 1) {
215 sway_abort("Invalid usage. See `man swaygrab` %d %d", argc, optind); 228 sway_abort("Invalid usage. See `man swaygrab` %d %d", argc, optind);
216 } 229 }
217 } else { 230 } else if (optind < argc) {
218 if (optind >= argc) {
219 sway_abort("Invalid usage. See `man swaygrab`");
220 }
221 file = argv[optind]; 231 file = argv[optind];
222 } 232 }
223 233
@@ -228,6 +238,14 @@ int main(int argc, char **argv) {
228 output = get_focused_output(socketfd); 238 output = get_focused_output(socketfd);
229 } 239 }
230 240
241 if (!file) {
242 if (!capture) {
243 file = default_filename("png");
244 } else {
245 file = default_filename("webm");
246 }
247 }
248
231 if (!capture) { 249 if (!capture) {
232 grab_and_apply_magick(file, output, socketfd, raw); 250 grab_and_apply_magick(file, output, socketfd, raw);
233 } else { 251 } else {
@@ -235,6 +253,7 @@ int main(int argc, char **argv) {
235 } 253 }
236 254
237 free(output); 255 free(output);
256 free(file);
238 close(socketfd); 257 close(socketfd);
239 return 0; 258 return 0;
240} 259}