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