1 ##############################################################################
2 ### The history window placement algorithm. ebind historyplacement.place ###
3 ### to the ob.EventAction.PlaceWindow event to use it. ###
4 ##############################################################################
6 import windowplacement, config
9 """Place a window usingthe history placement algorithm."""
12 export_functions = place
14 ##############################################################################
16 config.add('historyplacement',
17 'ignore_requested_positions',
18 'Ignore Requested Positions',
19 "When true, the placement algorithm will attempt to place " + \
20 "windows even when they request a position (like XMMS can)." + \
21 "Note this only applies to 'normal' windows, not to special " + \
22 "cases like desktops and docks.",
25 config.add('historyplacement',
28 "When true, if 2 copies of the same match in history are to be " + \
29 "placed before one of them is closed (so it would be placed " + \
30 "over-top of the last one), this will cause the second window to "+\
31 "not be placed via history, and the 'Fallback Algorithm' will be "+\
35 config.add('historyplacement',
37 'History Database Filename',
38 "The name of the file where history data will be stored. The " + \
39 "number of the screen is appended onto this name. The file will " +\
40 "be placed in ~/.openbox/.",
43 config.add('historyplacement',
46 "The window placement algorithm that will be used when history " + \
47 "placement does not have a place for the window.",
49 windowplacement.random,
50 options = windowplacement.export_functions)
51 config.add('historyplacement',
53 'Confirm Placement Callback',
54 "A function which will be called before attempting to place a " + \
55 "window via history. If the function returns true, then an " + \
56 "attempt will be made to place the window. If it returns false, " +\
57 "the 'Fallback Algorithm' will be directly applied instead. The " +\
58 "function must take 1 argument, which will be the callback data " +\
59 "which was passed to invoke the window placement.",
63 ###########################################################################
65 ###########################################################################
66 ### Internal stuff, should not be accessed outside the module. ###
67 ###########################################################################
77 def __init__(self, appname, appclass, role, x, y):
78 self.appname = appname
79 self.appclass = appclass
84 def __eq__(self, other):
85 if self.appname == other.appname and \
86 self.appclass == other.appclass and \
87 self.role == other.role:
94 file = open(os.environ['HOME'] + '/.openbox/' + \
95 config.get('historyplacement', 'filename') + \
96 "." + str(data.screen), 'r')
98 for line in file.readlines():
99 line = line[:-1] # drop the '\n'
101 s = string.split(line, '\0')
102 state = _state(s[0], s[1], s[2],
103 string.atoi(s[3]), string.atoi(s[4]))
105 while len(_data)-1 < data.screen:
107 _data[data.screen].append(state)
109 except ValueError: pass
110 except IndexError: pass
116 file = open(os.environ['HOME']+'/.openbox/'+ \
117 config.get('historyplacement', 'filename') + \
118 "." + str(data.screen), 'w')
120 while len(_data)-1 < data.screen:
122 for i in _data[data.screen]:
123 file.write(i.appname + '\0' +
130 def _create_state(data):
132 area = data.client.area()
133 return _state(data.client.appName(), data.client.appClass(),
134 data.client.role(), area.x(), area.y())
136 def _find(screen, state):
139 return _data[screen].index(state)
143 while len(_data)-1 < screen:
145 return _find(screen, state) # try again
150 if not (config.get('historyplacement', 'ignore_requested_positions') \
151 and data.client.normal()):
152 if data.client.positionRequested(): return
153 state = _create_state(data)
155 confirm = config.get('historyplacement', 'confirm_callback')
156 if not confirm or confirm(data):
157 print "looking for : " + state.appname + " : " + \
158 state.appclass + " : " + state.role
160 i = _find(data.screen, state)
162 coords = _data[data.screen][i]
163 print "Found in history ("+str(coords.x)+","+\
165 if not (config.get('historyplacement', 'dont_duplicate') \
167 data.client.move(coords.x, coords.y)
171 print "Already placed another window there"
173 print "No match in history"
176 fallback = config.get('historyplacement', 'fallback')
177 if fallback: fallback(data)
179 def _save_window(data):
182 state = _create_state(data)
183 print "looking for : " + state.appname + " : " + state.appclass + \
186 i = _find(data.screen, state)
189 _data[data.screen][i] = state # replace it
192 _data[data.screen].append(state)
194 ob.ebind(ob.EventAction.CloseWindow, _save_window)
195 ob.ebind(ob.EventAction.Startup, _load)
196 ob.ebind(ob.EventAction.Shutdown, _save)
198 print "Loaded historyplacement.py"