Skip to content

Instantly share code, notes, and snippets.

@dmedvinsky
Created September 1, 2010 10:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmedvinsky/560491 to your computer and use it in GitHub Desktop.
Save dmedvinsky/560491 to your computer and use it in GitHub Desktop.
Adds ability to execute sh scripts in xwrits before lock and after unlock.
diff -u xwrits-2.26//main.c xwrits/main.c
--- xwrits-2.26//main.c 2009-04-04 01:17:02.000000000 +0400
+++ xwrits/main.c 2010-09-01 14:06:52.531052264 +0400
@@ -10,6 +10,7 @@
#ifdef HAVE_XINERAMA
#include <X11/extensions/Xinerama.h>
#endif
+#include <signal.h>
static Options onormal;
Options *ocurrent;
@@ -63,6 +64,9 @@
static int force_mono = 0;
static int multiscreen = 0;
+static const char *before_lock_command = 0;
+static const char *after_lock_command = 0;
+
static void
short_usage(void)
@@ -757,6 +761,10 @@
;
else if (optparse(s, "wp", 2, "ss", &o->slideshow_text))
;
+ else if (optparse(s, "before-lock", 1, "ss", &before_lock_command))
+ ;
+ else if (optparse(s, "after-lock", 1, "ss", &after_lock_command))
+ ;
else
short_usage();
@@ -1089,6 +1097,30 @@
}
+void fork_and_run(const char* const cmd)
+{
+ pid_t pid;
+ pid = fork();
+ if (pid == 0) {
+ execl("/bin/sh", "/bin/sh", "-c", cmd, NULL);
+ _exit(0);
+ }
+}
+
+void on_before_lock()
+{
+ if (before_lock_command) {
+ fork_and_run(before_lock_command);
+ }
+}
+void on_after_lock()
+{
+ if (after_lock_command) {
+ fork_and_run(after_lock_command);
+ }
+}
+
+
/* main! */
typedef enum {
@@ -1159,7 +1191,9 @@
case ST_LOCK:
was_lock = 1;
+ on_before_lock();
tran = lock();
+ on_after_lock();
assert(tran == TRAN_AWAKE || tran == TRAN_FAIL);
if (tran == TRAN_AWAKE)
s = ST_AWAKE;
@@ -1198,6 +1232,8 @@
default_settings();
parse_options(argc - 1, argv + 1);
+ signal(SIGCLD, SIG_IGN);
+
/* At this point, all ports have 'display_name' valid and everything else
invalid. Open displays, check multiscreen */
orig_nports = nports;
diff -u xwrits-2.26//xwrits.1 xwrits/xwrits.1
--- xwrits-2.26//xwrits.1 2009-04-04 01:17:59.000000000 +0400
+++ xwrits/xwrits.1 2010-08-13 14:16:02.831761508 +0400
@@ -307,6 +307,16 @@
Sets the image that appears on the warning window to an arbitrary GIF.
Animations are acceptable.
'
+.TP 5
+\fBbefore-lock\fP=\fIfile.sh\fP
+Executes this sh script before locking the screen. User running xwrits should
+have execution flag on this file.
+'
+.TP 5
+\fBafter-lock\fP=\fIfile.sh\fP
+Executes this sh script after unlocking the screen. User running xwrits should
+have execution flag on this file.
+'
.SH EXAMPLES
Here is the way I run xwrits:
.nf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment