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!) ###
11 ##############################################################################
12 IGNORE_REQUESTED_POSITIONS = 0
13 """When true, the placement algorithm will attempt to place windows even
14 when they request a position (like XMMS). Note this only applies to
15 normal windows, not to special cases like desktops and docks."""
16 FALLBACK = windowplacement.random
17 """The window placement algorithm that will be used when history placement
18 does not have a place for the window."""
20 """Set this to a function to have the function called before attempting to
21 place a window via history. If the function returns a non-zero, then an
22 attempt will be made to place the window. If it returns zero, the
23 fallback method will be directly applied instead."""
24 FILENAME = 'historydb'
25 """The name of the file where history data will be stored. The number of
26 the screen is appended onto this filename."""
27 ##############################################################################
30 """Place a window usingthe history placement algorithm."""
33 ###########################################################################
34 ###########################################################################
36 ###########################################################################
37 ### Internal stuff, should not be accessed outside the module. ###
38 ###########################################################################
48 def __init__(self, appname, appclass, role, x, y):
49 self.appname = appname
50 self.appclass = appclass
54 def __eq__(self, other):
55 if self.appname == other.appname and \
56 self.appclass == other.appclass and \
57 self.role == other.role:
64 file = open(os.environ['HOME'] + '/.openbox/' + FILENAME+"." +
65 str(data.screen), 'r')
67 for line in file.readlines():
68 line = line[:-1] # drop the '\n'
70 s = string.split(line, '\0')
71 state = _state(s[0], s[1], s[2],
72 string.atoi(s[3]), string.atoi(s[4]))
74 while len(_data)-1 < data.screen:
76 _data[data.screen].append(state)
78 except ValueError: pass
79 except IndexError: pass
85 file = open(os.environ['HOME']+'/.openbox/'+FILENAME+"."+str(data.screen),
88 while len(_data)-1 < data.screen:
90 for i in _data[data.screen]:
91 file.write(i.appname + '\0' +
98 def _create_state(data):
100 area = data.client.area()
101 return _state(data.client.appName(), data.client.appClass(),
102 data.client.role(), area.x(), area.y())
104 def _find(screen, state):
107 return _data[screen].index(state)
111 while len(_data)-1 < screen:
113 return _find(screen, state) # try again
118 if not (IGNORE_REQUESTED_POSITIONS and data.client.normal()):
119 if data.client.positionRequested(): return
120 state = _create_state(data)
122 if not CONFIRM_CALLBACK or CONFIRM_CALLBACK(data):
123 print "looking for : " + state.appname + " : " + \
124 state.appclass + " : " + state.role
126 i = _find(data.screen, state)
128 coords = _data[data.screen][i]
129 print "Found in history ("+str(coords.x)+","+\
131 data.client.move(coords.x, coords.y)
134 print "No match in history"
137 if FALLBACK: FALLBACK(data)
139 def _save_window(data):
142 state = _create_state(data)
143 print "looking for : " + state.appname + " : " + state.appclass + \
146 i = _find(data.screen, state)
149 _data[data.screen][i] = state # replace it
152 _data[data.screen].append(state)
154 ob.ebind(ob.EventAction.CloseWindow, _save_window)
155 ob.ebind(ob.EventAction.Startup, _load)
156 ob.ebind(ob.EventAction.Shutdown, _save)
158 print "Loaded historyplacement.py"