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 l_debug (seat, "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 l_debug (seat, "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)
138 /* Move old file out of the way */
139 old_filename = g_strdup_printf ("%s.old", seat->priv->log_file);
140 rename (seat->priv->log_file, old_filename);
141 g_free (old_filename);
143 /* Create new file and log to it */
144 fd = g_open (seat->priv->log_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
146 l_warning (seat, "Failed to open log file %s: %s", seat->priv->log_file, g_strerror (errno));
149 dup2 (fd, STDOUT_FILENO);
150 dup2 (fd, STDERR_FILENO);
157 write_message (SeatUnity *seat, guint16 id, const guint8 *payload, guint16 payload_length)
160 gsize data_length = 4 + payload_length;
162 data = g_malloc (data_length);
165 data[2] = payload_length >> 8;
166 data[3] = payload_length & 0xFF;
167 memcpy (data + 4, payload, payload_length);
170 if (write (seat->priv->to_compositor_pipe[1], data, data_length) != data_length)
171 l_warning (seat, "Failed to write to compositor: %s", strerror (errno));
175 read_cb (GIOChannel *source, GIOCondition condition, gpointer data)
177 SeatUnity *seat = data;
179 guint16 id, payload_length;
182 if (condition == G_IO_HUP)
184 l_debug (seat, "Compositor closed communication channel");
188 /* Work out how much required for a message */
189 if (seat->priv->read_buffer_n_used < 4)
190 n_to_read = 4 - seat->priv->read_buffer_n_used;
193 payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
194 n_to_read = 4 + payload_length - seat->priv->read_buffer_n_used;
197 /* Read from compositor */
200 gsize n_total, n_read = 0;
202 GError *error = NULL;
204 n_total = seat->priv->read_buffer_n_used + n_to_read;
205 if (seat->priv->read_buffer_length < n_total)
206 seat->priv->read_buffer = g_realloc (seat->priv->read_buffer, n_total);
208 status = g_io_channel_read_chars (source,
209 (gchar *)seat->priv->read_buffer + seat->priv->read_buffer_n_used,
214 l_warning (seat, "Failed to read from compositor: %s", error->message);
215 if (status != G_IO_STATUS_NORMAL)
217 g_clear_error (&error);
218 seat->priv->read_buffer_n_used += n_read;
222 if (seat->priv->read_buffer_n_used < 4)
224 id = seat->priv->read_buffer[0] << 8 | seat->priv->read_buffer[1];
225 payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
228 if (seat->priv->read_buffer_n_used < 4 + payload_length)
230 /*payload = seat->priv->read_buffer + 4;*/
234 case USC_MESSAGE_PING:
235 l_debug (seat, "PING!");
236 write_message (seat, USC_MESSAGE_PONG, NULL, 0);
238 case USC_MESSAGE_PONG:
239 l_debug (seat, "PONG!");
241 case USC_MESSAGE_READY:
242 l_debug (seat, "READY");
243 if (!seat->priv->compositor_ready)
245 seat->priv->compositor_ready = TRUE;
246 l_debug (seat, "Compositor ready");
247 g_source_remove (seat->priv->compositor_timeout);
248 seat->priv->compositor_timeout = 0;
249 SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
252 case USC_MESSAGE_SESSION_CONNECTED:
253 l_debug (seat, "SESSION CONNECTED");
256 l_warning (seat, "Ignoring unknown message %d with %d octets from system compositor", id, payload_length);
261 seat->priv->read_buffer_n_used = 0;
267 get_absolute_command (const gchar *command)
270 gchar *absolute_binary, *absolute_command = NULL;
272 tokens = g_strsplit (command, " ", 2);
274 absolute_binary = g_find_program_in_path (tokens[0]);
278 absolute_command = g_strjoin (" ", absolute_binary, tokens[1], NULL);
280 absolute_command = g_strdup (absolute_binary);
281 g_free (absolute_binary);
284 absolute_command = g_strdup (command);
288 return absolute_command;
292 compositor_timeout_cb (gpointer data)
294 SeatUnity *seat = data;
296 /* Stop the compositor - it is not working */
297 process_stop (seat->priv->compositor_process);
303 seat_unity_start (Seat *seat)
305 const gchar *compositor_command;
306 gchar *command, *absolute_command, *dir;
310 /* Replace Plymouth if it is running */
311 if (plymouth_get_is_active () && plymouth_has_active_vt ())
313 gint active_vt = vt_get_active ();
314 if (active_vt >= vt_get_min ())
316 SEAT_UNITY (seat)->priv->vt = active_vt;
317 plymouth_quit (TRUE);
320 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 ());
322 if (plymouth_get_is_active ())
323 plymouth_quit (FALSE);
324 if (SEAT_UNITY (seat)->priv->vt < 0)
325 SEAT_UNITY (seat)->priv->vt = vt_get_unused ();
326 if (SEAT_UNITY (seat)->priv->vt < 0)
328 l_debug (seat, "Failed to get a VT to run on");
331 vt_ref (SEAT_UNITY (seat)->priv->vt);
333 /* Create pipes to talk to compositor */
334 if (pipe (SEAT_UNITY (seat)->priv->to_compositor_pipe) < 0 || pipe (SEAT_UNITY (seat)->priv->from_compositor_pipe) < 0)
336 l_debug (seat, "Failed to create compositor pipes: %s", g_strerror (errno));
340 /* Don't allow the daemon end of the pipes to be accessed in the compositor */
341 fcntl (SEAT_UNITY (seat)->priv->to_compositor_pipe[1], F_SETFD, FD_CLOEXEC);
342 fcntl (SEAT_UNITY (seat)->priv->from_compositor_pipe[0], F_SETFD, FD_CLOEXEC);
344 /* Listen for messages from the compositor */
345 SEAT_UNITY (seat)->priv->from_compositor_channel = g_io_channel_unix_new (SEAT_UNITY (seat)->priv->from_compositor_pipe[0]);
346 g_io_add_watch (SEAT_UNITY (seat)->priv->from_compositor_channel, G_IO_IN | G_IO_HUP, read_cb, seat);
349 dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
350 SEAT_UNITY (seat)->priv->log_file = g_build_filename (dir, "unity-system-compositor.log", NULL);
351 l_debug (seat, "Logging to %s", SEAT_UNITY (seat)->priv->log_file);
354 SEAT_UNITY (seat)->priv->mir_socket_filename = g_strdup ("/tmp/mir_socket"); // FIXME: Use this socket by default as XMir is hardcoded to this
355 timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
356 compositor_command = seat_get_string_property (seat, "unity-compositor-command");
357 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);
359 absolute_command = get_absolute_command (command);
362 /* Start the compositor */
363 process_set_command (SEAT_UNITY (seat)->priv->compositor_process, absolute_command);
364 g_free (absolute_command);
365 g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "stopped", G_CALLBACK (compositor_stopped_cb), seat);
366 g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "run", G_CALLBACK (compositor_run_cb), seat);
367 result = process_start (SEAT_UNITY (seat)->priv->compositor_process, FALSE);
369 /* Close compostor ends of the pipes */
370 close (SEAT_UNITY (seat)->priv->to_compositor_pipe[0]);
371 SEAT_UNITY (seat)->priv->to_compositor_pipe[0] = 0;
372 close (SEAT_UNITY (seat)->priv->from_compositor_pipe[1]);
373 SEAT_UNITY (seat)->priv->from_compositor_pipe[1] = 0;
378 /* Connect to the compositor */
379 timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
382 l_debug (seat, "Waiting for system compositor for %ds", timeout);
383 SEAT_UNITY (seat)->priv->compositor_timeout = g_timeout_add (timeout * 1000, compositor_timeout_cb, seat);
388 static DisplayServer *
389 create_x_server (Seat *seat)
391 XServerLocal *x_server;
392 const gchar *command = NULL, *layout = NULL, *config_file = NULL, *xdmcp_manager = NULL, *key_name = NULL;
396 l_debug (seat, "Starting X server on Unity compositor");
398 x_server = x_server_local_new ();
400 command = seat_get_string_property (seat, "xserver-command");
402 x_server_local_set_command (x_server, command);
404 if (SEAT_UNITY (seat)->priv->use_vt_switching)
405 x_server_local_set_vt (x_server, vt_get_unused ());
410 id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
411 SEAT_UNITY (seat)->priv->next_id++;
412 x_server_local_set_mir_id (x_server, id);
413 x_server_local_set_mir_socket (x_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
417 layout = seat_get_string_property (seat, "xserver-layout");
419 x_server_local_set_layout (x_server, layout);
421 config_file = seat_get_string_property (seat, "xserver-config");
423 x_server_local_set_config (x_server, config_file);
425 allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
426 x_server_local_set_allow_tcp (x_server, allow_tcp);
428 xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
430 x_server_local_set_xdmcp_server (x_server, xdmcp_manager);
432 port = seat_get_integer_property (seat, "xdmcp-port");
434 x_server_local_set_xdmcp_port (x_server, port);
436 key_name = seat_get_string_property (seat, "xdmcp-key");
442 GError *error = NULL;
444 dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
445 path = g_build_filename (dir, "keys.conf", NULL);
448 keys = g_key_file_new ();
449 result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
451 l_debug (seat, "Error getting key %s", error->message);
452 g_clear_error (&error);
458 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
459 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
461 l_debug (seat, "Key %s not defined", key_name);
464 x_server_local_set_xdmcp_key (x_server, key);
469 g_key_file_free (keys);
472 return DISPLAY_SERVER (x_server);
475 static DisplayServer *
476 create_mir_server (Seat *seat)
478 MirServer *mir_server;
480 mir_server = mir_server_new ();
481 mir_server_set_parent_socket (mir_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
483 if (SEAT_UNITY (seat)->priv->use_vt_switching)
484 mir_server_set_vt (mir_server, vt_get_unused ());
489 id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
490 SEAT_UNITY (seat)->priv->next_id++;
491 mir_server_set_id (mir_server, id);
492 mir_server_set_parent_socket (mir_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
496 return DISPLAY_SERVER (mir_server);
499 static DisplayServer *
500 seat_unity_create_display_server (Seat *seat, const gchar *session_type)
502 if (strcmp (session_type, "x") == 0)
503 return create_x_server (seat);
504 else if (strcmp (session_type, "mir") == 0)
505 return create_mir_server (seat);
508 l_warning (seat, "Can't create unsupported display server '%s'", session_type);
514 seat_unity_create_greeter_session (Seat *seat)
516 Greeter *greeter_session;
517 const gchar *xdg_seat;
519 greeter_session = SEAT_CLASS (seat_unity_parent_class)->create_greeter_session (seat);
521 l_debug (seat, "Setting XDG_SEAT=%s", xdg_seat);
522 session_set_env (SESSION (greeter_session), "XDG_SEAT", xdg_seat);
523 if (!SEAT_UNITY (seat)->priv->use_vt_switching)
525 gchar *value = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->vt);
526 l_debug (seat, "Setting XDG_VTNR=%s", value);
527 session_set_env (SESSION (greeter_session), "XDG_VTNR", value);
531 l_debug (seat, "Not setting XDG_VTNR");
533 return greeter_session;
537 seat_unity_create_session (Seat *seat)
540 const gchar *xdg_seat;
542 session = SEAT_CLASS (seat_unity_parent_class)->create_session (seat);
544 l_debug (seat, "Setting XDG_SEAT=%s", xdg_seat);
545 session_set_env (session, "XDG_SEAT", xdg_seat);
546 if (!SEAT_UNITY (seat)->priv->use_vt_switching)
548 gchar *value = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->vt);
549 l_debug (seat, "Setting XDG_VTNR=%s", value);
550 session_set_env (SESSION (session), "XDG_VTNR", value);
554 l_debug (seat, "Not setting XDG_VTNR");
560 seat_unity_set_active_session (Seat *seat, Session *session)
562 DisplayServer *display_server;
564 /* If no compositor, have to use VT switching */
565 if (SEAT_UNITY (seat)->priv->use_vt_switching)
567 gint vt = display_server_get_vt (session_get_display_server (session));
571 SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
575 if (session == SEAT_UNITY (seat)->priv->active_session)
577 SEAT_UNITY (seat)->priv->active_session = g_object_ref (session);
579 display_server = session_get_display_server (session);
580 if (SEAT_UNITY (seat)->priv->active_display_server != display_server)
582 const gchar *id = NULL;
584 SEAT_UNITY (seat)->priv->active_display_server = g_object_ref (display_server);
586 if (IS_X_SERVER_LOCAL (display_server))
587 id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
588 else if (IS_MIR_SERVER (display_server))
589 id = mir_server_get_id (MIR_SERVER (display_server));
593 l_debug (seat, "Switching to Mir session %s", id);
594 write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_ACTIVE_SESSION, (const guint8 *) id, strlen (id));
597 l_warning (seat, "Failed to work out session ID");
600 SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
604 seat_unity_get_active_session (Seat *seat)
606 if (SEAT_UNITY (seat)->priv->use_vt_switching)
610 vt = vt_get_active ();
614 for (link = seat_get_sessions (seat); link; link = link->next)
616 Session *session = link->data;
617 if (display_server_get_vt (session_get_display_server (session)) == vt)
624 return SEAT_UNITY (seat)->priv->active_session;
628 seat_unity_run_script (Seat *seat, DisplayServer *display_server, Process *script)
631 XServerLocal *x_server;
633 x_server = X_SERVER_LOCAL (display_server);
634 path = x_server_local_get_authority_file_path (x_server);
635 process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
636 process_set_env (script, "XAUTHORITY", path);
638 SEAT_CLASS (seat_unity_parent_class)->run_script (seat, display_server, script);
642 seat_unity_stop (Seat *seat)
644 /* Stop the compositor first */
645 if (process_get_is_running (SEAT_UNITY (seat)->priv->compositor_process))
647 process_stop (SEAT_UNITY (seat)->priv->compositor_process);
651 SEAT_CLASS (seat_unity_parent_class)->stop (seat);
655 seat_unity_init (SeatUnity *seat)
657 seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_UNITY_TYPE, SeatUnityPrivate);
659 seat->priv->compositor_process = process_new ();
663 seat_unity_finalize (GObject *object)
665 SeatUnity *seat = SEAT_UNITY (object);
667 if (seat->priv->vt >= 0)
668 vt_unref (seat->priv->vt);
669 g_free (seat->priv->log_file);
670 g_free (seat->priv->mir_socket_filename);
671 close (seat->priv->to_compositor_pipe[0]);
672 close (seat->priv->to_compositor_pipe[1]);
673 close (seat->priv->from_compositor_pipe[0]);
674 close (seat->priv->from_compositor_pipe[1]);
675 g_io_channel_unref (seat->priv->from_compositor_channel);
676 g_free (seat->priv->read_buffer);
677 g_object_unref (seat->priv->compositor_process);
678 if (seat->priv->active_session)
679 g_object_unref (seat->priv->active_session);
680 if (seat->priv->active_display_server)
681 g_object_unref (seat->priv->active_display_server);
683 G_OBJECT_CLASS (seat_unity_parent_class)->finalize (object);
687 seat_unity_class_init (SeatUnityClass *klass)
689 GObjectClass *object_class = G_OBJECT_CLASS (klass);
690 SeatClass *seat_class = SEAT_CLASS (klass);
692 object_class->finalize = seat_unity_finalize;
693 seat_class->get_start_local_sessions = seat_unity_get_start_local_sessions;
694 seat_class->setup = seat_unity_setup;
695 seat_class->start = seat_unity_start;
696 seat_class->create_display_server = seat_unity_create_display_server;
697 seat_class->create_greeter_session = seat_unity_create_greeter_session;
698 seat_class->create_session = seat_unity_create_session;
699 seat_class->set_active_session = seat_unity_set_active_session;
700 seat_class->get_active_session = seat_unity_get_active_session;
701 seat_class->run_script = seat_unity_run_script;
702 seat_class->stop = seat_unity_stop;
704 g_type_class_add_private (klass, sizeof (SeatUnityPrivate));