Skip to content

Instantly share code, notes, and snippets.

@avesus
Created December 12, 2017 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avesus/10d58b110da8906b44a50f1a673b9dea to your computer and use it in GitHub Desktop.
Save avesus/10d58b110da8906b44a50f1a673b9dea to your computer and use it in GitHub Desktop.
SpectrWM XTerm Background Color Change on Focus
finish() {
rm ${HOME}/.xterm-bash/$PPID
}
trap finish EXIT
echo -en $$ > ${HOME}/.xterm-bash/$PPID
diff --git a/spectrwm.c b/spectrwm.c
index 9b0ad2c..f76a828 100644
--- a/spectrwm.c
+++ b/spectrwm.c
@@ -1055,6 +1055,7 @@ void focus(struct binding *, struct swm_region *, union arg *);
void focus_flush(void);
void focus_pointer(struct binding *, struct swm_region *, union arg *);
void focus_region(struct swm_region *);
+void send_xterm_command(struct ws_win *, int, char *);
void focus_win(struct ws_win *);
void focusin(xcb_focus_in_event_t *);
#ifdef SWM_DEBUG
@@ -3960,6 +3961,63 @@ unfocus_win(struct ws_win *win)
ewmh[_NET_ACTIVE_WINDOW].atom, XCB_ATOM_WINDOW, 32, 1, &none);
DNPRINTF(SWM_D_FOCUS, "unfocus_win: done.\n");
+
+ send_xterm_command(win, 11, "GRAY");
+}
+
+void send_xterm_command(struct ws_win *win, int command, char *data) {
+
+ xcb_atom_t apid;
+ xcb_get_property_cookie_t pc;
+ xcb_get_property_reply_t *pr;
+ pid_t xterm_pid = 0;
+ char filename[256];
+ char bash_stdin[32];
+ int bash_pid = 0;
+ FILE *f = NULL;
+ char xterm_command[64];
+
+ if (!win || !win->id) {
+ return;
+ }
+
+ if (0 != strncmp("XTerm", win->ch.class_name, 6)) {
+ return;
+ }
+
+ // 1. Get PID of XTerm
+ apid = get_atom_from_string("_NET_WM_PID");
+ if (apid != XCB_ATOM_NONE) {
+ pc = xcb_get_property(conn, 0, win->id, apid, XCB_ATOM_CARDINAL, 0, 1);
+ pr = xcb_get_property_reply(conn, pc, NULL);
+ if (pr) {
+ if (pr->format == 32) {
+ xterm_pid = *((pid_t *)xcb_get_property_value(pr));
+ // 2. Get PID of child process (bash)
+ sprintf(filename, "%s/.xterm-bash/%d", getenv("HOME"), xterm_pid);
+ f = fopen(filename, "rb");
+ if (f) {
+ fscanf(f, "%d", &bash_pid);
+ fclose(f);
+ } else {
+ printf("\n%s file wasn't created!\n", filename);
+ }
+ }
+
+ free(pr);
+ }
+ }
+
+ if (bash_pid) {
+ // 3. Send escape sequence directly to that process
+ sprintf(bash_stdin, "/proc/%d/fd/0", bash_pid);
+ f = fopen(bash_stdin, "w");
+ if (f) {
+ sprintf(xterm_command, "\e]%d;%s\a", command, data);
+ fwrite(xterm_command, strlen(xterm_command), 1, f);
+ fclose(f);
+ }
+ }
}
void
@@ -4106,6 +4164,8 @@ focus_win(struct ws_win *win)
}
out:
+ send_xterm_command(win, 11, "WHITE");
+
free(gifr);
free(war);
DNPRINTF(SWM_D_FOCUS, "focus_win: done.\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment