]> Dogcows Code - chaz/openbox/blob - scripts/historyplacement.py
formatting
[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 (Options in the ###
10 ### windowplacement module also apply!): ###
11 ### ###
12 # ignore_requested_positions - When true, the placement algorithm will ###
13 ### attempt to place windows even when they ###
14 ### request a position (like XMMS). ###
15 ### Note this only applies to normal windows, ###
16 ### not to special cases like desktops and ###
17 ### docks. ###
18 ignore_requested_positions = 0 ###
19 ### ###
20 # fallback - The window placement algorithm that will be used when history ###
21 ### placement does not have a place for the window. ###
22 fallback = windowplacement.random ###
23 ### ###
24 # confirm_callback - set this to a function to have the function called ###
25 ### before attempting to place a window via history. If ###
26 ### the function returns 'true' then an attempt will be ###
27 ### made to place the window. If it returns 'false', the ###
28 ### fallback method will be directly applied instead. ###
29 confirm_callback = 0 ###
30 ### ###
31 # filename - The name of the file where history data will be stored. The ###
32 ### number of the screen is appended onto this filename. ###
33 filename = 'historydb' ###
34 ### ###
35 ##############################################################################
36
37 import otk
38 import ob
39 import os
40 import string
41
42 _data = []
43
44 class _state:
45 def __init__(self, appname, appclass, role, x, y):
46 self.appname = appname
47 self.appclass = appclass
48 self.role = role
49 self.x = x
50 self.y = y
51 def __eq__(self, other):
52 if self.appname == other.appname and \
53 self.appclass == other.appclass and \
54 self.role == other.role:
55 return 1
56 return 0
57
58 def _load(data):
59 global _data
60 file = open(os.environ['HOME']+'/.openbox/'+filename+"."+str(data.screen),
61 'r')
62 if file:
63 # read data
64 for line in file.readlines():
65 line = line[:-1] # drop the '\n'
66 try:
67 s = string.split(line, '\0')
68 state = _state(s[0], s[1], s[2],
69 string.atoi(s[3]), string.atoi(s[4]))
70
71 while len(_data)-1 < data.screen:
72 _data.append([])
73 _data[data.screen].append(state)
74
75 except ValueError:
76 pass
77 except IndexError:
78 pass
79 file.close()
80
81 def _save(data):
82 global _data
83 file = open(os.environ['HOME']+'/.openbox/'+filename+"."+str(data.screen),
84 'w')
85 if file:
86 while len(_data)-1 < data.screen:
87 _data.append([])
88 for i in _data[data.screen]:
89 file.write(i.appname + '\0' +
90 i.appclass + '\0' +
91 i.role + '\0' +
92 str(i.x) + '\0' +
93 str(i.y) + '\n')
94 file.close()
95
96 def _create_state(data):
97 global _data
98 area = data.client.area()
99 return _state(data.client.appName(), data.client.appClass(),
100 data.client.role(), area.x(), area.y())
101
102 def _find(screen, state):
103 global _data
104 try:
105 return _data[screen].index(state)
106 except ValueError:
107 return -1
108 except IndexError:
109 while len(_data)-1 < screen:
110 _data.append([])
111 return _find(screen, state) # try again
112
113 def place(data):
114 global _data
115 if data.client:
116 if not (ignore_requested_positions and data.client.normal()):
117 if data.client.positionRequested(): return
118 state = _create_state(data)
119 try:
120 if not confirm_callback or confirm_callback(data):
121 print "looking for : " + state.appname + " : " + \
122 state.appclass + " : " + state.role
123
124 i = _find(data.screen, state)
125 if i >= 0:
126 coords = _data[data.screen][i]
127 print "Found in history ("+str(coords.x)+","+\
128 str(coords.y)+")"
129 data.client.move(coords.x, coords.y)
130 return
131 else:
132 print "No match in history"
133 except TypeError:
134 pass
135 if fallback: fallback(data)
136
137 def _save_window(data):
138 global _data
139 if data.client:
140 state = _create_state(data)
141 print "looking for : " + state.appname + " : " + state.appclass + \
142 " : " + state.role
143
144 i = _find(data.screen, state)
145 if i >= 0:
146 print "replacing"
147 _data[data.screen][i] = state # replace it
148 else:
149 print "appending"
150 _data[data.screen].append(state)
151
152 ob.ebind(ob.EventAction.CloseWindow, _save_window)
153 ob.ebind(ob.EventAction.Startup, _load)
154 ob.ebind(ob.EventAction.Shutdown, _save)
155
156 print "Loaded historyplacement.py"
This page took 0.046475 seconds and 5 git commands to generate.