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 ###
20 # fallback - The window placement algorithm that will be used when history ###
21 ### placement does not have a place for the window. ###
22 fallback = windowplacement.random ###
24 # confirm_callback - set this to a function to have the function called ###
25 ### before attempting to place a window via history. If ###
26 ### the function returns 'true' then an attempt will be ###
27 ### made to place the window. If it returns 'false', the ###
28 ### fallback method will be directly applied instead. ###
29 confirm_callback = 0 ###
31 # filename - The name of the file where history data will be stored. The ###
32 ### number of the screen is appended onto this filename. ###
33 filename = 'historydb' ###
35 ##############################################################################
45 def __init__(self, appname, appclass, role, x, y):
46 self.appname = appname
47 self.appclass = appclass
51 def __eq__(self, other):
52 if self.appname == other.appname and \
53 self.appclass == other.appclass and \
54 self.role == other.role:
60 file = open(os.environ['HOME']+'/.openbox/'+filename+"."+str(data.screen),
64 for line in file.readlines():
65 line = line[:-1] # drop the '\n'
67 s = string.split(line, '\0')
68 state = _state(s[0], s[1], s[2],
69 string.atoi(s[3]), string.atoi(s[4]))
71 while len(_data)-1 < data.screen:
73 _data[data.screen].append(state)
83 file = open(os.environ['HOME']+'/.openbox/'+filename+"."+str(data.screen),
86 while len(_data)-1 < data.screen:
88 for i in _data[data.screen]:
89 file.write(i.appname + '\0' +
96 def _create_state(data):
98 area = data.client.area()
99 return _state(data.client.appName(), data.client.appClass(),
100 data.client.role(), area.x(), area.y())
102 def _find(screen, state):
105 return _data[screen].index(state)
109 while len(_data)-1 < screen:
111 return _find(screen, state) # try again
116 if not (ignore_requested_positions and data.client.normal()):
117 if data.client.positionRequested(): return
118 state = _create_state(data)
120 if not confirm_callback or confirm_callback(data):
121 print "looking for : " + state.appname + " : " + \
122 state.appclass + " : " + state.role
124 i = _find(data.screen, state)
126 coords = _data[data.screen][i]
127 print "Found in history ("+str(coords.x)+","+\
129 data.client.move(coords.x, coords.y)
132 print "No match in history"
135 if fallback: fallback(data)
137 def _save_window(data):
140 state = _create_state(data)
141 print "looking for : " + state.appname + " : " + state.appclass + \
144 i = _find(data.screen, state)
147 _data[data.screen][i] = state # replace it
150 _data[data.screen].append(state)
152 ob.ebind(ob.EventAction.CloseWindow, _save_window)
153 ob.ebind(ob.EventAction.Startup, _load)
154 ob.ebind(ob.EventAction.Shutdown, _save)
156 print "Loaded historyplacement.py"