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 "x-server-local.h"
20 #include "mir-server.h"
28 USC_MESSAGE_READY = 2,
29 USC_MESSAGE_SESSION_CONNECTED = 3,
30 USC_MESSAGE_SET_ACTIVE_SESSION = 4
33 struct SeatUnityPrivate
35 /* VT we are running on */
41 /* Filename of Mir socket */
42 gchar *mir_socket_filename;
44 /* Pipes to communicate with compositor */
45 int to_compositor_pipe[2];
46 int from_compositor_pipe[2];
48 /* IO channel listening on for messages from the compositor */
49 GIOChannel *from_compositor_channel;
51 /* TRUE when the compositor indicates it is ready */
52 gboolean compositor_ready;
54 /* Buffer reading from channel */
56 gsize read_buffer_length;
57 gsize read_buffer_n_used;
59 /* Compositor process */
60 Process *compositor_process;
62 /* Timeout when waiting for compositor to start */
63 guint compositor_timeout;
65 /* Next Mir ID to use for a compositor client */
68 /* TRUE if using VT switching fallback */
69 gboolean use_vt_switching;
71 /* The currently visible session */
72 Session *active_session;
73 DisplayServer *active_display_server;
76 G_DEFINE_TYPE (SeatUnity, seat_unity, SEAT_TYPE);
79 seat_unity_get_start_local_sessions (Seat *seat)
81 return !seat_get_string_property (seat, "xdmcp-manager");
85 seat_unity_setup (Seat *seat)
87 seat_set_can_switch (seat, TRUE);
88 SEAT_CLASS (seat_unity_parent_class)->setup (seat);
92 compositor_stopped_cb (Process *process, SeatUnity *seat)
94 if (seat->priv->compositor_timeout != 0)
95 g_source_remove (seat->priv->compositor_timeout);
96 seat->priv->compositor_timeout = 0;
98 /* Finished with the VT */
99 vt_unref (seat->priv->vt);
102 if (seat_get_is_stopping (SEAT (seat)))
104 SEAT_CLASS (seat_unity_parent_class)->stop (SEAT (seat));
108 /* If stopped before it was ready, then revert to VT mode */
109 if (!seat->priv->compositor_ready)
111 g_debug ("Compositor failed to start, switching to VT mode");
112 seat->priv->use_vt_switching = TRUE;
113 SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
117 g_debug ("Stopping Unity seat, compositor terminated");
119 seat_stop (SEAT (seat));
123 compositor_run_cb (Process *process, SeatUnity *seat)
127 /* Make input non-blocking */
128 fd = open ("/dev/null", O_RDONLY);
129 dup2 (fd, STDIN_FILENO);
132 /* Redirect output to logfile */
133 if (seat->priv->log_file)
137 fd = g_open (seat->priv->log_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
139 g_warning ("Failed to open log file %s: %s", seat->priv->log_file, g_strerror (errno));
142 dup2 (fd, STDOUT_FILENO);
143 dup2 (fd, STDERR_FILENO);
150 write_message (SeatUnity *seat, guint16 id, const guint8 *payload, guint16 payload_length)
153 gsize data_length = 4 + payload_length;
155 data = g_malloc (data_length);
158 data[2] = payload_length >> 8;
159 data[3] = payload_length & 0xFF;
160 memcpy (data + 4, payload, payload_length);
163 if (write (seat->priv->to_compositor_pipe[1], data, data_length) != data_length)
164 g_warning ("Failed to write to compositor: %s", strerror (errno));
168 read_cb (GIOChannel *source, GIOCondition condition, gpointer data)
170 SeatUnity *seat = data;
172 guint16 id, payload_length;
175 if (condition == G_IO_HUP)
177 g_debug ("Compositor closed communication channel");
181 /* Work out how much required for a message */
182 if (seat->priv->read_buffer_n_used < 4)
183 n_to_read = 4 - seat->priv->read_buffer_n_used;
186 payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
187 n_to_read = 4 + payload_length - seat->priv->read_buffer_n_used;
190 /* Read from compositor */
193 gsize n_total, n_read = 0;
195 GError *error = NULL;
197 n_total = seat->priv->read_buffer_n_used + n_to_read;
198 if (seat->priv->read_buffer_length < n_total)
199 seat->priv->read_buffer = g_realloc (seat->priv->read_buffer, n_total);
201 status = g_io_channel_read_chars (source,
202 (gchar *)seat->priv->read_buffer + seat->priv->read_buffer_n_used,
207 g_warning ("Failed to read from compositor: %s", error->message);
208 if (status != G_IO_STATUS_NORMAL)
210 g_clear_error (&error);
211 seat->priv->read_buffer_n_used += n_read;
215 if (seat->priv->read_buffer_n_used < 4)
217 id = seat->priv->read_buffer[0] << 8 | seat->priv->read_buffer[1];
218 payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
221 if (seat->priv->read_buffer_n_used < 4 + payload_length)
223 /*payload = seat->priv->read_buffer + 4;*/
227 case USC_MESSAGE_PING:
229 write_message (seat, USC_MESSAGE_PONG, NULL, 0);
231 case USC_MESSAGE_PONG:
234 case USC_MESSAGE_READY:
236 if (!seat->priv->compositor_ready)
238 seat->priv->compositor_ready = TRUE;
239 g_debug ("Compositor ready");
240 g_source_remove (seat->priv->compositor_timeout);
241 seat->priv->compositor_timeout = 0;
242 SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
245 case USC_MESSAGE_SESSION_CONNECTED:
246 g_debug ("SESSION CONNECTED");
249 g_warning ("Ingoring unknown message %d with %d octets from system compositor", id, payload_length);
254 seat->priv->read_buffer_n_used = 0;
260 get_absolute_command (const gchar *command)
263 gchar *absolute_binary, *absolute_command = NULL;
265 tokens = g_strsplit (command, " ", 2);
267 absolute_binary = g_find_program_in_path (tokens[0]);
271 absolute_command = g_strjoin (" ", absolute_binary, tokens[1], NULL);
273 absolute_command = g_strdup (absolute_binary);
274 g_free (absolute_binary);
277 absolute_command = g_strdup (command);
281 return absolute_command;
285 compositor_timeout_cb (gpointer data)
287 SeatUnity *seat = data;
289 /* Stop the compositor - it is not working */
290 process_stop (seat->priv->compositor_process);
296 seat_unity_start (Seat *seat)
298 const gchar *compositor_command;
299 gchar *command, *absolute_command, *dir;
303 /* Replace Plymouth if it is running */
304 if (plymouth_get_is_active () && plymouth_has_active_vt ())
306 gint active_vt = vt_get_active ();
307 if (active_vt >= vt_get_min ())
309 SEAT_UNITY (seat)->priv->vt = active_vt;
310 plymouth_quit (TRUE);
313 g_debug ("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 ());
315 if (plymouth_get_is_active ())
316 plymouth_quit (FALSE);
317 if (SEAT_UNITY (seat)->priv->vt < 0)
318 SEAT_UNITY (seat)->priv->vt = vt_get_unused ();
319 if (SEAT_UNITY (seat)->priv->vt < 0)
321 g_debug ("Failed to get a VT to run on");
324 vt_ref (SEAT_UNITY (seat)->priv->vt);
326 /* Create pipes to talk to compositor */
327 if (pipe (SEAT_UNITY (seat)->priv->to_compositor_pipe) < 0 || pipe (SEAT_UNITY (seat)->priv->from_compositor_pipe) < 0)
329 g_debug ("Failed to create compositor pipes: %s", g_strerror (errno));
333 /* Don't allow the daemon end of the pipes to be accessed in the compositor */
334 fcntl (SEAT_UNITY (seat)->priv->to_compositor_pipe[1], F_SETFD, FD_CLOEXEC);
335 fcntl (SEAT_UNITY (seat)->priv->from_compositor_pipe[0], F_SETFD, FD_CLOEXEC);
337 /* Listen for messages from the compositor */
338 SEAT_UNITY (seat)->priv->from_compositor_channel = g_io_channel_unix_new (SEAT_UNITY (seat)->priv->from_compositor_pipe[0]);
339 g_io_add_watch (SEAT_UNITY (seat)->priv->from_compositor_channel, G_IO_IN | G_IO_HUP, read_cb, seat);
342 dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
343 SEAT_UNITY (seat)->priv->log_file = g_build_filename (dir, "unity-system-compositor.log", NULL);
344 g_debug ("Logging to %s", SEAT_UNITY (seat)->priv->log_file);
347 SEAT_UNITY (seat)->priv->mir_socket_filename = g_strdup ("/tmp/mir_socket"); // FIXME: Use this socket by default as XMir is hardcoded to this
348 timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
349 compositor_command = seat_get_string_property (seat, "unity-compositor-command");
350 command = g_strdup_printf ("%s --from-dm-fd %d --to-dm-fd %d --vt %d", compositor_command, SEAT_UNITY (seat)->priv->to_compositor_pipe[0], SEAT_UNITY (seat)->priv->from_compositor_pipe[1], SEAT_UNITY (seat)->priv->vt);
352 absolute_command = get_absolute_command (command);
355 /* Start the compositor */
356 process_set_command (SEAT_UNITY (seat)->priv->compositor_process, absolute_command);
357 g_free (absolute_command);
358 g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "stopped", G_CALLBACK (compositor_stopped_cb), seat);
359 g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "run", G_CALLBACK (compositor_run_cb), seat);
360 result = process_start (SEAT_UNITY (seat)->priv->compositor_process, FALSE);
362 /* Close compostor ends of the pipes */
363 close (SEAT_UNITY (seat)->priv->to_compositor_pipe[0]);
364 SEAT_UNITY (seat)->priv->to_compositor_pipe[0] = 0;
365 close (SEAT_UNITY (seat)->priv->from_compositor_pipe[1]);
366 SEAT_UNITY (seat)->priv->from_compositor_pipe[1] = 0;
371 /* Connect to the compositor */
372 timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
375 g_debug ("Waiting for system compositor for %ds", timeout);
376 SEAT_UNITY (seat)->priv->compositor_timeout = g_timeout_add (timeout * 1000, compositor_timeout_cb, seat);
381 static DisplayServer *
382 create_x_server (Seat *seat)
384 XServerLocal *x_server;
385 const gchar *command = NULL, *layout = NULL, *config_file = NULL, *xdmcp_manager = NULL, *key_name = NULL;
389 g_debug ("Starting X server on Unity compositor");
391 x_server = x_server_local_new ();
393 command = seat_get_string_property (seat, "xserver-command");
395 x_server_local_set_command (x_server, command);
397 if (SEAT_UNITY (seat)->priv->use_vt_switching)
398 x_server_local_set_vt (x_server, vt_get_unused ());
403 id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
404 SEAT_UNITY (seat)->priv->next_id++;
405 x_server_local_set_mir_id (x_server, id);
406 x_server_local_set_mir_socket (x_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
410 layout = seat_get_string_property (seat, "xserver-layout");
412 x_server_local_set_layout (x_server, layout);
414 config_file = seat_get_string_property (seat, "xserver-config");
416 x_server_local_set_config (x_server, config_file);
418 allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
419 x_server_local_set_allow_tcp (x_server, allow_tcp);
421 xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
423 x_server_local_set_xdmcp_server (x_server, xdmcp_manager);
425 port = seat_get_integer_property (seat, "xdmcp-port");
427 x_server_local_set_xdmcp_port (x_server, port);
429 key_name = seat_get_string_property (seat, "xdmcp-key");
435 GError *error = NULL;
437 dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
438 path = g_build_filename (dir, "keys.conf", NULL);
441 keys = g_key_file_new ();
442 result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
444 g_debug ("Error getting key %s", error->message);
445 g_clear_error (&error);
451 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
452 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
454 g_debug ("Key %s not defined", key_name);
457 x_server_local_set_xdmcp_key (x_server, key);
462 g_key_file_free (keys);
465 return DISPLAY_SERVER (x_server);
468 static DisplayServer *
469 create_mir_server (Seat *seat)
471 MirServer *mir_server;
473 mir_server = mir_server_new ();
474 mir_server_set_parent_socket (mir_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
476 if (SEAT_UNITY (seat)->priv->use_vt_switching)
477 mir_server_set_vt (mir_server, vt_get_unused ());
482 id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
483 SEAT_UNITY (seat)->priv->next_id++;
484 mir_server_set_id (mir_server, id);
485 mir_server_set_parent_socket (mir_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
489 return DISPLAY_SERVER (mir_server);
492 static DisplayServer *
493 seat_unity_create_display_server (Seat *seat, const gchar *session_type)
495 if (strcmp (session_type, "x") == 0)
496 return create_x_server (seat);
497 else if (strcmp (session_type, "mir") == 0)
498 return create_mir_server (seat);
501 g_warning ("Can't create unsupported display server '%s'", session_type);
507 seat_unity_create_greeter_session (Seat *seat)
509 Greeter *greeter_session;
511 greeter_session = SEAT_CLASS (seat_unity_parent_class)->create_greeter_session (seat);
512 session_set_env (SESSION (greeter_session), "XDG_SEAT", "seat0");
513 if (!SEAT_UNITY (seat)->priv->use_vt_switching)
515 gchar *value = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->vt);
516 session_set_env (SESSION (greeter_session), "XDG_VTNR", value);
520 return greeter_session;
524 seat_unity_create_session (Seat *seat)
528 session = SEAT_CLASS (seat_unity_parent_class)->create_session (seat);
529 session_set_env (session, "XDG_SEAT", "seat0");
530 if (!SEAT_UNITY (seat)->priv->use_vt_switching)
532 gchar *value = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->vt);
533 session_set_env (SESSION (session), "XDG_VTNR", value);
541 seat_unity_set_active_session (Seat *seat, Session *session)
543 DisplayServer *display_server;
545 /* If no compositor, have to use VT switching */
546 if (SEAT_UNITY (seat)->priv->use_vt_switching)
548 gint vt = display_server_get_vt (session_get_display_server (session));
552 SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
556 if (session == SEAT_UNITY (seat)->priv->active_session)
558 SEAT_UNITY (seat)->priv->active_session = g_object_ref (session);
560 display_server = session_get_display_server (session);
561 if (SEAT_UNITY (seat)->priv->active_display_server != display_server)
563 const gchar *id = NULL;
565 SEAT_UNITY (seat)->priv->active_display_server = g_object_ref (display_server);
567 if (IS_X_SERVER_LOCAL (display_server))
568 id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
569 else if (IS_MIR_SERVER (display_server))
570 id = mir_server_get_id (MIR_SERVER (display_server));
574 g_debug ("Switching to Mir session %s", id);
575 write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_ACTIVE_SESSION, (const guint8 *) id, strlen (id));
578 g_warning ("Failed to work out session ID");
581 SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
585 seat_unity_get_active_session (Seat *seat)
587 if (SEAT_UNITY (seat)->priv->use_vt_switching)
591 vt = vt_get_active ();
595 for (link = seat_get_sessions (seat); link; link = link->next)
597 Session *session = link->data;
598 if (display_server_get_vt (session_get_display_server (session)) == vt)
605 return SEAT_UNITY (seat)->priv->active_session;
609 seat_unity_run_script (Seat *seat, DisplayServer *display_server, Process *script)
612 XServerLocal *x_server;
614 x_server = X_SERVER_LOCAL (display_server);
615 path = x_server_local_get_authority_file_path (x_server);
616 process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
617 process_set_env (script, "XAUTHORITY", path);
619 SEAT_CLASS (seat_unity_parent_class)->run_script (seat, display_server, script);
623 seat_unity_stop (Seat *seat)
625 /* Stop the compositor first */
626 if (process_get_is_running (SEAT_UNITY (seat)->priv->compositor_process))
628 process_stop (SEAT_UNITY (seat)->priv->compositor_process);
632 SEAT_CLASS (seat_unity_parent_class)->stop (seat);
636 seat_unity_init (SeatUnity *seat)
638 seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_UNITY_TYPE, SeatUnityPrivate);
640 seat->priv->compositor_process = process_new ();
644 seat_unity_finalize (GObject *object)
646 SeatUnity *seat = SEAT_UNITY (object);
648 if (seat->priv->vt >= 0)
649 vt_unref (seat->priv->vt);
650 g_free (seat->priv->log_file);
651 g_free (seat->priv->mir_socket_filename);
652 close (seat->priv->to_compositor_pipe[0]);
653 close (seat->priv->to_compositor_pipe[1]);
654 close (seat->priv->from_compositor_pipe[0]);
655 close (seat->priv->from_compositor_pipe[1]);
656 g_io_channel_unref (seat->priv->from_compositor_channel);
657 g_free (seat->priv->read_buffer);
658 g_object_unref (seat->priv->compositor_process);
659 if (seat->priv->active_session)
660 g_object_unref (seat->priv->active_session);
661 if (seat->priv->active_display_server)
662 g_object_unref (seat->priv->active_display_server);
664 G_OBJECT_CLASS (seat_unity_parent_class)->finalize (object);
668 seat_unity_class_init (SeatUnityClass *klass)
670 GObjectClass *object_class = G_OBJECT_CLASS (klass);
671 SeatClass *seat_class = SEAT_CLASS (klass);
673 object_class->finalize = seat_unity_finalize;
674 seat_class->get_start_local_sessions = seat_unity_get_start_local_sessions;
675 seat_class->setup = seat_unity_setup;
676 seat_class->start = seat_unity_start;
677 seat_class->create_display_server = seat_unity_create_display_server;
678 seat_class->create_greeter_session = seat_unity_create_greeter_session;
679 seat_class->create_session = seat_unity_create_session;
680 seat_class->set_active_session = seat_unity_set_active_session;
681 seat_class->get_active_session = seat_unity_get_active_session;
682 seat_class->run_script = seat_unity_run_script;
683 seat_class->stop = seat_unity_stop;
685 g_type_class_add_private (klass, sizeof (SeatUnityPrivate));