aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOmar Polo <op@omarpolo.com>2020-10-01 11:47:02 +0200
committerOmar Polo <op@omarpolo.com>2020-10-01 11:47:02 +0200
commite0041ee5139dcd4173fe8f47ff691c0d41951341 (patch)
tree2cf22ea1fb6a5838ef5a354f2315724a29cce8b3
parent13cc0916743705e32211da7fcf5499197e36e883 (diff)
downloadstar-platinum-e0041ee5139dcd4173fe8f47ff691c0d41951341.tar.gz
star-platinum-e0041ee5139dcd4173fe8f47ff691c0d41951341.tar.bz2
add exec action for keys
users can now exec command on specifics keys. For example: match class Something on "C-<F5>" do exec my-program
-rw-r--r--parse.y4
-rw-r--r--star-platinum.c55
-rw-r--r--star-platinum.h11
3 files changed, 60 insertions, 10 deletions
diff --git a/parse.y b/parse.y
index ed7bd95..32657d4 100644
--- a/parse.y
+++ b/parse.y
@@ -28,6 +28,7 @@
#define SPECIAL(X) ((struct action){.type = (ASPECIAL), .special = (X) })
#define FAKE_KEY(K) ((struct action){.type = (AFAKE), .send_key = (K) })
+#define EXEC(S) ((struct action){.type = (AEXEC), .str = (S) })
%}
@@ -42,7 +43,7 @@
%token TMATCH TCLASS
%token TON TDO
-%token TTOGGLE TACTIVATE TDEACTIVATE TIGNORE
+%token TTOGGLE TACTIVATE TDEACTIVATE TIGNORE TEXEC
%token TERR
%token <key> TKEY
@@ -85,4 +86,5 @@ action : TKEY { $$ = FAKE_KEY($1); }
| TACTIVATE { $$ = SPECIAL(AACTIVATE); }
| TDEACTIVATE { $$ = SPECIAL(ADEACTIVATE); }
| TIGNORE { $$ = SPECIAL(AIGNORE); }
+ | TEXEC TSTRING { $$ = EXEC($2); }
;
diff --git a/star-platinum.c b/star-platinum.c
index 232d734..ebe717c 100644
--- a/star-platinum.c
+++ b/star-platinum.c
@@ -17,6 +17,7 @@
#include "star-platinum.h"
#include <err.h>
+#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
@@ -123,6 +124,8 @@ main(int argc, char **argv)
XEvent e;
Window root;
+ signal(SIGCHLD, SIG_IGN); /* don't allow zombies */
+
fname = NULL;
while ((ch = getopt(argc, argv, "c:dn")) != -1) {
switch (ch) {
@@ -187,7 +190,7 @@ main(int argc, char **argv)
XSelectInput(d, root, KeyPressMask);
XFlush(d);
- pledge("stdio", "");
+ pledge("stdio proc exec", NULL);
while (1) {
XNextEvent(d, &e);
@@ -341,12 +344,38 @@ do_action(struct action a, Window focused, int pressed)
}
break;
+ case AEXEC: {
+ pid_t p;
+ const char *sh;
+
+ switch (p = fork()) {
+ case -1:
+ err(1, "fork");
+
+ case 0:
+ if ((sh = getenv("SHELL")) == NULL)
+ sh = "/bin/sh";
+ printf("before exec'ing %s -c '%s'\n", sh, a.str);
+ execlp(sh, sh, "-c", a.str, NULL);
+ err(1, "execlp");
+ }
+
+ break;
+ }
+
default:
/* unreachable */
abort();
}
}
+void
+free_action(struct action a)
+{
+ if (a.type == AEXEC)
+ free(a.str);
+}
+
/* match */
@@ -522,16 +551,29 @@ printkey(struct key k)
void
printaction(struct action a)
{
- if (a.type == ASPECIAL) {
+ switch (a.type) {
+ case ASPECIAL:
switch (a.special) {
case ATOGGLE: printf("toggle"); break;
case AACTIVATE: printf("activate"); break;
case ADEACTIVATE: printf("deactivate"); break;
case AIGNORE: printf("ignore"); break;
+ default: abort(); /* unreachable */
}
- } else {
+ break;
+
+ case AFAKE:
printf("send key ");
printkey(a.send_key);
+ break;
+
+ case AEXEC:
+ printf("exec %s", a.str);
+ break;
+
+ default:
+ /* unreachable */
+ abort();
}
}
@@ -544,13 +586,16 @@ printmatch(struct match *m)
}
printf("match ");
switch (m->prop) {
+ case MANY:
+ printf("all");
+ break;
case MCLASS:
- printf("class");
+ printf("class %s", m->str);
break;
default:
+ /* unreachable */
abort();
}
- printf(" %s", m->str);
if (m->next == NULL)
printf("\n");
diff --git a/star-platinum.h b/star-platinum.h
index 88db9ef..f6a752b 100644
--- a/star-platinum.h
+++ b/star-platinum.h
@@ -37,18 +37,21 @@ struct key {
struct action {
#define ASPECIAL 1
#define AFAKE 2
+#define AEXEC 3
int type;
union {
-#define ATOGGLE 3
-#define AACTIVATE 4
-#define ADEACTIVATE 5
-#define AIGNORE 6
+#define ATOGGLE 1
+#define AACTIVATE 2
+#define ADEACTIVATE 3
+#define AIGNORE 4
int special;
struct key send_key;
+ char *str;
};
};
void do_action(struct action, Window, int);
+void free_action(struct action);
struct match {
#define MANY 1