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 # fallback - The window placement algorithm that will be used when history ###
13 ### placement does not have a place for the window. ###
14 fallback = windowplacement.random ###
15 # confirm_callback - set this to a function to have the function called ###
16 ### before attempting to place a window via history. If ###
17 ### the function returns 'true' then an attempt will be ###
18 ### made to place the window. If it returns 'false', the ###
19 ### fallback method will be directly applied instead. ###
20 confirm_callback = 0 ###
22 # filename - The name of the file where history data will be stored. The ###
23 ### number of the screen is appended onto this filename. ###
24 filename = 'historydb' ###
26 ##############################################################################
36 def __init__(self, appname, appclass, role, x, y):
37 self.appname = appname
38 self.appclass = appclass
42 def __eq__(self, other):
43 if self.appname == other.appname and \
44 self.appclass == other.appclass and \
45 self.role == other.role:
51 file = open(os.environ['HOME']+'/.openbox/'+filename+"."+str(data.screen),
55 for line in file.readlines():
56 line = line[:-1] # drop the '\n'
58 s = string.split(line, '\0')
59 state = _state(s[0], s[1], s[2],
60 string.atoi(s[3]), string.atoi(s[4]))
62 while len(_data)-1 < data.screen:
64 _data[data.screen].append(state)
74 file = open(os.environ['HOME']+'/.openbox/'+filename+"."+str(data.screen),
77 while len(_data)-1 < data.screen:
79 for i in _data[data.screen]:
80 file.write(i.appname + '\0' +
87 def _create_state(data):
89 area = data.client.area()
90 return _state(data.client.appName(), data.client.appClass(),
91 data.client.role(), area.x(), area.y())
93 def _find(screen, state):
96 return _data[screen].index(state)
100 while len(_data)-1 < screen:
102 return _find(screen, state) # try again
107 if not (windowplacement.ignore_requested_positions and
108 data.client.normal()):
109 if data.client.positionRequested(): return
110 state = _create_state(data)
112 if not confirm_callback or confirm_callback(data):
113 print "looking for : " + state.appname + " : " + \
114 state.appclass + " : " + state.role
116 i = _find(data.screen, state)
118 coords = _data[data.screen][i]
119 print "Found in history ("+str(coords.x)+","+\
121 data.client.move(coords.x, coords.y)
124 print "No match in history"
127 if fallback: fallback(data)
129 def _save_window(data):
132 state = _create_state(data)
133 print "looking for : " + state.appname + " : " + state.appclass + \
136 i = _find(data.screen, state)
139 _data[data.screen][i] = state # replace it
142 _data[data.screen].append(state)
144 ob.ebind(ob.EventAction.CloseWindow, _save_window)
145 ob.ebind(ob.EventAction.Startup, _load)
146 ob.ebind(ob.EventAction.Shutdown, _save)
148 print "Loaded historyplacement.py"