]> icculus.org git repositories - dana/openbox.git/blob - scripts/historyplacement.py
historyplacement!!
[dana/openbox.git] / scripts / historyplacement.py
1 ##############################################################################
2 ### The history window placement algorithm. ebind historyplacement.place   ###
3 ### to the ob.EventAction.PlaceWindow event to use it.                     ###
4 ##############################################################################
5
6 import windowplacement # fallback routines
7
8 ##############################################################################
9 ### Options for the historyplacement module:                               ###
10 ###                                                                        ###
11 # fallback - The window placement algorithm that will be used when history ###
12 ###          placement does not have a place for the window.               ###
13 fallback = windowplacement.random                                          ###
14 ###                                                                        ###
15 # filename - The name of the file where history data will be stored. The   ###
16 ###          number of the screen is appended onto this filename.          ###
17 filename = 'historydb'                                                     ###
18 ###                                                                        ###
19 ##############################################################################
20
21 import otk
22 import ob
23 import os
24 import string
25
26 _data = []
27
28 class _state:
29     def __init__(self, appname, appclass, role, x, y):
30         self.appname = appname
31         self.appclass = appclass
32         self.role = role
33         self.x = x
34         self.y = y
35     def __eq__(self, other):
36         if self.appname == other.appname and \
37            self.appclass == other.appclass and \
38            self.role == other.role:
39             return 1
40         return 0
41
42 def _load(data):
43     file = open(os.environ['HOME']+'/.openbox/'+filename+"."+str(data.screen),
44                 'r')
45     if file:
46         print "loading: "
47         # read data
48         for line in file.readlines():
49             line = line[:-1] # drop the '\n'
50             try:
51                 print string.split(line, '\0')
52                 print line.count('\0')
53                 s = string.split(line, '\0')
54                 state = _state(s[0], s[1], s[2],
55                                string.atoi(s[3]), string.atoi(s[4]))
56
57                 while len(_data)-1 < data.screen:
58                     _data.append([])
59                 _data[data.screen].append(state)
60                 
61                 print "  "+s[0]+" "+s[1]+" "+s[2]
62                 print "     " + str(s[3]) + "," + str(s[4])
63             except ValueError:
64                 print "ValueError"
65                 pass
66             except IndexError:
67                 print "IndexError"
68                 pass
69         print "DONE loading."
70         file.close()
71
72 def _save(data):
73     file = open(os.environ['HOME']+'/.openbox/'+filename+"."+str(data.screen),
74                 'w')
75     if file:
76         print "saving: "
77         while len(_data)-1 < data.screen:
78             _data.append([])
79         for i in _data[data.screen]:
80             file.write(i.appname + '\0' +
81                        i.appclass + '\0' +
82                        i.role + '\0' +
83                        str(i.x) + '\0' +
84                        str(i.y) + '\n')
85             print "  "+i.appname+" "+i.appclass+" "+i.role
86             print "     " + str(i.x) + "," + str(i.y)
87         print "DONE saving."
88         file.close()
89
90 def place(data):
91     print "placing"
92     if data.client:
93         state = _state(data.client.appName(), data.client.appClass(),
94                        data.client.role(), 0, 0)
95         while len(_data)-1 < data.screen:
96             _data.append([])
97         print "looking for :"
98         print "  " + state.appname
99         print "  " + state.appclass
100         print "  " + state.role
101         try:
102             i = _data[data.screen].index(state)
103             print "got it"
104             coords = _data[data.screen][i]
105             print "Found in history ("+str(coords.x)+","+str(coords.y)+")"
106             data.client.move(coords.x, coords.y)
107         except ValueError:
108             print "No match in history"
109             fallback(data)
110
111 def _save_window(data):
112     print "saving"
113     if data.client:
114         area = data.client.area()
115         state = _state(data.client.appName(), data.client.appClass(),
116                        data.client.role(), area.x(), area.y())
117         while len(_data)-1 < data.screen:
118             _data.append([])
119         print "looking for :"
120         print "  " + state.appname
121         print "  " + state.appclass
122         print "  " + state.role
123         try:
124             i = _data[data.screen].index(state)
125             print "replacing"
126             _data[data.screen][i] = state # replace it
127         except ValueError:
128             print "appending"
129             _data[data.screen].append(state)
130
131 ob.ebind(ob.EventAction.CloseWindow, _save_window)
132 ob.ebind(ob.EventAction.Startup, _load)
133 ob.ebind(ob.EventAction.Shutdown, _save)
134
135 print "Loaded historyplacement.py"