You are not logged in.

#26 2015-10-20 01:34:54

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

Re: Trouble piping xev output - wm independent desktop menu [solved]

Thanks!
Whoa, that C code looks so clean and legible. Maybe I need to learn C... My little brother always (okay twice) told me to learn C or python.

Efficiency is concern because it's going to be running all the time and in bspwm enviroment which is meant to be light. I think the resource usage is currently acceptable, as it is already lighter than any alternative I know of, and no alternative provides quite the same functionality. But better is always better...

Another alternative optimization that came to mind would be moving config sourcing/creation and xev inside the awk function and make it pure awk script. But that probably is not worth it if I plan to move using C in future.

Thank you again for all this, it has been great learning experience in possibilities and syntax of awk. And some grep on the way as well smile.

Btw, do I understand/read correctly that the C snippet you posted actuslly removes need for xev, thereby removing dependency along with the heaviest process of the equation? Cause that is the part it seems to replace.

Last edited by Chrysostomus (2015-10-20 01:37:12)


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

Offline

#27 2015-10-20 11:26:57

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

Re: Trouble piping xev output - wm independent desktop menu [solved]

Chrysostomus wrote:

Btw, do I understand/read correctly that the C snippet you posted actuslly removes need for xev

Yes.

It does sacrifice the config file - but it'd be easy to add that as a new feature if you wanted.  I figured the simplest approach first would be the easiest to understand and build from.  And as the cmd array is readable and easy to edit (and as this should compile in a fraction of a second on any hardware that would run arch) there might not be a need for a seperate config file.

If you want to tinker with this, you might also consider replacing the "system()" function with a more robust fork() + exec().  Otherwise if you stick with system() you'll want to ensure that any command launched that will not exit immediately is backgrounded with a & at the end (otherwise this program will block waiting for the child program to finish).

The part of the code that may need the most explanation to a non C user would be the if statement.  I suspect the ev.xbutton.button could be intuitive - that's the button number pressed.  But the "if (cmd[N]) system(cmd[N]);" tests whether there is something defined at position N of the array, and if there is run that command, otherwise do nothing - this allows you to set whichever elements of 'cmd' you want without having to explicitly set the ones you don't want to respond to.  In otherwords, the following command array would work as expected too:

const char *cmd[] = {
   [1] = "echo button one",
   [3] = "echo button three"
};

This would respond to buttons one and three but ignore button two and anything else.


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

Online

#28 2015-10-20 12:00:43

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

Re: Trouble piping xev output - wm independent desktop menu [solved]

Cool!
I think I'd prefer to have the config file. If it was only for personal use then just editing the source would be sufficient (and for personal use there is little need for that either, since I know what I need the first time). But I intend to package this and wish it to be beginner friendly too, since there is currently no alternative tool for the task as ffar as I know.


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

Offline

#29 2015-10-20 13:01:38

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

Re: Trouble piping xev output - wm independent desktop menu [solved]

Sxhkd would do this - but it will also do a lot more.  If you want *just* mouse binding you could reinvent a smaller wheel.


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

Online

#30 2015-10-20 13:44:50

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

Re: Trouble piping xev output - wm independent desktop menu [solved]

It does? Cause that's like 100 times better if it does. How do you specify sxhkd to trigger binds only on root window? Cause I spent some days trying to figure out how to do that and came out empty handed.


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

Offline

#31 2015-10-20 14:24:15

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

Re: Trouble piping xev output - wm independent desktop menu [solved]

Sorry I've never used it - I assumed it would be able to do this, perhaps I was wrong.


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

Online

#32 2015-10-23 10:13:17

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

Re: Trouble piping xev output - wm independent desktop menu [solved]

Okay, I looked into making config file for c progrsms and looled through sxhkds source code to see how it is done there. I did not come across explanations/examples simple enough for me to understand. Maybe I google wrong terms, but it seems that I have to study C for real to do this properly. That's gonna take time with small kids and work, so it is long term goal.

My second ides was to make bash wrapper script to set enviroment variables and source them in the c script. But that causes a unnecessary extra process and proved to be complicadtef as well. Not much better thsn what I'm doing now.

Then I realized that instead of having bash script run c program, I could make c program run bash scripts smile.
I could call script buttonaction with system call, and have it run vommands according to config it first creates. That could be a nice workaround hack until I can learn me some C smile.

What do you think? Good hack or bad hack? Or won't it work due to some detail I have overlooked?


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

Offline

#33 2015-10-23 11:39:24

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

Re: Trouble piping xev output - wm independent desktop menu [solved]

Another alternative would just be to pass each command as a parameter:

#include <stdlib.h>
#include <X11/Xlib.h>

int main(int argc, const char **argv) {
	Display *dpy = XOpenDisplay(0x0);
	XSelectInput(dpy, DefaultRootWindow(dpy), ButtonPressMask);
	XEvent ev;
	while (!XNextEvent(dpy,&ev))
		if (ev.xbutton.button < argc ) system(argv[ev.xbutton.button]);
	return 0;
}

Compile with `gcc -o binder binder.c -lX11` then run with one positional parameter per button - be sure to include an empty string for any unused button:

binder "echo one" "" "echo three"

As this still uses the system function you'd want to background anything that doesn't complete immediately:

binder "dboxmenu &" "" "" "bspc desktop -f next" "bspc desktop -f prev"

Then you could put this in a script:

#!/bin/bash

commands=(
  'first command'
  'second command backgrounded &'
  ''
  ''
  'fifth command'
)

exec binder "${commands[@]}"

No extra process, and yet an easy to edit list of commands in a bash array.


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

Online

#34 2015-10-24 06:22:41

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

Re: Trouble piping xev output - wm independent desktop menu [solved]

Yeah, that seems cleaner than hardcoding it to a script name and changing script contents. Simple and elegant, like suckless software. Thanks!


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

Offline

Board footer

Powered by FluxBB