~robert-ancell/lightdm/weston-compositor-merge

« back to all changes in this revision

Viewing changes to src/seat-weston.c

  • Committer: Robert Ancell
  • Date: 2012-07-05 04:55:42 UTC
  • Revision ID: robert.ancell@canonical.com-20120705045542-dd1dopowti68gtwm
First work on supporting a weston system compositor

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2012 Robert Ancell.
 
3
 * Author: Robert Ancell <robert.ancell@canonical.com>
 
4
 * 
 
5
 * This program is free software: you can redistribute it and/or modify it under
 
6
 * the terms of the GNU General Public License as published by the Free Software
 
7
 * Foundation, either version 3 of the License, or (at your option) any later
 
8
 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
 
9
 * license.
 
10
 */
 
11
 
 
12
#include <string.h>
 
13
#include <sys/types.h>
 
14
#include <sys/socket.h>
 
15
 
 
16
#include "seat-weston.h"
 
17
#include "configuration.h"
 
18
#include "xserver-local.h"
 
19
#include "xsession.h"
 
20
#include "wayland-client.h" // FIXME: See https://bugs.freedesktop.org/show_bug.cgi?id=51740
 
21
#include "weston-protocol.h"
 
22
 
 
23
struct SeatWestonPrivate
 
24
{
 
25
    /* Compositor process */
 
26
    Process *compositor_process;
 
27
 
 
28
    /* Socket to communicate with compositor */
 
29
    int compositor_socket;
 
30
};
 
31
 
 
32
G_DEFINE_TYPE (SeatWeston, seat_weston, SEAT_TYPE);
 
33
 
 
34
static void
 
35
seat_weston_setup (Seat *seat)
 
36
{
 
37
    seat_set_can_switch (seat, TRUE);
 
38
    SEAT_CLASS (seat_weston_parent_class)->setup (seat);
 
39
}
 
40
 
 
41
static void
 
42
compositor_stopped_cb (Process *process, SeatWeston *seat)
 
43
{
 
44
    g_debug ("Stopping Weston seat, compositor terminated");
 
45
    seat_stop (SEAT (seat));
 
46
}
 
47
 
 
48
static gboolean
 
49
seat_weston_start (Seat *seat)
 
50
{
 
51
    gchar *command;
 
52
    int socket_fds[2], child_fd;
 
53
    gboolean result;
 
54
 
 
55
    if (socketpair (AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, socket_fds) == -1) 
 
56
    {
 
57
        //perror ("Failed to create compositor sockets");
 
58
        return FALSE;
 
59
    }
 
60
    child_fd = dup (socket_fds[0]);
 
61
    if (child_fd < 0)
 
62
        return FALSE;
 
63
    close (socket_fds[0]);
 
64
    SEAT_WESTON (seat)->priv->compositor_socket = socket_fds[1];
 
65
 
 
66
    if (socketpair (AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, socket_fds) == -1) 
 
67
    {
 
68
        //perror ("Failed to create compositor sockets");
 
69
        return FALSE;
 
70
    }
 
71
 
 
72
    command = g_strdup_printf ("weston --shell=system-compositor.so --display-manager-fd=%d", child_fd);
 
73
    process_set_command (SEAT_WESTON (seat)->priv->compositor_process, command);
 
74
    g_signal_connect (SEAT_WESTON (seat)->priv->compositor_process, "stopped", G_CALLBACK (compositor_stopped_cb), seat);
 
75
    result = process_start (SEAT_WESTON (seat)->priv->compositor_process);
 
76
    close (child_fd);
 
77
 
 
78
    if (!result)
 
79
        return FALSE;
 
80
 
 
81
    return SEAT_CLASS (seat_weston_parent_class)->start (seat);  
 
82
}
 
83
 
 
84
static DisplayServer *
 
85
seat_weston_create_display_server (Seat *seat)
 
86
{
 
87
    XServerLocal *xserver;
 
88
    const gchar *command = NULL, *layout = NULL, *config_file = NULL, *xdmcp_manager = NULL, *key_name = NULL;
 
89
    gboolean allow_tcp;
 
90
    gint port = 0;
 
91
    int socket_fds[2], child_fd;
 
92
 
 
93
    g_debug ("Starting X server on weston compositor");
 
94
 
 
95
    xserver = xserver_local_new ();
 
96
 
 
97
    if (socketpair (AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, socket_fds) == -1) 
 
98
    {
 
99
        //perror ("Failed to create compositor sockets");
 
100
        return FALSE;
 
101
    }
 
102
    child_fd = dup (socket_fds[0]);
 
103
    if (child_fd < 0)
 
104
        return FALSE;
 
105
    close (socket_fds[0]);
 
106
    xserver_local_set_wayland_socket (xserver, child_fd);
 
107
 
 
108
    command = seat_get_string_property (seat, "xserver-command");
 
109
    if (command)
 
110
        xserver_local_set_command (xserver, command);
 
111
 
 
112
    layout = seat_get_string_property (seat, "xserver-layout");
 
113
    if (layout)
 
114
        xserver_local_set_layout (xserver, layout);
 
115
 
 
116
    config_file = seat_get_string_property (seat, "xserver-config");
 
117
    if (config_file)
 
118
        xserver_local_set_config (xserver, config_file);
 
119
  
 
120
    allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
 
121
    xserver_local_set_allow_tcp (xserver, allow_tcp);    
 
122
 
 
123
    xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
 
124
    if (xdmcp_manager)
 
125
        xserver_local_set_xdmcp_server (xserver, xdmcp_manager);
 
126
 
 
127
    port = seat_get_integer_property (seat, "xdmcp-port");
 
128
    if (port > 0)
 
129
        xserver_local_set_xdmcp_port (xserver, port);
 
130
 
 
131
    key_name = seat_get_string_property (seat, "xdmcp-key");
 
132
    if (key_name)
 
133
    {
 
134
        gchar *dir, *path;
 
135
        GKeyFile *keys;
 
136
        gboolean result;
 
137
        GError *error = NULL;
 
138
 
 
139
        dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
 
140
        path = g_build_filename (dir, "keys.conf", NULL);
 
141
        g_free (dir);
 
142
 
 
143
        keys = g_key_file_new ();
 
144
        result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
 
145
        if (error)
 
146
            g_debug ("Error getting key %s", error->message);
 
147
        g_clear_error (&error);      
 
148
 
 
149
        if (result)
 
150
        {
 
151
            gchar *key = NULL;
 
152
 
 
153
            if (g_key_file_has_key (keys, "keyring", key_name, NULL))
 
154
                key = g_key_file_get_string (keys, "keyring", key_name, NULL);
 
155
            else
 
156
                g_debug ("Key %s not defined", key_name);
 
157
 
 
158
            if (key)
 
159
                xserver_local_set_xdmcp_key (xserver, key);
 
160
            g_free (key);
 
161
        }
 
162
 
 
163
        g_free (path);
 
164
        g_key_file_free (keys);
 
165
    }
 
166
 
 
167
    return DISPLAY_SERVER (xserver);
 
168
}
 
169
 
 
170
static Session *
 
171
seat_weston_create_session (Seat *seat, Display *display)
 
172
{
 
173
    XServerLocal *xserver;
 
174
    XSession *session;
 
175
    gchar *tty;
 
176
 
 
177
    xserver = XSERVER_LOCAL (display_get_display_server (display));
 
178
 
 
179
    session = xsession_new (XSERVER (xserver));
 
180
    tty = g_strdup_printf ("/dev/tty%d", xserver_local_get_vt (xserver));
 
181
    session_set_tty (SESSION (session), tty);
 
182
    g_free (tty);
 
183
 
 
184
    return SESSION (session);
 
185
}
 
186
 
 
187
static void
 
188
seat_weston_set_active_display (Seat *seat, Display *display)
 
189
{
 
190
    // FIXME
 
191
 
 
192
    SEAT_CLASS (seat_weston_parent_class)->set_active_display (seat, display);
 
193
}
 
194
 
 
195
static void
 
196
seat_weston_run_script (Seat *seat, Display *display, Process *script)
 
197
{
 
198
    gchar *path;
 
199
    XServerLocal *xserver;
 
200
 
 
201
    xserver = XSERVER_LOCAL (display_get_display_server (display));
 
202
    path = xserver_local_get_authority_file_path (xserver);
 
203
    process_set_env (script, "DISPLAY", xserver_get_address (XSERVER (xserver)));
 
204
    process_set_env (script, "XAUTHORITY", path);
 
205
    g_free (path);
 
206
 
 
207
    SEAT_CLASS (seat_weston_parent_class)->run_script (seat, display, script);
 
208
}
 
209
 
 
210
static void
 
211
seat_weston_display_removed (Seat *seat, Display *display)
 
212
{
 
213
    if (seat_get_is_stopping (seat))
 
214
        return;
 
215
 
 
216
    /* If this is the only display and it failed to start then stop this seat */
 
217
    if (g_list_length (seat_get_displays (seat)) == 0 && !display_get_is_ready (display))
 
218
    {
 
219
        g_debug ("Stopping Weston seat, failed to start a display");
 
220
        //seat_stop (seat);
 
221
        return;
 
222
    }
 
223
 
 
224
    /* Show a new greeter */  
 
225
    if (display == seat_get_active_display (seat))
 
226
    {
 
227
        g_debug ("Active display stopped, switching to greeter");
 
228
        seat_switch_to_greeter (seat);
 
229
    }
 
230
}
 
231
 
 
232
static void
 
233
seat_weston_init (SeatWeston *seat)
 
234
{
 
235
    seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_WESTON_TYPE, SeatWestonPrivate);
 
236
    seat->priv->compositor_process = process_new ();
 
237
}
 
238
 
 
239
static void
 
240
seat_weston_finalize (GObject *object)
 
241
{
 
242
    SeatWeston *seat = SEAT_WESTON (object);
 
243
 
 
244
    g_object_unref (seat->priv->compositor_process);
 
245
 
 
246
    G_OBJECT_CLASS (seat_weston_parent_class)->finalize (object);
 
247
}
 
248
 
 
249
static void
 
250
seat_weston_class_init (SeatWestonClass *klass)
 
251
{
 
252
    GObjectClass *object_class = G_OBJECT_CLASS (klass);  
 
253
    SeatClass *seat_class = SEAT_CLASS (klass);
 
254
 
 
255
    object_class->finalize = seat_weston_finalize;
 
256
    seat_class->setup = seat_weston_setup;
 
257
    seat_class->start = seat_weston_start;
 
258
    seat_class->create_display_server = seat_weston_create_display_server;
 
259
    seat_class->create_session = seat_weston_create_session;
 
260
    seat_class->set_active_display = seat_weston_set_active_display;
 
261
    seat_class->run_script = seat_weston_run_script;
 
262
    seat_class->display_removed = seat_weston_display_removed;
 
263
 
 
264
    g_type_class_add_private (klass, sizeof (SeatWestonPrivate));
 
265
}