Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
fluxbox: focus, key, conditional statement, xdotool
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Desktop Environments
View previous topic :: View next topic  
Author Message
dpaddy
Tux's lil' helper
Tux's lil' helper


Joined: 25 Jun 2008
Posts: 117

PostPosted: Sun Feb 24, 2019 10:44 pm    Post subject: fluxbox: focus, key, conditional statement, xdotool Reply with quote

I'ld like for fluxbox to perform an action when the mouse cursor enters a window (or a window gains focus), but I don't know how to do that (it seems unthinkable that fluxbox developers would have overlooked such useful functionality).

Failing that, I'ld like the else action of a fluxbox conditional statement that is triggered by a key press to simply leave the key event alone (i.e., not consume the key press but instead pass it along to whatever window has focus), but I don't know how to do that (it seems unthinkable that fluxbox developers would have overlooked such useful functionality).

Failing that, I'ld like xdotool to send, for example, ctrl+s (as in xdotool getactivewindow key --clearmodifiers ctrl+s) to the active window and have that window behave as if I typed ctrl+s EVEN THOUGH I HAVE ctrl DEPRESSED when the xdotool command is executing, but I don't know how to do that (it seems unthinkable that xdotool developers would have overlooked such useful functionality).

I am woefully ignorant as to how any of the above can be achieved. :oops:
Back to top
View user's profile Send private message
dpaddy
Tux's lil' helper
Tux's lil' helper


Joined: 25 Jun 2008
Posts: 117

PostPosted: Mon Feb 25, 2019 4:50 am    Post subject: Reply with quote

The program :idea:
Code:
/*
gcc -o send_key send_key.c -L/usr/lib/x86_64-linux-gnu -lX11 -lXtst
./send_key a b c d C-a C-f
*/

#include <X11/extensions/XTest.h>
#include <string.h>

KeySym      Mod;
const char *mod[] = { "", "Meta_L", "Control_L", "Alt_L", "Shift_L" };

KeyCode parse(char **argv, int arg)
{
  if (!strncasecmp(argv[arg], "M-", 2)){
    memmove(argv[arg], argv[arg]+2, strlen(argv[arg])-1);
    return Mod = 1;
  }
  if (!strncasecmp(argv[arg], "C-", 2)){
    memmove(argv[arg], argv[arg]+2, strlen(argv[arg])-1);
    return Mod = 2;
  }
  if (!strncasecmp(argv[arg], "A-", 2)){
    memmove(argv[arg], argv[arg]+2, strlen(argv[arg])-1);
    return Mod = 3;
  }
  if (!strncasecmp(argv[arg], "S-", 2)){
    memmove(argv[arg], argv[arg]+2, strlen(argv[arg])-1);
    return Mod = 4;
  }
  return Mod = 0;
}

int main(int argc, char **argv)
{
  Display *dsp;  KeySym sym;  KeyCode key, code;  int i = 0;

  if (!(dsp = XOpenDisplay(NULL))) return 1;
  while (++i < argc){
    if ((code = parse(argv, i))){
      if ((Mod = XStringToKeysym(mod[Mod])) == NoSymbol) continue;
      if (!(code = XKeysymToKeycode(dsp, Mod))         ) continue;
    }
    if ((sym = XStringToKeysym(argv[i])) == NoSymbol) continue;
    if (!(key = XKeysymToKeycode(dsp, sym))         ) continue;
    if (code)
      XTestFakeKeyEvent(dsp, code, True, 0);           // press
    XTestFakeKeyEvent(dsp, key, True, 0);              // press
    XTestFakeKeyEvent(dsp, key, False, 0);             // release
    if (code)
      XTestFakeKeyEvent(dsp, code, False, 0);          // release
    XFlush(dsp);
  }
  XCloseDisplay(dsp);
  return 0;
}
seems to work (i.e., send key events) when executed in an xterm, but attempts to use it within ~/.fluxbox/keys as for example
Code:
$ tail -n 1 ~/.fluxbox/keys
Control s :If {Matches (Navigator)} { Exec firefox_keys C-s } { Exec send_key C-s }
does not (this was an attempt to be the equivalent of the else action of a fluxbox conditional statement that is triggered by a key press to simply leave the key event alone (i.e., not consume the key press but instead pass it along to whatever window has focus).

I am probably overly optimistic, but perhaps I'm getting close... help / comments :?:
Back to top
View user's profile Send private message
Hu
Moderator
Moderator


Joined: 06 Mar 2007
Posts: 13498

PostPosted: Tue Feb 26, 2019 3:13 am    Post subject: Reply with quote

dpaddy wrote:
Code:
const char *mod[] = { "", "Meta_L", "Control_L", "Alt_L", "Shift_L" };
Missing const after *. This creates a mutable array of pointers to const char, but there is no need to allow mutation here.
dpaddy wrote:
Code:
KeySym      Mod;
Code:
KeyCode parse(char **argv, int arg)
Code:
    return Mod = 1;
This looks weird. You declared Mod as a KeySym. You declared parse to return a KeyCode. Your return Mod = 1 assigns Mod and also returns it. The return type of parse and the value of Mod should be the same type, but are not. Looking farther down, it appears there is no need for Mod to be global.
dpaddy wrote:
Code:
KeyCode parse(char **argv, int arg)
This should be static.
dpaddy wrote:
Code:
KeyCode parse(char **argv, int arg)

You never use any field other than argv[arg]. Why not pass this one field directly? This would save reloading the value after every comparison.
dpaddy wrote:
Code:
    memmove(argv[arg], argv[arg]+2, strlen(argv[arg])-1);
Instead of rewriting the string, why not just change the pointer?
Code:
argv[arg] += 2;
Your program appears not to handle C-M-x. Without this, how will you handle entering multiple modifier keys?
Back to top
View user's profile Send private message
dpaddy
Tux's lil' helper
Tux's lil' helper


Joined: 25 Jun 2008
Posts: 117

PostPosted: Tue Feb 26, 2019 10:55 pm    Post subject: Reply with quote

Yes, not handling all modifers or chords is far from complete -- it was a simple sanity check for work in progress...

Issues I've noticed are: potential recursion -- when fluxbox maps Control C to an action that sends Control C -- but that is easily handled, also appropriately dissableing and reenabling auto repeat keys, and finally managing the "state machine" that the X server maintains for keyboard events... I'm beginning to make progress wrapping my mind around the issues but will have to punt on this project because a mess of trouble has cropped up and I need to be putting out fires for quite awhile. :(

I do plan on returning to this in a few months and will eventually post (here) whatever I manage to make work. :)

Bye for now.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Desktop Environments All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum