]> Dogcows Code - chaz/openbox/blob - scripts/historyplacement.py
allow a callback to determine if a window should be history-placed
[chaz/openbox] / scripts / historyplacement.py
1 ##############################################################################
2 ### The history window placement algorithm. ebind historyplacement.place ###
3 ### to the ob.EventAction.PlaceWindow event to use it. ###
4 ##############################################################################
5
6 import windowplacement # fallback routines
7
8 ##############################################################################
9 ### Options for the historyplacement module: ###
10 ### ###
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 ###
24 ### ###
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' ###
28 ### ###
29 ##############################################################################
30
31 import otk
32 import ob
33 import os
34 import string
35
36 _data = []
37
38 class _state:
39 def __init__(self, appname, appclass, role, x, y):
40 self.appname = appname
41 self.appclass = appclass
42 self.role = role
43 self.x = x
44 self.y = y
45 def __eq__(self, other):
46 if self.appname == other.appname and \
47 self.appclass == other.appclass and \
48 self.role == other.role:
49 return 1
50 return 0
51
52 def _load(data):
53 global _data
54 file = open(os.environ['HOME']+'/.openbox/'+filename+"."+str(data.screen),
55 'r')
56 if file:
57 # read data
58 for line in file.readlines():
59 line = line[:-1] # drop the '\n'
60 try:
61 s = string.split(line, '\0')
62 state = _state(s[0], s[1], s[2],
63 string.atoi(s[3]), string.atoi(s[4]))
64
65 while len(_data)-1 < data.screen:
66 _data.append([])
67 _data[data.screen].append(state)
68
69 except ValueError:
70 pass
71 except IndexError:
72 pass
73 file.close()
74
75 def _save(data):
76 global _data
77 file = open(os.environ['HOME']+'/.openbox/'+filename+"."+str(data.screen),
78 'w')
79 if file:
80 while len(_data)-1 < data.screen:
81 _data.append([])
82 for i in _data[data.screen]:
83 file.write(i.appname + '\0' +
84 i.appclass + '\0' +
85 i.role + '\0' +
86 str(i.x) + '\0' +
87 str(i.y) + '\n')
88 file.close()
89
90 def _create_state(data):
91 global _data
92 area = data.client.area()
93 return _state(data.client.appName(), data.client.appClass(),
94 data.client.role(), area.x(), area.y())
95
96 def _find(screen, state):
97 global _data
98 try:
99 return _data[screen].index(state)
100 except ValueError:
101 return -1
102 except IndexError:
103 while len(_data)-1 < screen:
104 _data.append([])
105 return _find(screen, state) # try again
106
107 def place(data):
108 global _data
109 if data.client:
110 if not ignore_requested_positions:
111 if data.client.positionRequested(): return
112 state = _create_state(data)
113 try:
114 if not confirm_callback or confirm_callback(data):
115 print "looking for : " + state.appname + " : " + \
116 state.appclass + " : " + state.role
117
118 i = _find(data.screen, state)
119 if i >= 0:
120 coords = _data[data.screen][i]
121 print "Found in history ("+str(coords.x)+","+\
122 str(coords.y)+")"
123 data.client.move(coords.x, coords.y)
124 return
125 else:
126 print "No match in history"
127 except TypeError:
128 pass
129 if fallback: fallback(data)
130
131 def _save_window(data):
132 global _data
133 if data.client:
134 state = _create_state(data)
135 print "looking for : " + state.appname + " : " + state.appclass + \
136 " : " + state.role
137
138 i = _find(data.screen, state)
139 if i >= 0:
140 print "replacing"
141 _data[data.screen][i] = state # replace it
142 else:
143 print "appending"
144 _data[data.screen].append(state)
145
146 ob.ebind(ob.EventAction.CloseWindow, _save_window)
147 ob.ebind(ob.EventAction.Startup, _load)
148 ob.ebind(ob.EventAction.Shutdown, _save)
149
150 print "Loaded historyplacement.py"
This page took 0.046708 seconds and 5 git commands to generate.