summaryrefslogtreecommitdiff
path: root/src/FocusableList.cc
blob: 8a5c8f7c4ff397aa2beb16047a7c65dcf351e73c (plain)
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
// 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.

// $Id: $

#include "FocusableList.hh"

#include "Focusable.hh"
#include "FocusControl.hh"
#include "Screen.hh"
#include "WinClient.hh"
#include "Window.hh"

#include "FbTk/StringUtil.hh"

#include <vector>

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(), '{', '}');

    // the rest of the string is a ClientPattern
    pat = in.c_str() + err;

    // 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())
        m_screen.currentWorkspaceSig().attach(this);
    if (m_pat->dependsOnFocusedWindow())
        m_screen.focusedWindowSig().attach(this);
}

void FocusableList::update(FbTk::Subject *subj) {
    if (subj == 0 || m_screen.isShuttingdown())
        return;

    if (typeid(*subj) == typeid(Focusable::FocusSubject)) {
        Focusable::FocusSubject *fsubj =
            static_cast<Focusable::FocusSubject *>(subj);
        if (fsubj == &fsubj->win().dieSig())
            remove(fsubj->win());
        else if (fsubj == &fsubj->win().titleSig())
            checkUpdate(fsubj->win());
    }
    if (typeid(*subj) == typeid(FluxboxWindow::WinSubject)) {
        FluxboxWindow::WinSubject *fsubj =
            static_cast<FluxboxWindow::WinSubject *>(subj);
        // we only bind these for matching patterns, so skip finding out signal
        FluxboxWindow &fbwin = fsubj->win();
        if (m_parent->contains(fbwin))
            checkUpdate(fbwin);
        std::list<WinClient *> list = fbwin.clientList();
        std::list<WinClient *>::iterator it = list.begin(), it_end = list.end();
        for (; it != it_end; ++it) {
            if (m_parent->contains(**it))
                checkUpdate(**it);
        }
    }
    if (typeid(*subj) == typeid(FocusableListSubject)) {
        FocusableListSubject *fsubj =
            static_cast<FocusableListSubject *>(subj);
        if (subj == &m_parent->addSig()) {
            if (m_pat->match(*fsubj->win())) {
                insertFromParent(*fsubj->win());
                m_addsig.notify(fsubj->win());
            } else // we still want to watch it, in case it changes to match
                attachSignals(*fsubj->win());
        } else if (subj == &m_parent->removeSig())
            remove(*fsubj->win());
        else if (subj == &m_parent->resetSig())
            reset();
        else if (subj == &m_parent->orderSig()) {
            Focusable *win = fsubj->win();
            if (!win || !contains(*win))
                return;
            if (insertFromParent(*win))
                m_ordersig.notify(win);
        }
    } else if (subj == &m_screen.currentWorkspaceSig() ||
               subj == &m_screen.focusedWindowSig())
        reset();
}

void FocusableList::checkUpdate(Focusable &win) {
    if (contains(win)) {
        if (!m_pat->match(win)) {
            m_list.remove(&win);
            m_removesig.notify(&win);
        }
    } else if (m_pat->match(win)) {
        insertFromParent(win);
        m_addsig.notify(&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))
            pushBack(**it);
        else // we still want to watch it, in case it changes to match
            attachSignals(**it);
    }
}

void FocusableList::pushFront(Focusable &win) {
    m_list.push_front(&win);
    attachSignals(win);
    m_addsig.notify(&win);
}

void FocusableList::pushBack(Focusable &win) {
    m_list.push_back(&win);
    attachSignals(win);
    m_addsig.notify(&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.notify(&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.notify(&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.notify(&win);
}

void FocusableList::attachSignals(Focusable &win) {
    win.dieSig().attach(this);
    if (m_parent) {
        // attach various signals for matching
        win.titleSig().attach(this);
        FluxboxWindow *fbwin = win.fbwindow();
        if (!fbwin)
            return;
        fbwin->workspaceSig().attach(this);
        fbwin->stateSig().attach(this);
        fbwin->layerSig().attach(this);
        // TODO: can't watch (head=...) yet
    }
}

void FocusableList::detachSignals(Focusable &win) {
    win.dieSig().detach(this);
    if (m_parent) {
        // detach various signals for matching
        win.titleSig().detach(this);
        FluxboxWindow *fbwin = win.fbwindow();
        if (!fbwin)
            return;
        fbwin->workspaceSig().detach(this);
        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();
    }
    if (m_parent)
        addMatching();
    m_resetsig.notify(0);
}

bool FocusableList::contains(const Focusable &win) const {
    Focusables::const_iterator it = m_list.begin(), it_end = m_list.end();
    it = find(it, it_end, &win);
    return (it != it_end);
}

void FocusableList::attachChild(FocusableList &child) const {
    m_addsig.attach(&child);
    m_removesig.attach(&child);
    m_resetsig.attach(&child);
    m_ordersig.attach(&child);
}