Back to index

lightdm  1.3.2
xserver.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010-2011 Robert Ancell.
00003  * Author: Robert Ancell <robert.ancell@canonical.com>
00004  * 
00005  * This program is free software: you can redistribute it and/or modify it under
00006  * the terms of the GNU General Public License as published by the Free Software
00007  * Foundation, either version 3 of the License, or (at your option) any later
00008  * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
00009  * license.
00010  */
00011 
00012 #include <config.h>
00013 #include <string.h>
00014 #include <xcb/xcb.h>
00015 
00016 #include "xserver.h"
00017 #include "xsession.h"
00018 
00019 struct XServerPrivate
00020 {  
00021     /* Host running the server */
00022     gchar *hostname;
00023 
00024     /* Display number */
00025     guint number;
00026 
00027     /* Cached server address */
00028     gchar *address;
00029 
00030     /* Authority */
00031     XAuthority *authority;
00032 
00033     /* Connection to this X server */
00034     xcb_connection_t *connection;
00035 };
00036 
00037 G_DEFINE_TYPE (XServer, xserver, DISPLAY_SERVER_TYPE);
00038 
00039 void
00040 xserver_set_hostname (XServer *server, const gchar *hostname)
00041 {
00042     g_return_if_fail (server != NULL);
00043     g_free (server->priv->hostname);
00044     server->priv->hostname = g_strdup (hostname);
00045     g_free (server->priv->address);
00046     server->priv->address = NULL;
00047 }
00048 
00049 gchar *
00050 xserver_get_hostname (XServer *server)
00051 {
00052     g_return_val_if_fail (server != NULL, NULL);
00053     return server->priv->hostname;
00054 }
00055 
00056 void
00057 xserver_set_display_number (XServer *server, guint number)
00058 {
00059     g_return_if_fail (server != NULL);
00060     server->priv->number = number;
00061     g_free (server->priv->address);
00062     server->priv->address = NULL;
00063 }
00064 
00065 guint
00066 xserver_get_display_number (XServer *server)
00067 {
00068     g_return_val_if_fail (server != NULL, 0);
00069     return server->priv->number;
00070 }
00071 
00072 const gchar *
00073 xserver_get_address (XServer *server)
00074 {
00075     g_return_val_if_fail (server != NULL, NULL);
00076 
00077     if (!server->priv->address)
00078     {
00079         if (server->priv->hostname)
00080             server->priv->address = g_strdup_printf("%s:%d", server->priv->hostname, server->priv->number);
00081         else
00082             server->priv->address = g_strdup_printf(":%d", server->priv->number);
00083     }  
00084 
00085     return server->priv->address;
00086 }
00087 
00088 void
00089 xserver_set_authority (XServer *server, XAuthority *authority)
00090 {
00091     g_return_if_fail (server != NULL);
00092 
00093     if (server->priv->authority)
00094         g_object_unref (server->priv->authority);
00095     if (authority)
00096         server->priv->authority = g_object_ref (authority);
00097     else
00098         server->priv->authority = NULL;
00099 }
00100 
00101 XAuthority *
00102 xserver_get_authority (XServer *server)
00103 {
00104     g_return_val_if_fail (server != NULL, NULL);
00105     return server->priv->authority;
00106 }
00107 
00108 static gboolean
00109 xserver_start (DisplayServer *display_server)
00110 {
00111     XServer *server = XSERVER (display_server);
00112     xcb_auth_info_t *auth = NULL, a;
00113 
00114     if (server->priv->authority)
00115     {
00116         a.namelen = strlen (xauth_get_authorization_name (server->priv->authority));
00117         a.name = (char *) xauth_get_authorization_name (server->priv->authority);
00118         a.datalen = xauth_get_authorization_data_length (server->priv->authority);
00119         a.data = (char *) xauth_get_authorization_data (server->priv->authority);
00120         auth = &a;
00121     }
00122 
00123     /* Open connection */  
00124     g_debug ("Connecting to XServer %s", xserver_get_address (server));
00125     server->priv->connection = xcb_connect_to_display_with_auth_info (xserver_get_address (server), auth, NULL);
00126     if (xcb_connection_has_error (server->priv->connection))
00127     {
00128         g_debug ("Error connecting to XServer %s", xserver_get_address (server));
00129         return FALSE;
00130     }
00131 
00132     return DISPLAY_SERVER_CLASS (xserver_parent_class)->start (display_server);
00133 }
00134 
00135 static void
00136 xserver_init (XServer *server)
00137 {
00138     server->priv = G_TYPE_INSTANCE_GET_PRIVATE (server, XSERVER_TYPE, XServerPrivate);
00139 }
00140 
00141 static void
00142 xserver_finalize (GObject *object)
00143 {
00144     XServer *self;
00145 
00146     self = XSERVER (object);
00147 
00148     g_free (self->priv->hostname);
00149     g_free (self->priv->address);
00150     if (self->priv->authority)
00151         g_object_unref (self->priv->authority);
00152     if (self->priv->connection)
00153         xcb_disconnect (self->priv->connection);
00154 
00155     G_OBJECT_CLASS (xserver_parent_class)->finalize (object);
00156 }
00157 
00158 static void
00159 xserver_class_init (XServerClass *klass)
00160 {
00161     GObjectClass *object_class = G_OBJECT_CLASS (klass);
00162     DisplayServerClass *display_server_class = DISPLAY_SERVER_CLASS (klass);
00163 
00164     display_server_class->start = xserver_start;
00165     object_class->finalize = xserver_finalize;
00166 
00167     g_type_class_add_private (klass, sizeof (XServerPrivate));
00168 }