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