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;
50 guint from_compositor_watch;
52 /* TRUE when the compositor indicates it is ready */
53 gboolean compositor_ready;
55 /* Buffer reading from channel */
57 gsize read_buffer_length;
58 gsize read_buffer_n_used;
60 /* Compositor process */
61 Process *compositor_process;
63 /* Timeout when waiting for compositor to start */
64 guint compositor_timeout;
66 /* Next Mir ID to use for a compositor client */
69 /* TRUE if using VT switching fallback */
70 gboolean use_vt_switching;
72 /* The currently visible session */
73 Session *active_session;
74 DisplayServer *active_display_server;
77 G_DEFINE_TYPE (SeatUnity, seat_unity, SEAT_TYPE);
80 seat_unity_get_start_local_sessions (Seat *seat)
82 return !seat_get_string_property (seat, "xdmcp-manager");
86 seat_unity_setup (Seat *seat)
88 seat_set_can_switch (seat, TRUE);
89 SEAT_CLASS (seat_unity_parent_class)->setup (seat);
93 compositor_stopped_cb (Process *process, SeatUnity *seat)
95 if (seat->priv->compositor_timeout != 0)
96 g_source_remove (seat->priv->compositor_timeout);
97 seat->priv->compositor_timeout = 0;
99 /* Finished with the VT */
100 vt_unref (seat->priv->vt);
103 if (seat_get_is_stopping (SEAT (seat)))
105 SEAT_CLASS (seat_unity_parent_class)->stop (SEAT (seat));
109 /* If stopped before it was ready, then revert to VT mode */
110 if (!seat->priv->compositor_ready)
112 l_debug (seat, "Compositor failed to start, switching to VT mode");
113 seat->priv->use_vt_switching = TRUE;
114 SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
118 l_debug (seat, "Stopping Unity seat, compositor terminated");
120 seat_stop (SEAT (seat));
124 compositor_run_cb (Process *process, SeatUnity *seat)
128 /* Make input non-blocking */
129 fd = open ("/dev/null", O_RDONLY);
130 dup2 (fd, STDIN_FILENO);
133 /* Redirect output to logfile */
134 if (seat->priv->log_file)
139 /* Move old file out of the way */
140 old_filename = g_strdup_printf ("%s.old", seat->priv->log_file);
141 rename (seat->priv->log_file, old_filename);
142 g_free (old_filename);
144 /* Create new file and log to it */
145 fd = g_open (seat->priv->log_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
147 l_warning (seat, "Failed to open log file %s: %s", seat->priv->log_file, g_strerror (errno));
150 dup2 (fd, STDOUT_FILENO);
151 dup2 (fd, STDERR_FILENO);
158 write_message (SeatUnity *seat, guint16 id, const guint8 *payload, guint16 payload_length)
161 gsize data_length = 4 + payload_length;
163 data = g_malloc (data_length);
166 data[2] = payload_length >> 8;
167 data[3] = payload_length & 0xFF;
168 memcpy (data + 4, payload, payload_length);
171 if (write (seat->priv->to_compositor_pipe[1], data, data_length) != data_length)
172 l_warning (seat, "Failed to write to compositor: %s", strerror (errno));
176 read_cb (GIOChannel *source, GIOCondition condition, gpointer data)
178 SeatUnity *seat = data;
180 guint16 id, payload_length;
183 if (condition == G_IO_HUP)
185 l_debug (seat, "Compositor closed communication channel");
189 /* Work out how much required for a message */
190 if (seat->priv->read_buffer_n_used < 4)
191 n_to_read = 4 - seat->priv->read_buffer_n_used;
194 payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
195 n_to_read = 4 + payload_length - seat->priv->read_buffer_n_used;
198 /* Read from compositor */
201 gsize n_total, n_read = 0;
203 GError *error = NULL;
205 n_total = seat->priv->read_buffer_n_used + n_to_read;
206 if (seat->priv->read_buffer_length < n_total)
207 seat->priv->read_buffer = g_realloc (seat->priv->read_buffer, n_total);
209 status = g_io_channel_read_chars (source,
210 (gchar *)seat->priv->read_buffer + seat->priv->read_buffer_n_used,
215 l_warning (seat, "Failed to read from compositor: %s", error->message);
216 if (status != G_IO_STATUS_NORMAL)
218 g_clear_error (&error);
219 seat->priv->read_buffer_n_used += n_read;
223 if (seat->priv->read_buffer_n_used < 4)
225 id = seat->priv->read_buffer[0] << 8 | seat->priv->read_buffer[1];
226 payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
229 if (seat->priv->read_buffer_n_used < 4 + payload_length)
231 /*payload = seat->priv->read_buffer + 4;*/
235 case USC_MESSAGE_PING:
236 l_debug (seat, "PING!");
237 write_message (seat, USC_MESSAGE_PONG, NULL, 0);
239 case USC_MESSAGE_PONG:
240 l_debug (seat, "PONG!");
242 case USC_MESSAGE_READY:
243 l_debug (seat, "READY");
244 if (!seat->priv->compositor_ready)
246 seat->priv->compositor_ready = TRUE;
247 l_debug (seat, "Compositor ready");
248 g_source_remove (seat->priv->compositor_timeout);
249 seat->priv->compositor_timeout = 0;
250 SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
253 case USC_MESSAGE_SESSION_CONNECTED:
254 l_debug (seat, "SESSION CONNECTED");
257 l_warning (seat, "Ignoring unknown message %d with %d octets from system compositor", id, payload_length);
262 seat->priv->read_buffer_n_used = 0;
268 get_absolute_command (const gchar *command)
271 gchar *absolute_binary, *absolute_command = NULL;
273 tokens = g_strsplit (command, " ", 2);
275 absolute_binary = g_find_program_in_path (tokens[0]);
279 absolute_command = g_strjoin (" ", absolute_binary, tokens[1], NULL);
281 absolute_command = g_strdup (absolute_binary);
282 g_free (absolute_binary);
285 absolute_command = g_strdup (command);
289 return absolute_command;
293 compositor_timeout_cb (gpointer data)
295 SeatUnity *seat = data;
297 /* Stop the compositor - it is not working */
298 process_stop (seat->priv->compositor_process);
304 seat_unity_start (Seat *seat)
306 const gchar *compositor_command;
307 gchar *command, *absolute_command, *dir;
311 /* Replace Plymouth if it is running */
312 if (plymouth_get_is_active () && plymouth_has_active_vt ())
314 gint active_vt = vt_get_active ();
315 if (active_vt >= vt_get_min ())
317 SEAT_UNITY (seat)->priv->vt = active_vt;
318 plymouth_quit (TRUE);
321 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 ());
323 if (plymouth_get_is_active ())
324 plymouth_quit (FALSE);
325 if (SEAT_UNITY (seat)->priv->vt < 0)
326 SEAT_UNITY (seat)->priv->vt = vt_get_unused ();
327 if (SEAT_UNITY (seat)->priv->vt < 0)
329 l_debug (seat, "Failed to get a VT to run on");
332 vt_ref (SEAT_UNITY (seat)->priv->vt);
334 /* Create pipes to talk to compositor */
335 if (pipe (SEAT_UNITY (seat)->priv->to_compositor_pipe) < 0 || pipe (SEAT_UNITY (seat)->priv->from_compositor_pipe) < 0)
337 l_debug (seat, "Failed to create compositor pipes: %s", g_strerror (errno));
341 /* Don't allow the daemon end of the pipes to be accessed in the compositor */
342 fcntl (SEAT_UNITY (seat)->priv->to_compositor_pipe[1], F_SETFD, FD_CLOEXEC);
343 fcntl (SEAT_UNITY (seat)->priv->from_compositor_pipe[0], F_SETFD, FD_CLOEXEC);
345 /* Listen for messages from the compositor */
346 SEAT_UNITY (seat)->priv->from_compositor_channel = g_io_channel_unix_new (SEAT_UNITY (seat)->priv->from_compositor_pipe[0]);
347 SEAT_UNITY (seat)->priv->from_compositor_watch = g_io_add_watch (SEAT_UNITY (seat)->priv->from_compositor_channel, G_IO_IN | G_IO_HUP, read_cb, seat);
350 dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
351 SEAT_UNITY (seat)->priv->log_file = g_build_filename (dir, "unity-system-compositor.log", NULL);
352 l_debug (seat, "Logging to %s", SEAT_UNITY (seat)->priv->log_file);
355 SEAT_UNITY (seat)->priv->mir_socket_filename = g_strdup ("/tmp/mir_socket"); // FIXME: Use this socket by default as XMir is hardcoded to this
356 timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
357 compositor_command = seat_get_string_property (seat, "unity-compositor-command");
358 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);
360 absolute_command = get_absolute_command (command);
363 /* Start the compositor */
364 process_set_command (SEAT_UNITY (seat)->priv->compositor_process, absolute_command);
365 g_free (absolute_command);
366 g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "stopped", G_CALLBACK (compositor_stopped_cb), seat);
367 g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "run", G_CALLBACK (compositor_run_cb), seat);
368 result = process_start (SEAT_UNITY (seat)->priv->compositor_process, FALSE);
370 /* Close compostor ends of the pipes */
371 close (SEAT_UNITY (seat)->priv->to_compositor_pipe[0]);
372 SEAT_UNITY (seat)->priv->to_compositor_pipe[0] = 0;
373 close (SEAT_UNITY (seat)->priv->from_compositor_pipe[1]);
374 SEAT_UNITY (seat)->priv->from_compositor_pipe[1] = 0;
379 /* Connect to the compositor */
380 timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
383 l_debug (seat, "Waiting for system compositor for %ds", timeout);
384 SEAT_UNITY (seat)->priv->compositor_timeout = g_timeout_add (timeout * 1000, compositor_timeout_cb, seat);
389 static DisplayServer *
390 create_x_server (Seat *seat)
392 XServerLocal *x_server;
393 const gchar *command = NULL, *layout = NULL, *config_file = NULL, *xdmcp_manager = NULL, *key_name = NULL;
397 l_debug (seat, "Starting X server on Unity compositor");
399 x_server = x_server_local_new ();
401 command = seat_get_string_property (seat, "xserver-command");
403 x_server_local_set_command (x_server, command);
405 if (SEAT_UNITY (seat)->priv->use_vt_switching)
406 x_server_local_set_vt (x_server, vt_get_unused ());
411 id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
412 SEAT_UNITY (seat)->priv->next_id++;
413 x_server_local_set_mir_id (x_server, id);
414 x_server_local_set_mir_socket (x_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
418 layout = seat_get_string_property (seat, "xserver-layout");
420 x_server_local_set_layout (x_server, layout);
422 config_file = seat_get_string_property (seat, "xserver-config");
424 x_server_local_set_config (x_server, config_file);
426 allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
427 x_server_local_set_allow_tcp (x_server, allow_tcp);
429 xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
431 x_server_local_set_xdmcp_server (x_server, xdmcp_manager);
433 port = seat_get_integer_property (seat, "xdmcp-port");
435 x_server_local_set_xdmcp_port (x_server, port);
437 key_name = seat_get_string_property (seat, "xdmcp-key");
443 GError *error = NULL;
445 dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
446 path = g_build_filename (dir, "keys.conf", NULL);
449 keys = g_key_file_new ();
450 result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
452 l_debug (seat, "Error getting key %s", error->message);
453 g_clear_error (&error);
459 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
460 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
462 l_debug (seat, "Key %s not defined", key_name);
465 x_server_local_set_xdmcp_key (x_server, key);
470 g_key_file_free (keys);
473 return DISPLAY_SERVER (x_server);
476 static DisplayServer *
477 create_mir_server (Seat *seat)
479 MirServer *mir_server;
481 mir_server = mir_server_new ();
482 mir_server_set_parent_socket (mir_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
484 if (SEAT_UNITY (seat)->priv->use_vt_switching)
485 mir_server_set_vt (mir_server, vt_get_unused ());
490 id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
491 SEAT_UNITY (seat)->priv->next_id++;
492 mir_server_set_id (mir_server, id);
493 mir_server_set_parent_socket (mir_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
497 return DISPLAY_SERVER (mir_server);
500 static DisplayServer *
501 seat_unity_create_display_server (Seat *seat, const gchar *session_type)
503 if (strcmp (session_type, "x") == 0)
504 return create_x_server (seat);
505 else if (strcmp (session_type, "mir") == 0)
506 return create_mir_server (seat);
509 l_warning (seat, "Can't create unsupported display server '%s'", session_type);
515 seat_unity_create_greeter_session (Seat *seat)
517 Greeter *greeter_session;
518 const gchar *xdg_seat;
520 greeter_session = SEAT_CLASS (seat_unity_parent_class)->create_greeter_session (seat);
522 l_debug (seat, "Setting XDG_SEAT=%s", xdg_seat);
523 session_set_env (SESSION (greeter_session), "XDG_SEAT", xdg_seat);
524 if (!SEAT_UNITY (seat)->priv->use_vt_switching)
526 gchar *value = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->vt);
527 l_debug (seat, "Setting XDG_VTNR=%s", value);
528 session_set_env (SESSION (greeter_session), "XDG_VTNR", value);
532 l_debug (seat, "Not setting XDG_VTNR");
534 return greeter_session;
538 seat_unity_create_session (Seat *seat)
541 const gchar *xdg_seat;
543 session = SEAT_CLASS (seat_unity_parent_class)->create_session (seat);
545 l_debug (seat, "Setting XDG_SEAT=%s", xdg_seat);
546 session_set_env (session, "XDG_SEAT", xdg_seat);
547 if (!SEAT_UNITY (seat)->priv->use_vt_switching)
549 gchar *value = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->vt);
550 l_debug (seat, "Setting XDG_VTNR=%s", value);
551 session_set_env (SESSION (session), "XDG_VTNR", value);
555 l_debug (seat, "Not setting XDG_VTNR");
561 seat_unity_set_active_session (Seat *seat, Session *session)
563 DisplayServer *display_server;
565 /* If no compositor, have to use VT switching */
566 if (SEAT_UNITY (seat)->priv->use_vt_switching)
568 gint vt = display_server_get_vt (session_get_display_server (session));
572 SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
576 if (session == SEAT_UNITY (seat)->priv->active_session)
578 SEAT_UNITY (seat)->priv->active_session = g_object_ref (session);
580 display_server = session_get_display_server (session);
581 if (SEAT_UNITY (seat)->priv->active_display_server != display_server)
583 const gchar *id = NULL;
585 SEAT_UNITY (seat)->priv->active_display_server = g_object_ref (display_server);
587 if (IS_X_SERVER_LOCAL (display_server))
588 id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
589 else if (IS_MIR_SERVER (display_server))
590 id = mir_server_get_id (MIR_SERVER (display_server));
594 l_debug (seat, "Switching to Mir session %s", id);
595 write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_ACTIVE_SESSION, (const guint8 *) id, strlen (id));
598 l_warning (seat, "Failed to work out session ID");
601 SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
605 seat_unity_get_active_session (Seat *seat)
607 if (SEAT_UNITY (seat)->priv->use_vt_switching)
611 vt = vt_get_active ();
615 for (link = seat_get_sessions (seat); link; link = link->next)
617 Session *session = link->data;
618 if (display_server_get_vt (session_get_display_server (session)) == vt)
625 return SEAT_UNITY (seat)->priv->active_session;
629 seat_unity_run_script (Seat *seat, DisplayServer *display_server, Process *script)
632 XServerLocal *x_server;
634 x_server = X_SERVER_LOCAL (display_server);
635 path = x_server_local_get_authority_file_path (x_server);
636 process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
637 process_set_env (script, "XAUTHORITY", path);
639 SEAT_CLASS (seat_unity_parent_class)->run_script (seat, display_server, script);
643 seat_unity_stop (Seat *seat)
645 /* Stop the compositor first */
646 if (process_get_is_running (SEAT_UNITY (seat)->priv->compositor_process))
648 process_stop (SEAT_UNITY (seat)->priv->compositor_process);
652 SEAT_CLASS (seat_unity_parent_class)->stop (seat);
656 seat_unity_init (SeatUnity *seat)
658 seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_UNITY_TYPE, SeatUnityPrivate);
660 seat->priv->compositor_process = process_new ();
664 seat_unity_finalize (GObject *object)
666 SeatUnity *seat = SEAT_UNITY (object);
668 if (seat->priv->vt >= 0)
669 vt_unref (seat->priv->vt);
670 g_free (seat->priv->log_file);
671 g_free (seat->priv->mir_socket_filename);
672 close (seat->priv->to_compositor_pipe[0]);
673 close (seat->priv->to_compositor_pipe[1]);
674 close (seat->priv->from_compositor_pipe[0]);
675 close (seat->priv->from_compositor_pipe[1]);
676 g_io_channel_unref (seat->priv->from_compositor_channel);
677 g_source_remove (seat->priv->from_compositor_watch);
678 g_free (seat->priv->read_buffer);
679 g_object_unref (seat->priv->compositor_process);
680 if (seat->priv->active_session)
681 g_object_unref (seat->priv->active_session);
682 if (seat->priv->active_display_server)
683 g_object_unref (seat->priv->active_display_server);
685 G_OBJECT_CLASS (seat_unity_parent_class)->finalize (object);
689 seat_unity_class_init (SeatUnityClass *klass)
691 GObjectClass *object_class = G_OBJECT_CLASS (klass);
692 SeatClass *seat_class = SEAT_CLASS (klass);
694 object_class->finalize = seat_unity_finalize;
695 seat_class->get_start_local_sessions = seat_unity_get_start_local_sessions;
696 seat_class->setup = seat_unity_setup;
697 seat_class->start = seat_unity_start;
698 seat_class->create_display_server = seat_unity_create_display_server;
699 seat_class->create_greeter_session = seat_unity_create_greeter_session;
700 seat_class->create_session = seat_unity_create_session;
701 seat_class->set_active_session = seat_unity_set_active_session;
702 seat_class->get_active_session = seat_unity_get_active_session;
703 seat_class->run_script = seat_unity_run_script;
704 seat_class->stop = seat_unity_stop;
706 g_type_class_add_private (klass, sizeof (SeatUnityPrivate));