commit 5498d1acd0b8f94474a05fde5c2a86a425e76e5e
parent 096366bc8dc4a0218422fee3ed7716e406bf5697
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Mon, 14 Mar 2022 22:45:50 -0500
waycopy: fork by default
Diffstat:
6 files changed, 24 insertions(+), 6 deletions(-)
diff --git a/README.md b/README.md
@@ -17,7 +17,7 @@ To copy data, just pipe it into `waycopy`, and optionally specify a seat and mim
```
$ echo "howdy" | waycopy -s seat0 -t text/plain &
```
-(note that `waycopy` does not automatically fork into the background)
+(note that `waycopy` will fork into the background)
To paste, optionally specify a seat and desired mimetype:
```
diff --git a/common.h b/common.h
@@ -9,4 +9,5 @@ extern const char *argv0;
extern struct {
const char *type;
const char *seat;
+ bool foreground;
} options;
diff --git a/util.c b/util.c
@@ -1,4 +1,5 @@
#include <stdlib.h>
+#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
@@ -52,13 +53,14 @@ copyfd(int out, int in)
struct {
const char *type;
const char *seat;
+ bool foreground;
} options;
void
-parseopts(int argc, char *const argv[])
+parseopts(const char *opts, int argc, char *const argv[])
{
while (1) {
- int next = getopt(argc, argv, "s:t:");
+ int next = getopt(argc, argv, opts);
if (next == -1) {
if (argv[optind] && *argv[optind] != '-') {
fprintf(stderr, "usage: %s [-s seat] [-t mimetype]\n", argv0);
@@ -72,6 +74,9 @@ parseopts(int argc, char *const argv[])
}
switch (next) {
+ case 'f':
+ options.foreground = true;
+ break;
case 's':
options.seat = optarg;
break;
diff --git a/util.h b/util.h
@@ -1,4 +1,4 @@
void die(const char *const error);
void warn(const char *const error);
void copyfd(int out, int in);
-void parseopts(int argc, char *const argv[]);
+void parseopts(const char *opts, int argc, char *const argv[]);
diff --git a/waycopy.c b/waycopy.c
@@ -47,7 +47,7 @@ main(int argc, char *argv[])
options.type = "text/plain";
options.seat = NULL;
- parseopts(argc, argv);
+ parseopts("fs:t:", argc, argv);
char path[PATH_MAX] = {0};
char *ptr = getenv("TMPDIR");
@@ -101,6 +101,18 @@ main(int argc, char *argv[])
zwlr_data_control_source_v1_add_listener(source, &data_source_listener, NULL);
zwlr_data_control_device_v1_set_selection(device, source);
+ if (!options.foreground) {
+ pid_t pid = fork();
+ if (pid == -1) {
+ die("failed to fork");
+ }
+
+ if (pid != 0) {
+ close(STDERR_FILENO);
+ return 0;
+ }
+ }
+
while (wl_display_dispatch(display) != -1 && running);
return running;
diff --git a/waypaste.c b/waypaste.c
@@ -59,7 +59,7 @@ main(int argc, char *argv[])
options.type = "text/plain";
options.seat = NULL;
- parseopts(argc, argv);
+ parseopts("s:t:", argc, argv);
display = wl_display_connect(NULL);
if (display == NULL)