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->compositor && !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 get_unity_system_compositor (SeatLocal *seat)
192 const gchar *command;
195 if (seat->priv->compositor)
196 return seat->priv->compositor;
198 seat->priv->compositor = unity_system_compositor_new ();
200 command = seat_get_string_property (SEAT (seat), "unity-compositor-command");
202 unity_system_compositor_set_command (seat->priv->compositor, command);
204 timeout = seat_get_integer_property (SEAT (seat), "unity-compositor-timeout");
207 unity_system_compositor_set_timeout (seat->priv->compositor, timeout);
209 vt = get_vt (seat, DISPLAY_SERVER (seat->priv->compositor));
211 unity_system_compositor_set_vt (seat->priv->compositor, vt);
213 seat->priv->next_xmir_id = 0;
214 g_signal_connect (seat->priv->compositor, DISPLAY_SERVER_SIGNAL_STOPPED, G_CALLBACK (compositor_stopped_cb), seat);
216 return seat->priv->compositor;
219 static XServerLocal *
220 create_x_server (SeatLocal *seat)
222 const gchar *x_server_backend;
223 XServerLocal *x_server;
226 const gchar *layout = NULL, *config_file = NULL;
230 x_server_backend = seat_get_string_property (SEAT (seat), "xserver-backend");
231 if (g_strcmp0 (x_server_backend, "mir") == 0)
233 UnitySystemCompositor *compositor;
234 const gchar *command;
237 compositor = get_unity_system_compositor (SEAT_LOCAL (seat));
238 x_server = X_SERVER_LOCAL (x_server_xmir_new (compositor));
240 command = seat_get_string_property (SEAT (seat), "xmir-command");
242 x_server_local_set_command (x_server, command);
244 id = g_strdup_printf ("x-%d", seat->priv->next_xmir_id);
245 seat->priv->next_xmir_id++;
246 x_server_xmir_set_mir_id (X_SERVER_XMIR (x_server), id);
247 x_server_xmir_set_mir_socket (X_SERVER_XMIR (x_server), unity_system_compositor_get_socket (compositor));
252 const gchar *command = NULL;
254 x_server = x_server_local_new ();
256 vt = get_vt (seat, DISPLAY_SERVER (x_server));
258 x_server_local_set_vt (x_server, vt);
261 l_debug (seat, "Starting local X display on VT %d", vt);
263 l_debug (seat, "Starting local X display");
265 /* If running inside an X server use Xephyr instead */
266 if (g_getenv ("DISPLAY"))
269 command = seat_get_string_property (SEAT (seat), "xserver-command");
271 x_server_local_set_command (x_server, command);
274 number = g_strdup_printf ("%d", x_server_get_display_number (X_SERVER (x_server)));
275 cookie = x_authority_new_local_cookie (number);
276 x_server_set_authority (X_SERVER (x_server), cookie);
278 g_object_unref (cookie);
280 layout = seat_get_string_property (SEAT (seat), "xserver-layout");
282 x_server_local_set_layout (x_server, layout);
284 x_server_local_set_xdg_seat (x_server, seat_get_name (SEAT (seat)));
286 config_file = seat_get_string_property (SEAT (seat), "xserver-config");
288 x_server_local_set_config (x_server, config_file);
290 allow_tcp = seat_get_boolean_property (SEAT (seat), "xserver-allow-tcp");
291 x_server_local_set_allow_tcp (x_server, allow_tcp);
296 static DisplayServer *
297 create_wayland_session (SeatLocal *seat)
299 WaylandSession *session;
302 session = wayland_session_new ();
304 vt = get_vt (seat, DISPLAY_SERVER (session));
306 wayland_session_set_vt (session, vt);
308 return DISPLAY_SERVER (session);
311 static DisplayServer *
312 seat_local_create_display_server (Seat *s, Session *session)
314 SeatLocal *seat = SEAT_LOCAL (s);
315 const gchar *session_type;
317 session_type = session_get_session_type (session);
318 if (strcmp (session_type, "x") == 0)
319 return DISPLAY_SERVER (create_x_server (seat));
320 else if (strcmp (session_type, "mir") == 0)
321 return g_object_ref (get_unity_system_compositor (seat));
322 else if (strcmp (session_type, "wayland") == 0)
323 return create_wayland_session (seat);
326 l_warning (seat, "Can't create unsupported display server '%s'", session_type);
332 seat_local_display_server_is_used (Seat *seat, DisplayServer *display_server)
334 if (display_server == DISPLAY_SERVER (SEAT_LOCAL (seat)->priv->compositor))
337 return SEAT_CLASS (seat_local_parent_class)->display_server_is_used (seat, display_server);
340 static GreeterSession *
341 seat_local_create_greeter_session (Seat *seat)
343 GreeterSession *greeter_session;
345 greeter_session = SEAT_CLASS (seat_local_parent_class)->create_greeter_session (seat);
346 session_set_env (SESSION (greeter_session), "XDG_SEAT", seat_get_name (seat));
348 return greeter_session;
352 seat_local_create_session (Seat *seat)
356 session = SEAT_CLASS (seat_local_parent_class)->create_session (seat);
357 session_set_env (SESSION (session), "XDG_SEAT", seat_get_name (seat));
363 seat_local_set_active_session (Seat *s, Session *session)
365 SeatLocal *seat = SEAT_LOCAL (s);
366 DisplayServer *display_server;
368 display_server = session_get_display_server (session);
370 gint vt = display_server_get_vt (display_server);
374 g_clear_object (&seat->priv->active_compositor_session);
375 if (IS_UNITY_SYSTEM_COMPOSITOR (display_server))
377 unity_system_compositor_set_active_session (UNITY_SYSTEM_COMPOSITOR (display_server), session_get_env (session, "MIR_SERVER_NAME"));
378 seat->priv->active_compositor_session = g_object_ref (session);
380 if (IS_X_SERVER_XMIR (display_server))
382 unity_system_compositor_set_active_session (seat->priv->compositor, x_server_xmir_get_mir_id (X_SERVER_XMIR (display_server)));
383 seat->priv->active_compositor_session = g_object_ref (session);
386 SEAT_CLASS (seat_local_parent_class)->set_active_session (s, session);
390 seat_local_get_active_session (Seat *s)
392 SeatLocal *seat = SEAT_LOCAL (s);
396 vt = vt_get_active ();
400 /* If the compositor is active return the session it is displaying */
401 if (seat->priv->compositor && display_server_get_vt (DISPLAY_SERVER (seat->priv->compositor)) == vt)
402 return seat->priv->active_compositor_session;
404 /* Otherwise find out which session is on this VT */
405 for (link = seat_get_sessions (s); link; link = link->next)
407 Session *session = link->data;
408 DisplayServer *display_server;
410 display_server = session_get_display_server (session);
411 if (display_server && display_server_get_vt (display_server) == vt)
419 seat_local_set_next_session (Seat *seat, Session *session)
421 DisplayServer *display_server;
422 const gchar *id = NULL;
427 display_server = session_get_display_server (session);
429 if (IS_X_SERVER_XMIR (display_server))
430 id = x_server_xmir_get_mir_id (X_SERVER_XMIR (display_server));
432 id = session_get_env (session, "MIR_SERVER_NAME");
436 l_debug (seat, "Marking Mir session %s as the next session", id);
437 unity_system_compositor_set_next_session (SEAT_LOCAL (seat)->priv->compositor, id);
440 l_debug (seat, "Failed to work out session ID to mark");
442 SEAT_CLASS (seat_local_parent_class)->set_next_session (seat, session);
446 seat_local_run_script (Seat *seat, DisplayServer *display_server, Process *script)
448 if (IS_X_SERVER_LOCAL (display_server))
451 XServerLocal *x_server;
453 x_server = X_SERVER_LOCAL (display_server);
454 path = x_server_local_get_authority_file_path (x_server);
455 process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
456 process_set_env (script, "XAUTHORITY", path);
459 SEAT_CLASS (seat_local_parent_class)->run_script (seat, display_server, script);
463 seat_local_stop (Seat *s)
465 SeatLocal *seat = SEAT_LOCAL (s);
467 /* Stop the compositor */
468 if (seat->priv->compositor)
469 display_server_stop (DISPLAY_SERVER (seat->priv->compositor));
471 /* Stop the XDMCP X server */
472 if (seat->priv->xdmcp_x_server)
473 display_server_stop (DISPLAY_SERVER (seat->priv->xdmcp_x_server));
475 check_stopped (seat);
479 seat_local_init (SeatLocal *seat)
481 seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_LOCAL_TYPE, SeatLocalPrivate);
485 seat_local_finalize (GObject *object)
487 SeatLocal *seat = SEAT_LOCAL (object);
489 g_clear_object (&seat->priv->compositor);
490 g_clear_object (&seat->priv->active_compositor_session);
491 if (seat->priv->xdmcp_x_server)
493 g_signal_handlers_disconnect_matched (seat->priv->xdmcp_x_server, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
494 g_object_unref (seat->priv->xdmcp_x_server);
497 G_OBJECT_CLASS (seat_local_parent_class)->finalize (object);
501 seat_local_class_init (SeatLocalClass *klass)
503 GObjectClass *object_class = G_OBJECT_CLASS (klass);
504 SeatClass *seat_class = SEAT_CLASS (klass);
506 object_class->finalize = seat_local_finalize;
508 seat_class->setup = seat_local_setup;
509 seat_class->start = seat_local_start;
510 seat_class->create_display_server = seat_local_create_display_server;
511 seat_class->display_server_is_used = seat_local_display_server_is_used;
512 seat_class->create_greeter_session = seat_local_create_greeter_session;
513 seat_class->create_session = seat_local_create_session;
514 seat_class->set_active_session = seat_local_set_active_session;
515 seat_class->get_active_session = seat_local_get_active_session;
516 seat_class->set_next_session = seat_local_set_next_session;
517 seat_class->run_script = seat_local_run_script;
518 seat_class->stop = seat_local_stop;
520 g_type_class_add_private (klass, sizeof (SeatLocalPrivate));