1 ##############################################################################
2 ### The history window placement algorithm. ebind historyplacement.place ###
3 ### to the ob.EventAction.PlaceWindow event to use it. ###
4 ##############################################################################
6 import windowplacement # fallback routines
8 ##############################################################################
9 ### Options for the historyplacement module (Options in the ###
10 ### windowplacement module also apply!): ###
12 # ignore_requested_positions - When true, the placement algorithm will ###
13 ### attempt to place windows even when they ###
14 ### request a position (like XMMS). ###
15 ### Note this only applies to normal windows, ###
16 ### not to special cases like desktops and ###
18 ignore_requested_positions = 0 ###
19 # fallback - The window placement algorithm that will be used when history ###
20 ### placement does not have a place for the window. ###
21 fallback = windowplacement.random ###
22 # confirm_callback - set this to a function to have the function called ###
23 ### before attempting to place a window via history. If ###
24 ### the function returns 'true' then an attempt will be ###
25 ### made to place the window. If it returns 'false', the ###
26 ### fallback method will be directly applied instead. ###
27 confirm_callback = 0 ###
29 # filename - The name of the file where history data will be stored. The ###
30 ### number of the screen is appended onto this filename. ###
31 filename = 'historydb' ###
33 ##############################################################################
43 def __init__(self, appname, appclass, role, x, y):
44 self.appname = appname
45 self.appclass = appclass
49 def __eq__(self, other):
50 if self.appname == other.appname and \
51 self.appclass == other.appclass and \
52 self.role == other.role:
58 file = open(os.environ['HOME']+'/.openbox/'+filename+"."+str(data.screen),
62 for line in file.readlines():
63 line = line[:-1] # drop the '\n'
65 s = string.split(line, '\0')
66 state = _state(s[0], s[1], s[2],
67 string.atoi(s[3]), string.atoi(s[4]))
69 while len(_data)-1 < data.screen:
71 _data[data.screen].append(state)
81 file = open(os.environ['HOME']+'/.openbox/'+filename+"."+str(data.screen),
84 while len(_data)-1 < data.screen:
86 for i in _data[data.screen]:
87 file.write(i.appname + '\0' +
94 def _create_state(data):
96 area = data.client.area()
97 return _state(data.client.appName(), data.client.appClass(),
98 data.client.role(), area.x(), area.y())
100 def _find(screen, state):
103 return _data[screen].index(state)
107 while len(_data)-1 < screen:
109 return _find(screen, state) # try again
114 if not (ignore_requested_positions and data.client.normal()):
115 if data.client.positionRequested(): return
116 state = _create_state(data)
118 if not confirm_callback or confirm_callback(data):
119 print "looking for : " + state.appname + " : " + \
120 state.appclass + " : " + state.role
122 i = _find(data.screen, state)
124 coords = _data[data.screen][i]
125 print "Found in history ("+str(coords.x)+","+\
127 data.client.move(coords.x, coords.y)
130 print "No match in history"
133 if fallback: fallback(data)
135 def _save_window(data):
138 state = _create_state(data)
139 print "looking for : " + state.appname + " : " + state.appclass + \
142 i = _find(data.screen, state)
145 _data[data.screen][i] = state # replace it
148 _data[data.screen].append(state)
150 ob.ebind(ob.EventAction.CloseWindow, _save_window)
151 ob.ebind(ob.EventAction.Startup, _load)
152 ob.ebind(ob.EventAction.Shutdown, _save)
154 print "Loaded historyplacement.py"