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
:
63 file = open(os
.environ
['HOME']+'/.openbox/'+FILENAME
+"."+str(data
.screen
),
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
84 file = open(os
.environ
['HOME']+'/.openbox/'+FILENAME
+"."+str(data
.screen
),
87 while len(_data
)-1 < data
.screen
:
89 for i
in _data
[data
.screen
]:
90 file.write(i
.appname
+ '\0' +
97 def _create_state(data
):
99 area
= data
.client
.area()
100 return _state(data
.client
.appName(), data
.client
.appClass(),
101 data
.client
.role(), area
.x(), area
.y())
103 def _find(screen
, state
):
106 return _data
[screen
].index(state
)
110 while len(_data
)-1 < screen
:
112 return _find(screen
, state
) # try again
117 if not (IGNORE_REQUESTED_POSITIONS
and data
.client
.normal()):
118 if data
.client
.positionRequested(): return
119 state
= _create_state(data
)
121 if not CONFIRM_CALLBACK
or CONFIRM_CALLBACK(data
):
122 print "looking for : " + state
.appname
+ " : " + \
123 state
.appclass
+ " : " + state
.role
125 i
= _find(data
.screen
, state
)
127 coords
= _data
[data
.screen
][i
]
128 print "Found in history ("+str(coords
.x
)+","+\
130 data
.client
.move(coords
.x
, coords
.y
)
133 print "No match in history"
136 if FALLBACK
: FALLBACK(data
)
138 def _save_window(data
):
141 state
= _create_state(data
)
142 print "looking for : " + state
.appname
+ " : " + state
.appclass
+ \
145 i
= _find(data
.screen
, state
)
148 _data
[data
.screen
][i
] = state
# replace it
151 _data
[data
.screen
].append(state
)
153 ob
.ebind(ob
.EventAction
.CloseWindow
, _save_window
)
154 ob
.ebind(ob
.EventAction
.Startup
, _load
)
155 ob
.ebind(ob
.EventAction
.Shutdown
, _save
)
157 print "Loaded historyplacement.py"