You are not logged in.

#1 2015-11-20 03:01:12

Chrysostomus
Member
Registered: 2015-09-13
Posts: 64
Website

Editing stream with multiple dynamically generated patterns

Greetings!

I have a question that is probably quite basic, but unfortunately I could not find answers though long googling.

I have two excellent lemonbar scripts for bspwm. I figured I could make them usable for other emwh compatiple window managers too by replacing bspc control --subscribe with xprop -root -spy. I'm almost finished, only thing missing is marking occupied desktops (I could mark focused desktop without problems, because only one is focused).

So I have this command:

  xprop -root _NET_DESKTOP_NAMES | sed -e 's/_NET_DESKTOP_NAMES(UTF8_STRING) =/WM/g;s/ "/:f/g;s/",//g;s/"/:/g' | sed "s/f$current_desktop:/O$current_desktop:/g" 

It gives me this output:

WM:OI:fII:fIII:fIV:fV:fVI:fVII:fVIII:fIX:fX:

Now I need to change every "f" of occupied desktop to "o".
I can get list of occupied desktops (counting from 0) with

wmctrl -l | awk '{print $2}' | uniq

It can be converted to desktop names, but it is lighter/better if could be done without that, as my method is messy and involves temporary files.

So I'd need to convert first letter of column to o if it is f, on Nth columns, where there can be multiple Ns and it is not preknown how many, columns are separated by ":" and desktop number 0 matches second column of the string.

What would be the best/most efficient way of doing this?

Last edited by Chrysostomus (2015-11-20 03:16:46)


The difference between reality and fiction is that fiction has to make sense.

Offline

#2 2015-11-20 03:56:03

Chrysostomus
Member
Registered: 2015-09-13
Posts: 64
Website

Re: Editing stream with multiple dynamically generated patterns

Something similar to

awk -F, -v N=$((desktop_number+2)) OFS=: '{$N="NEW"; print }'

but with "NEW" being o$(last character of $N) and done for all instances of {desktop_number}.

This repeating of operation for multiple variables is the most difficult part for me.


The difference between reality and fiction is that fiction has to make sense.

Offline

#3 2015-11-20 04:15:28

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,529
Website

Re: Editing stream with multiple dynamically generated patterns

I find it odd to use xprop and wmctrl with have a lot of code to get every possible bit of information about these windows only to pass that into several agressive filters with a few different sed and awk processes.  Just ask X for the information you want:

#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>

int main() {
	Display *dpy;
	if (!(dpy=XOpenDisplay(0x0))) return 1;
	Window root = DefaultRootWindow(dpy);
	/* get desktop names */
	char **names = NULL;
	int ndesks = 0;
	XTextProperty text;
	XGetTextProperty(dpy, root, &text, XInternAtom(dpy, "_NET_DESKTOP_NAMES", False));
	XmbTextPropertyToTextList(dpy, &text, &names, &ndesks);
	/* get current desktop number */
	long cur_desk = 0;
	int i; Atom aa;
	unsigned long ul, ul2; unsigned char *uc = NULL, *uc2;
	if (XGetWindowProperty(dpy, root, XInternAtom(dpy, "_NET_CURRENT_DESKTOP", False), 0L, 32, False, XA_CARDINAL, &aa, &i, &ul, &ul, &uc) == Success && uc)
	cur_desk = *(long *)uc;
	XFree(uc);
	/* get window list */
	unsigned long nwin;
	if (XGetWindowProperty(dpy, root, XInternAtom(dpy, "_NET_CLIENT_LIST", False), 0L, 32, False, XA_WINDOW, &aa, &i, &nwin, &ul, &uc) == Success && uc);
	Window *wins = (Window *)uc;
	/* check which desktops are occupied */
	unsigned long flags = 0;
	Atom WM_DESK = XInternAtom(dpy, "_NET_WM_DESKTOP", False);
	for (ul = 0; ul < nwin; ul++) {
		XGetWindowProperty(dpy, wins[ul], WM_DESK, 0L, 32, False, XA_CARDINAL, &aa, &i, &ul2, &ul2, &uc2);
		ul2 = *(long *)uc2;
		if (ul2 > ndesks || ul2 > sizeof(unsigned long) * 8) continue;
		flags |= 1<<ul2;
	}
	/* print out names */
	if (!names) return 1;
	printf("WM:");
	for (i = 0; i < ndesks; i++) {
		if (!names[i]) continue;
		printf("%s%s:", (flags & (1<<ul2) ? "O" : "o"), names[i]);
	}
	printf("\n");
	/* clean up */
	if (wins) XFree(wins);
	if (names) XFreeStringList(names);
	XCloseDisplay(dpy);
	return 0;
}

Note that I don't have an EMWH window manager to test this on, and it's been a while since I bothered working with EWMH, so this might need some tweaking - but it compiles and runs, and has the basics of what would be needed.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2015-11-20 06:50:55

Chrysostomus
Member
Registered: 2015-09-13
Posts: 64
Website

Re: Editing stream with multiple dynamically generated patterns

Trilby wrote:

I find it odd to use xprop and wmctrl with have a lot of code to get every possible bit of information about these windows only to pass that into several agressive filters with a few different sed and awk processes.  Just ask X for the information you want:

I agree that it does not make resource wise and is very inefficient. But I was approaching problem with tools I had within my very limited skillset. And the inefficiency is mitigated by the fact that script does not loop, but only runs updates when workspace is switched. So while very inefficient, it would have been feasible solution regardles. And I think asking for as great help as you have now provided would have been too much to ask. This was my rationale behind the approach.

Thank you very much for that piece of code! I'll see if I can use that instead as a basis for solution.


The difference between reality and fiction is that fiction has to make sense.

Offline

#5 2015-11-20 08:06:35

Chrysostomus
Member
Registered: 2015-09-13
Posts: 64
Website

Re: Editing stream with multiple dynamically generated patterns

Although my question is not best answer to my problem, it is still an interesting question. Closest I got to answering my question was:

awk -v N="/$4" -F':' -vOFS=':' '{ gsub("f", "o", N) ; print }'

This substitutes "f" with "o" in Nth column, passed to awk with variable N. Is it possible to pass multiple variables to awk this way, without knowing the number of variables beforehand nor what they are?


The difference between reality and fiction is that fiction has to make sense.

Offline

Board footer

Powered by FluxBB