summaryrefslogtreecommitdiff
path: root/src/aquamarine.cpp
blob: bcc96e3824d74c9f82b160089a32bc487c02a184 (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
/*
 *  Aquamarine the KDE window decorator
 *
 *  Copyright (c) 2006 Dennis Kasprzyk <onestone@beryl-project.org>
 *  Copyright (c) 2006 Volker Krause <vkrause@kde.org>
 *
 *  Uses code of:
 *      Emerald window decorator (www.beryl-project.org)
 *      gtk-window-decorator (www.freedesktop.org/wiki/Software/Compiz)
 *      KWin window manager (www.kde.org)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#  include "../config.h"
#endif

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xrender.h>

#include <fixx11h.h>

#include <dcopclient.h>
#include <kapplication.h>
#include <kcmdlineargs.h>
#include <kglobal.h>
#include <klocale.h>

#include <stdlib.h>

#include "decorator.h"
#include "utils.h"

static const KCmdLineOptions options[] = {
    {"replace", "Replace existing window decorator", 0},
    {"deco <deco>", "Decoration", 0},
    KCmdLineLastOption
};


int
main (int argc, char **argv)
{
    bool argb_visual = false;
    char *display = 0;
    Display *dpy = XOpenDisplay (display);
    int screen = DefaultScreen (dpy);

    KCmdLineArgs::init (argc, argv, "aquamarine", I18N_NOOP ("Aquamarine"),
                        I18N_NOOP ("KDE Window Decorator for Beryl"), VERSION);
    KCmdLineArgs::addCmdLineOptions (options);

    Atom wm_name = XInternAtom (dpy, "_NET_WM_NAME", false);
    char buf[128];
    sprintf(buf,"WM_S%d",screen);
    Atom wm_sn = XInternAtom (dpy, buf, false);
    Atom manager = XInternAtom (dpy, "MANAGER", false);
    Atom utf8_string = XInternAtom (dpy, "UTF8_STRING", false);

    Bool restart = FALSE;
    int rv = 1;
    do
    {

        if (!dpy)
        {
            qWarning ("cannot connect to X server ");
            exit (1);
        }


        Colormap colormap = 0;
        Visual *visual = 0;
        int event_base, error_base;

        if (XRenderQueryExtension (dpy, &event_base, &error_base))
        {
            int nvi;
            XVisualInfo templ;
            templ.screen = screen;
            templ.depth = 32;
            templ.c_class = TrueColor;
            XVisualInfo *xvi =
                XGetVisualInfo (dpy,
                                VisualScreenMask | VisualDepthMask |
                                VisualClassMask, &templ, &nvi);

            for (int i = 0; i < nvi; i++)
            {
                XRenderPictFormat *format =
                    XRenderFindVisualFormat (dpy, xvi[i].visual);
                if (format->type == PictTypeDirect && format->direct.alphaMask)
                {
                    visual = xvi[i].visual;
                    colormap =
                        XCreateColormap (dpy, RootWindow (dpy, screen), visual,
                                        AllocNone);
                    argb_visual = true;
                    break;
                }
            }
        }

        Bool wmRunning = false;
        do
        {
            XEvent xev;
            WId wm = XGetSelectionOwner(dpy,wm_sn);
            if (wm != None)
            {
                Atom actual;
                int result, format;
                unsigned long n, left;
                unsigned char * data;

                result = XGetWindowProperty (dpy, wm, wm_name,
                                    0L, 2L, FALSE, utf8_string, &actual, &format,
                                    &n, &left, &data);

                if (result == Success && n && data)
                {
                    printf("Window Manager \"%s\"\n",data);
                    sprintf(buf,"%s",data);
                    if (strcmp(buf,"beryl") == 0)
                    {
                        wmRunning = true;
                    }
                }

                if (!wmRunning)
                {
                    XSelectInput(dpy, wm, StructureNotifyMask);
                    printf("Found not compatible window manager. Waiting...\n");
                    do
                    {
                        XWindowEvent(dpy, wm, StructureNotifyMask, &xev);
                    } while (xev.type != DestroyNotify);
                }
            }
            else
            {
                printf("No window manager. Waiting...\n");
                XSelectInput(dpy, RootWindow(dpy, screen), StructureNotifyMask);
                Bool foundWM = false;
                do
                {
                    XNextEvent(dpy, &xev);
                    if (xev.type == ClientMessage &&
                        xev.xclient.message_type == manager &&
                        (Atom)xev.xclient.data.l[1] == wm_sn)
                    {
                        printf("New Window Manager\n");
                        foundWM = true;
                    }
                } while (xev.type == ClientMessage &&
                        xev.xclient.message_type == manager &&
                        (Atom)xev.xclient.data.l[1] == wm_sn);
            }
        } while (!wmRunning);

        Aquamarine::Decorator *app = new Aquamarine::Decorator(dpy, Qt::HANDLE (visual),
                                Qt::HANDLE (colormap));

        DCOPClient *client = app->dcopClient ();
        client->registerAs ("kwin", false);
        client->setDefaultObject ("KWinInterface");

        if (app->isReady ()) {
            rv = app->exec ();
            if (rv == 0)
                restart = app->restart();
        }
        delete app;
        delete KGlobal::_locale; // HACK to fix crash during KApplication restart
        KGlobal::_locale = 0;

    } while (restart);
    return rv;
}