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 non-zero, the placement algorithm will attempt to place windows even
14 when they request a position (like XMMS). Note this only applies to normal
15 windows, not to special cases like desktops and docks."""
17 """When non-zero, if 2 copies of the same match in history are to be placed
18 before one of them is closed (so it would be placed over-top of the last
19 one), this will cause the second window to not be placed via history, and
20 the FALLBACK will be used instead."""
21 FALLBACK = windowplacement.random
22 """The window placement algorithm that will be used when history placement
23 does not have a place for the window."""
25 """Set this to a function to have the function called before attempting to
26 place a window via history. If the function returns a non-zero, then an
27 attempt will be made to place the window. If it returns zero, the
28 FALLBACK method will be directly applied instead."""
29 FILENAME = 'historydb'
30 """The name of the file where history data will be stored. The number of
31 the screen is appended onto this filename."""
32 ##############################################################################
35 """Place a window usingthe history placement algorithm."""
38 ###########################################################################
39 ###########################################################################
41 ###########################################################################
42 ### Internal stuff, should not be accessed outside the module. ###
43 ###########################################################################
53 def __init__(self, appname, appclass, role, x, y):
54 self.appname = appname
55 self.appclass = appclass
60 def __eq__(self, other):
61 if self.appname == other.appname and \
62 self.appclass == other.appclass and \
63 self.role == other.role:
70 file = open(os.environ['HOME'] + '/.openbox/' + FILENAME+"." +
71 str(data.screen), 'r')
73 for line in file.readlines():
74 line = line[:-1] # drop the '\n'
76 s = string.split(line, '\0')
77 state = _state(s[0], s[1], s[2],
78 string.atoi(s[3]), string.atoi(s[4]))
80 while len(_data)-1 < data.screen:
82 _data[data.screen].append(state)
84 except ValueError: pass
85 except IndexError: pass
91 file = open(os.environ['HOME']+'/.openbox/'+FILENAME+"."+str(data.screen),
94 while len(_data)-1 < data.screen:
96 for i in _data[data.screen]:
97 file.write(i.appname + '\0' +
104 def _create_state(data):
106 area = data.client.area()
107 return _state(data.client.appName(), data.client.appClass(),
108 data.client.role(), area.x(), area.y())
110 def _find(screen, state):
113 return _data[screen].index(state)
117 while len(_data)-1 < screen:
119 return _find(screen, state) # try again
124 if not (IGNORE_REQUESTED_POSITIONS and data.client.normal()):
125 if data.client.positionRequested(): return
126 state = _create_state(data)
128 if not CONFIRM_CALLBACK or CONFIRM_CALLBACK(data):
129 print "looking for : " + state.appname + " : " + \
130 state.appclass + " : " + state.role
132 i = _find(data.screen, state)
134 coords = _data[data.screen][i]
135 print "Found in history ("+str(coords.x)+","+\
137 if not (DONT_DUPLICATE and coords.placed):
138 data.client.move(coords.x, coords.y)
142 print "Already placed another window there"
144 print "No match in history"
147 if FALLBACK: FALLBACK(data)
149 def _save_window(data):
152 state = _create_state(data)
153 print "looking for : " + state.appname + " : " + state.appclass + \
156 i = _find(data.screen, state)
159 _data[data.screen][i] = state # replace it
162 _data[data.screen].append(state)
164 ob.ebind(ob.EventAction.CloseWindow, _save_window)
165 ob.ebind(ob.EventAction.Startup, _load)
166 ob.ebind(ob.EventAction.Shutdown, _save)
168 print "Loaded historyplacement.py"