From d11d45f19fff4ff8e9fe0d9e5efcf06f71f8f9da Mon Sep 17 00:00:00 2001 From: Philip Allison Date: Wed, 23 Apr 2008 15:03:27 +0100 Subject: [PATCH] Add options for window sticking and backgrounding Window will set stickiness (show on all desktops) hint if "-stick" option is given, and will by default try to fork into the background, overridden by "-nobg". Signed-off-by: Philip Allison --- src/cteddy.cxx | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/cteddy.cxx b/src/cteddy.cxx index 3012fe9..bb8dddd 100644 --- a/src/cteddy.cxx +++ b/src/cteddy.cxx @@ -23,6 +23,8 @@ // Language headers #include #include +#include +#include // Library headers #include @@ -31,6 +33,7 @@ // System headers #include #include +#include #include @@ -130,6 +133,12 @@ int main (int argc, char* argv[]) // Whether or not to set the window's keep-on-top hint bool ontop = false; + // Whether or not the window's sticky hint should be set + bool sticky = false; + + // Whether or not to fork into the background + bool background = true; + // Mimic the image loading behaviour of xteddy, to an extent. { // Start from the name of the executable itself @@ -153,6 +162,12 @@ int main (int argc, char* argv[]) // Allow keep-on-top else if (strncmp(argv[i], "-float", 6) == 0) ontop = true; + // Allow sticking to all desktops + else if (strncmp(argv[i], "-stick", 6) == 0) + sticky = true; + // Allow disablement of backgrounding + else if (strncmp(argv[i], "-nobg", 5) == 0) + background = false; } // If the given path contains slashes, assume it is absolute or @@ -314,10 +329,66 @@ int main (int argc, char* argv[]) // Don't steal input focus when we start g_object_set(G_OBJECT(window), "focus-on-map", false, NULL); + // Honour float & sticky options if (ontop) gtk_window_set_keep_above(GTK_WINDOW(window), true); + if (sticky) + gtk_window_stick(GTK_WINDOW(window)); gtk_widget_show_all(window); + + // Fork into background if desired + if (background) + { + pid_t p = fork(); + if (p < 0) + { + std::cerr << "Could not fork: " << strerror(errno) << std::endl; + return 1; + } + else if (p == 0) + { + // Close stdin + while (close(0) != 0) + { + if (errno == EINTR) + continue; + std::cerr << "Could not close stdin: " << strerror(errno) << std::endl; + return 1; + } + // Redirect stdout & stderr to /dev/null + int nullfd; + while ((nullfd = open("/dev/null", O_WRONLY)) < 0) + { + if (errno == EINTR) + continue; + std::cerr << "Cannot open /dev/null: " << strerror(errno) << std::endl; + return 1; + } + while (dup2(nullfd, 1) < 0) + { + if (errno == EINTR) + continue; + std::cerr << "Cannot redirect stdout to /dev/null: " << strerror(errno) << std::endl; + return 1; + } + while (dup2(nullfd, 2) < 0) + { + if (errno == EINTR) + continue; + std::cerr << "Cannot redirect stderr to /dev/null: " << strerror(errno) << std::endl; + return 1; + } + // Create new process session + if (setsid() < 0) + { + std::cerr << "Cannot create new session: " << strerror(errno) << std::endl; + return 1; + } + } + else + return 0; + } gtk_main(); -- 2.11.4.GIT