aboutsummaryrefslogtreecommitdiff
path: root/daemonic.c
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2020-06-17 12:29:41 +0530
committerAkshay Nair <phenax5@gmail.com>2020-06-17 12:29:41 +0530
commitedce91f10a954cfbedbc74cc94f200efcec074df (patch)
treef86db82733f40db3e973e49552864b8d0faedc24 /daemonic.c
parent0c9d36aa5bb64029c254a6fcd645b892b37d5661 (diff)
downloadshotkey-edce91f10a954cfbedbc74cc94f200efcec074df.tar.gz
shotkey-edce91f10a954cfbedbc74cc94f200efcec074df.zip
Adds on mode change callback
Diffstat (limited to '')
-rw-r--r--daemonic.c47
1 files changed, 38 insertions, 9 deletions
diff --git a/daemonic.c b/daemonic.c
index f429134..309b3f2 100644
--- a/daemonic.c
+++ b/daemonic.c
@@ -1,5 +1,6 @@
-#include<stdio.h>
+#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <unistd.h>
@@ -52,16 +53,45 @@ int error_handler(Display *disp, XErrorEvent *xe) {
return 1;
}
+void spawn_with_env(char** command, char** env) {
+ if (fork() == 0) {
+ setsid();
+ execve(command[0], command, env);
+ fprintf(stderr, "daemonic: execvp %s", command[0]);
+ perror(" failed");
+ exit(0);
+ }
+}
+
void spawn(char ** command) {
if (fork() == 0) {
setsid();
execvp(command[0], command);
- fprintf(stderr, "dwm: execvp %s", command[0]);
+ fprintf(stderr, "daemonic: execvp %s", command[0]);
perror(" failed");
exit(0);
}
}
+char* get_mode_label() {
+ if (current_mode == -1) return "";
+ ModeProperties props = mode_properties[current_mode];
+ return props.label;
+}
+
+extern char** environ;
+void handle_mode_change() {
+ char str[255];
+ sprintf(str, "%d", current_mode);
+ setenv("MODE_ID", str, 1);
+
+ char* label = get_mode_label();
+ setenv("MODE_LABEL", label, 1);
+
+ char* cmd[] = {shell, "-c", on_mode_change, NULL};
+ spawn_with_env(cmd, environ);
+}
+
void run(Display* dpy, Window win, Command command) {
Key mode_key;
unsigned int i;
@@ -82,6 +112,8 @@ void run(Display* dpy, Window win, Command command) {
// Bind an escape key to quit mode
bind_key(dpy, win, 0, XK_Escape);
+
+ handle_mode_change();
}
}
@@ -126,23 +158,18 @@ void keypress(Display *dpy, Window win, XKeyEvent *ev) {
if (!is_mode_persistent) {
current_mode = -1;
is_mode_persistent = False;
+ handle_mode_change();
}
}
}
-char* get_mode_label() {
- if (current_mode == -1) return "";
- ModeProperties props = mode_properties[current_mode];
- return props.label;
-}
-
void help_menu(char* x) {
printf("Usage: %s [-vh]\n", x);
}
int main(int argc, char *argv[]) {
int opt;
- while ((opt = getopt(argc, argv, "vh")) != -1) {
+ while (argc > 1 && (opt = getopt(argc, argv, "vh")) != -1) {
switch (opt) {
case 'v': printf("%s\n", VERSION); return 0;
/*case 'm': printf("%s\n", get_mode_label()); return 0;*/
@@ -167,6 +194,8 @@ int main(int argc, char *argv[]) {
XSelectInput(dpy, root, KeyPressMask);
+ handle_mode_change();
+
/* main event loop */
XEvent ev;
XSync(dpy, False);