aboutsummaryrefslogtreecommitdiff
path: root/src/SystemTray.cc
blob: 2d1b3168393a821d81bd85b1b1552cfd3553b566 (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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
// SystemTray.cc
// Copyright (c) 2003 - 2006 Henrik Kinnunen (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 "SystemTray.hh"

#include "FbTk/EventManager.hh"
#include "FbTk/ImageControl.hh"
#include "FbTk/TextUtils.hh"
#include "FbTk/MemFun.hh"

#include "AtomHandler.hh"
#include "fluxbox.hh"
#include "WinClient.hh"
#include "Screen.hh"
#include "ButtonTheme.hh"
#include "Debug.hh"

#include <X11/Xutil.h>
#include <X11/Xatom.h>

#include <string>
#include <sstream>
#include <vector>
#include <memory>
#include <algorithm>


using std::string;
using std::endl;
using std::hex;
using std::dec;


namespace {

void getScreenCoordinates(Window win, int x, int y, int &screen_x, int &screen_y) {

    XWindowAttributes attr;
    if (XGetWindowAttributes(FbTk::App::instance()->display(), win, &attr) == 0) {
        return;
    }

    Window unused_win;
    Window parent_win;
    Window root_win = 0;
    Window* unused_childs = 0;
    unsigned int unused_number;

    XQueryTree(FbTk::App::instance()->display(), win,
               &root_win,
               &parent_win,
               &unused_childs, &unused_number);

    if (unused_childs != 0) {
        XFree(unused_childs);
    }

    XTranslateCoordinates(FbTk::App::instance()->display(),
                          parent_win, root_win,
                          x, y,
                          &screen_x, &screen_y, &unused_win);
}

};

static SystemTray *s_theoneandonly = 0;

static std::string trim(const std::string& str)
{
    // removes trailing and leading whitespace from a string

    const std::string whitespace(" \t");
    const auto strBegin = str.find_first_not_of(whitespace);
    if (strBegin == std::string::npos)
        return ""; // no content

    const auto strEnd = str.find_last_not_of(whitespace);
    const auto strRange = strEnd - strBegin + 1;

    return str.substr(strBegin, strRange);
}

static void parse_order(const std::string s, std::vector<std::string> &out) {
    // splits a comma seperated list and performs trimming

    std::stringstream ss(s);
    std::string item;

    while (std::getline(ss, item, ','))
        out.push_back(trim(item));
}

/// helper class for tray windows, so we dont call XDestroyWindow
class SystemTray::TrayWindow : public FbTk::FbWindow {
public:
    TrayWindow(Window win, bool using_xembed):FbTk::FbWindow(win), m_order(0), m_visible(false), m_xembedded(using_xembed) {
        setEventMask(PropertyChangeMask);
    }

    void pinByClassname(const std::vector<std::string> left, const std::vector<std::string> right);

    bool isVisible() { return m_visible; }
    bool isXEmbedded() { return m_xembedded; }
    void show() {
        if (!m_visible) {
            m_visible = true;
            FbTk::FbWindow::show();
        }
    }
    void hide() {
        if (m_visible) {
            m_visible = false;
            FbTk::FbWindow::hide();
        }
    }

/* Flags for _XEMBED_INFO */
#define XEMBED_MAPPED                   (1 << 0)

    bool getMappedDefault() const {
        Atom actual_type;
        int actual_format;
        unsigned long nitems, bytes_after;
        unsigned long *prop;
        Atom embed_info = SystemTray::getXEmbedInfoAtom();
        if (property(embed_info, 0l, 2l, false, embed_info,
                     &actual_type, &actual_format, &nitems, &bytes_after,
                     (unsigned char **) &prop) && prop != 0) {

            XFree(static_cast<void *>(prop));
            fbdbg << "(SystemTray::TrayWindow::getMappedDefault(): XEMBED_MAPPED = "
                << (bool)(static_cast<unsigned long>(prop[1]) & XEMBED_MAPPED)
                << endl;
        }
        return true;
    }

    int m_order;
private:
    bool m_visible;
    bool m_xembedded; // using xembed protocol? (i.e. unmap when done)
};

void TrayWindow::pinByClassname(const std::vector<std::string> left,
        const std::vector<std::string> right) {
    // based on the parsed order list and a given window returns an
    // ordinal used to sort the tray icons.

    auto deleter = [](XClassHint *x){if(x) XFree(x);};

    std::unique_ptr<XClassHint, decltype(deleter)>
        xclasshint(XAllocClassHint(), deleter);

    if(XGetClassHint(Fluxbox::instance()->display(),
                this->window(), xclasshint.get()))
    {
        std::string classname(xclasshint.get()->res_class);

        auto ix = std::find(left.begin(), left.end(), classname);
        if (ix != left.end())
            m_order = -(left.end()-ix); // the more left, the negative (<0)
        else {
            ix = std::find(right.begin(), right.end(), classname);
            if (ix != right.end())
                // the more right, the positive (>0)
                m_order = ix-right.begin()+1;
        }
    }
    // in neither list or invalid window (=0)
}

/// handles clientmessage event and notifies systemtray
class SystemTrayHandler: public AtomHandler {
public:
    SystemTrayHandler(SystemTray &tray):m_tray(tray) {
    }
    // client message is the only thing we care about
    bool checkClientMessage(const XClientMessageEvent &ce,
                            BScreen * screen, WinClient * const winclient) {
        // must be on the same screen
        if ((screen && screen->screenNumber() != m_tray.window().screenNumber()) ||
            (winclient && winclient->screenNumber() != m_tray.window().screenNumber()) )
            return false;
        return m_tray.clientMessage(ce);
    }

    void initForScreen(BScreen &screen) { };
    void setupFrame(FluxboxWindow &win) { };
    void setupClient(WinClient &winclient) {
        // must be on the same screen
        if (winclient.screenNumber() != m_tray.window().screenNumber())
            return;

        // we dont want a managed window
        if (winclient.fbwindow() != 0)
            return;
        // if not kde dockapp...
        if (!winclient.screen().isKdeDockapp(winclient.window()))
            return;
        // if not our screen...
        if (winclient.screenNumber() != m_tray.window().screenNumber())
            return;
        winclient.setEventMask(StructureNotifyMask |
                               SubstructureNotifyMask | EnterWindowMask);
        m_tray.addClient(winclient.window(), false);
    };

    void updateWorkarea(BScreen &) { }
    void updateFocusedWindow(BScreen &, Window) { }
    void updateClientList(BScreen &screen) { };
    void updateWorkspaceNames(BScreen &screen) { };
    void updateCurrentWorkspace(BScreen &screen) { };
    void updateWorkspaceCount(BScreen &screen) { };

    void updateFrameClose(FluxboxWindow &win) { };
    void updateClientClose(WinClient &winclient) { };
    void updateWorkspace(FluxboxWindow &win) { };
    void updateState(FluxboxWindow &win) { };
    void updateHints(FluxboxWindow &win) { };
    void updateLayer(FluxboxWindow &win) { };

    virtual bool propertyNotify(WinClient &winclient, Atom the_property) { return false; }

private:
    SystemTray &m_tray;
};

SystemTray::SystemTray(const FbTk::FbWindow& parent,
        FbTk::ThemeProxy<ToolTheme> &theme, BScreen& screen):
    ToolbarItem(ToolbarItem::FIXED),
    m_window(parent, 0, 0, 1, 1, ExposureMask | ButtonPressMask | ButtonReleaseMask |
             SubstructureNotifyMask | SubstructureRedirectMask),
    m_theme(theme),
    m_screen(screen),
    m_pixmap(0), m_num_visible_clients(0),
    m_selection_owner(m_window, 0, 0, 1, 1, SubstructureNotifyMask, false, false, CopyFromParent, InputOnly),
    m_rc_systray_pinleft(screen.resourceManager(),
            "", screen.name() + ".systray.pinLeft",
            screen.altName() + ".Systray.PinLeft"),
    m_rc_systray_pinright(screen.resourceManager(),
            "", screen.name() + ".systray.pinRight",
            screen.altName() + ".Systray.PinRight") {

    parse_order(m_rc_systray_pinleft, m_pinleft);
    parse_order(m_rc_systray_pinright, m_pinright);

    FbTk::EventManager::instance()->add(*this, m_window);
    FbTk::EventManager::instance()->add(*this, m_selection_owner);
    // setup signals
    join(m_theme->reconfigSig(), FbTk::MemFun(*this, &SystemTray::update));

    join(screen.bgChangeSig(),
         FbTk::MemFunIgnoreArgs(*this, &SystemTray::update));


    Fluxbox* fluxbox = Fluxbox::instance();
    Display *disp = fluxbox->display();

    // get selection owner and see if it's free
    string atom_name = getNetSystemTrayAtom(m_window.screenNumber());
    Atom tray_atom = XInternAtom(disp, atom_name.c_str(), False);
    Window owner = XGetSelectionOwner(disp, tray_atom);
    if (owner != 0) {
        fbdbg<<"(SystemTray(const FbTk::FbWindow)): can't set owner!"<<endl;
        return;  // the're can't be more than one owner
    }

    // ok, it was free. Lets set owner

    fbdbg<<"(SystemTray(const FbTk::FbWindow)): SETTING OWNER!"<<endl;

    // set owner
    XSetSelectionOwner(disp, tray_atom, m_selection_owner.window(), CurrentTime);

    s_theoneandonly = this;

    m_handler.reset(new SystemTrayHandler(*this));

    m_handler.get()->setName(atom_name);
    fluxbox->addAtomHandler(m_handler.get());


    // send selection owner msg
    Window root_window = m_screen.rootWindow().window();
    XEvent ce;
    ce.xclient.type = ClientMessage;
    ce.xclient.message_type = XInternAtom(disp, "MANAGER", False);
    ce.xclient.display = disp;
    ce.xclient.window = root_window;
    ce.xclient.format = 32;
    ce.xclient.data.l[0] = CurrentTime; // timestamp
    ce.xclient.data.l[1] = tray_atom; // manager selection atom
    ce.xclient.data.l[2] = m_selection_owner.window(); // the window owning the selection
    ce.xclient.data.l[3] = 0l; // selection specific data
    ce.xclient.data.l[4] = 0l; // selection specific data

    XSendEvent(disp, root_window, false, StructureNotifyMask, &ce);

    update();
}

SystemTray::~SystemTray() {
    // remove us, else fluxbox might delete the memory too
    if (s_theoneandonly == this)
        s_theoneandonly = 0;
    Fluxbox* fluxbox = Fluxbox::instance();
    fluxbox->removeAtomHandler(m_handler.get());
    Display *disp = fluxbox->display();

    // get selection owner and see if it's free
    string atom_name = getNetSystemTrayAtom(m_window.screenNumber());
    Atom tray_atom = XInternAtom(disp, atom_name.c_str(), False);

    // Properly give up selection.
    XSetSelectionOwner(disp, tray_atom, None, CurrentTime);
    removeAllClients();

    if (m_pixmap)
        m_screen.imageControl().removeImage(m_pixmap);

    // ~FbWindow cleans EventManager
}

void SystemTray::move(int x, int y) {
    m_window.move(x, y);
}

void SystemTray::resize(unsigned int width, unsigned int height) {
    if (width != m_window.width() ||
        height != m_window.height()) {
        m_window.resize(width, height);
        if (m_num_visible_clients)
            rearrangeClients();
        resizeSig().emit();
    }
}

void SystemTray::moveResize(int x, int y,
                            unsigned int width, unsigned int height) {
    if (width != m_window.width() ||
        height != m_window.height()) {
        m_window.moveResize(x, y, width, height);
        if (m_num_visible_clients)
            rearrangeClients();
        resizeSig().emit();
    } else {
        move(x, y);
    }
}

void SystemTray::hide() {
    m_window.hide();
}

void SystemTray::show() {

    update();
    m_window.show();
}

unsigned int SystemTray::width() const {
    if (orientation() == FbTk::ROT90 || orientation() == FbTk::ROT270)
        return m_window.width();

    return m_num_visible_clients * (height() + 2 * m_theme->border().width());
}

unsigned int SystemTray::height() const {
    if (orientation() == FbTk::ROT0 || orientation() == FbTk::ROT180)
        return m_window.height();

    return m_num_visible_clients * (width() + 2 * m_theme->border().width());
}

unsigned int SystemTray::borderWidth() const {
    return m_window.borderWidth();
}

bool SystemTray::clientMessage(const XClientMessageEvent &event) {
    static const int SYSTEM_TRAY_REQUEST_DOCK  =  0;
    //    static const int SYSTEM_TRAY_BEGIN_MESSAGE =  1;
    //    static const int SYSTEM_TRAY_CANCEL_MESSAGE = 2;
    static Atom systray_opcode_atom = XInternAtom(FbTk::App::instance()->display(), "_NET_SYSTEM_TRAY_OPCODE", False);

    if (event.message_type == systray_opcode_atom) {

        int type = event.data.l[1];
        if (type == SYSTEM_TRAY_REQUEST_DOCK) {

            fbdbg<<"SystemTray::clientMessage(const XClientMessageEvent): SYSTEM_TRAY_REQUEST_DOCK"<<endl;
            fbdbg<<"window = event.data.l[2] = "<<event.data.l[2]<<endl;

            addClient(event.data.l[2], true);
        }
        /*
        else if (type == SYSTEM_TRAY_BEGIN_MESSAGE)
            fbdbg<<"BEGIN MESSAGE"<<endl;
        else if (type == SYSTEM_TRAY_CANCEL_MESSAGE)
            fbdbg<<"CANCEL MESSAGE"<<endl;
        */

        return true;
    }

    return false;
}

SystemTray::ClientList::iterator SystemTray::findClient(Window win) {

    ClientList::iterator it = m_clients.begin();
    ClientList::iterator it_end = m_clients.end();
    for (; it != it_end; ++it) {
        if ((*it)->window() == win)
            break;
    }

    return it;
}

void SystemTray::addClient(Window win, bool using_xembed) {
    if (win == 0)
        return;

    ClientList::iterator it = findClient(win);
    if (it != m_clients.end())
        return;

    Display *disp = Fluxbox::instance()->display();
    // make sure we have the same screen number
    XWindowAttributes attr;
    attr.screen = 0;
    if (XGetWindowAttributes(disp, win, &attr) != 0 &&
        attr.screen != 0 &&
        XScreenNumberOfScreen(attr.screen) != window().screenNumber()) {
        return;
    }

    TrayWindow *traywin = new TrayWindow(win, using_xembed);

    fbdbg<<"SystemTray::addClient(Window): 0x"<<hex<<win<<dec<<endl;

    m_clients.push_back(traywin);
    FbTk::EventManager::instance()->add(*this, win);
    traywin->reparent(m_window, 0, 0);
    traywin->addToSaveSet();

    if (using_xembed) {
        static Atom xembed_atom = XInternAtom(disp, "_XEMBED", False);

#define XEMBED_EMBEDDED_NOTIFY		0
        // send embedded message
        XEvent ce;
        ce.xclient.type = ClientMessage;
        ce.xclient.message_type = xembed_atom;
        ce.xclient.display = disp;
        ce.xclient.window = win;
        ce.xclient.format = 32;
        ce.xclient.data.l[0] = CurrentTime; // timestamp
        ce.xclient.data.l[1] = XEMBED_EMBEDDED_NOTIFY;
        ce.xclient.data.l[2] = 0l; // The protocol version we support
        ce.xclient.data.l[3] = m_window.window(); // the window owning the selection
        ce.xclient.data.l[4] = 0l; // unused

        XSendEvent(disp, win, false, NoEventMask, &ce);
    }

    if (traywin->getMappedDefault())
        showClient(traywin);

    traywin->pinByClassname(m_pinleft, m_pinright);
    sortClients();
}

void SystemTray::removeClient(Window win, bool destroyed) {
    ClientList::iterator tray_it = findClient(win);
    if (tray_it == m_clients.end())
        return;

    fbdbg<<"(SystemTray::removeClient(Window)): 0x"<<hex<<win<<dec<<endl;

    TrayWindow *traywin = *tray_it;
    m_clients.erase(tray_it);
    if (!destroyed) {
        traywin->setEventMask(NoEventMask);
        traywin->removeFromSaveSet();
    }
    hideClient(traywin, destroyed);
    delete traywin;

    sortClients();
}

void SystemTray::exposeEvent(XExposeEvent &event) {
    m_window.clear();
}

void SystemTray::handleEvent(XEvent &event) {
    if (event.type == DestroyNotify) {
        removeClient(event.xdestroywindow.window, true);
    } else if (event.type == ReparentNotify && event.xreparent.parent != m_window.window()) {
        removeClient(event.xreparent.window, false);
    } else if (event.type == UnmapNotify && event.xany.send_event) {
        // we ignore server-generated events, which can occur
        // on restart. The ICCCM says that a client must send
        // a synthetic event for the withdrawn state
        ClientList::iterator it = findClient(event.xunmap.window);
        if (it != m_clients.end())
            hideClient(*it);
    } else if (event.type == ConfigureNotify) {
        // we got configurenotify from an client
        // check and see if we need to update it's size
        // and we must reposition and resize them to fit
        // our toolbar

        ClientList::iterator it = findClient(event.xconfigure.window);
        if (it != m_clients.end()) {
            if (static_cast<unsigned int>(event.xconfigure.width) != (*it)->width() ||
                static_cast<unsigned int>(event.xconfigure.height) != (*it)->height()) {
                // the position might differ so we update from our local
                // copy of position
                XMoveResizeWindow(FbTk::App::instance()->display(), (*it)->window(),
                                  (*it)->x(), (*it)->y(),
                                  (*it)->width(), (*it)->height());

                // this was why gaim wasn't centring the icon
                (*it)->sendConfigureNotify(0, 0, (*it)->width(), (*it)->height());
                // so toolbar know that we changed size
                // done inside this loop, because otherwise we can get into nasty looping
                resizeSig().emit();
            }
        }
    } else if (event.type == PropertyNotify) {
        ClientList::iterator it = findClient(event.xproperty.window);
        if (it != m_clients.end()) {
            if (event.xproperty.atom == getXEmbedInfoAtom()) {
                if ((*it)->getMappedDefault())
                    showClient(*it);
                else
                    hideClient(*it);
            }
        }
    }
}

void SystemTray::rearrangeClients() {
    unsigned int w_rot0 = width(), h_rot0 = height();
    const unsigned int bw = m_theme->border().width();
    FbTk::translateSize(orientation(), w_rot0, h_rot0);
    unsigned int trayw = m_num_visible_clients*h_rot0 + bw, trayh = h_rot0;
    FbTk::translateSize(orientation(), trayw, trayh);
    resize(trayw, trayh);
    update();

    // move and resize clients
    ClientList::iterator client_it = m_clients.begin();
    ClientList::iterator client_it_end = m_clients.end();
    int next_x = bw;
    for (; client_it != client_it_end; ++client_it) {
        if (!(*client_it)->isVisible())
            continue;
        int x = next_x, y = bw;
        next_x += h_rot0+bw;
        translateCoords(orientation(), x, y, w_rot0, h_rot0);
        translatePosition(orientation(), x, y, h_rot0, h_rot0, 0);
        int screen_x = 0, screen_y = 0;
        getScreenCoordinates((*client_it)->window(), (*client_it)->x(), (*client_it)->y(), screen_x, screen_y);

        (*client_it)->moveResize(x, y, h_rot0, h_rot0);
        (*client_it)->sendConfigureNotify(screen_x, screen_y, h_rot0, h_rot0);
    }
}

void SystemTray::removeAllClients() {
    BScreen *screen = Fluxbox::instance()->findScreen(window().screenNumber());
    while (!m_clients.empty()) {
        TrayWindow * traywin = m_clients.back();
        traywin->setEventMask(NoEventMask);

        if (traywin->isXEmbedded())
            traywin->hide();

        if (screen)
            traywin->reparent(screen->rootWindow(), 0, 0, false);
        traywin->removeFromSaveSet();
        delete traywin;
        m_clients.pop_back();
    }
    m_num_visible_clients = 0;
}

void SystemTray::hideClient(TrayWindow *traywin, bool destroyed) {
    if (!traywin || !traywin->isVisible())
        return;

    if (!destroyed)
        traywin->hide();
    m_num_visible_clients--;
    rearrangeClients();
}

void SystemTray::showClient(TrayWindow *traywin) {
    if (!traywin || traywin->isVisible())
        return;

    if (!m_num_visible_clients)
        show();

    traywin->show();
    m_num_visible_clients++;
    rearrangeClients();
}

void SystemTray::sortClients() {
    m_clients.sort([](TrayWindow *a, TrayWindow *b){return a->m_order < b->m_order;});
    rearrangeClients();
}

void SystemTray::update() {

    if (!m_theme->texture().usePixmap()) {
        m_window.setBackgroundColor(m_theme->texture().color());
    }
    else {
        if(m_pixmap)
            m_screen.imageControl().removeImage(m_pixmap);
        m_pixmap = m_screen.imageControl().renderImage(width(), height(),
                                                       m_theme->texture(), orientation());
        m_window.setBackgroundPixmap(m_pixmap);
    }

    ClientList::iterator client_it = m_clients.begin();
    ClientList::iterator client_it_end = m_clients.end();
    for (; client_it != client_it_end; ++client_it) {

        // maybe not the best solution (yet), force a refresh of the
        // background of the client
        if (!(*client_it)->isVisible())
            continue;
        (*client_it)->hide();
        (*client_it)->show();
    }

}

Atom SystemTray::getXEmbedInfoAtom() {
    static Atom theatom = XInternAtom(Fluxbox::instance()->display(), "_XEMBED_INFO", False);
    return theatom;
}

string SystemTray::getNetSystemTrayAtom(int screen_nr) {

    string atom_name("_NET_SYSTEM_TRAY_S");
    atom_name += FbTk::StringUtil::number2String(screen_nr);

    return atom_name;
}

bool SystemTray::doesControl(Window win) {
    if (win == None || !s_theoneandonly)
        return false;
    return win == s_theoneandonly->window().window() ||
           s_theoneandonly->findClient(win) != s_theoneandonly->m_clients.end();
}