From 6ccd5924201013ad9c204e3f2d3e21b1f800a71d Mon Sep 17 00:00:00 2001 From: Konstantin Pereiaslov Date: Mon, 15 May 2023 21:45:59 -0500 Subject: [PATCH] Open display before running the command so that it doesn't start execution in case X is not available. --- main.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index 76854b8..30ee6b3 100644 --- a/main.c +++ b/main.c @@ -75,6 +75,14 @@ int main(int argc, char *argv[]) { return 1; } + //Open display and initialize XScreensaverInfo for querying idle time + Display *dpy = XOpenDisplay(NULL); + if (!dpy) { + fprintf(stderr, "Couldn't open an X11 display!\n"); + return 1; + } + XScreenSaverInfo *info = XScreenSaverAllocInfo(); + shell_command_to_run = argv[optind]; // Fork a child process to run the command @@ -89,13 +97,6 @@ int main(int argc, char *argv[]) { exit(1); } - Display *dpy = XOpenDisplay(NULL); - if (!dpy) { - fprintf(stderr, "Couldn't open an X11 display!\n"); - return 1; - } - - XScreenSaverInfo *info = XScreenSaverAllocInfo(); int polling_interval_seconds = 1; int sleep_time_seconds = polling_interval_seconds; int command_paused = 0;