summaryrefslogtreecommitdiff
path: root/src/utils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.cpp')
-rw-r--r--src/utils.cpp532
1 files changed, 109 insertions, 423 deletions
diff --git a/src/utils.cpp b/src/utils.cpp
index 9761055..0aebc12 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -3,10 +3,9 @@
*
* 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:
- * 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
@@ -30,497 +29,170 @@
#include <qt.h>
#include <X11/Xlib.h>
-#include <X11/X.h>
#include <X11/Xatom.h>
-#include <X11/Xmd.h>
#include <stdlib.h>
-static char program_name[] = "aquamarine";
-
-static int trapped_error_code = 0;
-Time dm_sn_timestamp;
+static int trappedErrorCode = 0;
namespace Aquamarine
{
- WId dm_window = None;
-
namespace Atoms
{
-
- Atom select_window;
-
- Atom manager;
- Atom targets;
- Atom multiple;
- Atom timestamp;
- Atom version;
- Atom atom_pair;
-
- Atom opacity;
- Atom saturation;
- Atom brightness;
-
- Atom utf8_string;
-
- Atom dm_name;
- Atom dm_sn;
-
- Atom net_frame_window;
- Atom net_window_decor;
- Atom net_wm_moveresize;
-
- Atom wm_delete_window;
- Atom wm_take_focus;
- Atom net_wm_context_help;
- Atom net_wm_ping;
- Atom net_wm_take_activity;
- Atom wm_protocols;
-
- Atom net_desktop_viewport;
- Atom net_desktop_geometry;
-
- Atom wm_name;
- Atom wm_sn;
-
- Atom change_frame_window;
-
+ Atom switchSelectWindow;
+ Atom netFrameWindow;
+ Atom netWindowDecor;
+ Atom netWindowDecorNormal;
+ Atom netWindowDecorActive;
+ Atom netWindowDecorBare;
+ Atom wmTakeFocus;
+ Atom netWmContextHelp;
+ Atom wmProtocols;
Atom toolkitActionAtom;
Atom toolkitActionWindowMenuAtom;
Atom toolkitActionMainMenuAtom;
Atom toolkitActionRunDialogAtom;
Atom toolkitActionForceQuitDialogAtom;
-
+ Atom wmSn;
+ Atom wmName;
+ Atom netDesktopViewport;
+ Atom netDesktopGeometry;
+
+ Atom netWmWindowOpacity;
+ Atom netWmWindowSaturation;
+ Atom netWmWindowBrightness;
}
}
-static int (*old_error_handler) (Display * d, XErrorEvent * e);
-static int
-xErrorHandler (Display *, XErrorEvent * error)
+static int (*oldErrorHandler) (Display *display, XErrorEvent *error);
+
+static int xErrorHandler (Display *display, XErrorEvent *error)
{
- trapped_error_code = error->error_code;
+ (void) display;
+ trappedErrorCode = error->error_code;
return 0;
}
-void
-Aquamarine::trapXError (void)
+void Aquamarine::trapXError (void)
{
- trapped_error_code = 0;
- old_error_handler = XSetErrorHandler (xErrorHandler);
+ trappedErrorCode = 0;
+ oldErrorHandler = XSetErrorHandler (xErrorHandler);
}
-int
-Aquamarine::popXError (void)
+int Aquamarine::popXError (void)
{
- XSetErrorHandler (old_error_handler);
- return trapped_error_code;
+ XSync (qt_xdisplay(), false);
+ XSetErrorHandler (oldErrorHandler);
+
+ return trappedErrorCode;
}
-void *
-Aquamarine::readXProperty (WId window, Atom property, Atom type, int *items)
+void *Aquamarine::readXProperty (WId window,
+ Atom property,
+ Atom type,
+ int *items)
{
- long offset = 0, length = 2048l;
- Atom actual_type;
- int actual_format;
- unsigned long nitems, bytes_remaining;
+ long offset = 0, length = 2048L;
+ Atom actualType;
+ int format;
+ unsigned long nItems, bytesRemaining;
unsigned char *data = 0l;
+ int result;
Aquamarine::trapXError ();
- int result =
- XGetWindowProperty (qt_xdisplay (), window, property, offset, length,
- false, type,
- &actual_type, &actual_format, &nitems,
- &bytes_remaining, &data);
+ result = XGetWindowProperty (qt_xdisplay (), window, property, offset,
+ length, false, type,
+ &actualType, &format, &nItems,
+ &bytesRemaining, &data);
if (Aquamarine::popXError ())
- return NULL;
- if (result == Success && actual_type == type
- && actual_format == 32 && nitems > 0)
+ return NULL;
+
+ if (result == Success && actualType == type && format == 32 && nItems > 0)
{
- if (items)
- *items = nitems;
+ if (items)
+ *items = nItems;
- return reinterpret_cast < void *>(data);
+ return reinterpret_cast <void *>(data);
}
if (data)
- XFree (data);
+ XFree (data);
if (items)
- *items = 0;
+ *items = 0;
return NULL;
}
-bool
-Aquamarine::readWindowProperty (long window, long property, long *value)
+bool Aquamarine::readWindowProperty (long window,
+ long property,
+ long *value)
{
void *data = readXProperty (window, property, XA_WINDOW, NULL);
if (data)
{
- if (value)
- *value = *reinterpret_cast < int *>(data);
- XFree (data);
+ if (value)
+ *value = *reinterpret_cast <int *>(data);
+
+ XFree (data);
- return true;
+ return true;
}
return false;
}
-unsigned short
-Aquamarine::readWindowPropertyDefault (WId id, Atom property,
- unsigned short defaultValue)
+unsigned short Aquamarine::readPropertyShort (WId id,
+ Atom property,
+ unsigned short defaultValue)
{
- Atom actual;
- int result, format;
+ Atom actual;
+ int result, format;
unsigned long n, left;
unsigned char *data;
+ Aquamarine::trapXError ();
result = XGetWindowProperty (qt_xdisplay (), id, property,
- 0L, 1L, FALSE, XA_CARDINAL, &actual, &format,
- &n, &left, &data);
+ 0L, 1L, FALSE, XA_CARDINAL, &actual, &format,
+ &n, &left, &data);
+ if (Aquamarine::popXError ())
+ return defaultValue;
if (result == Success && n && data)
{
- CARD32 value;
+ unsigned int value;
- memcpy (&value, data, sizeof (CARD32));
+ memcpy (&value, data, sizeof (unsigned int));
- XFree (data);
+ XFree (data);
- return value >> 16;
+ return value >> 16;
}
return defaultValue;
}
-void
-Aquamarine::setWindowProperty (WId id, Atom property, unsigned short value)
-{
- CARD32 value32;
-
- value32 = value << 16 | value;
-
- XEvent ev;
-
- memset (&ev, 0, sizeof (ev));
- ev.xclient.type = ClientMessage;
- ev.xclient.window = id;
- ev.xclient.message_type = property;
- ev.xclient.format = 32;
- ev.xclient.data.l[0] = value32;
- ev.xclient.data.l[1] = 0;
- ev.xclient.data.l[2] = 0;
- ev.xclient.data.l[3] = 0;
- ev.xclient.data.l[4] = 0;
-
- XSendEvent (qt_xdisplay (), qt_xrootwin(), False,
- SubstructureRedirectMask | SubstructureNotifyMask, &ev);
-}
-
-
-
-bool
-Aquamarine::acquireDmSession (Display * xdisplay, int screen, bool replace)
-{
- XEvent event;
- XSetWindowAttributes attr;
- Window current_dm_sn_owner, new_dm_sn_owner;
- char buf[128];
-
- sprintf (buf, "DM_S%d", screen);
- Atoms::dm_sn = XInternAtom (xdisplay, buf, 0);
-
- current_dm_sn_owner = XGetSelectionOwner (xdisplay, Atoms::dm_sn);
-
- if (current_dm_sn_owner != dm_window)
- {
- if (!replace)
- {
- fprintf (stderr,
- "%s: Screen %d on display \"%s\" already "
- "has a decoration manager; try using the "
- "--replace option to replace the current "
- "decoration manager.\n",
- program_name, screen, DisplayString (xdisplay));
-
- return false;
- }
-
- XSelectInput (xdisplay, current_dm_sn_owner, StructureNotifyMask);
- }
-
- if (current_dm_sn_owner == dm_window && current_dm_sn_owner != None)
- {
- return true;
- }
-
- attr.override_redirect = true;
- attr.event_mask = PropertyChangeMask;
-
- new_dm_sn_owner =
- XCreateWindow (xdisplay, XRootWindow (xdisplay, screen),
- -100, -100, 1, 1, 0,
- CopyFromParent, CopyFromParent,
- CopyFromParent,
- CWOverrideRedirect | CWEventMask, &attr);
-
- XChangeProperty (xdisplay,
- new_dm_sn_owner,
- Atoms::dm_name,
- Atoms::utf8_string, 8,
- PropModeReplace, (unsigned char *)"gwd", strlen ("gwd"));
-
- XWindowEvent (xdisplay, new_dm_sn_owner, PropertyChangeMask, &event);
-
- dm_sn_timestamp = event.xproperty.time;
-
- XSetSelectionOwner (xdisplay, Atoms::dm_sn, new_dm_sn_owner,
- dm_sn_timestamp);
-
- if (XGetSelectionOwner (xdisplay, Atoms::dm_sn) != new_dm_sn_owner)
- {
- fprintf (stderr,
- "%s: Could not acquire decoration manager "
- "selection on screen %d display \"%s\"\n",
- program_name, screen, DisplayString (xdisplay));
-
- XDestroyWindow (xdisplay, new_dm_sn_owner);
-
- return false;
- }
-
- /* Send client message indicating that we are now the DM */
- event.xclient.type = ClientMessage;
- event.xclient.window = XRootWindow (xdisplay, screen);
- event.xclient.message_type = Atoms::manager;
- event.xclient.format = 32;
- event.xclient.data.l[0] = dm_sn_timestamp;
- event.xclient.data.l[1] = Atoms::dm_name;
- event.xclient.data.l[2] = 0;
- event.xclient.data.l[3] = 0;
- event.xclient.data.l[4] = 0;
-
- XSendEvent (xdisplay, XRootWindow (xdisplay, screen), false,
- StructureNotifyMask, &event);
-
- /* Wait for old decoration manager to go away */
- if (current_dm_sn_owner != None)
- {
- do
- {
- XWindowEvent (xdisplay, current_dm_sn_owner,
- StructureNotifyMask, &event);
- } while (event.type != DestroyNotify);
- }
-
- dm_window = new_dm_sn_owner;
-
- return true;
-}
-
-void
-Aquamarine::set_dm_check_hint (int screen)
+void Aquamarine::Atoms::init (void)
{
- XSetWindowAttributes attrs;
- unsigned long data[1];
- Window xroot;
- Display *xdisplay = qt_xdisplay ();
- Atom atom;
-
- attrs.override_redirect = true;
- attrs.event_mask = PropertyChangeMask;
-
- xroot = RootWindow (xdisplay, screen);
-
- data[0] = XCreateWindow (xdisplay,
- xroot,
- -100, -100, 1, 1,
- 0,
- CopyFromParent,
- CopyFromParent,
- (Visual *) CopyFromParent,
- CWOverrideRedirect | CWEventMask, &attrs);
-
- atom = XInternAtom (xdisplay, "_NET_SUPPORTING_DM_CHECK", FALSE);
-
- XChangeProperty (xdisplay, xroot,
- atom,
- XA_WINDOW,
- 32, PropModeReplace, (unsigned char *)data, 1);
-}
-
-/* from fvwm2, Copyright Matthias Clasen, Dominik Vogt */
-bool
-Aquamarine::convertProperty (WId w, Atom target, Atom property)
-{
-
-#define N_TARGETS 4
-
- Atom conversion_targets[N_TARGETS];
- long icccm_version[] = { 2, 0 };
-
- conversion_targets[0] = Atoms::targets;
- conversion_targets[1] = Atoms::multiple;
- conversion_targets[2] = Atoms::timestamp;
- conversion_targets[3] = Atoms::version;
-
- if (target == Atoms::targets)
- XChangeProperty (qt_xdisplay (), w, property,
- XA_ATOM, 32, PropModeReplace,
- (unsigned char *)conversion_targets, N_TARGETS);
- else if (target == Atoms::timestamp)
- XChangeProperty (qt_xdisplay (), w, property,
- XA_INTEGER, 32, PropModeReplace,
- (unsigned char *)&dm_sn_timestamp, 1);
- else if (target == Atoms::version)
- XChangeProperty (qt_xdisplay (), w, property,
- XA_INTEGER, 32, PropModeReplace,
- (unsigned char *)icccm_version, 2);
- else
- return FALSE;
-
- /* Be sure the PropertyNotify has arrived so we
- * can send SelectionNotify
- */
- XSync (qt_xdisplay (), FALSE);
-
- return TRUE;
-}
-
-void
-Aquamarine::handleSelectionRequest (XEvent * event)
-{
- XSelectionEvent reply;
-
- reply.type = SelectionNotify;
- reply.display = qt_xdisplay ();
- reply.requestor = event->xselectionrequest.requestor;
- reply.selection = event->xselectionrequest.selection;
- reply.target = event->xselectionrequest.target;
- reply.property = None;
- reply.time = event->xselectionrequest.time;
-
- if (event->xselectionrequest.target == Atoms::multiple)
- {
- if (event->xselectionrequest.property != None)
- {
- Atom type, *adata;
- int i, format;
- unsigned long num, rest;
- unsigned char *data;
-
- if (XGetWindowProperty (qt_xdisplay (),
- event->xselectionrequest.requestor,
- event->xselectionrequest.property,
- 0, 256, FALSE,
- Atoms::atom_pair,
- &type, &format, &num, &rest,
- &data) != Success)
- return;
-
- /* FIXME: to be 100% correct, should deal with rest > 0,
- * but since we have 4 possible targets, we will hardly ever
- * meet multiple requests with a length > 8
- */
- adata = (Atom *) data;
- i = 0;
- while (i < (int)num)
- {
- if (!convertProperty (event->xselectionrequest.requestor,
- adata[i], adata[i + 1]))
- adata[i + 1] = None;
-
- i += 2;
- }
-
- XChangeProperty (qt_xdisplay (),
- event->xselectionrequest.requestor,
- event->xselectionrequest.property,
- Atoms::atom_pair,
- 32, PropModeReplace, data, num);
- }
- }
- else
- {
- if (event->xselectionrequest.property == None)
- event->xselectionrequest.property =
- event->xselectionrequest.target;
-
- if (convertProperty (event->xselectionrequest.requestor,
- event->xselectionrequest.target,
- event->xselectionrequest.property))
- reply.property = event->xselectionrequest.property;
- }
-
- XSendEvent (qt_xdisplay (),
- event->xselectionrequest.requestor,
- FALSE, 0L, (XEvent *) & reply);
-}
-
-bool
-Aquamarine::validWindow (WId id)
-{
- Aquamarine::trapXError ();
- XWindowAttributes
- attr;
- if (XGetWindowAttributes (qt_xdisplay (), id, &attr)
- && !Aquamarine::popXError ())
- return true;
- return false;
-}
-
-void
-Aquamarine::Atoms::init ()
-{
- Display *
- xdisplay = qt_xdisplay ();
-
- net_frame_window = XInternAtom (xdisplay, "_NET_FRAME_WINDOW", false);
- net_window_decor = XInternAtom (xdisplay, "_NET_WINDOW_DECOR", false);
- net_wm_moveresize = XInternAtom (xdisplay, "_NET_WM_MOVERESIZE", false);
-
- select_window = XInternAtom (xdisplay, "_SWITCH_SELECT_WINDOW", false);
-
- manager = XInternAtom (xdisplay, "MANAGER", false);
- targets = XInternAtom (xdisplay, "TARGETS", false);
- multiple = XInternAtom (xdisplay, "MULTIPLE", false);
- timestamp = XInternAtom (xdisplay, "TIMESTAMP", false);
- version = XInternAtom (xdisplay, "VERSION", false);
- atom_pair = XInternAtom (xdisplay, "ATOM_PAIR", false);
-
- utf8_string = XInternAtom (xdisplay, "UTF8_STRING", false);
-
- dm_name = XInternAtom (xdisplay, "_NET_DM_NAME", false);
-
- wm_delete_window = XInternAtom (xdisplay, "WM_DELETE_WINDOW", false);
- wm_take_focus = XInternAtom (xdisplay, "WM_TAKE_FOCUS", false);
- net_wm_context_help =
- XInternAtom (xdisplay, "_NET_WM_CONTEXT_HELP", false);
- net_wm_ping = XInternAtom (xdisplay, "_NET_WM_PING", false);
- net_wm_take_activity =
- XInternAtom (xdisplay, "_NET_WM_TAKE_ACTIVITY", false);
- wm_protocols = XInternAtom (xdisplay, "WM_PROTOCOLS", false);
-
- net_desktop_viewport =
- XInternAtom (xdisplay, "_NET_DESKTOP_VIEWPORT", false);
- net_desktop_geometry =
- XInternAtom (xdisplay, "_NET_DESKTOP_GEOMETRY", false);
-
- opacity = XInternAtom (xdisplay, "_NET_WM_WINDOW_OPACITY", false);
- brightness = XInternAtom (xdisplay, "_NET_WM_WINDOW_BRIGHTNESS", false);
- saturation = XInternAtom (xdisplay, "_NET_WM_WINDOW_SATURATION", false);
-
- wm_name = XInternAtom (xdisplay, "_NET_WM_NAME", false);
- char buf[128];
- sprintf(buf,"WM_S%d",qt_xscreen());
- wm_sn = XInternAtom (xdisplay, buf, false);
-
- change_frame_window =
- XInternAtom (xdisplay, "_BERYL_CHANGE_FRAME_WINDOW", false);
-
+ Display *xdisplay = qt_xdisplay ();
+
+ netFrameWindow = XInternAtom (xdisplay, "_NET_FRAME_WINDOW", false);
+ netWindowDecor = XInternAtom (xdisplay, "_NET_WINDOW_DECOR", false);
+ netWindowDecorNormal =
+ XInternAtom (xdisplay, "_NET_WINDOW_DECOR_NORMAL", false);
+ netWindowDecorActive =
+ XInternAtom (xdisplay, "_NET_WINDOW_DECOR_ACTIVE", false);
+ netWindowDecorBare =
+ XInternAtom (xdisplay, "_NET_WINDOW_DECOR_BARE", false);
+ switchSelectWindow = XInternAtom (xdisplay, "_SWITCH_SELECT_WINDOW", false);
+ wmTakeFocus = XInternAtom (xdisplay, "WM_TAKE_FOCUS", false);
+ netWmContextHelp =
+ XInternAtom (xdisplay, "_NET_WM_CONTEXT_HELP", false);
+ wmProtocols = XInternAtom (xdisplay, "WM_PROTOCOLS", false);
+ netWmWindowOpacity =
+ XInternAtom (xdisplay, "_NET_WM_WINDOW_OPACITY", false);
toolkitActionAtom = XInternAtom (xdisplay, "_COMPIZ_TOOLKIT_ACTION", false);
toolkitActionWindowMenuAtom =
XInternAtom (xdisplay, "_COMPIZ_TOOLKIT_ACTION_WINDOW_MENU", false);
@@ -529,6 +201,20 @@ Aquamarine::Atoms::init ()
toolkitActionRunDialogAtom =
XInternAtom (xdisplay, "_COMPIZ_TOOLKIT_ACTION_RUN_DIALOG", false);
toolkitActionForceQuitDialogAtom =
- XInternAtom (xdisplay, "_COMPIZ_TOOLKIT_ACTION_FORCE_QUIT_DIALOG", false);
-
+ XInternAtom (xdisplay, "_COMPIZ_TOOLKIT_ACTION_FORCE_QUIT_DIALOG",
+ false);
+
+ wmName = XInternAtom (xdisplay, "_NET_WM_NAME", false);
+ char buf[128];
+ sprintf(buf,"WM_S%d",qt_xscreen());
+ wmSn = XInternAtom (xdisplay, buf, false);
+
+ netDesktopViewport =
+ XInternAtom (xdisplay, "_NET_DESKTOP_VIEWPORT", false);
+ netDesktopGeometry =
+ XInternAtom (xdisplay, "_NET_DESKTOP_GEOMETRY", false);
+
+ netWmWindowOpacity = XInternAtom (xdisplay, "_NET_WM_WINDOW_OPACITY", false);
+ netWmWindowBrightness = XInternAtom (xdisplay, "_NET_WM_WINDOW_BRIGHTNESS", false);
+ netWmWindowSaturation = XInternAtom (xdisplay, "_NET_WM_WINDOW_SATURATION", false);
}