Register
It is currently Sun Dec 21, 2014 11:35 am

Wmctrl Cycle through Windows


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Thu Jul 21, 2011 8:45 am   

Joined: Thu Jul 21, 2011 8:39 am
Posts: 1
Hi All,

Firstly, I want to be able to cycle through open windows using a LIRC remote. Now before you say irxevent can handle that with Alt-Tab - it doesn't work. Firstly Alt-Tab Switches between the most recent windows and secondly Key Combinations like that require Alt to be pressed and HELD and Tab to then be press - it just won't work with LIRC. So my idea was to use a bash script to handle this called by irexec. I was planning on using wmctrl.

I've found at least 2 scripts for cycling through Virtual Desktops/Workspace, but none for cycling through open windows.

If anyone could help it would be greatly appreciated. I've spend all day on this and am getting no where fast.

Thanks in advance.


Top
 Profile  
 PostPosted: Tue Aug 23, 2011 5:36 pm   
User avatar

Joined: Sat Jul 02, 2011 10:43 am
Posts: 23
Location: Devon,UK
Is this what you are looking for:
1st run this script
Code:
#!/bin/bash

cat >./getcurpos.c <<EOF
/*
   Print the current coordinates of the mouse pointer to stdout

   Copyright (C) 2007 by Robert Manea  <rob dot manea at gmail dot com>

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   THE SOFTWARE.
   */

//   Tweeked by: K.D.Hedger Mon 15 Aug 2011 04:42:54 PM BST


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

int main(int argc, char *argv[])
{
   XEvent e;
   Display *d=XOpenDisplay(NULL);
   
   long int ans=strtol(argv[1], NULL,0);
   if(!d)
      return EXIT_FAILURE;

   /* get info about current pointer position */
   XQueryPointer(d,ans,
         &e.xbutton.root, &e.xbutton.window,
         &e.xbutton.x_root, &e.xbutton.y_root,
         &e.xbutton.x, &e.xbutton.y,
         &e.xbutton.state);

   printf("Local %d %d Global %d %d Button %d\n", e.xbutton.x, e.xbutton.y, e.xbutton.x_root, e.xbutton.y_root,e.xbutton.state);

   XCloseDisplay(d);

   return EXIT_SUCCESS;
}
EOF

rm ./getcurpos 2>/dev/null
gcc -Wall -lX11 getcurpos.c -o ./getcurpos
strip ./getcurpos
rm ./getcurpos.c
sudo mv ./getcurpos /usr/local/bin
exit 0

this installs a small c program to read the global,local mouse positions and buttons
2nd save this script as for instance "cycle" and make it executable, then run it
Code:
#!/bin/bash

############################
#
# cycle windows until mouse press
# K.D.Hedger Wed 24 Aug 2011 12:19:45 AM BST
# kdhedger@yahoo.co.uk
#
############################

#strip tabs and space
stripclean ()
{
   read
   echo "$REPLY"|sed 's/^[ \t]*//;s/[ \t]*$//'
}

#get the window list
refreshwindowlist ()
{   
   WINDOWS=""
   WINDIDS=""
   IDCNT=0

   while read
      do
         DATA=$(wnckprop --window=$REPLY 2>/dev/null)

#only interested in "normal" windows
         if [ "$(echo "$DATA"|grep -i "Window Type:"|awk -F : '{print $2}'|stripclean )" = "normal window" ];then

#window name
            WINDOWS[IDCNT]=$(echo "$DATA"|grep -i "Class Group:"|awk -F : '{print $2}'|stripclean)
#window id
            WINDIDS[$IDCNT]=$REPLY
            ((IDCNT++))
         fi
      done < <(wnckprop --list|awk -F : '{print $1}')
}

GETCURSPOS=/usr/local/bin/getcurpos

refreshwindowlist
#pause at each window 50*0.01 about half a second ( not very accurate )
PAUSE=50

while :
   do
      for ((CNT=0;CNT<IDCNT;CNT++))
         do
#activate window ( switch workspace and bring to front )
            wmctrl -ia ${WINDIDS[$CNT]}

#pause the window and check for left mouse down
            for ((WAIT=0;WAIT<$PAUSE;WAIT++))
               do
                  x=$($GETCURSPOS ${WINDIDS[$CNT]})
                  if [ $(echo $x|awk '{print $8}') -eq 256 ];then
#we want this window
                     break 3
                  fi
                  sleep 0.01
               done
         done
   done

#do some more stuff
echo ${WINDOWS[$CNT]}


The windows will cycle until the left mouse button is pressed, I don't have a IR remote so you will have to get it to simulate a mouse press ( can't help you on that bit ) or possibly just run a kill command ( killall cycle )


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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 post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP