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;
78 G_DEFINE_TYPE (SeatUnity, seat_unity, SEAT_TYPE);
81 seat_unity_setup (Seat *seat)
83 seat_set_can_switch (seat, TRUE);
84 SEAT_CLASS (seat_unity_parent_class)->setup (seat);
88 compositor_stopped_cb (Process *process, SeatUnity *seat)
90 if (seat->priv->compositor_timeout != 0)
91 g_source_remove (seat->priv->compositor_timeout);
92 seat->priv->compositor_timeout = 0;
94 if (seat_get_is_stopping (SEAT (seat)))
96 SEAT_CLASS (seat_unity_parent_class)->stop (SEAT (seat));
100 /* If stopped before it was ready, then revert to VT mode */
101 if (!seat->priv->compositor_ready)
103 g_debug ("Compositor failed to start, switching to VT mode");
104 seat->priv->use_vt_switching = TRUE;
105 SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
109 g_debug ("Stopping Unity seat, compositor terminated");
111 if (seat->priv->stopping_plymouth)
113 g_debug ("Stopping Plymouth, compositor failed to start");
114 plymouth_quit (FALSE);
115 seat->priv->stopping_plymouth = FALSE;
118 seat_stop (SEAT (seat));
122 compositor_run_cb (Process *process, SeatUnity *seat)
126 /* Make input non-blocking */
127 fd = open ("/dev/null", O_RDONLY);
128 dup2 (fd, STDIN_FILENO);
131 /* Redirect output to logfile */
132 if (seat->priv->log_file)
136 fd = g_open (seat->priv->log_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
138 g_warning ("Failed to open log file %s: %s", seat->priv->log_file, g_strerror (errno));
141 dup2 (fd, STDOUT_FILENO);
142 dup2 (fd, STDERR_FILENO);
149 write_message (SeatUnity *seat, guint16 id, const guint8 *payload, guint16 payload_length)
152 gsize data_length = 4 + payload_length;
154 data = g_malloc (data_length);
157 data[2] = payload_length >> 8;
158 data[3] = payload_length & 0xFF;
159 memcpy (data + 4, payload, payload_length);
162 if (write (seat->priv->to_compositor_pipe[1], data, data_length) != data_length)
163 g_warning ("Failed to write to compositor: %s", strerror (errno));
167 read_cb (GIOChannel *source, GIOCondition condition, gpointer data)
169 SeatUnity *seat = data;
171 guint16 id, payload_length;
174 if (condition == G_IO_HUP)
176 g_debug ("Compositor closed communication channel");
180 /* Work out how much required for a message */
181 if (seat->priv->read_buffer_n_used < 4)
182 n_to_read = 4 - seat->priv->read_buffer_n_used;
185 payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
186 n_to_read = 4 + payload_length - seat->priv->read_buffer_n_used;
189 /* Read from compositor */
192 gsize n_total, n_read = 0;
194 GError *error = NULL;
196 n_total = seat->priv->read_buffer_n_used + n_to_read;
197 if (seat->priv->read_buffer_length < n_total)
198 seat->priv->read_buffer = g_realloc (seat->priv->read_buffer, n_total);
200 status = g_io_channel_read_chars (source,
201 seat->priv->read_buffer + seat->priv->read_buffer_n_used,
206 g_warning ("Failed to read from compositor: %s", error->message);
207 if (status != G_IO_STATUS_NORMAL)
209 g_clear_error (&error);
210 seat->priv->read_buffer_n_used += n_read;
214 if (seat->priv->read_buffer_n_used < 4)
216 id = seat->priv->read_buffer[0] << 8 | seat->priv->read_buffer[1];
217 payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
220 if (seat->priv->read_buffer_n_used < 4 + payload_length)
222 payload = seat->priv->read_buffer + 4;
226 case USC_MESSAGE_PING:
228 write_message (seat, USC_MESSAGE_PONG, NULL, 0);
230 case USC_MESSAGE_PONG:
233 case USC_MESSAGE_READY:
235 if (!seat->priv->compositor_ready)
237 seat->priv->compositor_ready = TRUE;
238 g_debug ("Compositor ready");
239 g_source_remove (seat->priv->compositor_timeout);
240 seat->priv->compositor_timeout = 0;
241 SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
244 case USC_MESSAGE_SESSION_CONNECTED:
245 g_debug ("SESSION CONNECTED");
248 g_warning ("Ingoring unknown message %d with %d octets from system compositor", id, payload_length);
253 seat->priv->read_buffer_n_used = 0;
259 get_absolute_command (const gchar *command)
262 gchar *absolute_binary, *absolute_command = NULL;
264 tokens = g_strsplit (command, " ", 2);
266 absolute_binary = g_find_program_in_path (tokens[0]);
270 absolute_command = g_strjoin (" ", absolute_binary, tokens[1], NULL);
272 absolute_command = g_strdup (absolute_binary);
273 g_free (absolute_binary);
276 absolute_command = g_strdup (command);
280 return absolute_command;
284 compositor_timeout_cb (gpointer data)
286 SeatUnity *seat = data;
288 /* Stop the compositor - it is not working */
289 process_stop (seat->priv->compositor_process);
295 seat_unity_start (Seat *seat)
297 const gchar *compositor_command;
298 gchar *command, *absolute_command, *dir;
302 /* Replace Plymouth if it is running */
303 if (plymouth_get_is_active () && plymouth_has_active_vt ())
305 gint active_vt = vt_get_active ();
306 if (active_vt >= vt_get_min ())
308 g_debug ("Compositor will replace Plymouth");
309 SEAT_UNITY (seat)->priv->vt = active_vt;
310 plymouth_deactivate ();
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 (SEAT_UNITY (seat)->priv->vt < 0)
316 SEAT_UNITY (seat)->priv->vt = vt_get_unused ();
317 if (SEAT_UNITY (seat)->priv->vt < 0)
319 g_debug ("Failed to get a VT to run on");
322 vt_ref (SEAT_UNITY (seat)->priv->vt);
324 /* Create pipes to talk to compositor */
325 if (pipe (SEAT_UNITY (seat)->priv->to_compositor_pipe) < 0 || pipe (SEAT_UNITY (seat)->priv->from_compositor_pipe) < 0)
327 g_debug ("Failed to create compositor pipes: %s", g_strerror (errno));
331 /* Don't allow the daemon end of the pipes to be accessed in the compositor */
332 fcntl (SEAT_UNITY (seat)->priv->to_compositor_pipe[1], F_SETFD, FD_CLOEXEC);
333 fcntl (SEAT_UNITY (seat)->priv->from_compositor_pipe[0], F_SETFD, FD_CLOEXEC);
335 /* Listen for messages from the compositor */
336 SEAT_UNITY (seat)->priv->from_compositor_channel = g_io_channel_unix_new (SEAT_UNITY (seat)->priv->from_compositor_pipe[0]);
337 g_io_add_watch (SEAT_UNITY (seat)->priv->from_compositor_channel, G_IO_IN | G_IO_HUP, read_cb, seat);
340 dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
341 SEAT_UNITY (seat)->priv->log_file = g_build_filename (dir, "unity-system-compositor.log", NULL);
342 g_debug ("Logging to %s", SEAT_UNITY (seat)->priv->log_file);
345 SEAT_UNITY (seat)->priv->mir_socket_filename = g_strdup ("/tmp/mir_socket"); // FIXME: Use this socket by default as XMir is hardcoded to this
346 timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
347 compositor_command = seat_get_string_property (seat, "unity-compositor-command");
348 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);
350 absolute_command = get_absolute_command (command);
353 /* Start the compositor */
354 process_set_command (SEAT_UNITY (seat)->priv->compositor_process, absolute_command);
355 g_free (absolute_command);
356 g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "stopped", G_CALLBACK (compositor_stopped_cb), seat);
357 g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "run", G_CALLBACK (compositor_run_cb), seat);
358 result = process_start (SEAT_UNITY (seat)->priv->compositor_process, FALSE);
360 /* Close compostor ends of the pipes */
361 close (SEAT_UNITY (seat)->priv->to_compositor_pipe[0]);
362 SEAT_UNITY (seat)->priv->to_compositor_pipe[0] = 0;
363 close (SEAT_UNITY (seat)->priv->from_compositor_pipe[1]);
364 SEAT_UNITY (seat)->priv->from_compositor_pipe[1] = 0;
369 /* Connect to the compositor */
370 timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
373 g_debug ("Waiting for system compositor for %ds", timeout);
374 SEAT_UNITY (seat)->priv->compositor_timeout = g_timeout_add (timeout * 1000, compositor_timeout_cb, seat);
379 static DisplayServer *
380 seat_unity_create_display_server (Seat *seat)
382 XServerLocal *xserver;
383 const gchar *command = NULL, *layout = NULL, *config_file = NULL, *xdmcp_manager = NULL, *key_name = NULL;
388 g_debug ("Starting X server on Unity compositor");
390 xserver = xserver_local_new ();
392 if (!SEAT_UNITY (seat)->priv->use_vt_switching)
394 id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
395 SEAT_UNITY (seat)->priv->next_id++;
396 xserver_local_set_mir_id (xserver, id);
397 xserver_local_set_mir_socket (xserver, SEAT_UNITY (seat)->priv->mir_socket_filename);
401 command = seat_get_string_property (seat, "xserver-command");
403 xserver_local_set_command (xserver, command);
405 layout = seat_get_string_property (seat, "xserver-layout");
407 xserver_local_set_layout (xserver, layout);
409 config_file = seat_get_string_property (seat, "xserver-config");
411 xserver_local_set_config (xserver, config_file);
413 allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
414 xserver_local_set_allow_tcp (xserver, allow_tcp);
416 xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
418 xserver_local_set_xdmcp_server (xserver, xdmcp_manager);
420 port = seat_get_integer_property (seat, "xdmcp-port");
422 xserver_local_set_xdmcp_port (xserver, port);
424 key_name = seat_get_string_property (seat, "xdmcp-key");
430 GError *error = NULL;
432 dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
433 path = g_build_filename (dir, "keys.conf", NULL);
436 keys = g_key_file_new ();
437 result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
439 g_debug ("Error getting key %s", error->message);
440 g_clear_error (&error);
446 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
447 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
449 g_debug ("Key %s not defined", key_name);
452 xserver_local_set_xdmcp_key (xserver, key);
457 g_key_file_free (keys);
460 return DISPLAY_SERVER (xserver);
464 seat_unity_create_greeter_session (Seat *seat)
466 XGreeter *greeter_session;
468 greeter_session = xgreeter_new ();
469 session_set_env (SESSION (greeter_session), "XDG_SEAT", "seat0");
471 return GREETER (greeter_session);
475 seat_unity_create_session (Seat *seat)
479 session = xsession_new ();
480 session_set_env (SESSION (session), "XDG_SEAT", "seat0");
482 return SESSION (session);
486 seat_unity_set_active_session (Seat *seat, Session *session)
488 XServerLocal *xserver;
491 /* If no compositor, have to use VT switching */
492 if (SEAT_UNITY (seat)->priv->use_vt_switching)
494 gint vt = display_server_get_vt (session_get_display_server (session));
498 SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
502 if (session == SEAT_UNITY (seat)->priv->active_session)
504 SEAT_UNITY (seat)->priv->active_session = session;
506 xserver = XSERVER_LOCAL (session_get_display_server (session));
507 id = xserver_local_get_mir_id (xserver);
509 g_debug ("Switching to Mir session %s", id);
510 write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_ACTIVE_SESSION, id, strlen (id));
512 SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
516 seat_unity_get_active_session (Seat *seat)
518 if (SEAT_UNITY (seat)->priv->use_vt_switching)
522 vt = vt_get_active ();
526 for (link = seat_get_sessions (seat); link; link = link->next)
528 Session *session = link->data;
529 if (display_server_get_vt (session_get_display_server (session)) == vt)
536 return SEAT_UNITY (seat)->priv->active_session;
540 seat_unity_run_script (Seat *seat, DisplayServer *display_server, Process *script)
543 XServerLocal *xserver;
545 xserver = XSERVER_LOCAL (display_server);
546 path = xserver_local_get_authority_file_path (xserver);
547 process_set_env (script, "DISPLAY", xserver_get_address (XSERVER (xserver)));
548 process_set_env (script, "XAUTHORITY", path);
550 SEAT_CLASS (seat_unity_parent_class)->run_script (seat, display_server, script);
554 seat_unity_stop (Seat *seat)
556 /* Stop the compositor first */
557 if (process_get_is_running (SEAT_UNITY (seat)->priv->compositor_process))
559 process_stop (SEAT_UNITY (seat)->priv->compositor_process);
563 SEAT_CLASS (seat_unity_parent_class)->stop (seat);
567 seat_unity_init (SeatUnity *seat)
569 seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_UNITY_TYPE, SeatUnityPrivate);
571 seat->priv->compositor_process = process_new ();
575 seat_unity_finalize (GObject *object)
577 SeatUnity *seat = SEAT_UNITY (object);
579 if (seat->priv->vt >= 0)
580 vt_unref (seat->priv->vt);
581 g_free (seat->priv->log_file);
582 g_free (seat->priv->mir_socket_filename);
583 close (seat->priv->to_compositor_pipe[0]);
584 close (seat->priv->to_compositor_pipe[1]);
585 close (seat->priv->from_compositor_pipe[0]);
586 close (seat->priv->from_compositor_pipe[1]);
587 g_io_channel_unref (seat->priv->from_compositor_channel);
588 g_free (seat->priv->read_buffer);
589 g_object_unref (seat->priv->compositor_process);
591 G_OBJECT_CLASS (seat_unity_parent_class)->finalize (object);
595 seat_unity_class_init (SeatUnityClass *klass)
597 GObjectClass *object_class = G_OBJECT_CLASS (klass);
598 SeatClass *seat_class = SEAT_CLASS (klass);
600 object_class->finalize = seat_unity_finalize;
601 seat_class->setup = seat_unity_setup;
602 seat_class->start = seat_unity_start;
603 seat_class->create_display_server = seat_unity_create_display_server;
604 seat_class->create_greeter_session = seat_unity_create_greeter_session;
605 seat_class->create_session = seat_unity_create_session;
606 seat_class->set_active_session = seat_unity_set_active_session;
607 seat_class->get_active_session = seat_unity_get_active_session;
608 seat_class->run_script = seat_unity_run_script;
609 seat_class->stop = seat_unity_stop;
611 g_type_class_add_private (klass, sizeof (SeatUnityPrivate));