summaryrefslogtreecommitdiff
path: root/src/aquamarine.cpp
blob: 0ec9fd7933c2e1afa6e418dac1c1dc550b9aa005 (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
/*
 *  Aquamarine the KDE window decorator
 *
 *  Copyright (c) 2006 Dennis Kasprzyk <onestone@beryl-project.org>
 *  Copyright (c) 2006 Volker Krause <vkrause@kde.org>
 *  Copyright (c) 2006 David Reveman <davidr@novell.com>
 *
 *  Uses code of:
 *      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)
{
    char *display = 0;
    Display *dpy = XOpenDisplay (display);
	if (!dpy)
	{
		qWarning ("cannot connect to X server ");
		exit (1);
	}
    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
    {
		Aquamarine::trapXError();

        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 = NULL;

                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 (data)
					XFree(data);

                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 (!foundWM);
            }
			Aquamarine::popXError(dpy);

        } while (!wmRunning);

        Aquamarine::Decorator *app = new Aquamarine::Decorator();

        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;
}