2 * Copyright (C) 2010-2011 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
14 #include "seat-local.h"
15 #include "configuration.h"
16 #include "x-server-local.h"
17 #include "x-server-xmir.h"
18 #include "unity-system-compositor.h"
19 #include "wayland-session.h"
23 struct SeatLocalPrivate
25 /* System compositor being used for Mir sessions */
26 UnitySystemCompositor *compositor;
28 /* Session currently active on compositor */
29 Session *active_compositor_session;
31 /* Counter for Mir IDs to use */
34 /* X server being used for XDMCP */
35 XServerLocal *xdmcp_x_server;
38 G_DEFINE_TYPE (SeatLocal, seat_local, SEAT_TYPE);
40 static XServerLocal *create_x_server (SeatLocal *seat);
43 seat_local_setup (Seat *seat)
45 seat_set_supports_multi_session (seat, TRUE);
46 seat_set_share_display_server (seat, seat_get_boolean_property (seat, "xserver-share"));
47 SEAT_CLASS (seat_local_parent_class)->setup (seat);
51 check_stopped (SeatLocal *seat)
53 if (!seat->priv->xdmcp_x_server)
54 SEAT_CLASS (seat_local_parent_class)->stop (SEAT (seat));
58 xdmcp_x_server_stopped_cb (DisplayServer *display_server, SeatLocal *seat)
60 l_debug (seat, "XDMCP X server stopped");
62 g_signal_handlers_disconnect_matched (seat->priv->xdmcp_x_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
63 g_clear_object (&seat->priv->xdmcp_x_server);
65 if (seat_get_is_stopping (SEAT (seat)))
68 seat_stop (SEAT (seat));
72 compositor_stopped_cb (UnitySystemCompositor *compositor, SeatLocal *seat)
74 l_debug (seat, "Compositor stopped");
76 g_clear_object (&seat->priv->compositor);
78 if (seat_get_is_stopping (SEAT (seat)))
83 seat_local_start (Seat *seat)
85 const gchar *xdmcp_manager = NULL;
87 /* If running as an XDMCP client then just start an X server */
88 xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
91 SeatLocal *s = SEAT_LOCAL (seat);
92 const gchar *key_name = NULL;
95 s->priv->xdmcp_x_server = create_x_server (s);
96 x_server_local_set_xdmcp_server (s->priv->xdmcp_x_server, xdmcp_manager);
97 port = seat_get_integer_property (seat, "xdmcp-port");
99 x_server_local_set_xdmcp_port (s->priv->xdmcp_x_server, port);
100 key_name = seat_get_string_property (seat, "xdmcp-key");
106 GError *error = NULL;
108 path = g_build_filename (config_get_directory (config_get_instance ()), "keys.conf", NULL);
110 keys = g_key_file_new ();
111 result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
113 l_debug (seat, "Error getting key %s", error->message);
114 g_clear_error (&error);
120 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
121 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
123 l_debug (seat, "Key %s not defined", key_name);
126 x_server_local_set_xdmcp_key (s->priv->xdmcp_x_server, key);
131 g_key_file_free (keys);
134 g_signal_connect (s->priv->xdmcp_x_server, DISPLAY_SERVER_SIGNAL_STOPPED, G_CALLBACK (xdmcp_x_server_stopped_cb), seat);
135 return display_server_start (DISPLAY_SERVER (s->priv->xdmcp_x_server));
138 return SEAT_CLASS (seat_local_parent_class)->start (seat);
142 display_server_ready_cb (DisplayServer *display_server, Seat *seat)
145 plymouth_quit (TRUE);
149 display_server_transition_plymouth_cb (DisplayServer *display_server, Seat *seat)
151 /* Quit Plymouth if we didn't do the transition */
152 if (plymouth_get_is_running ())
153 plymouth_quit (FALSE);
155 g_signal_handlers_disconnect_matched (display_server, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, display_server_transition_plymouth_cb, NULL);
159 get_vt (SeatLocal *seat, DisplayServer *display_server)
162 const gchar *xdg_seat = seat_get_name (SEAT (seat));
164 if (strcmp (xdg_seat, "seat0") != 0)
167 /* If Plymouth is running, stop it */
168 if (plymouth_get_is_active () && plymouth_has_active_vt ())
170 gint active_vt = vt_get_active ();
171 if (active_vt >= vt_get_min ())
174 g_signal_connect (display_server, DISPLAY_SERVER_SIGNAL_READY, G_CALLBACK (display_server_ready_cb), seat);
175 g_signal_connect (display_server, DISPLAY_SERVER_SIGNAL_STOPPED, G_CALLBACK (display_server_transition_plymouth_cb), seat);
176 plymouth_deactivate ();
179 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 ());
181 if (plymouth_get_is_active ())
182 plymouth_quit (FALSE);
184 vt = vt_get_unused ();
189 static UnitySystemCompositor *
190 create_unity_system_compositor (SeatLocal *seat)
192 UnitySystemCompositor *compositor;
193 const gchar *command;
196 compositor = unity_system_compositor_new ();
198 command = seat_get_string_property (SEAT (seat), "unity-compositor-command");
200 unity_system_compositor_set_command (compositor, command);
202 timeout = seat_get_integer_property (SEAT (seat), "unity-compositor-timeout");
205 unity_system_compositor_set_timeout (compositor, timeout);
207 vt = get_vt (seat, DISPLAY_SERVER (compositor));
209 unity_system_compositor_set_vt (compositor, vt);
214 static UnitySystemCompositor *
215 get_unity_system_compositor (SeatLocal *seat)
217 if (seat->priv->compositor)
218 return seat->priv->compositor;
220 seat->priv->compositor = create_unity_system_compositor (seat);
221 seat->priv->next_xmir_id = 0;
222 g_signal_connect (seat->priv->compositor, DISPLAY_SERVER_SIGNAL_STOPPED, G_CALLBACK (compositor_stopped_cb), seat);
224 return seat->priv->compositor;
227 static XServerLocal *
228 create_x_server (SeatLocal *seat)
230 const gchar *x_server_backend;
231 XServerLocal *x_server;
234 const gchar *layout = NULL, *config_file = NULL;
238 x_server_backend = seat_get_string_property (SEAT (seat), "xserver-backend");
239 if (g_strcmp0 (x_server_backend, "mir") == 0)
241 UnitySystemCompositor *compositor;
242 const gchar *command;
245 compositor = get_unity_system_compositor (SEAT_LOCAL (seat));
246 x_server = X_SERVER_LOCAL (x_server_xmir_new (compositor));
248 command = seat_get_string_property (SEAT (seat), "xmir-command");
250 x_server_local_set_command (x_server, command);
252 id = g_strdup_printf ("x-%d", seat->priv->next_xmir_id);
253 seat->priv->next_xmir_id++;
254 x_server_xmir_set_mir_id (X_SERVER_XMIR (x_server), id);
255 x_server_xmir_set_mir_socket (X_SERVER_XMIR (x_server), unity_system_compositor_get_socket (compositor));
260 const gchar *command = NULL;
262 x_server = x_server_local_new ();
264 vt = get_vt (seat, DISPLAY_SERVER (x_server));
266 x_server_local_set_vt (x_server, vt);
269 l_debug (seat, "Starting local X display on VT %d", vt);
271 l_debug (seat, "Starting local X display");
273 /* If running inside an X server use Xephyr instead */
274 if (g_getenv ("DISPLAY"))
277 command = seat_get_string_property (SEAT (seat), "xserver-command");
279 x_server_local_set_command (x_server, command);
282 number = g_strdup_printf ("%d", x_server_get_display_number (X_SERVER (x_server)));
283 cookie = x_authority_new_local_cookie (number);
284 x_server_set_authority (X_SERVER (x_server), cookie);
286 g_object_unref (cookie);
288 layout = seat_get_string_property (SEAT (seat), "xserver-layout");
290 x_server_local_set_layout (x_server, layout);
292 x_server_local_set_xdg_seat (x_server, seat_get_name (SEAT (seat)));
294 config_file = seat_get_string_property (SEAT (seat), "xserver-config");
296 x_server_local_set_config (x_server, config_file);
298 allow_tcp = seat_get_boolean_property (SEAT (seat), "xserver-allow-tcp");
299 x_server_local_set_allow_tcp (x_server, allow_tcp);
304 static DisplayServer *
305 create_wayland_session (SeatLocal *seat)
307 WaylandSession *session;
310 session = wayland_session_new ();
312 vt = get_vt (seat, DISPLAY_SERVER (session));
314 wayland_session_set_vt (session, vt);
316 return DISPLAY_SERVER (session);
319 static DisplayServer *
320 seat_local_create_display_server (Seat *s, Session *session)
322 SeatLocal *seat = SEAT_LOCAL (s);
323 const gchar *session_type;
325 session_type = session_get_session_type (session);
326 if (strcmp (session_type, "x") == 0)
327 return DISPLAY_SERVER (create_x_server (seat));
328 else if (strcmp (session_type, "mir") == 0)
329 return g_object_ref (get_unity_system_compositor (seat));
330 else if (strcmp (session_type, "wayland") == 0)
331 return create_wayland_session (seat);
332 else if (strcmp (session_type, "mir-container") == 0)
334 UnitySystemCompositor *compositor;
335 const gchar *compositor_command;
337 compositor = create_unity_system_compositor (seat);
338 compositor_command = session_config_get_compositor_command (session_get_config (session));
339 if (compositor_command)
340 unity_system_compositor_set_command (compositor, compositor_command);
342 return DISPLAY_SERVER (compositor);
346 l_warning (seat, "Can't create unsupported display server '%s'", session_type);
352 seat_local_display_server_is_used (Seat *seat, DisplayServer *display_server)
354 if (display_server == DISPLAY_SERVER (SEAT_LOCAL (seat)->priv->compositor))
357 return SEAT_CLASS (seat_local_parent_class)->display_server_is_used (seat, display_server);
360 static GreeterSession *
361 seat_local_create_greeter_session (Seat *seat)
363 GreeterSession *greeter_session;
365 greeter_session = SEAT_CLASS (seat_local_parent_class)->create_greeter_session (seat);
366 session_set_env (SESSION (greeter_session), "XDG_SEAT", seat_get_name (seat));
368 return greeter_session;
372 seat_local_create_session (Seat *seat)
376 session = SEAT_CLASS (seat_local_parent_class)->create_session (seat);
377 session_set_env (SESSION (session), "XDG_SEAT", seat_get_name (seat));
383 seat_local_set_active_session (Seat *s, Session *session)
385 SeatLocal *seat = SEAT_LOCAL (s);
386 DisplayServer *display_server;
388 display_server = session_get_display_server (session);
390 gint vt = display_server_get_vt (display_server);
394 g_clear_object (&seat->priv->active_compositor_session);
395 if (IS_UNITY_SYSTEM_COMPOSITOR (display_server))
397 unity_system_compositor_set_active_session (UNITY_SYSTEM_COMPOSITOR (display_server), session_get_env (session, "MIR_SERVER_NAME"));
398 seat->priv->active_compositor_session = g_object_ref (session);
400 if (IS_X_SERVER_XMIR (display_server))
402 unity_system_compositor_set_active_session (seat->priv->compositor, x_server_xmir_get_mir_id (X_SERVER_XMIR (display_server)));
403 seat->priv->active_compositor_session = g_object_ref (session);
406 SEAT_CLASS (seat_local_parent_class)->set_active_session (s, session);
410 seat_local_get_active_session (Seat *s)
412 SeatLocal *seat = SEAT_LOCAL (s);
416 vt = vt_get_active ();
420 /* If the compositor is active return the session it is displaying */
421 if (seat->priv->compositor && display_server_get_vt (DISPLAY_SERVER (seat->priv->compositor)) == vt)
422 return seat->priv->active_compositor_session;
424 /* Otherwise find out which session is on this VT */
425 for (link = seat_get_sessions (s); link; link = link->next)
427 Session *session = link->data;
428 DisplayServer *display_server;
430 display_server = session_get_display_server (session);
431 if (display_server && display_server_get_vt (display_server) == vt)
439 seat_local_set_next_session (Seat *seat, Session *session)
441 DisplayServer *display_server;
442 const gchar *id = NULL;
447 display_server = session_get_display_server (session);
449 if (IS_X_SERVER_XMIR (display_server))
450 id = x_server_xmir_get_mir_id (X_SERVER_XMIR (display_server));
452 id = session_get_env (session, "MIR_SERVER_NAME");
456 l_debug (seat, "Marking Mir session %s as the next session", id);
457 unity_system_compositor_set_next_session (SEAT_LOCAL (seat)->priv->compositor, id);
460 l_debug (seat, "Failed to work out session ID to mark");
462 SEAT_CLASS (seat_local_parent_class)->set_next_session (seat, session);
466 seat_local_run_script (Seat *seat, DisplayServer *display_server, Process *script)
468 if (IS_X_SERVER_LOCAL (display_server))
471 XServerLocal *x_server;
473 x_server = X_SERVER_LOCAL (display_server);
474 path = x_server_local_get_authority_file_path (x_server);
475 process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
476 process_set_env (script, "XAUTHORITY", path);
479 SEAT_CLASS (seat_local_parent_class)->run_script (seat, display_server, script);
483 seat_local_stop (Seat *s)
485 SeatLocal *seat = SEAT_LOCAL (s);
487 /* Stop the compositor */
488 if (seat->priv->compositor)
489 display_server_stop (DISPLAY_SERVER (seat->priv->compositor));
491 /* Stop the XDMCP X server */
492 if (seat->priv->xdmcp_x_server)
493 display_server_stop (DISPLAY_SERVER (seat->priv->xdmcp_x_server));
495 check_stopped (seat);
499 seat_local_init (SeatLocal *seat)
501 seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_LOCAL_TYPE, SeatLocalPrivate);
505 seat_local_finalize (GObject *object)
507 SeatLocal *seat = SEAT_LOCAL (object);
509 g_clear_object (&seat->priv->compositor);
510 g_clear_object (&seat->priv->active_compositor_session);
511 if (seat->priv->xdmcp_x_server)
513 g_signal_handlers_disconnect_matched (seat->priv->xdmcp_x_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
514 g_object_unref (seat->priv->xdmcp_x_server);
517 G_OBJECT_CLASS (seat_local_parent_class)->finalize (object);
521 seat_local_class_init (SeatLocalClass *klass)
523 GObjectClass *object_class = G_OBJECT_CLASS (klass);
524 SeatClass *seat_class = SEAT_CLASS (klass);
526 object_class->finalize = seat_local_finalize;
528 seat_class->setup = seat_local_setup;
529 seat_class->start = seat_local_start;
530 seat_class->create_display_server = seat_local_create_display_server;
531 seat_class->display_server_is_used = seat_local_display_server_is_used;
532 seat_class->create_greeter_session = seat_local_create_greeter_session;
533 seat_class->create_session = seat_local_create_session;
534 seat_class->set_active_session = seat_local_set_active_session;
535 seat_class->get_active_session = seat_local_get_active_session;
536 seat_class->set_next_session = seat_local_set_next_session;
537 seat_class->run_script = seat_local_run_script;
538 seat_class->stop = seat_local_stop;
540 g_type_class_add_private (klass, sizeof (SeatLocalPrivate));