Skip to content

Commit

Permalink
Merge pull request #25 from michaeladler/forking_mode
Browse files Browse the repository at this point in the history
Implement forking mode
  • Loading branch information
Airblader committed May 24, 2015
2 parents ec73ca9 + d5fda16 commit f81ef40
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
3 changes: 3 additions & 0 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,7 @@ typedef struct Config {

/* How much spam should we generate? */
log_level_t log_level;

/* If true, fork on startup */
bool fork_mode;
} Config;
5 changes: 4 additions & 1 deletion man/xedgewarp.man
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ xedgewarp - window manager agnostic pointer warping between outputs

== SYNOPSIS

xedgewarp [-m closest|relative] [-l error|debug|trace] [-h] [-v]
xedgewarp [-b] [-m closest|relative] [-l error|debug|trace] [-h] [-v]

== OPTIONS

-b::
Run in background mode, i.e. fork on startup.

-m closest|relative::
Specifies how the mouse pointer should be warped. With "closest", the pointer
is warped only as little as necessary to the closest edge on the output. With
Expand Down
50 changes: 47 additions & 3 deletions src/xedgewarp.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
// vim:ts=4:sw=4:expandtab
#include "all.h"
#include <getopt.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#ifndef __VERSION
#define __VERSION "unknown"
#endif

typedef void (*callback)(void);

/* Forward declarations */
static void run();
static void safe_fork(callback child_callback);

xcb_connection_t *connection;
xcb_window_t root;
Config config;
Config config = {
.fake_outputs = NULL,
.warp_mode = WM_CLOSEST,
.log_level = L_ERROR,
.fork_mode = false
};

int main(int argc, char *argv[]) {
atexit(on_xedgewarp_exit);
parse_arguments(argc, argv);

if (config.fork_mode) {
safe_fork(run);
} else {
run();
}
}

static void run() {
initialize_x11();
initialize_xedgewarp();

Expand Down Expand Up @@ -68,7 +90,7 @@ void on_xedgewarp_exit(void) {
*/
void parse_arguments(int argc, char *argv[]) {
int c;
while ((c = getopt(argc, argv, "m:l:o:vh")) != -1) {
while ((c = getopt(argc, argv, "m:l:o:bvh")) != -1) {
switch (c) {
case 'm':
if (strcasecmp(optarg, "closest") == 0)
Expand All @@ -92,16 +114,20 @@ void parse_arguments(int argc, char *argv[]) {
case 'o':
config.fake_outputs = strdup(optarg);
break;
case 'b':
config.fork_mode = true;
break;
case 'v':
fprintf(stderr, "%s version %s\n", argv[0], __VERSION);
exit(EXIT_SUCCESS);
break;
case 'h':
default:
fprintf(stderr, "Usage: %s [-m closest|relative] [-l error|debug|trace] [-v] [-h]", argv[0]);
fprintf(stderr, "Usage: %s [-b] [-m closest|relative] [-l error|debug|trace] [-v] [-h]", argv[0]);
fprintf(stderr, "\n");
fprintf(stderr, "\t-h display the usage and exit\n");
fprintf(stderr, "\t-v display the version and exit\n");
fprintf(stderr, "\t-b run in background mode, i.e. fork on startup\n");
fprintf(stderr, "\t-m closest|relative\n"
"\t\tSpecifies how the mouse pointer should be warped.\n");
fprintf(stderr, "\t-l error|debug|trace\n"
Expand All @@ -112,3 +138,21 @@ void parse_arguments(int argc, char *argv[]) {
}
}
}

/* Double fork to prevent zombie processes */
static void safe_fork(callback child_callback) {
pid_t pid = fork();
if (!pid) {
if (!fork()) {
/* this is the child that keeps going */
(*child_callback)();
} else {
/* the first child process exits */
exit(EXIT_SUCCESS);
}
} else {
/* this is the original process */
/* wait for the first child to exit which it will immediately */
waitpid(pid, NULL, 0);
}
}

0 comments on commit f81ef40

Please sign in to comment.