2006-11-26 11:13:41 -07:00
|
|
|
/*
|
|
|
|
* Copyright 2002-2003 Red Hat Inc., Durham, North Carolina.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* 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 on 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 (including the
|
|
|
|
* next paragraph) 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
|
|
|
|
* NON-INFRINGEMENT. IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Authors:
|
|
|
|
* Rickard E. (Rik) Faith <faith@redhat.com>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
*
|
|
|
|
* It is possible for one of the DMX "backend displays" to actually be
|
|
|
|
* smaller than the dimensions of the backend X server. Therefore, it
|
|
|
|
* is possible for more than one of the DMX "backend displays" to be
|
|
|
|
* physically located on the same backend X server. This situation must
|
|
|
|
* be detected so that cursor motion can be handled in an expected
|
|
|
|
* fashion.
|
|
|
|
*
|
|
|
|
* We could analyze the names used for the DMX "backend displays" (e.g.,
|
|
|
|
* the names passed to the -display command-line parameter), but there
|
|
|
|
* are many possible names for a single X display, and failing to detect
|
|
|
|
* sameness leads to very unexpected results. Therefore, whenever the
|
|
|
|
* DMX server opens a window on a backend X server, a property value is
|
|
|
|
* queried and set on that backend to detect when another window is
|
|
|
|
* already open on that server.
|
|
|
|
*
|
|
|
|
* Further, it is possible that two different DMX server instantiations
|
|
|
|
* both have windows on the same physical backend X server. This case
|
|
|
|
* is also detected so that pointer input is not taken from that
|
|
|
|
* particular backend X server.
|
|
|
|
*
|
|
|
|
* The routines in this file handle the property management. */
|
|
|
|
|
|
|
|
#ifdef HAVE_DMX_CONFIG_H
|
|
|
|
#include <dmx-config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "dmx.h"
|
|
|
|
#include "dmxprop.h"
|
|
|
|
#include "dmxlog.h"
|
2012-06-10 07:21:05 -06:00
|
|
|
#include <X11/Xmu/SysUtil.h> /* For XmuGetHostname */
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
/** Holds the window id of all DMX windows on the backend X server. */
|
|
|
|
#define DMX_ATOMNAME "DMX_NAME"
|
|
|
|
|
|
|
|
/** The identification string of this DMX server */
|
|
|
|
#define DMX_IDENT "Xdmx"
|
|
|
|
|
|
|
|
extern char *display;
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
static int
|
|
|
|
dmxPropertyErrorHandler(Display * dpy, XErrorEvent * ev)
|
2006-11-26 11:13:41 -07:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
static const unsigned char *
|
|
|
|
dmxPropertyIdentifier(void)
|
2006-11-26 11:13:41 -07:00
|
|
|
{
|
2012-06-10 07:21:05 -06:00
|
|
|
/* RATS: These buffers are only used in
|
|
|
|
* length-limited calls. */
|
|
|
|
char hostname[256];
|
|
|
|
static char buf[128];
|
|
|
|
static int initialized = 0;
|
|
|
|
|
|
|
|
if (initialized++)
|
|
|
|
return (unsigned char *) buf;
|
|
|
|
|
2006-11-26 11:13:41 -07:00
|
|
|
XmuGetHostname(hostname, sizeof(hostname));
|
2012-06-10 07:21:05 -06:00
|
|
|
snprintf(buf, sizeof(buf), "%s:%s:%s", DMX_IDENT, hostname, display);
|
|
|
|
return (unsigned char *) buf;
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Starting with the \a start screen, iterate over all of the screens
|
|
|
|
* on the same physical X server as \a start, calling \a f with the
|
|
|
|
* screen and the \a closure. (The common case is that \a start is the
|
|
|
|
* only DMX window on the backend X server.) */
|
2012-06-10 07:21:05 -06:00
|
|
|
void *
|
|
|
|
dmxPropertyIterate(DMXScreenInfo * start,
|
|
|
|
void *(*f) (DMXScreenInfo * dmxScreen, void *),
|
|
|
|
void *closure)
|
2006-11-26 11:13:41 -07:00
|
|
|
{
|
|
|
|
DMXScreenInfo *pt;
|
|
|
|
|
|
|
|
if (!start->next) {
|
2012-06-10 07:21:05 -06:00
|
|
|
if (!start->beDisplay)
|
|
|
|
return NULL;
|
2006-11-26 11:13:41 -07:00
|
|
|
return f(start, closure);
|
|
|
|
}
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
for (pt = start->next; /* condition at end of loop */ ; pt = pt->next) {
|
2006-11-26 11:13:41 -07:00
|
|
|
void *retval;
|
2012-06-10 07:21:05 -06:00
|
|
|
|
2006-11-26 11:13:41 -07:00
|
|
|
/* beDisplay ban be NULL if a screen was detached */
|
|
|
|
dmxLog(dmxDebug, "pt = %p\n", pt);
|
|
|
|
dmxLog(dmxDebug, "pt->beDisplay = %p\n", pt->beDisplay);
|
2012-06-10 07:21:05 -06:00
|
|
|
if (pt->beDisplay && (retval = f(pt, closure)))
|
|
|
|
return retval;
|
|
|
|
if (pt == start)
|
|
|
|
break;
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns 0 if this is the only Xdmx session on the display; 1
|
|
|
|
* otherwise. */
|
2012-06-10 07:21:05 -06:00
|
|
|
static int
|
|
|
|
dmxPropertyCheckOtherServers(DMXScreenInfo * dmxScreen, Atom atom)
|
2006-11-26 11:13:41 -07:00
|
|
|
{
|
2012-06-10 07:21:05 -06:00
|
|
|
Display *dpy = dmxScreen->beDisplay;
|
2006-11-26 11:13:41 -07:00
|
|
|
XTextProperty tp;
|
|
|
|
XTextProperty tproot;
|
2012-06-10 07:21:05 -06:00
|
|
|
const char *pt;
|
|
|
|
int retcode = 0;
|
|
|
|
char **list = NULL;
|
|
|
|
int count = 0;
|
|
|
|
int i;
|
|
|
|
int (*dmxOldHandler) (Display *, XErrorEvent *);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
if (!dpy)
|
2012-06-10 07:21:05 -06:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!XGetTextProperty(dpy, RootWindow(dpy, 0), &tproot, atom)
|
|
|
|
|| !tproot.nitems)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Ignore BadWindow errors for this
|
|
|
|
* routine because the window id stored
|
|
|
|
* in the property might be old */
|
2006-11-26 11:13:41 -07:00
|
|
|
dmxOldHandler = XSetErrorHandler(dmxPropertyErrorHandler);
|
2012-06-10 07:21:05 -06:00
|
|
|
for (pt = (const char *) tproot.value; pt && *pt; pt = pt ? pt + 1 : NULL) {
|
2006-11-26 11:13:41 -07:00
|
|
|
if ((pt = strchr(pt, ','))) {
|
2012-06-10 07:21:05 -06:00
|
|
|
Window win = strtol(pt + 1, NULL, 10);
|
|
|
|
|
2006-11-26 11:13:41 -07:00
|
|
|
if (XGetTextProperty(dpy, win, &tp, atom) && tp.nitems) {
|
2012-06-10 07:21:05 -06:00
|
|
|
if (!strncmp((char *) tp.value, DMX_IDENT, strlen(DMX_IDENT))) {
|
2006-11-26 11:13:41 -07:00
|
|
|
int flag = 0;
|
2012-06-10 07:21:05 -06:00
|
|
|
|
2006-11-26 11:13:41 -07:00
|
|
|
for (i = 0; i < count; i++)
|
2012-06-10 07:21:05 -06:00
|
|
|
if (!strcmp(list[i], (char *) tp.value)) {
|
2006-11-26 11:13:41 -07:00
|
|
|
++flag;
|
|
|
|
break;
|
|
|
|
}
|
2012-06-10 07:21:05 -06:00
|
|
|
if (flag)
|
|
|
|
continue;
|
2006-11-26 11:13:41 -07:00
|
|
|
++retcode;
|
|
|
|
dmxLogOutputWarning(dmxScreen,
|
|
|
|
"%s also running on %s\n",
|
|
|
|
tp.value, dmxScreen->name);
|
2010-12-05 08:36:02 -07:00
|
|
|
list = realloc(list, ++count * sizeof(*list));
|
2012-06-10 07:21:05 -06:00
|
|
|
list[count - 1] = malloc(tp.nitems + 2);
|
|
|
|
strncpy(list[count - 1], (char *) tp.value, tp.nitems + 1);
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
XFree(tp.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
XSetErrorHandler(dmxOldHandler);
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
free(list[i]);
|
2010-12-05 08:36:02 -07:00
|
|
|
free(list);
|
2006-11-26 11:13:41 -07:00
|
|
|
XFree(tproot.value);
|
|
|
|
if (!retcode)
|
|
|
|
dmxLogOutput(dmxScreen, "No Xdmx server running on backend\n");
|
|
|
|
return retcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns NULL if this is the only Xdmx window on the display.
|
|
|
|
* Otherwise, returns a pointer to the dmxScreen of the other windows on
|
|
|
|
* the display. */
|
2012-06-10 07:21:05 -06:00
|
|
|
static DMXScreenInfo *
|
|
|
|
dmxPropertyCheckOtherWindows(DMXScreenInfo * dmxScreen, Atom atom)
|
2006-11-26 11:13:41 -07:00
|
|
|
{
|
2012-06-10 07:21:05 -06:00
|
|
|
Display *dpy = dmxScreen->beDisplay;
|
|
|
|
const unsigned char *id = dmxPropertyIdentifier();
|
|
|
|
XTextProperty tproot;
|
|
|
|
XTextProperty tp;
|
|
|
|
const char *pt;
|
|
|
|
int (*dmxOldHandler) (Display *, XErrorEvent *);
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
if (!dpy)
|
2012-06-10 07:21:05 -06:00
|
|
|
return NULL;
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
if (!XGetTextProperty(dpy, RootWindow(dpy, 0), &tproot, atom)
|
|
|
|
|| !tproot.nitems)
|
|
|
|
return 0;
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
/* Ignore BadWindow errors for this
|
|
|
|
* routine because the window id stored
|
|
|
|
* in the property might be old */
|
2006-11-26 11:13:41 -07:00
|
|
|
dmxOldHandler = XSetErrorHandler(dmxPropertyErrorHandler);
|
2012-06-10 07:21:05 -06:00
|
|
|
for (pt = (const char *) tproot.value; pt && *pt; pt = pt ? pt + 1 : NULL) {
|
2006-11-26 11:13:41 -07:00
|
|
|
if ((pt = strchr(pt, ','))) {
|
2012-06-10 07:21:05 -06:00
|
|
|
Window win = strtol(pt + 1, NULL, 10);
|
|
|
|
|
2006-11-26 11:13:41 -07:00
|
|
|
if (XGetTextProperty(dpy, win, &tp, atom) && tp.nitems) {
|
2012-06-10 07:21:05 -06:00
|
|
|
dmxLog(dmxDebug, "On %s/%lu: %s\n",
|
2015-11-07 09:48:51 -07:00
|
|
|
dmxScreen->name, (unsigned long) win, tp.value);
|
2012-06-10 07:21:05 -06:00
|
|
|
if (!strncmp((char *) tp.value, (char *) id,
|
|
|
|
strlen((char *) id))) {
|
2006-11-26 11:13:41 -07:00
|
|
|
int idx;
|
2012-06-10 07:21:05 -06:00
|
|
|
|
|
|
|
if (!(pt = strchr((char *) tp.value, ',')))
|
|
|
|
continue;
|
|
|
|
idx = strtol(pt + 1, NULL, 10);
|
|
|
|
if (idx < 0 || idx >= dmxNumScreens)
|
|
|
|
continue;
|
|
|
|
if (dmxScreens[idx].scrnWin != win)
|
|
|
|
continue;
|
2006-11-26 11:13:41 -07:00
|
|
|
XSetErrorHandler(dmxOldHandler);
|
|
|
|
return &dmxScreens[idx];
|
|
|
|
}
|
|
|
|
XFree(tp.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
XSetErrorHandler(dmxOldHandler);
|
|
|
|
XFree(tproot.value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns 0 if this is the only Xdmx session on the display; 1
|
|
|
|
* otherwise. */
|
2012-06-10 07:21:05 -06:00
|
|
|
int
|
|
|
|
dmxPropertyDisplay(DMXScreenInfo * dmxScreen)
|
2006-11-26 11:13:41 -07:00
|
|
|
{
|
2012-06-10 07:21:05 -06:00
|
|
|
Atom atom;
|
|
|
|
const unsigned char *id = dmxPropertyIdentifier();
|
|
|
|
Display *dpy = dmxScreen->beDisplay;
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
if (!dpy)
|
2012-06-10 07:21:05 -06:00
|
|
|
return 0;
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
atom = XInternAtom(dpy, DMX_ATOMNAME, False);
|
|
|
|
if (dmxPropertyCheckOtherServers(dmxScreen, atom)) {
|
|
|
|
dmxScreen->shared = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
2012-06-10 07:21:05 -06:00
|
|
|
XChangeProperty(dpy, RootWindow(dpy, 0), atom, XA_STRING, 8,
|
|
|
|
PropModeReplace, id, strlen((char *) id));
|
2006-11-26 11:13:41 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns 1 if the dmxScreen and the display in \a name are on the
|
|
|
|
* same display, or 0 otherwise. We can't just compare the display
|
|
|
|
* names because there can be multiple synonyms for the same display,
|
|
|
|
* some of which cannot be determined without accessing the display
|
|
|
|
* itself (e.g., domain aliases or machines with multiple NICs). */
|
2012-06-10 07:21:05 -06:00
|
|
|
int
|
|
|
|
dmxPropertySameDisplay(DMXScreenInfo * dmxScreen, const char *name)
|
2006-11-26 11:13:41 -07:00
|
|
|
{
|
2012-06-10 07:21:05 -06:00
|
|
|
Display *dpy0 = dmxScreen->beDisplay;
|
|
|
|
Atom atom0;
|
|
|
|
XTextProperty tp0;
|
|
|
|
Display *dpy1 = NULL;
|
|
|
|
Atom atom1;
|
|
|
|
XTextProperty tp1;
|
|
|
|
int retval = 0;
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
if (!dpy0)
|
2012-06-10 07:21:05 -06:00
|
|
|
return 0;
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
tp0.nitems = 0;
|
|
|
|
tp1.nitems = 0;
|
|
|
|
|
|
|
|
if ((atom0 = XInternAtom(dpy0, DMX_ATOMNAME, True)) == None) {
|
|
|
|
dmxLog(dmxWarning, "No atom on %s\n", dmxScreen->name);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-06-10 07:21:05 -06:00
|
|
|
if (!XGetTextProperty(dpy0, RootWindow(dpy0, 0), &tp0, atom0)
|
2006-11-26 11:13:41 -07:00
|
|
|
|| !tp0.nitems) {
|
|
|
|
dmxLog(dmxWarning, "No text property on %s\n", dmxScreen->name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(dpy1 = XOpenDisplay(name))) {
|
|
|
|
dmxLog(dmxWarning, "Cannot open %s\n", name);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
atom1 = XInternAtom(dpy1, DMX_ATOMNAME, True);
|
|
|
|
if (atom1 == None) {
|
|
|
|
dmxLog(dmxDebug, "No atom on %s\n", name);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-06-10 07:21:05 -06:00
|
|
|
if (!XGetTextProperty(dpy1, RootWindow(dpy1, 0), &tp1, atom1)
|
2006-11-26 11:13:41 -07:00
|
|
|
|| !tp1.nitems) {
|
|
|
|
dmxLog(dmxDebug, "No text property on %s\n", name);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-06-10 07:21:05 -06:00
|
|
|
if (!strcmp((char *) tp0.value, (char *) tp1.value))
|
|
|
|
retval = 1;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (tp0.nitems)
|
|
|
|
XFree(tp0.value);
|
|
|
|
if (tp1.nitems)
|
|
|
|
XFree(tp1.value);
|
|
|
|
if (dpy1)
|
|
|
|
XCloseDisplay(dpy1);
|
2006-11-26 11:13:41 -07:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Prints a log message if \a dmxScreen is on the same backend X server
|
|
|
|
* as some other DMX backend (output) screen. Modifies the property
|
|
|
|
* (#DMX_ATOMNAME) on the backend X server to reflect the creation of \a
|
|
|
|
* dmxScreen.
|
|
|
|
*
|
|
|
|
* The root window of the backend X server holds a list of window ids
|
|
|
|
* for all DMX windows (on this DMX server or some other DMX server).
|
|
|
|
*
|
|
|
|
* This list can then be iterated, and the property for each window can
|
|
|
|
* be examined. This property contains the following tuple (no quotes):
|
|
|
|
*
|
|
|
|
* "#DMX_IDENT:<hostname running DMX>:<display name of DMX>,<screen number>"
|
|
|
|
*/
|
2012-06-10 07:21:05 -06:00
|
|
|
void
|
|
|
|
dmxPropertyWindow(DMXScreenInfo * dmxScreen)
|
2006-11-26 11:13:41 -07:00
|
|
|
{
|
2012-06-10 07:21:05 -06:00
|
|
|
Atom atom;
|
|
|
|
const unsigned char *id = dmxPropertyIdentifier();
|
|
|
|
Display *dpy = dmxScreen->beDisplay;
|
|
|
|
Window win = dmxScreen->scrnWin;
|
|
|
|
DMXScreenInfo *other;
|
|
|
|
char buf[128]; /* RATS: only used with snprintf */
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
if (!dpy)
|
2012-06-10 07:21:05 -06:00
|
|
|
return; /* FIXME: What should be done here if Xdmx is started
|
|
|
|
* with this screen initially detached?
|
|
|
|
*/
|
2006-11-26 11:13:41 -07:00
|
|
|
|
|
|
|
atom = XInternAtom(dpy, DMX_ATOMNAME, False);
|
|
|
|
if ((other = dmxPropertyCheckOtherWindows(dmxScreen, atom))) {
|
|
|
|
DMXScreenInfo *tmp = dmxScreen->next;
|
2012-06-10 07:21:05 -06:00
|
|
|
|
|
|
|
dmxScreen->next = (other->next ? other->next : other);
|
|
|
|
other->next = (tmp ? tmp : dmxScreen);
|
2006-11-26 11:13:41 -07:00
|
|
|
dmxLog(dmxDebug, "%d/%s/%lu and %d/%s/%lu are on the same backend\n",
|
2015-11-07 09:48:51 -07:00
|
|
|
dmxScreen->index, dmxScreen->name, (unsigned long) dmxScreen->scrnWin,
|
|
|
|
other->index, other->name, (unsigned long) other->scrnWin);
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
snprintf(buf, sizeof(buf), ".%d,%lu", dmxScreen->index,
|
|
|
|
(long unsigned) win);
|
|
|
|
XChangeProperty(dpy, RootWindow(dpy, 0), atom, XA_STRING, 8,
|
|
|
|
PropModeAppend, (unsigned char *) buf, strlen(buf));
|
2006-11-26 11:13:41 -07:00
|
|
|
|
2012-06-10 07:21:05 -06:00
|
|
|
snprintf(buf, sizeof(buf), "%s,%d", id, dmxScreen->index);
|
2006-11-26 11:13:41 -07:00
|
|
|
XChangeProperty(dpy, win, atom, XA_STRING, 8,
|
2012-06-10 07:21:05 -06:00
|
|
|
PropModeAppend, (unsigned char *) buf, strlen(buf));
|
2006-11-26 11:13:41 -07:00
|
|
|
}
|