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