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: ###
11 # fallback - The window placement algorithm that will be used when history ###
12 ### placement does not have a place for the window. ###
13 fallback
= windowplacement
.random
###
14 # ignore_requested_positions - When true, the history algorithm will ###
15 ### attempt to place windows even when they ###
16 ### request a position (like XMMS). ###
17 ignore_requested_positions
= 0 ###
18 # confirm_callback - set this to a function to have the function called ###
19 ### before attempting to place a window via history. If ###
20 ### the function returns 'true' then an attempt will be ###
21 ### made to place the window. If it returns 'false', the ###
22 ### fallback method will be directly applied instead. ###
23 confirm_callback
= 0 ###
25 # filename - The name of the file where history data will be stored. The ###
26 ### number of the screen is appended onto this filename. ###
27 filename
= 'historydb' ###
29 ##############################################################################
39 def __init__(self
, appname
, appclass
, role
, x
, y
):
40 self
.appname
= appname
41 self
.appclass
= appclass
45 def __eq__(self
, other
):
46 if self
.appname
== other
.appname
and \
47 self
.appclass
== other
.appclass
and \
48 self
.role
== other
.role
:
54 file = open(os
.environ
['HOME']+'/.openbox/'+filename
+"."+str(data
.screen
),
58 for line
in file.readlines():
59 line
= line
[:-1] # drop the '\n'
61 s
= string
.split(line
, '\0')
62 state
= _state(s
[0], s
[1], s
[2],
63 string
.atoi(s
[3]), string
.atoi(s
[4]))
65 while len(_data
)-1 < data
.screen
:
67 _data
[data
.screen
].append(state
)
77 file = open(os
.environ
['HOME']+'/.openbox/'+filename
+"."+str(data
.screen
),
80 while len(_data
)-1 < data
.screen
:
82 for i
in _data
[data
.screen
]:
83 file.write(i
.appname
+ '\0' +
90 def _create_state(data
):
92 area
= data
.client
.area()
93 return _state(data
.client
.appName(), data
.client
.appClass(),
94 data
.client
.role(), area
.x(), area
.y())
96 def _find(screen
, state
):
99 return _data
[screen
].index(state
)
103 while len(_data
)-1 < screen
:
105 return _find(screen
, state
) # try again
110 if not ignore_requested_positions
:
111 if data
.client
.positionRequested(): return
112 state
= _create_state(data
)
114 if not confirm_callback
or confirm_callback(data
):
115 print "looking for : " + state
.appname
+ " : " + \
116 state
.appclass
+ " : " + state
.role
118 i
= _find(data
.screen
, state
)
120 coords
= _data
[data
.screen
][i
]
121 print "Found in history ("+str(coords
.x
)+","+\
123 data
.client
.move(coords
.x
, coords
.y
)
126 print "No match in history"
129 if fallback
: fallback(data
)
131 def _save_window(data
):
134 state
= _create_state(data
)
135 print "looking for : " + state
.appname
+ " : " + state
.appclass
+ \
138 i
= _find(data
.screen
, state
)
141 _data
[data
.screen
][i
] = state
# replace it
144 _data
[data
.screen
].append(state
)
146 ob
.ebind(ob
.EventAction
.CloseWindow
, _save_window
)
147 ob
.ebind(ob
.EventAction
.Startup
, _load
)
148 ob
.ebind(ob
.EventAction
.Shutdown
, _save
)
150 print "Loaded historyplacement.py"