1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
// FocusableList.cc
// Copyright (c) 2007 Fluxbox Team (fluxgen at fluxbox dot org)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#include "FocusableList.hh"
#include "Focusable.hh"
#include "FocusControl.hh"
#include "Screen.hh"
#include "WinClient.hh"
#include "Window.hh"
#include "FbTk/StringUtil.hh"
#include "FbTk/MemFun.hh"
#include <vector>
#include <algorithm>
#ifdef HAVE_CSTRING
#include <cstring>
#else
#include <string.h>
#endif
using std::string;
using std::vector;
void FocusableList::parseArgs(const string &in, int &opts, string &pat) {
string options;
int err = FbTk::StringUtil::getStringBetween(options, in.c_str(), '{', '}',
" \t\n");
// the rest of the string is a ClientPattern
pat = in.c_str() + (err > 0 ? err : 0);
// now parse the options
vector<string> args;
FbTk::StringUtil::stringtok(args, options);
vector<string>::iterator it = args.begin(), it_end = args.end();
opts = 0;
for (; it != it_end; ++it) {
if (strcasecmp((*it).c_str(), "static") == 0)
opts |= STATIC_ORDER;
else if (strcasecmp((*it).c_str(), "groups") == 0)
opts |= LIST_GROUPS;
}
}
const FocusableList *FocusableList::getListFromOptions(BScreen &scr, int opts) {
if (opts & LIST_GROUPS)
return (opts & STATIC_ORDER) ?
&scr.focusControl().creationOrderWinList() :
&scr.focusControl().focusedOrderWinList();
return (opts & STATIC_ORDER) ?
&scr.focusControl().creationOrderList() :
&scr.focusControl().focusedOrderList();
}
FocusableList::FocusableList(BScreen &scr, const string pat):
m_pat(0), m_parent(0), m_screen(scr) {
int options = 0;
string pattern;
parseArgs(pat, options, pattern);
m_parent = getListFromOptions(scr, options);
m_pat.reset(new ClientPattern(pattern.c_str()));
init();
}
FocusableList::FocusableList(BScreen &scr, const FocusableList &parent,
const string pat):
m_pat(new ClientPattern(pat.c_str())), m_parent(&parent), m_screen(scr) {
init();
}
void FocusableList::init() {
addMatching();
m_parent->attachChild(*this);
// TODO: can't handle (head=[mouse]) yet
if (m_pat->dependsOnCurrentWorkspace()) {
join(m_screen.currentWorkspaceSig(),
FbTk::MemFun(*this, &FocusableList::workspaceChanged));
}
if (m_pat->dependsOnFocusedWindow()) {
join(m_screen.focusedWindowSig(),
FbTk::MemFun(*this, &FocusableList::focusedWindowChanged));
}
}
void FocusableList::update(FbTk::Subject *subj) {
if (subj == 0 || m_screen.isShuttingdown())
return;
if (typeid(*subj) == typeid(FluxboxWindow::WinSubject)) {
FluxboxWindow::WinSubject *fsubj =
static_cast<FluxboxWindow::WinSubject *>(subj);
windowUpdated(fsubj->win());
}
}
void FocusableList::windowUpdated(FluxboxWindow &fbwin) {
if (m_screen.isShuttingdown())
return;
// we only bind these for matching patterns, so skip finding out signal
if (m_parent->contains(fbwin))
checkUpdate(fbwin);
const std::list<WinClient *> &list = fbwin.clientList();
std::list<WinClient *>::const_iterator it = list.begin(), it_end = list.end();
for (; it != it_end; ++it) {
if (m_parent->contains(**it))
checkUpdate(**it);
}
}
void FocusableList::parentOrderChanged(Focusable *win) {
if(!m_screen.isShuttingdown() && contains(*win)) {
if(insertFromParent(*win))
m_ordersig.emit(win);
}
}
void FocusableList::parentWindowAdded(Focusable *win) {
if(!m_screen.isShuttingdown()) {
attachSignals(*win);
if (m_pat->match(*win)) {
insertFromParent(*win);
m_addsig.emit(win);
}
}
}
void FocusableList::parentWindowRemoved(Focusable *win) {
if(!m_screen.isShuttingdown())
remove(*win);
}
void FocusableList::checkUpdate(Focusable &win) {
if (contains(win)) {
if (!m_pat->match(win)) {
m_list.remove(&win);
m_pat->removeMatch();
m_removesig.emit(&win);
}
} else if (m_pat->match(win)) {
insertFromParent(win);
m_pat->addMatch();
m_addsig.emit(&win);
}
}
// returns whether or not the window was moved
bool FocusableList::insertFromParent(Focusable &win) {
const Focusables list = m_parent->clientList();
Focusables::const_iterator p_it = list.begin(), p_it_end = list.end();
Focusables::iterator our_it = m_list.begin(), our_it_end = m_list.end();
// walk through our list looking for corresponding entries in
// parent's list, until we find the window that moved
for (; our_it != our_it_end && p_it != p_it_end; p_it++) {
if (*p_it == &win) {
if (*our_it == &win) // win didn't move in our list
return false;
m_list.remove(&win);
m_list.insert(our_it, &win);
return true;
}
if (*p_it == *our_it)
++our_it;
}
m_list.remove(&win);
m_list.push_back(&win);
return true;
}
void FocusableList::addMatching() {
if (!m_parent)
return;
const Focusables list = m_parent->clientList();
Focusables::const_iterator it = list.begin(), it_end = list.end();
for (; it != it_end; ++it) {
if (m_pat->match(**it)) {
m_list.push_back(*it);
m_pat->addMatch();
}
attachSignals(**it);
}
}
void FocusableList::pushFront(Focusable &win) {
m_list.push_front(&win);
attachSignals(win);
m_addsig.emit(&win);
}
void FocusableList::pushBack(Focusable &win) {
m_list.push_back(&win);
attachSignals(win);
m_addsig.emit(&win);
}
void FocusableList::moveToFront(Focusable &win) {
// if the window isn't already in this list, we could accidentally add it
if (!contains(win))
return;
m_list.remove(&win);
m_list.push_front(&win);
m_ordersig.emit(&win);
}
void FocusableList::moveToBack(Focusable &win) {
// if the window isn't already in this list, we could accidentally add it
if (!contains(win))
return;
m_list.remove(&win);
m_list.push_back(&win);
m_ordersig.emit(&win);
}
void FocusableList::remove(Focusable &win) {
// if the window isn't already in this list, we could send a bad signal
bool contained = contains(win);
detachSignals(win);
if (!contained) {
return;
}
m_list.remove(&win);
m_removesig.emit(&win);
}
void FocusableList::updateTitle(Focusable& win) {
checkUpdate(win);
}
#include "Debug.hh"
void FocusableList::attachSignals(Focusable &win) {
if (m_parent) {
FluxboxWindow *fbwin = win.fbwindow();
// attach various signals for matching
FbTk::RefCount<FbTk::SignalTracker> &tracker = m_signal_map[&win];
if (! tracker) {
// we have not attached to this window yet
tracker = new SignalTracker;
tracker->join(win.titleSig(), MemFunSelectArg1(*this, &FocusableList::updateTitle));
tracker->join(win.dieSig(), MemFun(*this, &FocusableList::remove));
if(fbwin) {
tracker->join(fbwin->workspaceSig(),
MemFun(*this, &FocusableList::windowUpdated)
);
}
}
if (!fbwin)
return;
fbwin->stateSig().attach(this);
fbwin->layerSig().attach(this);
// TODO: can't watch (head=...) yet
}
}
void FocusableList::detachSignals(Focusable &win) {
m_signal_map.erase(&win);
if (m_parent) {
// detach various signals for matching
FluxboxWindow *fbwin = win.fbwindow();
if (!fbwin)
return;
fbwin->stateSig().detach(this);
fbwin->layerSig().detach(this);
// TODO: can't watch (head=...) yet
}
}
void FocusableList::reset() {
while (!m_list.empty()) {
detachSignals(*m_list.back());
m_list.pop_back();
}
m_pat->resetMatches();
if (m_parent)
addMatching();
m_resetsig.emit();
}
bool FocusableList::contains(const Focusable &win) const {
Focusables::const_iterator it = m_list.begin(), it_end = m_list.end();
it = std::find(it, it_end, &win);
return (it != it_end);
}
Focusable *FocusableList::find(const ClientPattern &pat) const {
Focusables::const_iterator it = m_list.begin(), it_end = m_list.end();
for (; it != it_end; ++it) {
if (pat.match(**it))
return *it;
}
return 0;
}
void FocusableList::attachChild(FocusableList &child) const {
m_addsig.connect(FbTk::MemFun(child, &FocusableList::parentWindowAdded));
m_ordersig.connect(FbTk::MemFun(child, &FocusableList::parentOrderChanged));
m_removesig.connect(FbTk::MemFun(child, &FocusableList::parentWindowRemoved));
m_resetsig.connect(FbTk::MemFun(child, &FocusableList::reset));
}
void FocusableList::workspaceChanged(BScreen &screen) {
reset();
}
void FocusableList::focusedWindowChanged(BScreen &screen,
FluxboxWindow *focused_win,
WinClient *client) {
reset();
}
|