summaryrefslogtreecommitdiff
path: root/utils/gdm-dmx-reconnect-proxy.c
blob: c3bab83e67b91a90035b27da98883dd9297b323c (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
/*
 * Copyright (C) 2005 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <stdlib.h>
#include <locale.h>
#include <glib.h>
#include <glib/gi18n.h>

#include <X11/Xlib.h>
#include <X11/extensions/dmxext.h>

static char *to_display = NULL;
static char *backend_display = NULL;
static char *to_authfile = NULL;
static char *backend_authfile = NULL;

static GOptionEntry options[] = {
	{
		"to", 0, 0, G_OPTION_ARG_STRING, &to_display,
		N_("DMX display to migrate to"),
		N_("DISPLAY")
	},
	{
		"display", 0, 0, G_OPTION_ARG_STRING, &backend_display,
		N_("Backend display name"),
		N_("DISPLAY")
	},
	{
		"to-authfile", 0, 0, G_OPTION_ARG_STRING, &to_authfile,
		N_("Xauthority file for destination display"),
		N_("AUTHFILE")
	},
	{
		"display-authfile", 0, 0, G_OPTION_ARG_STRING, &backend_authfile,
		N_("Xauthority file for backend display"),
		N_("AUTHFILE")
	},
	{ NULL }
};

static Display *
get_dmx_display (const char *display_name,
		 const char *authfile)
{
	Display *display;
	int event_base, error_base;
	const char *old_authfile;

	old_authfile = getenv ("XAUTHORITY");
	g_setenv ("XAUTHORITY", authfile, TRUE);

	if ((display = XOpenDisplay (display_name)) == NULL)
		g_printerr (_("Failed to open display \"%s\"\n"), display_name);

	if (display != NULL &&
	    !DMXQueryExtension (display, &event_base, &error_base)) {
		g_printerr (_("DMX extension not present on \"%s\"\n"), display_name);
		XCloseDisplay (display);
		display = NULL;
	}

	g_setenv ("XAUTHORITY", old_authfile, TRUE);

	return display;
}

int
main (int argc, char **argv)
{
	GOptionContext *options_context;
	Display *display;
	DMXScreenAttributes attr;
	guint mask;
	int screen;

	setlocale (LC_ALL, "");
	bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
	textdomain (GETTEXT_PACKAGE);

	options_context = g_option_context_new (_("- migrate a backend display from one DMX display to another"));
	g_option_context_add_main_entries (options_context, options, GETTEXT_PACKAGE);
	g_option_context_parse (options_context, &argc, &argv, NULL);
	g_option_context_free (options_context);

	if (to_display == NULL) {
		g_printerr (_("You must specify a destination DMX display using %s\n"), "--to");
		return 1;
	}

	if (backend_display == NULL) {
		g_printerr (_("You must specify a backend display by using %s\n"), "--display");
		return 1;
	}

	if ((display = get_dmx_display (to_display, to_authfile)) == NULL)
		return 1;

	/* Note, we have no way yet of using backend_authfile to ensure
	 * that the DMX server can authenticate against the backend Xserver.
	 * For now, we must disable access control on the backend server.
	 */

	mask = 0;
	screen = 0;
	if (!DMXAddScreen (display, backend_display, mask, &attr, &screen)) {
		g_printerr (_("DMXAddScreen \"%s\" failed on \"%s\"\n"),
			    backend_display, to_display);
		XCloseDisplay (display);
		return 1;
	}

	XCloseDisplay (display);

	return 0;
}