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