]> icculus.org git repositories - mikachu/openbox.git/blob - scripts/historyplacement.py
make sure windows aren't doing things they aren't allowed when their allowed actions...
[mikachu/openbox.git] / 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: pass
79             except IndexError: pass
80         file.close()
81
82 def _save(data):
83     global _data
84     file = open(os.environ['HOME']+'/.openbox/'+FILENAME+"."+str(data.screen),
85                 'w')
86     if file:
87         while len(_data)-1 < data.screen:
88             _data.append([])
89         for i in _data[data.screen]:
90             file.write(i.appname + '\0' +
91                        i.appclass + '\0' +
92                        i.role + '\0' +
93                        str(i.x) + '\0' +
94                        str(i.y) + '\n')
95         file.close()
96
97 def _create_state(data):
98     global _data
99     area = data.client.area()
100     return _state(data.client.appName(), data.client.appClass(),
101                   data.client.role(), area.x(), area.y())
102
103 def _find(screen, state):
104     global _data
105     try:
106         return _data[screen].index(state)
107     except ValueError:
108         return -1
109     except IndexError:
110         while len(_data)-1 < screen:
111             _data.append([])
112         return _find(screen, state) # try again
113
114 def _place(data):
115     global _data
116     if data.client:
117         if not (IGNORE_REQUESTED_POSITIONS and data.client.normal()):
118             if data.client.positionRequested(): return
119         state = _create_state(data)
120         try:
121             if not CONFIRM_CALLBACK or CONFIRM_CALLBACK(data):
122                 print "looking for : " + state.appname +  " : " + \
123                       state.appclass + " : " + state.role
124
125                 i = _find(data.screen, state)
126                 if i >= 0:
127                     coords = _data[data.screen][i]
128                     print "Found in history ("+str(coords.x)+","+\
129                           str(coords.y)+")"
130                     data.client.move(coords.x, coords.y)
131                     return
132                 else:
133                     print "No match in history"
134         except TypeError:
135             pass
136     if FALLBACK: FALLBACK(data)
137
138 def _save_window(data):
139     global _data
140     if data.client:
141         state = _create_state(data)
142         print "looking for : " + state.appname +  " : " + state.appclass + \
143               " : " + state.role
144
145         i = _find(data.screen, state)
146         if i >= 0:
147             print "replacing"
148             _data[data.screen][i] = state # replace it
149         else:
150             print "appending"
151             _data[data.screen].append(state)
152
153 ob.ebind(ob.EventAction.CloseWindow, _save_window)
154 ob.ebind(ob.EventAction.Startup, _load)
155 ob.ebind(ob.EventAction.Shutdown, _save)
156
157 print "Loaded historyplacement.py"