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,
31 USC_MESSAGE_SET_NEXT_SESSION = 5,
34 struct SeatUnityPrivate
36 /* VT we are running on */
42 /* Filename of Mir socket */
43 gchar *mir_socket_filename;
45 /* Pipes to communicate with compositor */
46 int to_compositor_pipe[2];
47 int from_compositor_pipe[2];
49 /* IO channel listening on for messages from the compositor */
50 GIOChannel *from_compositor_channel;
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 g_debug ("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 g_debug ("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)
138 fd = g_open (seat->priv->log_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
140 g_warning ("Failed to open log file %s: %s", seat->priv->log_file, g_strerror (errno));
143 dup2 (fd, STDOUT_FILENO);
144 dup2 (fd, STDERR_FILENO);
151 write_message (SeatUnity *seat, guint16 id, const guint8 *payload, guint16 payload_length)
154 gsize data_length = 4 + payload_length;
156 data = g_malloc (data_length);
159 data[2] = payload_length >> 8;
160 data[3] = payload_length & 0xFF;
161 memcpy (data + 4, payload, payload_length);
164 if (write (seat->priv->to_compositor_pipe[1], data, data_length) != data_length)
165 g_warning ("Failed to write to compositor: %s", strerror (errno));
169 read_cb (GIOChannel *source, GIOCondition condition, gpointer data)
171 SeatUnity *seat = data;
173 guint16 id, payload_length;
176 if (condition == G_IO_HUP)
178 g_debug ("Compositor closed communication channel");
182 /* Work out how much required for a message */
183 if (seat->priv->read_buffer_n_used < 4)
184 n_to_read = 4 - seat->priv->read_buffer_n_used;
187 payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
188 n_to_read = 4 + payload_length - seat->priv->read_buffer_n_used;
191 /* Read from compositor */
194 gsize n_total, n_read = 0;
196 GError *error = NULL;
198 n_total = seat->priv->read_buffer_n_used + n_to_read;
199 if (seat->priv->read_buffer_length < n_total)
200 seat->priv->read_buffer = g_realloc (seat->priv->read_buffer, n_total);
202 status = g_io_channel_read_chars (source,
203 (gchar *)seat->priv->read_buffer + seat->priv->read_buffer_n_used,
208 g_warning ("Failed to read from compositor: %s", error->message);
209 if (status != G_IO_STATUS_NORMAL)
211 g_clear_error (&error);
212 seat->priv->read_buffer_n_used += n_read;
216 if (seat->priv->read_buffer_n_used < 4)
218 id = seat->priv->read_buffer[0] << 8 | seat->priv->read_buffer[1];
219 payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
222 if (seat->priv->read_buffer_n_used < 4 + payload_length)
224 /*payload = seat->priv->read_buffer + 4;*/
228 case USC_MESSAGE_PING:
230 write_message (seat, USC_MESSAGE_PONG, NULL, 0);
232 case USC_MESSAGE_PONG:
235 case USC_MESSAGE_READY:
237 if (!seat->priv->compositor_ready)
239 seat->priv->compositor_ready = TRUE;
240 g_debug ("Compositor ready");
241 g_source_remove (seat->priv->compositor_timeout);
242 seat->priv->compositor_timeout = 0;
243 SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
246 case USC_MESSAGE_SESSION_CONNECTED:
247 g_debug ("SESSION CONNECTED");
250 g_warning ("Ingoring unknown message %d with %d octets from system compositor", id, payload_length);
255 seat->priv->read_buffer_n_used = 0;
261 get_absolute_command (const gchar *command)
264 gchar *absolute_binary, *absolute_command = NULL;
266 tokens = g_strsplit (command, " ", 2);
268 absolute_binary = g_find_program_in_path (tokens[0]);
272 absolute_command = g_strjoin (" ", absolute_binary, tokens[1], NULL);
274 absolute_command = g_strdup (absolute_binary);
275 g_free (absolute_binary);
278 absolute_command = g_strdup (command);
282 return absolute_command;
286 compositor_timeout_cb (gpointer data)
288 SeatUnity *seat = data;
290 /* Stop the compositor - it is not working */
291 process_stop (seat->priv->compositor_process);
297 seat_unity_start (Seat *seat)
299 const gchar *compositor_command;
300 gchar *command, *absolute_command, *dir;
304 /* Replace Plymouth if it is running */
305 if (plymouth_get_is_active () && plymouth_has_active_vt ())
307 gint active_vt = vt_get_active ();
308 if (active_vt >= vt_get_min ())
310 SEAT_UNITY (seat)->priv->vt = active_vt;
311 plymouth_quit (TRUE);
314 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 ());
316 if (plymouth_get_is_active ())
317 plymouth_quit (FALSE);
318 if (SEAT_UNITY (seat)->priv->vt < 0)
319 SEAT_UNITY (seat)->priv->vt = vt_get_unused ();
320 if (SEAT_UNITY (seat)->priv->vt < 0)
322 g_debug ("Failed to get a VT to run on");
325 vt_ref (SEAT_UNITY (seat)->priv->vt);
327 /* Create pipes to talk to compositor */
328 if (pipe (SEAT_UNITY (seat)->priv->to_compositor_pipe) < 0 || pipe (SEAT_UNITY (seat)->priv->from_compositor_pipe) < 0)
330 g_debug ("Failed to create compositor pipes: %s", g_strerror (errno));
334 /* Don't allow the daemon end of the pipes to be accessed in the compositor */
335 fcntl (SEAT_UNITY (seat)->priv->to_compositor_pipe[1], F_SETFD, FD_CLOEXEC);
336 fcntl (SEAT_UNITY (seat)->priv->from_compositor_pipe[0], F_SETFD, FD_CLOEXEC);
338 /* Listen for messages from the compositor */
339 SEAT_UNITY (seat)->priv->from_compositor_channel = g_io_channel_unix_new (SEAT_UNITY (seat)->priv->from_compositor_pipe[0]);
340 g_io_add_watch (SEAT_UNITY (seat)->priv->from_compositor_channel, G_IO_IN | G_IO_HUP, read_cb, seat);
343 dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
344 SEAT_UNITY (seat)->priv->log_file = g_build_filename (dir, "unity-system-compositor.log", NULL);
345 g_debug ("Logging to %s", SEAT_UNITY (seat)->priv->log_file);
348 SEAT_UNITY (seat)->priv->mir_socket_filename = g_strdup ("/tmp/mir_socket"); // FIXME: Use this socket by default as XMir is hardcoded to this
349 timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
350 compositor_command = seat_get_string_property (seat, "unity-compositor-command");
351 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);
353 absolute_command = get_absolute_command (command);
356 /* Start the compositor */
357 process_set_command (SEAT_UNITY (seat)->priv->compositor_process, absolute_command);
358 g_free (absolute_command);
359 g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "stopped", G_CALLBACK (compositor_stopped_cb), seat);
360 g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "run", G_CALLBACK (compositor_run_cb), seat);
361 result = process_start (SEAT_UNITY (seat)->priv->compositor_process, FALSE);
363 /* Close compostor ends of the pipes */
364 close (SEAT_UNITY (seat)->priv->to_compositor_pipe[0]);
365 SEAT_UNITY (seat)->priv->to_compositor_pipe[0] = 0;
366 close (SEAT_UNITY (seat)->priv->from_compositor_pipe[1]);
367 SEAT_UNITY (seat)->priv->from_compositor_pipe[1] = 0;
372 /* Connect to the compositor */
373 timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
376 g_debug ("Waiting for system compositor for %ds", timeout);
377 SEAT_UNITY (seat)->priv->compositor_timeout = g_timeout_add (timeout * 1000, compositor_timeout_cb, seat);
382 static DisplayServer *
383 create_x_server (Seat *seat)
385 XServerLocal *x_server;
386 const gchar *command = NULL, *layout = NULL, *config_file = NULL, *xdmcp_manager = NULL, *key_name = NULL;
390 g_debug ("Starting X server on Unity compositor");
392 x_server = x_server_local_new ();
394 command = seat_get_string_property (seat, "xserver-command");
396 x_server_local_set_command (x_server, command);
398 if (SEAT_UNITY (seat)->priv->use_vt_switching)
399 x_server_local_set_vt (x_server, vt_get_unused ());
404 id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
405 SEAT_UNITY (seat)->priv->next_id++;
406 x_server_local_set_mir_id (x_server, id);
407 x_server_local_set_mir_socket (x_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
411 layout = seat_get_string_property (seat, "xserver-layout");
413 x_server_local_set_layout (x_server, layout);
415 config_file = seat_get_string_property (seat, "xserver-config");
417 x_server_local_set_config (x_server, config_file);
419 allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
420 x_server_local_set_allow_tcp (x_server, allow_tcp);
422 xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
424 x_server_local_set_xdmcp_server (x_server, xdmcp_manager);
426 port = seat_get_integer_property (seat, "xdmcp-port");
428 x_server_local_set_xdmcp_port (x_server, port);
430 key_name = seat_get_string_property (seat, "xdmcp-key");
436 GError *error = NULL;
438 dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
439 path = g_build_filename (dir, "keys.conf", NULL);
442 keys = g_key_file_new ();
443 result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
445 g_debug ("Error getting key %s", error->message);
446 g_clear_error (&error);
452 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
453 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
455 g_debug ("Key %s not defined", key_name);
458 x_server_local_set_xdmcp_key (x_server, key);
463 g_key_file_free (keys);
466 return DISPLAY_SERVER (x_server);
469 static DisplayServer *
470 create_mir_server (Seat *seat)
472 MirServer *mir_server;
474 mir_server = mir_server_new ();
475 mir_server_set_parent_socket (mir_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
477 if (SEAT_UNITY (seat)->priv->use_vt_switching)
478 mir_server_set_vt (mir_server, vt_get_unused ());
483 id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
484 SEAT_UNITY (seat)->priv->next_id++;
485 mir_server_set_id (mir_server, id);
486 mir_server_set_parent_socket (mir_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
490 return DISPLAY_SERVER (mir_server);
493 static DisplayServer *
494 seat_unity_create_display_server (Seat *seat, const gchar *session_type)
496 if (strcmp (session_type, "x") == 0)
497 return create_x_server (seat);
498 else if (strcmp (session_type, "mir") == 0)
499 return create_mir_server (seat);
502 g_warning ("Can't create unsupported display server '%s'", session_type);
508 seat_unity_create_greeter_session (Seat *seat)
510 Greeter *greeter_session;
512 greeter_session = SEAT_CLASS (seat_unity_parent_class)->create_greeter_session (seat);
513 session_set_env (SESSION (greeter_session), "XDG_SEAT", "seat0");
514 if (!SEAT_UNITY (seat)->priv->use_vt_switching)
516 gchar *value = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->vt);
517 session_set_env (SESSION (greeter_session), "XDG_VTNR", value);
521 return greeter_session;
525 seat_unity_create_session (Seat *seat, Session *user_session)
529 session = SEAT_CLASS (seat_unity_parent_class)->create_session (seat, user_session);
530 session_set_env (session, "XDG_SEAT", "seat0");
531 if (!SEAT_UNITY (seat)->priv->use_vt_switching)
533 gchar *value = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->vt);
534 session_set_env (SESSION (session), "XDG_VTNR", value);
542 seat_unity_set_active_session (Seat *seat, Session *session)
544 DisplayServer *display_server;
546 /* If no compositor, have to use VT switching */
547 if (SEAT_UNITY (seat)->priv->use_vt_switching)
549 gint vt = display_server_get_vt (session_get_display_server (session));
553 SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
557 if (session == SEAT_UNITY (seat)->priv->active_session)
559 SEAT_UNITY (seat)->priv->active_session = g_object_ref (session);
561 display_server = session_get_display_server (session);
562 if (SEAT_UNITY (seat)->priv->active_display_server != display_server)
564 const gchar *id = NULL;
566 SEAT_UNITY (seat)->priv->active_display_server = g_object_ref (display_server);
568 if (IS_X_SERVER_LOCAL (display_server))
569 id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
570 else if (IS_MIR_SERVER (display_server))
571 id = mir_server_get_id (MIR_SERVER (display_server));
575 g_debug ("Switching to Mir session %s", id);
576 write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_ACTIVE_SESSION, (const guint8 *) id, strlen (id));
579 g_warning ("Failed to work out session ID");
582 SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
586 seat_unity_get_active_session (Seat *seat)
588 if (SEAT_UNITY (seat)->priv->use_vt_switching)
592 vt = vt_get_active ();
596 for (link = seat_get_sessions (seat); link; link = link->next)
598 Session *session = link->data;
599 if (display_server_get_vt (session_get_display_server (session)) == vt)
606 return SEAT_UNITY (seat)->priv->active_session;
610 seat_unity_set_next_session (Seat *seat, Session *session)
612 DisplayServer *display_server;
613 const gchar *id = NULL;
615 /* If no compositor, don't worry about it */
616 if (SEAT_UNITY (seat)->priv->use_vt_switching)
619 display_server = session_get_display_server (session);
620 if (IS_MIR_SERVER (display_server))
622 id = mir_server_get_id (MIR_SERVER (display_server));
626 g_debug ("Marking Mir session %s as the next session", id);
627 write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_NEXT_SESSION, (const guint8 *) id, strlen (id));
631 g_warning ("Failed to work out session ID to mark");
637 seat_unity_run_script (Seat *seat, DisplayServer *display_server, Process *script)
640 XServerLocal *x_server;
642 x_server = X_SERVER_LOCAL (display_server);
643 path = x_server_local_get_authority_file_path (x_server);
644 process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
645 process_set_env (script, "XAUTHORITY", path);
647 SEAT_CLASS (seat_unity_parent_class)->run_script (seat, display_server, script);
651 seat_unity_stop (Seat *seat)
653 /* Stop the compositor first */
654 if (process_get_is_running (SEAT_UNITY (seat)->priv->compositor_process))
656 process_stop (SEAT_UNITY (seat)->priv->compositor_process);
660 SEAT_CLASS (seat_unity_parent_class)->stop (seat);
664 seat_unity_init (SeatUnity *seat)
666 seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_UNITY_TYPE, SeatUnityPrivate);
668 seat->priv->compositor_process = process_new ();
672 seat_unity_finalize (GObject *object)
674 SeatUnity *seat = SEAT_UNITY (object);
676 if (seat->priv->vt >= 0)
677 vt_unref (seat->priv->vt);
678 g_free (seat->priv->log_file);
679 g_free (seat->priv->mir_socket_filename);
680 close (seat->priv->to_compositor_pipe[0]);
681 close (seat->priv->to_compositor_pipe[1]);
682 close (seat->priv->from_compositor_pipe[0]);
683 close (seat->priv->from_compositor_pipe[1]);
684 g_io_channel_unref (seat->priv->from_compositor_channel);
685 g_free (seat->priv->read_buffer);
686 g_object_unref (seat->priv->compositor_process);
687 if (seat->priv->active_session)
688 g_object_unref (seat->priv->active_session);
689 if (seat->priv->active_display_server)
690 g_object_unref (seat->priv->active_display_server);
692 G_OBJECT_CLASS (seat_unity_parent_class)->finalize (object);
696 seat_unity_class_init (SeatUnityClass *klass)
698 GObjectClass *object_class = G_OBJECT_CLASS (klass);
699 SeatClass *seat_class = SEAT_CLASS (klass);
701 object_class->finalize = seat_unity_finalize;
702 seat_class->get_start_local_sessions = seat_unity_get_start_local_sessions;
703 seat_class->setup = seat_unity_setup;
704 seat_class->start = seat_unity_start;
705 seat_class->create_display_server = seat_unity_create_display_server;
706 seat_class->create_greeter_session = seat_unity_create_greeter_session;
707 seat_class->create_session = seat_unity_create_session;
708 seat_class->set_active_session = seat_unity_set_active_session;
709 seat_class->get_active_session = seat_unity_get_active_session;
710 seat_class->set_next_session = seat_unity_set_next_session;
711 seat_class->run_script = seat_unity_run_script;
712 seat_class->stop = seat_unity_stop;
714 g_type_class_add_private (klass, sizeof (SeatUnityPrivate));