~vlijm/wswitcher/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
import subprocess    
import os
import time

"""
UNITY WallpaperSwitcher
Copyright (C) 2015  Jacob Vlijm
contact: vlijm@planet.nl
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or any later version. This
program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details. You
should have received a copy of the GNU General Public License along with this
program.  If not, see <http://www.gnu.org/licenses/>.
"""

workspace_data = os.environ["HOME"]+"/.config/wswitcher/wallpaper_data_"
key = [
    "gsettings get ",
    "gsettings set ",
    "org.gnome.desktop.background picture-uri",
    ]

def getwall():
    return subprocess.check_output(
        ["/bin/bash", "-c", key[0]+key[2]]
        ).decode("utf-8").strip()

def get_res():
    # get resolution
    xr = subprocess.check_output(["xrandr"]).decode("utf-8").split()
    pos = xr.index("current")
    return [int(xr[pos+1]), int(xr[pos+3].replace(",", "") )]

def current():
    # get the current viewport
    res = get_res()
    vp_data = subprocess.check_output(
        ["wmctrl", "-d"]
        ).decode("utf-8").split()
    dt = [int(n) for n in vp_data[3].split("x")]
    cols = int(dt[0]/res[0])
    curr_vpdata = [int(n) for n in vp_data[5].split(",")]
    curr_col = int(curr_vpdata[0]/res[0])+1
    curr_row = int(curr_vpdata[1]/res[1])
    return str(curr_col+curr_row*cols)

curr_ws1 = current()
currwall1 = getwall()

while True:
    time.sleep(1)
    currwall2 = getwall()
    # print(currwall2)
    curr_ws2 = current()
    datafile = workspace_data+curr_ws2
    if curr_ws2 == curr_ws1:
        if currwall2 != currwall1:
            open(datafile, "wt").write(currwall2)
    else:
        if not os.path.exists(datafile):
            open(datafile, "wt").write(currwall2)
        else:
            curr_set = open(datafile).read()
            command = key[1]+key[2]+' "'+str(curr_set)+'"'
            subprocess.Popen(["/bin/bash", "-c", command])
    curr_ws1 = curr_ws2
    currwall1 = getwall()