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
16 #include "seat-unity.h"
17 #include "configuration.h"
18 #include "xserver-local.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 */
38 /* TRUE if waiting for X server to start before stopping Plymouth */
39 gboolean stopping_plymouth;
44 /* Filename of Mir socket */
45 gchar *mir_socket_filename;
47 /* Pipes to communicate with compositor */
48 int to_compositor_pipe[2];
49 int from_compositor_pipe[2];
51 /* IO channel listening on for messages from the compositor */
52 GIOChannel *from_compositor_channel;
54 /* TRUE when the compositor indicates it is ready */
55 gboolean compositor_ready;
57 /* Buffer reading from channel */
59 gsize read_buffer_length;
60 gsize read_buffer_n_used;
62 /* Compositor process */
63 Process *compositor_process;
65 /* Timeout when waiting for compositor to start */
66 guint compositor_timeout;
68 /* Next Mir ID to use for a compositor client */
71 /* TRUE if using VT switching fallback */
72 gboolean use_vt_switching;
74 /* The currently visible session */
75 Session *active_session;
76 DisplayServer *active_display_server;
79 G_DEFINE_TYPE (SeatUnity, seat_unity, SEAT_TYPE);
82 seat_unity_setup (Seat *seat)
84 seat_set_can_switch (seat, TRUE);
85 SEAT_CLASS (seat_unity_parent_class)->setup (seat);
89 compositor_stopped_cb (Process *process, SeatUnity *seat)
91 if (seat->priv->compositor_timeout != 0)
92 g_source_remove (seat->priv->compositor_timeout);
93 seat->priv->compositor_timeout = 0;
95 /* Finished with the VT */
96 vt_unref (seat->priv->vt);
99 if (seat_get_is_stopping (SEAT (seat)))
101 SEAT_CLASS (seat_unity_parent_class)->stop (SEAT (seat));
105 /* If stopped before it was ready, then revert to VT mode */
106 if (!seat->priv->compositor_ready)
108 g_debug ("Compositor failed to start, switching to VT mode");
109 seat->priv->use_vt_switching = TRUE;
110 SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
114 g_debug ("Stopping Unity seat, compositor terminated");
116 if (seat->priv->stopping_plymouth)
118 g_debug ("Stopping Plymouth, compositor failed to start");
119 plymouth_quit (FALSE);
120 seat->priv->stopping_plymouth = FALSE;
123 seat_stop (SEAT (seat));
127 compositor_run_cb (Process *process, SeatUnity *seat)
131 /* Make input non-blocking */
132 fd = open ("/dev/null", O_RDONLY);
133 dup2 (fd, STDIN_FILENO);
136 /* Redirect output to logfile */
137 if (seat->priv->log_file)
141 fd = g_open (seat->priv->log_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
143 g_warning ("Failed to open log file %s: %s", seat->priv->log_file, g_strerror (errno));
146 dup2 (fd, STDOUT_FILENO);
147 dup2 (fd, STDERR_FILENO);
152 if (seat->priv->stopping_plymouth)
154 seat->priv->stopping_plymouth = FALSE;
155 plymouth_quit (TRUE);
160 write_message (SeatUnity *seat, guint16 id, const guint8 *payload, guint16 payload_length)
163 gsize data_length = 4 + payload_length;
165 data = g_malloc (data_length);
168 data[2] = payload_length >> 8;
169 data[3] = payload_length & 0xFF;
170 memcpy (data + 4, payload, payload_length);
173 if (write (seat->priv->to_compositor_pipe[1], data, data_length) != data_length)
174 g_warning ("Failed to write to compositor: %s", strerror (errno));
178 read_cb (GIOChannel *source, GIOCondition condition, gpointer data)
180 SeatUnity *seat = data;
182 guint16 id, payload_length;
185 if (condition == G_IO_HUP)
187 g_debug ("Compositor closed communication channel");
191 /* Work out how much required for a message */
192 if (seat->priv->read_buffer_n_used < 4)
193 n_to_read = 4 - seat->priv->read_buffer_n_used;
196 payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
197 n_to_read = 4 + payload_length - seat->priv->read_buffer_n_used;
200 /* Read from compositor */
203 gsize n_total, n_read = 0;
205 GError *error = NULL;
207 n_total = seat->priv->read_buffer_n_used + n_to_read;
208 if (seat->priv->read_buffer_length < n_total)
209 seat->priv->read_buffer = g_realloc (seat->priv->read_buffer, n_total);
211 status = g_io_channel_read_chars (source,
212 seat->priv->read_buffer + seat->priv->read_buffer_n_used,
217 g_warning ("Failed to read from compositor: %s", error->message);
218 if (status != G_IO_STATUS_NORMAL)
220 g_clear_error (&error);
221 seat->priv->read_buffer_n_used += n_read;
225 if (seat->priv->read_buffer_n_used < 4)
227 id = seat->priv->read_buffer[0] << 8 | seat->priv->read_buffer[1];
228 payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
231 if (seat->priv->read_buffer_n_used < 4 + payload_length)
233 payload = seat->priv->read_buffer + 4;
237 case USC_MESSAGE_PING:
239 write_message (seat, USC_MESSAGE_PONG, NULL, 0);
241 case USC_MESSAGE_PONG:
244 case USC_MESSAGE_READY:
246 if (!seat->priv->compositor_ready)
248 seat->priv->compositor_ready = TRUE;
249 g_debug ("Compositor ready");
250 g_source_remove (seat->priv->compositor_timeout);
251 seat->priv->compositor_timeout = 0;
252 SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
255 case USC_MESSAGE_SESSION_CONNECTED:
256 g_debug ("SESSION CONNECTED");
259 g_warning ("Ingoring unknown message %d with %d octets from system compositor", id, payload_length);
264 seat->priv->read_buffer_n_used = 0;
270 get_absolute_command (const gchar *command)
273 gchar *absolute_binary, *absolute_command = NULL;
275 tokens = g_strsplit (command, " ", 2);
277 absolute_binary = g_find_program_in_path (tokens[0]);
281 absolute_command = g_strjoin (" ", absolute_binary, tokens[1], NULL);
283 absolute_command = g_strdup (absolute_binary);
284 g_free (absolute_binary);
287 absolute_command = g_strdup (command);
291 return absolute_command;
295 compositor_timeout_cb (gpointer data)
297 SeatUnity *seat = data;
299 /* Stop the compositor - it is not working */
300 process_stop (seat->priv->compositor_process);
306 seat_unity_start (Seat *seat)
308 const gchar *compositor_command;
309 gchar *command, *absolute_command, *dir;
313 /* Replace Plymouth if it is running */
314 if (plymouth_get_is_active () && plymouth_has_active_vt ())
316 gint active_vt = vt_get_active ();
317 if (active_vt >= vt_get_min ())
319 g_debug ("Compositor will replace Plymouth");
320 SEAT_UNITY (seat)->priv->stopping_plymouth = TRUE;
321 SEAT_UNITY (seat)->priv->vt = active_vt;
322 plymouth_deactivate ();
325 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 ());
327 if (SEAT_UNITY (seat)->priv->vt < 0)
328 SEAT_UNITY (seat)->priv->vt = vt_get_unused ();
329 if (SEAT_UNITY (seat)->priv->vt < 0)
331 g_debug ("Failed to get a VT to run on");
334 vt_ref (SEAT_UNITY (seat)->priv->vt);
336 /* Create pipes to talk to compositor */
337 if (pipe (SEAT_UNITY (seat)->priv->to_compositor_pipe) < 0 || pipe (SEAT_UNITY (seat)->priv->from_compositor_pipe) < 0)
339 g_debug ("Failed to create compositor pipes: %s", g_strerror (errno));
343 /* Don't allow the daemon end of the pipes to be accessed in the compositor */
344 fcntl (SEAT_UNITY (seat)->priv->to_compositor_pipe[1], F_SETFD, FD_CLOEXEC);
345 fcntl (SEAT_UNITY (seat)->priv->from_compositor_pipe[0], F_SETFD, FD_CLOEXEC);
347 /* Listen for messages from the compositor */
348 SEAT_UNITY (seat)->priv->from_compositor_channel = g_io_channel_unix_new (SEAT_UNITY (seat)->priv->from_compositor_pipe[0]);
349 g_io_add_watch (SEAT_UNITY (seat)->priv->from_compositor_channel, G_IO_IN | G_IO_HUP, read_cb, seat);
352 dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
353 SEAT_UNITY (seat)->priv->log_file = g_build_filename (dir, "unity-system-compositor.log", NULL);
354 g_debug ("Logging to %s", SEAT_UNITY (seat)->priv->log_file);
357 SEAT_UNITY (seat)->priv->mir_socket_filename = g_strdup ("/tmp/mir_socket"); // FIXME: Use this socket by default as XMir is hardcoded to this
358 timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
359 compositor_command = seat_get_string_property (seat, "unity-compositor-command");
360 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);
362 absolute_command = get_absolute_command (command);
365 /* Start the compositor */
366 process_set_command (SEAT_UNITY (seat)->priv->compositor_process, absolute_command);
367 g_free (absolute_command);
368 g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "stopped", G_CALLBACK (compositor_stopped_cb), seat);
369 g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "run", G_CALLBACK (compositor_run_cb), seat);
370 result = process_start (SEAT_UNITY (seat)->priv->compositor_process, FALSE);
372 /* Close compostor ends of the pipes */
373 close (SEAT_UNITY (seat)->priv->to_compositor_pipe[0]);
374 SEAT_UNITY (seat)->priv->to_compositor_pipe[0] = 0;
375 close (SEAT_UNITY (seat)->priv->from_compositor_pipe[1]);
376 SEAT_UNITY (seat)->priv->from_compositor_pipe[1] = 0;
381 /* Connect to the compositor */
382 timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
385 g_debug ("Waiting for system compositor for %ds", timeout);
386 SEAT_UNITY (seat)->priv->compositor_timeout = g_timeout_add (timeout * 1000, compositor_timeout_cb, seat);
391 static DisplayServer *
392 seat_unity_create_display_server (Seat *seat)
394 XServerLocal *xserver;
395 const gchar *command = NULL, *layout = NULL, *config_file = NULL, *xdmcp_manager = NULL, *key_name = NULL;
400 g_debug ("Starting X server on Unity compositor");
402 xserver = xserver_local_new ();
404 if (!SEAT_UNITY (seat)->priv->use_vt_switching)
406 id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
407 SEAT_UNITY (seat)->priv->next_id++;
408 xserver_local_set_mir_id (xserver, id);
409 xserver_local_set_mir_socket (xserver, SEAT_UNITY (seat)->priv->mir_socket_filename);
413 command = seat_get_string_property (seat, "xserver-command");
415 xserver_local_set_command (xserver, command);
417 layout = seat_get_string_property (seat, "xserver-layout");
419 xserver_local_set_layout (xserver, layout);
421 config_file = seat_get_string_property (seat, "xserver-config");
423 xserver_local_set_config (xserver, config_file);
425 allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
426 xserver_local_set_allow_tcp (xserver, allow_tcp);
428 xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
430 xserver_local_set_xdmcp_server (xserver, xdmcp_manager);
432 port = seat_get_integer_property (seat, "xdmcp-port");
434 xserver_local_set_xdmcp_port (xserver, 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 g_debug ("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 g_debug ("Key %s not defined", key_name);
464 xserver_local_set_xdmcp_key (xserver, key);
469 g_key_file_free (keys);
472 return DISPLAY_SERVER (xserver);
476 seat_unity_create_greeter_session (Seat *seat)
478 XGreeter *greeter_session;
480 greeter_session = xgreeter_new ();
481 session_set_env (SESSION (greeter_session), "XDG_SEAT", "seat0");
483 return GREETER (greeter_session);
487 seat_unity_create_session (Seat *seat)
491 session = xsession_new ();
492 session_set_env (SESSION (session), "XDG_SEAT", "seat0");
494 return SESSION (session);
498 seat_unity_set_active_session (Seat *seat, Session *session)
500 DisplayServer *display_server;
502 /* If no compositor, have to use VT switching */
503 if (SEAT_UNITY (seat)->priv->use_vt_switching)
505 gint vt = display_server_get_vt (session_get_display_server (session));
509 SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
513 if (session == SEAT_UNITY (seat)->priv->active_session)
515 SEAT_UNITY (seat)->priv->active_session = g_object_ref (session);
517 display_server = session_get_display_server (session);
518 if (SEAT_UNITY (seat)->priv->active_display_server != display_server)
522 SEAT_UNITY (seat)->priv->active_display_server = g_object_ref (display_server);
523 id = xserver_local_get_mir_id (XSERVER_LOCAL (display_server));
525 g_debug ("Switching to Mir session %s", id);
526 write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_ACTIVE_SESSION, id, strlen (id));
529 SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
533 seat_unity_get_active_session (Seat *seat)
535 if (SEAT_UNITY (seat)->priv->use_vt_switching)
539 vt = vt_get_active ();
543 for (link = seat_get_sessions (seat); link; link = link->next)
545 Session *session = link->data;
546 if (display_server_get_vt (session_get_display_server (session)) == vt)
553 return SEAT_UNITY (seat)->priv->active_session;
557 seat_unity_run_script (Seat *seat, DisplayServer *display_server, Process *script)
560 XServerLocal *xserver;
562 xserver = XSERVER_LOCAL (display_server);
563 path = xserver_local_get_authority_file_path (xserver);
564 process_set_env (script, "DISPLAY", xserver_get_address (XSERVER (xserver)));
565 process_set_env (script, "XAUTHORITY", path);
567 SEAT_CLASS (seat_unity_parent_class)->run_script (seat, display_server, script);
571 seat_unity_stop (Seat *seat)
573 /* Stop the compositor first */
574 if (process_get_is_running (SEAT_UNITY (seat)->priv->compositor_process))
576 process_stop (SEAT_UNITY (seat)->priv->compositor_process);
580 SEAT_CLASS (seat_unity_parent_class)->stop (seat);
584 seat_unity_init (SeatUnity *seat)
586 seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_UNITY_TYPE, SeatUnityPrivate);
588 seat->priv->compositor_process = process_new ();
592 seat_unity_finalize (GObject *object)
594 SeatUnity *seat = SEAT_UNITY (object);
596 if (seat->priv->vt >= 0)
597 vt_unref (seat->priv->vt);
598 g_free (seat->priv->log_file);
599 g_free (seat->priv->mir_socket_filename);
600 close (seat->priv->to_compositor_pipe[0]);
601 close (seat->priv->to_compositor_pipe[1]);
602 close (seat->priv->from_compositor_pipe[0]);
603 close (seat->priv->from_compositor_pipe[1]);
604 g_io_channel_unref (seat->priv->from_compositor_channel);
605 g_free (seat->priv->read_buffer);
606 g_object_unref (seat->priv->compositor_process);
607 if (seat->priv->active_session)
608 g_object_unref (seat->priv->active_session);
609 if (seat->priv->active_display_server)
610 g_object_unref (seat->priv->active_display_server);
612 G_OBJECT_CLASS (seat_unity_parent_class)->finalize (object);
616 seat_unity_class_init (SeatUnityClass *klass)
618 GObjectClass *object_class = G_OBJECT_CLASS (klass);
619 SeatClass *seat_class = SEAT_CLASS (klass);
621 object_class->finalize = seat_unity_finalize;
622 seat_class->setup = seat_unity_setup;
623 seat_class->start = seat_unity_start;
624 seat_class->create_display_server = seat_unity_create_display_server;
625 seat_class->create_greeter_session = seat_unity_create_greeter_session;
626 seat_class->create_session = seat_unity_create_session;
627 seat_class->set_active_session = seat_unity_set_active_session;
628 seat_class->get_active_session = seat_unity_get_active_session;
629 seat_class->run_script = seat_unity_run_script;
630 seat_class->stop = seat_unity_stop;
632 g_type_class_add_private (klass, sizeof (SeatUnityPrivate));