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