2 * Copyright (C) 2012-2013 Robert Ancell.
3 * Author: Robert Ancell <robert.ancell@canonical.com>
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
15 #include <glib/gstdio.h>
17 #include "seat-unity.h"
18 #include "configuration.h"
19 #include "unity-system-compositor.h"
20 #include "x-server-local.h"
21 #include "mir-server.h"
25 struct SeatUnityPrivate
27 /* System compositor */
28 UnitySystemCompositor *compositor;
30 /* X server being used for XDMCP */
31 XServerLocal *xdmcp_x_server;
33 /* Next Mir ID to use for a Mir sessions, X server and greeters */
35 gint next_x_server_id;
38 /* The currently visible session */
39 Session *active_session;
40 DisplayServer *active_display_server;
43 G_DEFINE_TYPE (SeatUnity, seat_unity, SEAT_TYPE);
45 static XServerLocal *create_x_server (Seat *seat);
48 seat_unity_setup (Seat *seat)
50 seat_set_supports_multi_session (seat, TRUE);
51 SEAT_CLASS (seat_unity_parent_class)->setup (seat);
55 check_stopped (SeatUnity *seat)
57 if (!seat->priv->compositor && !seat->priv->xdmcp_x_server)
58 SEAT_CLASS (seat_unity_parent_class)->stop (SEAT (seat));
62 xdmcp_x_server_stopped_cb (DisplayServer *display_server, Seat *seat)
64 l_debug (seat, "XDMCP X server stopped");
66 g_signal_handlers_disconnect_matched (SEAT_UNITY (seat)->priv->xdmcp_x_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
67 SEAT_UNITY (seat)->priv->xdmcp_x_server = NULL;
69 g_object_unref (display_server);
71 if (seat_get_is_stopping (seat))
72 check_stopped (SEAT_UNITY (seat));
78 compositor_ready_cb (UnitySystemCompositor *compositor, SeatUnity *seat)
80 const gchar *xdmcp_manager = NULL;
82 l_debug (seat, "Compositor ready");
84 /* If running as an XDMCP client then just start an X server */
85 xdmcp_manager = seat_get_string_property (SEAT (seat), "xdmcp-manager");
88 const gchar *key_name = NULL;
91 seat->priv->xdmcp_x_server = create_x_server (SEAT (seat));
92 x_server_local_set_xdmcp_server (seat->priv->xdmcp_x_server, xdmcp_manager);
93 port = seat_get_integer_property (SEAT (seat), "xdmcp-port");
95 x_server_local_set_xdmcp_port (seat->priv->xdmcp_x_server, port);
96 key_name = seat_get_string_property (SEAT (seat), "xdmcp-key");
102 GError *error = NULL;
104 path = g_build_filename (config_get_directory (config_get_instance ()), "keys.conf", NULL);
106 keys = g_key_file_new ();
107 result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
109 l_debug (seat, "Error getting key %s", error->message);
110 g_clear_error (&error);
116 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
117 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
119 l_debug (seat, "Key %s not defined", key_name);
122 x_server_local_set_xdmcp_key (seat->priv->xdmcp_x_server, key);
127 g_key_file_free (keys);
130 g_signal_connect (seat->priv->xdmcp_x_server, DISPLAY_SERVER_SIGNAL_STOPPED, G_CALLBACK (xdmcp_x_server_stopped_cb), seat);
131 if (!display_server_start (DISPLAY_SERVER (seat->priv->xdmcp_x_server)))
132 seat_stop (SEAT (seat));
135 SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
139 compositor_stopped_cb (UnitySystemCompositor *compositor, SeatUnity *seat)
141 l_debug (seat, "Compositor stopped");
143 g_object_unref (seat->priv->compositor);
144 seat->priv->compositor = NULL;
146 if (seat_get_is_stopping (SEAT (seat)))
147 check_stopped (seat);
149 seat_stop (SEAT (seat));
153 seat_unity_start (Seat *seat)
158 /* Replace Plymouth if it is running */
159 if (plymouth_get_is_active () && plymouth_has_active_vt ())
161 gint active_vt = vt_get_active ();
162 if (active_vt >= vt_get_min ())
165 plymouth_quit (TRUE);
168 l_debug (seat, "Plymouth is running on VT %d, but this is less than the configured minimum of %d so not replacing it", active_vt, vt_get_min ());
170 if (plymouth_get_is_active ())
171 plymouth_quit (FALSE);
173 vt = vt_can_multi_seat () ? vt_get_unused () : 0;
176 l_debug (seat, "Failed to get a VT to run on");
180 timeout = seat_get_integer_property (SEAT (seat), "unity-compositor-timeout");
184 SEAT_UNITY (seat)->priv->compositor = unity_system_compositor_new ();
185 g_signal_connect (SEAT_UNITY (seat)->priv->compositor, DISPLAY_SERVER_SIGNAL_READY, G_CALLBACK (compositor_ready_cb), seat);
186 g_signal_connect (SEAT_UNITY (seat)->priv->compositor, DISPLAY_SERVER_SIGNAL_STOPPED, G_CALLBACK (compositor_stopped_cb), seat);
187 unity_system_compositor_set_command (SEAT_UNITY (seat)->priv->compositor, seat_get_string_property (SEAT (seat), "unity-compositor-command"));
188 unity_system_compositor_set_vt (SEAT_UNITY (seat)->priv->compositor, vt);
189 unity_system_compositor_set_timeout (SEAT_UNITY (seat)->priv->compositor, timeout);
191 return display_server_start (DISPLAY_SERVER (SEAT_UNITY (seat)->priv->compositor));
194 static XServerLocal *
195 create_x_server (Seat *seat)
197 XServerLocal *x_server;
198 const gchar *command = NULL, *layout = NULL, *config_file = NULL;
202 l_debug (seat, "Starting X server on Unity compositor");
204 x_server = x_server_local_new ();
206 command = seat_get_string_property (seat, "xserver-command");
208 x_server_local_set_command (x_server, command);
210 id = g_strdup_printf ("x-%d", SEAT_UNITY (seat)->priv->next_x_server_id);
211 SEAT_UNITY (seat)->priv->next_x_server_id++;
212 x_server_local_set_mir_id (x_server, id);
213 x_server_local_set_mir_socket (x_server, unity_system_compositor_get_socket (SEAT_UNITY (seat)->priv->compositor));
216 layout = seat_get_string_property (seat, "xserver-layout");
218 x_server_local_set_layout (x_server, layout);
220 x_server_local_set_xdg_seat (x_server, seat_get_name (seat));
222 config_file = seat_get_string_property (seat, "xserver-config");
224 x_server_local_set_config (x_server, config_file);
226 allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
227 x_server_local_set_allow_tcp (x_server, allow_tcp);
232 static DisplayServer *
233 create_mir_server (Seat *seat)
235 MirServer *mir_server;
237 mir_server = mir_server_new ();
238 mir_server_set_parent_socket (mir_server, unity_system_compositor_get_socket (SEAT_UNITY (seat)->priv->compositor));
240 return DISPLAY_SERVER (mir_server);
243 static DisplayServer *
244 seat_unity_create_display_server (Seat *seat, Session *session)
246 const gchar *session_type;
248 session_type = session_get_session_type (session);
249 if (strcmp (session_type, "x") == 0)
250 return DISPLAY_SERVER (create_x_server (seat));
251 else if (strcmp (session_type, "mir") == 0)
252 return create_mir_server (seat);
255 l_warning (seat, "Can't create unsupported display server '%s'", session_type);
261 seat_unity_create_greeter_session (Seat *seat)
263 Greeter *greeter_session;
267 greeter_session = SEAT_CLASS (seat_unity_parent_class)->create_greeter_session (seat);
268 session_set_env (SESSION (greeter_session), "XDG_SEAT", seat_get_name (seat));
270 id = g_strdup_printf ("greeter-%d", SEAT_UNITY (seat)->priv->next_greeter_id);
271 SEAT_UNITY (seat)->priv->next_greeter_id++;
272 session_set_env (SESSION (greeter_session), "MIR_SERVER_NAME", id);
275 vt = display_server_get_vt (DISPLAY_SERVER (SEAT_UNITY (seat)->priv->compositor));
278 gchar *value = g_strdup_printf ("%d", vt);
279 session_set_env (SESSION (greeter_session), "XDG_VTNR", value);
283 return greeter_session;
287 seat_unity_create_session (Seat *seat)
293 session = SEAT_CLASS (seat_unity_parent_class)->create_session (seat);
294 session_set_env (session, "XDG_SEAT", seat_get_name (seat));
296 id = g_strdup_printf ("session-%d", SEAT_UNITY (seat)->priv->next_session_id);
297 SEAT_UNITY (seat)->priv->next_session_id++;
298 session_set_env (session, "MIR_SERVER_NAME", id);
301 vt = display_server_get_vt (DISPLAY_SERVER (SEAT_UNITY (seat)->priv->compositor));
304 gchar *value = g_strdup_printf ("%d", vt);
305 session_set_env (SESSION (session), "XDG_VTNR", value);
313 seat_unity_set_active_session (Seat *seat, Session *session)
315 DisplayServer *display_server;
317 if (session == SEAT_UNITY (seat)->priv->active_session)
319 SEAT_UNITY (seat)->priv->active_session = g_object_ref (session);
321 display_server = session_get_display_server (session);
322 if (SEAT_UNITY (seat)->priv->active_display_server != display_server)
324 const gchar *id = NULL;
326 SEAT_UNITY (seat)->priv->active_display_server = g_object_ref (display_server);
328 if (IS_X_SERVER_LOCAL (display_server))
329 id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
331 id = session_get_env (session, "MIR_SERVER_NAME");
335 l_debug (seat, "Switching to Mir session %s", id);
336 unity_system_compositor_set_active_session (SEAT_UNITY (seat)->priv->compositor, id);
339 l_warning (seat, "Failed to work out session ID");
342 SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
346 seat_unity_get_active_session (Seat *seat)
348 return SEAT_UNITY (seat)->priv->active_session;
352 seat_unity_set_next_session (Seat *seat, Session *session)
354 DisplayServer *display_server;
355 const gchar *id = NULL;
360 display_server = session_get_display_server (session);
362 if (IS_X_SERVER_LOCAL (display_server))
363 id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
365 id = session_get_env (session, "MIR_SERVER_NAME");
369 l_debug (seat, "Marking Mir session %s as the next session", id);
370 unity_system_compositor_set_next_session (SEAT_UNITY (seat)->priv->compositor, id);
374 l_debug (seat, "Failed to work out session ID to mark");
377 SEAT_CLASS (seat_unity_parent_class)->set_next_session (seat, session);
381 seat_unity_run_script (Seat *seat, DisplayServer *display_server, Process *script)
383 if (IS_X_SERVER_LOCAL (display_server))
385 XServerLocal *x_server;
388 x_server = X_SERVER_LOCAL (display_server);
389 path = x_server_local_get_authority_file_path (x_server);
390 process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
391 process_set_env (script, "XAUTHORITY", path);
394 SEAT_CLASS (seat_unity_parent_class)->run_script (seat, display_server, script);
398 seat_unity_stop (Seat *seat)
400 /* Stop the compositor first */
401 if (SEAT_UNITY (seat)->priv->compositor)
402 display_server_stop (DISPLAY_SERVER (SEAT_UNITY (seat)->priv->compositor));
404 /* Stop the XDMCP X server first */
405 if (SEAT_UNITY (seat)->priv->xdmcp_x_server)
406 display_server_stop (DISPLAY_SERVER (SEAT_UNITY (seat)->priv->xdmcp_x_server));
408 check_stopped (SEAT_UNITY (seat));
412 seat_unity_init (SeatUnity *seat)
414 seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_UNITY_TYPE, SeatUnityPrivate);
418 seat_unity_finalize (GObject *object)
420 SeatUnity *seat = SEAT_UNITY (object);
422 if (seat->priv->compositor)
423 g_object_unref (seat->priv->compositor);
424 if (seat->priv->xdmcp_x_server)
426 g_signal_handlers_disconnect_matched (seat->priv->xdmcp_x_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
427 g_object_unref (seat->priv->xdmcp_x_server);
429 if (seat->priv->active_session)
430 g_object_unref (seat->priv->active_session);
431 if (seat->priv->active_display_server)
432 g_object_unref (seat->priv->active_display_server);
434 G_OBJECT_CLASS (seat_unity_parent_class)->finalize (object);
438 seat_unity_class_init (SeatUnityClass *klass)
440 GObjectClass *object_class = G_OBJECT_CLASS (klass);
441 SeatClass *seat_class = SEAT_CLASS (klass);
443 object_class->finalize = seat_unity_finalize;
444 seat_class->setup = seat_unity_setup;
445 seat_class->start = seat_unity_start;
446 seat_class->create_display_server = seat_unity_create_display_server;
447 seat_class->create_greeter_session = seat_unity_create_greeter_session;
448 seat_class->create_session = seat_unity_create_session;
449 seat_class->set_active_session = seat_unity_set_active_session;
450 seat_class->get_active_session = seat_unity_get_active_session;
451 seat_class->set_next_session = seat_unity_set_next_session;
452 seat_class->run_script = seat_unity_run_script;
453 seat_class->stop = seat_unity_stop;
455 g_type_class_add_private (klass, sizeof (SeatUnityPrivate));