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"
27 USC_MESSAGE_READY = 2,
28 USC_MESSAGE_SESSION_CONNECTED = 3,
29 USC_MESSAGE_SET_ACTIVE_SESSION = 4
32 struct SeatUnityPrivate
34 /* VT we are running on */
37 /* TRUE if waiting for X server to start before stopping Plymouth */
38 gboolean stopping_plymouth;
43 /* Filename of Mir socket */
44 gchar *mir_socket_filename;
46 /* Pipes to communicate with compositor */
47 int to_compositor_pipe[2];
48 int from_compositor_pipe[2];
50 /* IO channel listening on for messages from the compositor */
51 GIOChannel *from_compositor_channel;
53 /* TRUE when the compositor indicates it is ready */
54 gboolean compositor_ready;
56 /* Buffer reading from channel */
58 gsize read_buffer_length;
59 gsize read_buffer_n_used;
61 /* Compositor process */
62 Process *compositor_process;
64 /* Timeout when waiting for compositor to start */
65 guint compositor_timeout;
67 /* Next Mir ID to use for a compositor client */
70 /* TRUE if using VT switching fallback */
71 gboolean use_vt_switching;
73 /* The currently visible display */
74 Display *active_display;
77 G_DEFINE_TYPE (SeatUnity, seat_unity, SEAT_TYPE);
80 seat_unity_setup (Seat *seat)
82 seat_set_can_switch (seat, TRUE);
83 SEAT_CLASS (seat_unity_parent_class)->setup (seat);
87 compositor_stopped_cb (Process *process, SeatUnity *seat)
89 if (seat->priv->compositor_timeout != 0)
90 g_source_remove (seat->priv->compositor_timeout);
91 seat->priv->compositor_timeout = 0;
93 if (seat_get_is_stopping (SEAT (seat)))
95 SEAT_CLASS (seat_unity_parent_class)->stop (SEAT (seat));
99 /* If stopped before it was ready, then revert to VT mode */
100 if (!seat->priv->compositor_ready)
102 g_debug ("Compositor failed to start, switching to VT mode");
103 seat->priv->use_vt_switching = TRUE;
104 SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
108 g_debug ("Stopping Unity seat, compositor terminated");
110 if (seat->priv->stopping_plymouth)
112 g_debug ("Stopping Plymouth, compositor failed to start");
113 plymouth_quit (FALSE);
114 seat->priv->stopping_plymouth = FALSE;
117 seat_stop (SEAT (seat));
121 compositor_run_cb (Process *process, SeatUnity *seat)
125 /* Make input non-blocking */
126 fd = open ("/dev/null", O_RDONLY);
127 dup2 (fd, STDIN_FILENO);
130 /* Redirect output to logfile */
131 if (seat->priv->log_file)
135 fd = g_open (seat->priv->log_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
137 g_warning ("Failed to open log file %s: %s", seat->priv->log_file, g_strerror (errno));
140 dup2 (fd, STDOUT_FILENO);
141 dup2 (fd, STDERR_FILENO);
148 write_message (SeatUnity *seat, guint16 id, const guint8 *payload, guint16 payload_length)
151 gsize data_length = 4 + payload_length;
153 data = g_malloc (data_length);
156 data[2] = payload_length >> 8;
157 data[3] = payload_length & 0xFF;
158 memcpy (data + 4, payload, payload_length);
161 if (write (seat->priv->to_compositor_pipe[1], data, data_length) != data_length)
162 g_warning ("Failed to write to compositor: %s", strerror (errno));
166 read_cb (GIOChannel *source, GIOCondition condition, gpointer data)
168 SeatUnity *seat = data;
170 guint16 id, payload_length;
173 if (condition == G_IO_HUP)
175 g_debug ("Compositor closed communication channel");
179 /* Work out how much required for a message */
180 if (seat->priv->read_buffer_n_used < 4)
181 n_to_read = 4 - seat->priv->read_buffer_n_used;
184 payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
185 n_to_read = 4 + payload_length - seat->priv->read_buffer_n_used;
188 /* Read from compositor */
191 gsize n_total, n_read = 0;
193 GError *error = NULL;
195 n_total = seat->priv->read_buffer_n_used + n_to_read;
196 if (seat->priv->read_buffer_length < n_total)
197 seat->priv->read_buffer = g_realloc (seat->priv->read_buffer, n_total);
199 status = g_io_channel_read_chars (source,
200 seat->priv->read_buffer + seat->priv->read_buffer_n_used,
205 g_warning ("Failed to read from compositor: %s", error->message);
206 if (status != G_IO_STATUS_NORMAL)
208 g_clear_error (&error);
209 seat->priv->read_buffer_n_used += n_read;
213 if (seat->priv->read_buffer_n_used < 4)
215 id = seat->priv->read_buffer[0] << 8 | seat->priv->read_buffer[1];
216 payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
219 if (seat->priv->read_buffer_n_used < 4 + payload_length)
221 payload = seat->priv->read_buffer + 4;
225 case USC_MESSAGE_PING:
227 write_message (seat, USC_MESSAGE_PONG, NULL, 0);
229 case USC_MESSAGE_PONG:
232 case USC_MESSAGE_READY:
234 if (!seat->priv->compositor_ready)
236 seat->priv->compositor_ready = TRUE;
237 g_debug ("Compositor ready");
238 g_source_remove (seat->priv->compositor_timeout);
239 seat->priv->compositor_timeout = 0;
240 SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
243 case USC_MESSAGE_SESSION_CONNECTED:
244 g_debug ("SESSION CONNECTED");
247 g_warning ("Ingoring unknown message %d with %d octets from system compositor", id, payload_length);
252 seat->priv->read_buffer_n_used = 0;
258 get_absolute_command (const gchar *command)
261 gchar *absolute_binary, *absolute_command = NULL;
263 tokens = g_strsplit (command, " ", 2);
265 absolute_binary = g_find_program_in_path (tokens[0]);
269 absolute_command = g_strjoin (" ", absolute_binary, tokens[1], NULL);
271 absolute_command = g_strdup (absolute_binary);
272 g_free (absolute_binary);
275 absolute_command = g_strdup (command);
279 return absolute_command;
283 compositor_timeout_cb (gpointer data)
285 SeatUnity *seat = data;
287 /* Stop the compositor - it is not working */
288 process_stop (seat->priv->compositor_process);
294 seat_unity_start (Seat *seat)
296 const gchar *compositor_command;
297 gchar *command, *absolute_command, *dir;
301 /* Replace Plymouth if it is running */
302 if (plymouth_get_is_active () && plymouth_has_active_vt ())
304 gint active_vt = vt_get_active ();
305 if (active_vt >= vt_get_min ())
307 g_debug ("Compositor will replace Plymouth");
308 SEAT_UNITY (seat)->priv->vt = active_vt;
309 plymouth_deactivate ();
312 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 ());
314 if (SEAT_UNITY (seat)->priv->vt < 0)
315 SEAT_UNITY (seat)->priv->vt = vt_get_unused ();
316 if (SEAT_UNITY (seat)->priv->vt < 0)
318 g_debug ("Failed to get a VT to run on");
321 vt_ref (SEAT_UNITY (seat)->priv->vt);
323 /* Create pipes to talk to compositor */
324 if (pipe (SEAT_UNITY (seat)->priv->to_compositor_pipe) < 0 || pipe (SEAT_UNITY (seat)->priv->from_compositor_pipe) < 0)
326 g_debug ("Failed to create compositor pipes: %s", g_strerror (errno));
330 /* Don't allow the daemon end of the pipes to be accessed in the compositor */
331 fcntl (SEAT_UNITY (seat)->priv->to_compositor_pipe[1], F_SETFD, FD_CLOEXEC);
332 fcntl (SEAT_UNITY (seat)->priv->from_compositor_pipe[0], F_SETFD, FD_CLOEXEC);
334 /* Listen for messages from the compositor */
335 SEAT_UNITY (seat)->priv->from_compositor_channel = g_io_channel_unix_new (SEAT_UNITY (seat)->priv->from_compositor_pipe[0]);
336 g_io_add_watch (SEAT_UNITY (seat)->priv->from_compositor_channel, G_IO_IN | G_IO_HUP, read_cb, seat);
339 dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
340 SEAT_UNITY (seat)->priv->log_file = g_build_filename (dir, "unity-system-compositor.log", NULL);
341 g_debug ("Logging to %s", SEAT_UNITY (seat)->priv->log_file);
344 SEAT_UNITY (seat)->priv->mir_socket_filename = g_strdup ("/tmp/mir_socket"); // FIXME: Use this socket by default as XMir is hardcoded to this
345 timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
346 compositor_command = seat_get_string_property (seat, "unity-compositor-command");
347 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);
349 absolute_command = get_absolute_command (command);
352 /* Start the compositor */
353 process_set_command (SEAT_UNITY (seat)->priv->compositor_process, absolute_command);
354 g_free (absolute_command);
355 g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "stopped", G_CALLBACK (compositor_stopped_cb), seat);
356 g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "run", G_CALLBACK (compositor_run_cb), seat);
357 result = process_start (SEAT_UNITY (seat)->priv->compositor_process, FALSE);
359 /* Close compostor ends of the pipes */
360 close (SEAT_UNITY (seat)->priv->to_compositor_pipe[0]);
361 SEAT_UNITY (seat)->priv->to_compositor_pipe[0] = 0;
362 close (SEAT_UNITY (seat)->priv->from_compositor_pipe[1]);
363 SEAT_UNITY (seat)->priv->from_compositor_pipe[1] = 0;
368 /* Connect to the compositor */
369 timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
372 g_debug ("Waiting for system compositor for %ds", timeout);
373 SEAT_UNITY (seat)->priv->compositor_timeout = g_timeout_add (timeout * 1000, compositor_timeout_cb, seat);
378 static DisplayServer *
379 seat_unity_create_display_server (Seat *seat)
381 XServerLocal *xserver;
382 const gchar *command = NULL, *layout = NULL, *config_file = NULL, *xdmcp_manager = NULL, *key_name = NULL;
387 g_debug ("Starting X server on Unity compositor");
389 xserver = xserver_local_new ();
391 if (!SEAT_UNITY (seat)->priv->use_vt_switching)
393 id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
394 SEAT_UNITY (seat)->priv->next_id++;
395 xserver_local_set_mir_id (xserver, id);
396 xserver_local_set_mir_socket (xserver, SEAT_UNITY (seat)->priv->mir_socket_filename);
400 command = seat_get_string_property (seat, "xserver-command");
402 xserver_local_set_command (xserver, command);
404 layout = seat_get_string_property (seat, "xserver-layout");
406 xserver_local_set_layout (xserver, layout);
408 config_file = seat_get_string_property (seat, "xserver-config");
410 xserver_local_set_config (xserver, config_file);
412 allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
413 xserver_local_set_allow_tcp (xserver, allow_tcp);
415 xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
417 xserver_local_set_xdmcp_server (xserver, xdmcp_manager);
419 port = seat_get_integer_property (seat, "xdmcp-port");
421 xserver_local_set_xdmcp_port (xserver, port);
423 key_name = seat_get_string_property (seat, "xdmcp-key");
429 GError *error = NULL;
431 dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
432 path = g_build_filename (dir, "keys.conf", NULL);
435 keys = g_key_file_new ();
436 result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
438 g_debug ("Error getting key %s", error->message);
439 g_clear_error (&error);
445 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
446 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
448 g_debug ("Key %s not defined", key_name);
451 xserver_local_set_xdmcp_key (xserver, key);
456 g_key_file_free (keys);
459 return DISPLAY_SERVER (xserver);
463 seat_unity_create_session (Seat *seat, Display *display)
465 XServerLocal *xserver;
470 xserver = XSERVER_LOCAL (display_get_display_server (display));
472 if (SEAT_UNITY (seat)->priv->use_vt_switching)
473 vt_number = xserver_local_get_vt (xserver);
475 vt_number = SEAT_UNITY (seat)->priv->vt;
477 session = xsession_new (XSERVER (xserver));
478 t = g_strdup_printf ("/dev/tty%d", vt_number);
479 session_set_tty (SESSION (session), t);
482 /* Set variables for logind */
483 session_set_env (SESSION (session), "XDG_SEAT", "seat0");
484 t = g_strdup_printf ("%d", vt_number);
485 session_set_env (SESSION (session), "XDG_VTNR", t);
488 return SESSION (session);
492 seat_unity_set_active_display (Seat *seat, Display *display)
494 XServerLocal *xserver;
497 /* If no compositor, have to use VT switching */
498 if (SEAT_UNITY (seat)->priv->use_vt_switching)
500 gint vt = xserver_local_get_vt (XSERVER_LOCAL (display_get_display_server (display)));
504 SEAT_CLASS (seat_unity_parent_class)->set_active_display (seat, display);
508 if (display == SEAT_UNITY (seat)->priv->active_display)
510 SEAT_UNITY (seat)->priv->active_display = display;
512 xserver = XSERVER_LOCAL (display_get_display_server (display));
513 id = xserver_local_get_mir_id (xserver);
515 g_debug ("Switching to Mir session %s", id);
516 write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_ACTIVE_SESSION, id, strlen (id));
518 SEAT_CLASS (seat_unity_parent_class)->set_active_display (seat, display);
522 seat_unity_get_active_display (Seat *seat)
524 if (SEAT_UNITY (seat)->priv->use_vt_switching)
528 vt = vt_get_active ();
532 for (link = seat_get_displays (seat); link; link = link->next)
534 Display *display = link->data;
535 XServerLocal *xserver;
537 xserver = XSERVER_LOCAL (display_get_display_server (display));
538 if (xserver_local_get_vt (xserver) == vt)
545 return SEAT_UNITY (seat)->priv->active_display;
549 seat_unity_run_script (Seat *seat, Display *display, Process *script)
552 XServerLocal *xserver;
554 xserver = XSERVER_LOCAL (display_get_display_server (display));
555 path = xserver_local_get_authority_file_path (xserver);
556 process_set_env (script, "DISPLAY", xserver_get_address (XSERVER (xserver)));
557 process_set_env (script, "XAUTHORITY", path);
559 SEAT_CLASS (seat_unity_parent_class)->run_script (seat, display, script);
563 seat_unity_stop (Seat *seat)
565 /* Stop the compositor first */
566 if (process_get_is_running (SEAT_UNITY (seat)->priv->compositor_process))
568 process_stop (SEAT_UNITY (seat)->priv->compositor_process);
572 SEAT_CLASS (seat_unity_parent_class)->stop (seat);
576 seat_unity_display_removed (Seat *seat, Display *display)
578 if (seat_get_is_stopping (seat))
581 /* If this is the only display and it failed to start then stop this seat */
582 if (g_list_length (seat_get_displays (seat)) == 0 && !display_get_is_ready (display))
584 g_debug ("Stopping Unity seat, failed to start a display");
589 /* Show a new greeter */
590 if (display == seat_get_active_display (seat))
592 g_debug ("Active display stopped, switching to greeter");
593 seat_switch_to_greeter (seat);
598 seat_unity_init (SeatUnity *seat)
600 seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_UNITY_TYPE, SeatUnityPrivate);
602 seat->priv->compositor_process = process_new ();
606 seat_unity_finalize (GObject *object)
608 SeatUnity *seat = SEAT_UNITY (object);
610 if (seat->priv->vt >= 0)
611 vt_unref (seat->priv->vt);
612 g_free (seat->priv->log_file);
613 g_free (seat->priv->mir_socket_filename);
614 close (seat->priv->to_compositor_pipe[0]);
615 close (seat->priv->to_compositor_pipe[1]);
616 close (seat->priv->from_compositor_pipe[0]);
617 close (seat->priv->from_compositor_pipe[1]);
618 g_io_channel_unref (seat->priv->from_compositor_channel);
619 g_free (seat->priv->read_buffer);
620 g_object_unref (seat->priv->compositor_process);
622 G_OBJECT_CLASS (seat_unity_parent_class)->finalize (object);
626 seat_unity_class_init (SeatUnityClass *klass)
628 GObjectClass *object_class = G_OBJECT_CLASS (klass);
629 SeatClass *seat_class = SEAT_CLASS (klass);
631 object_class->finalize = seat_unity_finalize;
632 seat_class->setup = seat_unity_setup;
633 seat_class->start = seat_unity_start;
634 seat_class->create_display_server = seat_unity_create_display_server;
635 seat_class->create_session = seat_unity_create_session;
636 seat_class->set_active_display = seat_unity_set_active_display;
637 seat_class->get_active_display = seat_unity_get_active_display;
638 seat_class->run_script = seat_unity_run_script;
639 seat_class->stop = seat_unity_stop;
640 seat_class->display_removed = seat_unity_display_removed;
642 g_type_class_add_private (klass, sizeof (SeatUnityPrivate));