summaryrefslogtreecommitdiff
path: root/src/ClientPattern.cc
blob: f57513db834305f4cd0e7dc91f96f276cda48c48 (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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// ClientPattern.cc for Fluxbox Window Manager
// Copyright (c) 2003 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
//                and Simon Bowden    (rathnor at users.sourceforge.net)
//
// 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 "ClientPattern.hh"

#include "fluxbox.hh"
#include "FocusControl.hh"
#include "Layer.hh"
#include "Screen.hh"
#include "WinClient.hh"
#include "Workspace.hh"

#include "FbTk/StringUtil.hh"
#include "FbTk/App.hh"
#include "FbTk/stringstream.hh"
#include "FbTk/STLUtil.hh"

// use GNU extensions
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif // _GNU_SOURCE

#include <fstream>
#include <string>
#include <memory>
#include <algorithm>
#ifdef HAVE_CSTDIO
  #include <cstdio>
#else
  #include <stdio.h>
#endif

#ifdef HAVE_CSTRING
  #include <cstring>
#else
  #include <string.h>
#endif

// needed as well for index on some systems (e.g. solaris)
#include <strings.h>

using std::string;

namespace {

struct Name2WinProperty {
    const char* name;
    ClientPattern::WinProperty prop;
};

// sorted alphabetically for 'bsearch'
const Name2WinProperty name_2_winproperties[] = {
    { "class", ClientPattern::CLASS },
    { "focushidden", ClientPattern::FOCUSHIDDEN },
    { "fullscreen", ClientPattern::FULLSCREEN },
    { "head", ClientPattern::HEAD },
    { "iconhidden", ClientPattern::ICONHIDDEN },
    { "layer", ClientPattern::LAYER },
    { "maximized", ClientPattern::MAXIMIZED },
    { "maximizedhorizontal", ClientPattern::HORZMAX },
    { "maximizedvertical", ClientPattern::VERTMAX },
    { "minimized", ClientPattern::MINIMIZED },
    { "name", ClientPattern::NAME },
    { "role", ClientPattern::ROLE },
    { "screen", ClientPattern::SCREEN },
    { "shaded", ClientPattern::SHADED },
    { "stuck", ClientPattern::STUCK },
    { "title", ClientPattern::TITLE },
    { "transient", ClientPattern::TRANSIENT },
    { "urgent", ClientPattern::URGENT },
    { "workspace", ClientPattern::WORKSPACE },
    { "workspacename", ClientPattern::WORKSPACENAME }
};

extern "C" {
int name_2_winproperty_cmp(const void* a, const void* b) {
    return strcmp(
            reinterpret_cast<const Name2WinProperty*>(a)->name,
            reinterpret_cast<const Name2WinProperty*>(b)->name);
}
}

const Name2WinProperty* find_winproperty_by_name(const FbTk::FbString& name) {

    const Name2WinProperty key = { name.c_str(), ClientPattern::CLASS };
    const Name2WinProperty* result = reinterpret_cast<Name2WinProperty*>(
            bsearch(&key, name_2_winproperties,
                sizeof(name_2_winproperties) / sizeof(Name2WinProperty),
                sizeof(Name2WinProperty),
                name_2_winproperty_cmp));

    return result;
}


struct Prop2String {
    ClientPattern::WinProperty prop;
    const char* str;
};

Prop2String property_2_strings[] = { // sorted by 'prop'
    { ClientPattern::TITLE, "title" },
    { ClientPattern::CLASS, "class" },
    { ClientPattern::NAME, "name" },
    { ClientPattern::ROLE, "role" },
    { ClientPattern::TRANSIENT, "transient" },
    { ClientPattern::MAXIMIZED,  "maximized" },
    { ClientPattern::MINIMIZED, "minimized" }, 
    { ClientPattern::SHADED, "shaded" },
    { ClientPattern::STUCK, "stuck" },
    { ClientPattern::FOCUSHIDDEN, "focushidden" },
    { ClientPattern::ICONHIDDEN, "iconhidden" },
    { ClientPattern::WORKSPACE, "workspace" },
    { ClientPattern::WORKSPACENAME, "workspacename" },
    { ClientPattern::HEAD, "head" },
    { ClientPattern::LAYER, "layer" },
    { ClientPattern::URGENT, "urgent" },
    { ClientPattern::SCREEN, "screen" },
    { ClientPattern::XPROP, "@" },
    { ClientPattern::FULLSCREEN, "fullscreen" },
    { ClientPattern::VERTMAX, "maximizedvertical" },
    { ClientPattern::HORZMAX, "maximizedhorizontal" },
};


} // end of anonymous namespace


/**
 * This is the type of the actual pattern we want to match against
 * We have a "term" in the whole expression which is the full pattern
 * we also need to keep track of the uncompiled regular expression
 * for final output
 */
struct ClientPattern::Term {

    Term(const FbTk::FbString& _regstr, WinProperty _prop, bool _negate, const FbTk::FbString& _xprop) :
        regstr(_regstr),
        xpropstr(_xprop),
        regexp(_regstr, true),
        prop(_prop),
        negate(_negate) {

        xprop = XInternAtom(FbTk::App::instance()->display(), xpropstr.c_str(), False);
    }

    // (title=.*bar) or (@FOO=.*bar)
    FbTk::FbString regstr;     // .*bar
    FbTk::FbString xpropstr;  // @FOO=.*bar
    Atom xprop;                // Atom of 'FOO'
    FbTk::RegExp regexp;       // compiled version of '.*bar'
    WinProperty prop;
    bool negate;
};

ClientPattern::ClientPattern():
    m_matchlimit(0),
    m_nummatches(0) {}

// parse the given pattern (to end of line)
ClientPattern::ClientPattern(const char *str):
    m_matchlimit(0),
    m_nummatches(0)
{
    /* A rough grammar of a pattern is:
       PATTERN ::= MATCH+ LIMIT?
       MATCH ::= '(' word ')'
                 | '(' propertyname '=' word ')'
       LIMIT ::= '{' number '}'

       i.e. one or more match definitions, followed by
            an optional limit on the number of apps to match to

       Match definitions are enclosed in parentheses, and if no
       property name is given, then CLASSNAME is assumed.
       If no limit is specified, no limit is applied (i.e. limit = infinity)
    */

    bool had_error = false;

    int pos = 0;
    string match;
    int err = 1; // for starting first loop
    while (!had_error && err > 0) {
        err = FbTk::StringUtil::getStringBetween(match,
                                                 str + pos,
                                                 '(', ')', " \t\n", true);

        if (err > 0) {

            WinProperty prop = NAME;
            std::string expr;
            std::string xprop;
            bool negate = false;

            // need to determine the property used, potential patterns:
            //
            //  A) foo               (short for 'title=foo')
            //  B) foo=bar
            //  C) foo!=bar
            //
            //  D) @foo=bar          (xproperty 'foo' equal to 'bar')
            //

            string propstr = match;
            string::size_type eq = propstr.find_first_of('=');

            if (eq == propstr.npos) {           // A
                expr = "[current]";
            } else {                            // B or C, so strip away the '='

                // 'bar'
                expr.assign(propstr.begin() + eq + 1, propstr.end());

                // 'foo' or 'foo!'
                propstr.resize(eq);
                if (propstr.rfind("!", propstr.npos, 1) != propstr.npos) { // C 'foo!'
                    negate = true;
                    propstr.resize(propstr.size()-1);
                }
            }

            if (propstr[0] != '@') { // not D

                const Name2WinProperty* p = find_winproperty_by_name(FbTk::StringUtil::toLower(propstr));

                if (p) {
                    prop = p->prop;
                } else {
                    expr = match;
                }
            } else { // D
                prop = XPROP;
                xprop.assign(propstr, 1, propstr.size());
            }

            had_error = !addTerm(expr, prop, negate, xprop);
            pos += err;
        }
    }
    if (pos == 0 && !had_error) { // no match terms given, this is not allowed
        had_error = true;
    }

    if (!had_error) { // otherwise, we check for a number

        string number;
        err = FbTk::StringUtil::getStringBetween(number,
                                             str+pos,
                                             '{', '}');
        if (err > 0) {
            FbTk::StringUtil::extractNumber(number, m_matchlimit);
            pos+=err;
        }
        // we don't care if there isn't one

        // there shouldn't be anything else on the line
        match = str + pos;
        size_t uerr;// need a special type here
        uerr = match.find_first_not_of(" \t\n", pos);
        if (uerr != match.npos) {
            // found something, not good
            had_error = true;
        }
    }

    if (had_error) {
        FbTk::STLUtil::destroyAndClear(m_terms);
    }
}

ClientPattern::~ClientPattern() {
    FbTk::STLUtil::destroyAndClear(m_terms);
}

// return a string representation of this pattern
string ClientPattern::toString() const {
    string result;
    Terms::const_iterator it = m_terms.begin();
    Terms::const_iterator it_end = m_terms.end();
    for (; it != it_end; ++it) {
        const Term& term = *(*it);
        result.append(" (");
        result.append(property_2_strings[term.prop].str);
        if (term.prop == XPROP)
            result.append(term.xpropstr);
        result.append(term.negate ? "!=" : "=");
        result.append(term.regstr);
        result.append(")");
    }

    if (m_matchlimit > 0) {
        result.append(" {");
        result.append(FbTk::StringUtil::number2String(m_matchlimit));
        result.append("}");
    }
    return result;
}

// does this client match this pattern?
bool ClientPattern::match(const Focusable &win) const {
    if (m_matchlimit != 0 && m_nummatches >= m_matchlimit)
        return false; // already matched out

    // regmatch everything
    // currently, we use an "AND" policy for multiple terms
    // changing to OR would require minor modifications in this function only
    Terms::const_iterator it = m_terms.begin();
    Terms::const_iterator it_end = m_terms.end();
    for (; it != it_end; ++it) {
        const Term& term = *(*it);
        if (term.prop == XPROP) {
            if (!term.negate ^ ((term.regexp.match(win.getTextProperty(term.xprop))) || term.regexp.match(FbTk::StringUtil::number2String(win.getCardinalProperty(term.xprop)))))
                return false;
        } else if (term.regstr == "[current]") {
            WinClient *focused = FocusControl::focusedWindow();
            if (term.prop == WORKSPACE) {
                if (!term.negate ^ (getProperty(term.prop, win) == FbTk::StringUtil::number2String(win.screen().currentWorkspaceID())))
                    return false;
            } else if (term.prop == WORKSPACENAME) {
                const Workspace *w = win.screen().currentWorkspace();
                if (!w || (!term.negate ^ (getProperty(term.prop, win) == w->name())))
                    return false;
            } else if (!focused || (!term.negate ^ (getProperty(term.prop, win) == getProperty(term.prop, *focused))))
                return false;
        } else if (term.prop == HEAD && term.regstr == "[mouse]") {
            if (!term.negate ^ (getProperty(term.prop, win) == FbTk::StringUtil::number2String(win.screen().getCurrHead())))
                return false;

        } else if (!term.negate ^ term.regexp.match(getProperty(term.prop, win)))
            return false;
    }
    return true;
}

bool ClientPattern::dependsOnFocusedWindow() const {
    Terms::const_iterator it = m_terms.begin(), it_end = m_terms.end();
    for (; it != it_end; ++it) {
        if ((*it)->prop != WORKSPACE && (*it)->prop != WORKSPACENAME &&
            (*it)->regstr == "[current]")
            return true;
    }
    return false;
}

bool ClientPattern::dependsOnCurrentWorkspace() const {
    Terms::const_iterator it = m_terms.begin(), it_end = m_terms.end();
    for (; it != it_end; ++it) {
        if (((*it)->prop == WORKSPACE || (*it)->prop == WORKSPACENAME) &&
            (*it)->regstr == "[current]")
            return true;
    }
    return false;
}

// add an expression to match against
// The first argument is a regular expression, the second is the member
// function that we wish to match against.
bool ClientPattern::addTerm(const FbTk::FbString &str, WinProperty prop, bool negate, const FbTk::FbString& xprop) {

    bool rc = false;
    Term* term = new Term(str, prop, negate, xprop);

    if (!term)
        return rc;

    if ((rc = !term->regexp.error())) {
        m_terms.push_back(term);
    } else {
        delete term;
    }

    return rc;
}

FbTk::FbString ClientPattern::getProperty(WinProperty prop, const Focusable &client) {

    FbTk::FbString result;

    // we need this for some of the window properties
    const FluxboxWindow *fbwin = client.fbwindow();

    switch (prop) {
    case TITLE:
        result = client.title().logical();
        break;
    case CLASS:
        result = client.getWMClassClass();
        break;
    case ROLE:
        result = client.getWMRole();
        break;
    case TRANSIENT:
        result = client.isTransient() ? "yes" : "no";
        break;
    case MAXIMIZED:
        result = (fbwin && fbwin->isMaximized()) ? "yes" : "no";
        break;
    case MINIMIZED:
        result = (fbwin && fbwin->isIconic()) ? "yes" : "no";
        break;
    case FULLSCREEN:
        result = (fbwin && fbwin->isFullscreen()) ? "yes" : "no";
        break;
    case VERTMAX:
        result = (fbwin && fbwin->isMaximizedVert()) ? "yes" : "no";
        break;
    case HORZMAX:
        result = (fbwin && fbwin->isMaximizedHorz()) ? "yes" : "no";
        break;
    case SHADED:
        result = (fbwin && fbwin->isShaded()) ? "yes" : "no";
        break;
    case STUCK:
        result = (fbwin && fbwin->isStuck()) ? "yes" : "no";
        break;
    case FOCUSHIDDEN:
        result = (fbwin && fbwin->isFocusHidden()) ? "yes" : "no";
        break;
    case ICONHIDDEN:
        result = (fbwin && fbwin->isIconHidden()) ? "yes" : "no";
        break;
    case WORKSPACE: {
        unsigned int wsnum = (fbwin ? fbwin->workspaceNumber() : client.screen().currentWorkspaceID());
        result = FbTk::StringUtil::number2String(wsnum);
        break;
    }
    case WORKSPACENAME: {
        const Workspace *w = (fbwin ?
                client.screen().getWorkspace(fbwin->workspaceNumber()) :
                client.screen().currentWorkspace());
        if (w) {
            result = w->name();
        }
        break;
    }
    case HEAD:
        if (fbwin) {
            result = FbTk::StringUtil::number2String(client.screen().getHead(fbwin->fbWindow()));
        }
        break;
    case LAYER:
        if (fbwin) {
            result = ::ResourceLayer::getString(fbwin->layerNum());
        }
        break;
    case URGENT:
        result = Fluxbox::instance()->attentionHandler()
                .isDemandingAttention(client) ? "yes" : "no";
        break;
    case SCREEN:
        result = FbTk::StringUtil::number2String(client.screen().screenNumber());
        break;

    case XPROP:
        break;

    case NAME:
    default:
        result = client.getWMClassName();
        break;
    }
    return result;
}

bool ClientPattern::operator ==(const ClientPattern &pat) const {
    // we require the terms to be identical (order too)
    Terms::const_iterator it = m_terms.begin();
    Terms::const_iterator it_end = m_terms.end();
    Terms::const_iterator other_it = pat.m_terms.begin();
    Terms::const_iterator other_it_end = pat.m_terms.end();
    for (; it != it_end && other_it != other_it_end; ++it, ++other_it) {
        const Term& i = *(*it);
        const Term& o = *(*other_it);
        if (i.regstr != o.regstr ||
            i.negate != o.negate ||
            i.xpropstr != o.xpropstr)
            return false;
    }
    if (it != it_end || other_it != other_it_end)
        return false;

    return true;
}