You are not logged in.

#1 2015-12-06 23:09:51

pingu
Member
Registered: 2015-09-29
Posts: 128

Cyle through monitor layouts (.screenlayout/*.sh)

I  missed the the W-F8 keybind from windoze that would cycle through monitor layouts, and I finally decided to do something about it big_smile

In case it might be useful to someone else here it is:

#!/usr/bin/env python
import os

# ALL SCRIPTS INSIDE $HOME/.screenlayout WILL BE EXECUTED; BE CAREFUL.
# You can use arandr to write the layout scripts (which is just a script to execute the 
# xrandr command --which is can be tricky to know by heart. 
# reads from $HOME/.screenlayout and
# writes to a file called .ref in it by default.

HOME = os.path.expanduser("~")
path = ("%s/.screenlayout/" % HOME)

# writes a key for the current layout to the .ref file
# if the file is not found, writes 0 as default

def set_reference(ref):
    if '.ref' not in os.listdir(path):
	 set_reference(0)
    with open("%s.ref" % path, 'w') as f:
        f.write("%i" % ref)
        f.close()

# reads from the .ref file to figure out the current layout

def get_reference():
    with open("%s.ref" % path, 'r') as f:
        ref = int(f.readline())
        f.close()
    return ref

# adds every file in path that's not the .ref file"""
# to a dictionary with an incremental number as a key

def get_layouts():
    n = 0
    layouts = {}
    for layout in os.listdir(path):
        if layout != '.ref':
            layouts.update({n:layout})
            n += 1
    return layouts

# executes the layout script

def exec_layout(layout):
    os.system("%s./%s" % (path, layout))

def main ():
    layouts = get_layouts()
    ref = get_reference()
    ref += 1
    if ref in layout.keys():
        exec_layout(layouts.get(ref))
    else:
        ref = 0
        exec_layout(layouts.get(ref))
    set_reference(ref)
main()

(It's probably not the best implementation but it solved my problem.)

and I added  these lines in .config/openbox/rc.xml inside the keyboard section (after <keyboard> and before </keyboard>):

    <keybind key="W-8">
      <action name="Execute">
        <command>python /home/pingu/scripts/layout_next.py</command>
      </action>
    </keybind>

And it only works if there are only layout scripts (no weird stuff) in the .screenlayout/ (they are created when you save a screen layout with arandr).

edit: and DO NOT put this script in .screenlayout/ . that will be super crazy!

edit2: i just realized it only works with python2.. at least on my setup.
ps: any suggestion would be very welcomed.

edit3: I figured out what wasn't working on python3 (dictionaries don't have the "has_key()" function) so it works on both now, and I've added some comments.

Last edited by pingu (2015-12-15 17:13:20)


"Chuck Norris can compile syntax errors."

Offline

#2 2015-12-07 06:22:42

Snap
Member
Registered: 2015-10-02
Posts: 465

Re: Cyle through monitor layouts (.screenlayout/*.sh)

Thanks, pingu. Very interesting. I think it will be really useful for the computer at my job. We often need it dual head and extended desktop to run clips and images into a projector during shows, but for most of the shows the projector is off. Toggling modes with a simple keystroke sounds like a perfect fit. Great job.

Offline

#3 2015-12-07 11:29:17

pingu
Member
Registered: 2015-09-29
Posts: 128

Re: Cyle through monitor layouts (.screenlayout/*.sh)

I'm happy it helps. Just be sure to test it before the big meeting!
And remember it will execute any script inside .screenlayout/, so don't place NukeIt.sh  in there. wink


"Chuck Norris can compile syntax errors."

Offline

Board footer

Powered by FluxBB