swapmon / xswapmonitors

Window managers, icon programs, widgets, etc.
Post Reply
Message
Author
User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

swapmon / xswapmonitors

#1 Post by smokey01 »

swapmon is a small GUI script that swaps you monitors. In other words your left monitor becomes you right and vice versa. It does nothing else.

It uses xrandr to do all the smart stuff which most pups have installed by default. You will need to use xrandr version 1.5 or later as it uses the --listmonitors switch to identify your monitors. Some earlier versions did not have this switch.

Version 1.0 of swapmon has two comboboxes. Select the monitors for left and right then press the swap button. If you don't know the names of your monitors, don't worry, just reverse the selection until you get what you want. Nothing will happen if you select the same monitor for left and right. It will only work if you select two different monitors. The comboboxes are blank when first run and pressing the swap button will do nothing.

When you click on the swap button, three actions take place.

1. It swaps your monitors,
2. It places a script in /root/Startup so the changes are persistent, and
3. It makes the script executable.

As this is a script that will probably only be run at setup I haven't provided a .desktop file. If you place it in one of bins, say /usr/bin, /usr/local/bin or /usr/sbin, it will run from a terminal when you type swapmon or just click on the script.

There are two other ways I have considered to swap monitors.

1. Make a script with a single toggle button. Each time the button is pressed the monitors are swapped back and forth with no other input required.

2. An even simpler method might be to have a script than swaps the monitors without any input from the user other than running the script. Still thinking about how to do this.

Code: Select all

#!/bin/sh
# swapmon ver 1.0
# Written by smokey01
# 01 September 2016
# It requires xrandr 1.5 or later

[ -z $GTKDIALOG ] && GTKDIALOG=gtkdialog

# This creates a list of your connected monitors
xrandr --listmonitors | awk '{print $NF}' | tail -n +2 > /tmp/list

# This generates a combobox list of monitors 
MONITORS="<item>$monitor</item>"
for I in $(cat /tmp/list) ; do MONITORS=`echo "$MONITORS<item>$I</item>"`; done

# This is the function that swaps the monitors and saves the results in /root/Startup/setmonitors
SWAP () {
	xrandr --output $MONITOR0 --auto --output $MONITOR1 --auto --right-of $MONITOR0
	echo xrandr --output $MONITOR0 --auto --output $MONITOR1 --auto --right-of $MONITOR0 > /root/Startup/setmonitors
	chmod 755 /root/Startup/setmonitors
}
export -f SWAP

MAIN_DIALOG='
<window icon-name="gtk-preferences" title="Swap Monitors" width-request="215">
	<vbox>
		<hbox>
			<combobox width-request="100" tooltip-text="Select Left Monitor">
				<variable>MONITOR0</variable>
				'$MONITORS'
			</combobox>
			<combobox width-request="100" tooltip-text="Select Right Monitor">
				<variable>MONITOR1</variable>
				'$MONITORS'
			</combobox>
		</hbox>	
		<hbox>
			<button>
			<input file stock="gtk-quit"></input>
			<label>Quit</label>
			</button>
			<button>
			<input file stock="gtk-network"></input>
			<label>Swap</label>
			<action>SWAP</action>
			</button>
		</hbox>
	</vbox>
</window>
'
export MAIN_DIALOG

case $1 in
	-d | --dump) echo "$MAIN_DIALOG" ;;
	*) $GTKDIALOG --program=MAIN_DIALOG ;;
esac
If it works, use it, if it doesn't, don't. It may not work in all pups. I have only tested it in Fatdog64-710 and slacko-700a.

Cheers

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#2 Post by smokey01 »

jlst has a far more elegant solution here:
http://www.murga-linux.com/puppy/viewto ... 397#921397
The problem is it didn't work in Fatdog64-710 as Fatdog64 doesn't have the directory below.
./xswapmonitors: line 61: /usr/lib/gtkdialog/xml_info: No such file or directory
./xswapmonitors: line 66: /usr/lib/gtkdialog/xml_info: No such file or directory
I copied the directory from slacko64-700a to Fatdog-710 and it works fine but this will be a snag for many Fatdog users.

jlst

#3 Post by jlst »

This should work fine in fatdog, it falls back to yad for the simple dialogs
Attachments
xswapmonitors.gz
(1.19 KiB) Downloaded 168 times

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#4 Post by smokey01 »

jlst wrote:This should work fine in fatdog, it falls back to yad for the simple dialogs
I don't think yad comes with fatdog by default. I normally compile it and add it. I will check it out later when I get home.

Thanks

gyro
Posts: 1798
Joined: Tue 28 Oct 2008, 21:35
Location: Brisbane, Australia

Re: swapmon / xswapmonitors

#5 Post by gyro »

smokey01 wrote:1. Make a script with a single toggle button. Each time the button is pressed the monitors are swapped back and forth with no other input required.

2. An even simpler method might be to have a script than swaps the monitors without any input from the user other than running the script. Still thinking about how to do this.
For either of these to work you need to know which monitor is already on the left.
I haven't worked out the code to do it but the information is in 3rd word of each of the lines from xrandr, for mine, the relevant parts of the lines are:
VGA1 connected 1280x1024+1280+0
HDMI1 connected 1280x1024+0+0
The +1280+0 and +0+0 are surely the current offsets to that monitor, so my HDMI1 is on the left because it has offset +0+0.

Modifying the thing so it works out which one is currently on the left would be nice. As I said in the slacko64 thread, the first time I used it I was not sure which of the names referred to the monitor on the left. I only found out when "swap" did nothing, then changing them around meant "swap" dit it.

gyro

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#6 Post by smokey01 »

gyro my idea for a single button would be just to swap the monitors. It doesn't matter which monitor is on the left. If it's not they way you want it, press the button and they would swap, job done.

I don't need to know which monitor is physically connect to the left or right.
:lol: :lol: :lol: :wink:

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#7 Post by smokey01 »

jlst wrote:This should work fine in fatdog, it falls back to yad for the simple dialogs
It does thanks but I have yad installed.

Without yad it stills works but doesn't give you the option to restart X, which is understandable.

gyro
Posts: 1798
Joined: Tue 28 Oct 2008, 21:35
Location: Brisbane, Australia

#8 Post by gyro »

smokey01 wrote: don't need to know which monitor is physically connect to the left or right.
:lol: :lol: :lol: :wink:
No, you just have to know what order you used last time.
And of course if it's been run already the answer is already in /root/Startup.
So, you could just modify the current code so it presets the values ready for a swap. First run, the selection is arbitary, so it could just be the order they are reported.
gyro

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#9 Post by smokey01 »

@gyro
Here is my attempt at a single toggle button version.

Code: Select all

#!/bin/sh
# swapmon ver 1.1
# Written by smokey01 with help from jlst and CatDude
# 03 September 2016

xrandr | grep connected | grep -v disconnected | sed 's| .*||' | awk '{print}' ORS=' ' > /tmp/mons

read -r mon0 mon1 < /tmp/mons
export mon0 mon1

FIRST () {
   xrandr --output $mon0 --auto --output $mon1 --auto --right-of $mon0
   echo xrandr --output $mon0 --auto --output $mon1 --auto --right-of $mon0 > /root/Startup/setmonitors
   chmod 755 /root/Startup/setmonitors
}
export -f FIRST

SECOND () {
   xrandr --output $mon1 --auto --output $mon0 --auto --right-of $mon1
   echo xrandr --output $mon1 --auto --output $mon0 --auto --right-of $mon1 > /root/Startup/setmonitors
   chmod 755 /root/Startup/setmonitors
}
export -f SECOND

DELETE () {
	rm -f /root/Startup/setmonitors
}
export -f DELETE

export MAIN_DIALOG='
<window icon-name="gtk-preferences" window-position="3" title="Swap Monitors 1.1" width-request="210">
   <vbox homogenous="true">      
      <hbox>               
         <togglebutton tooltip-text=" Swaps your monitors left to right " active="false" width-request="200">
            <label>Swap Your Monitors</label>
            <input file stock="gtk-refresh"></input>
               <action>if true FIRST</action>
               <action>if false SECOND</action>
            </togglebutton>
		</hbox>
		<hbox homogeneous="true">
			<button tooltip-text=" Quit the application. ">
				<label>Quit</label>
				<input file stock="gtk-quit"></input>
				<action>exit:EXIT</action>
			</button>
			<button tooltip-text=" This will delete your monitor settings. ">
				<label>Delete</label>
				<input file stock="gtk-delete"></input>
				<action>DELETE</action>
			</button>
		</hbox>
   </vbox>
   <variable>MAIN_DIALOG</variable>
</window>'

gtkdialog --program=MAIN_DIALOG

gyro
Posts: 1798
Joined: Tue 28 Oct 2008, 21:35
Location: Brisbane, Australia

#10 Post by gyro »

Yes, that works for me.
gyro

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#11 Post by smokey01 »

I just added jlst monitor checking routine. If two monitors are not connected then the script exits after popping up an Xdialog message. No gettext though.

It's a real gz file, just click on it to extract it. You may have to make it executable.
Attachments
swapmon-1.2.gz
(921 Bytes) Downloaded 172 times

Post Reply