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 "xserver-local.h"
22 #include "mir-server.h"
30 USC_MESSAGE_READY = 2,
31 USC_MESSAGE_SESSION_CONNECTED = 3,
32 USC_MESSAGE_SET_ACTIVE_SESSION = 4
35 struct SeatUnityPrivate
37 /* VT we are running on */
40 /* TRUE if waiting for X server to start before stopping Plymouth */
41 gboolean stopping_plymouth;
46 /* Filename of Mir socket */
47 gchar *mir_socket_filename;
49 /* Pipes to communicate with compositor */
50 int to_compositor_pipe[2];
51 int from_compositor_pipe[2];
53 /* IO channel listening on for messages from the compositor */
54 GIOChannel *from_compositor_channel;
56 /* TRUE when the compositor indicates it is ready */
57 gboolean compositor_ready;
59 /* Buffer reading from channel */
61 gsize read_buffer_length;
62 gsize read_buffer_n_used;
64 /* Compositor process */
65 Process *compositor_process;
67 /* Timeout when waiting for compositor to start */
68 guint compositor_timeout;
70 /* Next Mir ID to use for a compositor client */
73 /* TRUE if using VT switching fallback */
74 gboolean use_vt_switching;
76 /* The currently visible session */
77 Session *active_session;
78 DisplayServer *active_display_server;
81 G_DEFINE_TYPE (SeatUnity, seat_unity, SEAT_TYPE);
84 seat_unity_setup (Seat *seat)
86 seat_set_can_switch (seat, TRUE);
87 SEAT_CLASS (seat_unity_parent_class)->setup (seat);
91 compositor_stopped_cb (Process *process, SeatUnity *seat)
93 if (seat->priv->compositor_timeout != 0)
94 g_source_remove (seat->priv->compositor_timeout);
95 seat->priv->compositor_timeout = 0;
97 /* Finished with the VT */
98 vt_unref (seat->priv->vt);
101 if (seat_get_is_stopping (SEAT (seat)))
103 SEAT_CLASS (seat_unity_parent_class)->stop (SEAT (seat));
107 /* If stopped before it was ready, then revert to VT mode */
108 if (!seat->priv->compositor_ready)
110 g_debug ("Compositor failed to start, switching to VT mode");
111 seat->priv->use_vt_switching = TRUE;
112 SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
116 g_debug ("Stopping Unity seat, compositor terminated");
118 if (seat->priv->stopping_plymouth)
120 g_debug ("Stopping Plymouth, compositor failed to start");
121 plymouth_quit (FALSE);
122 seat->priv->stopping_plymouth = FALSE;
125 seat_stop (SEAT (seat));
129 compositor_run_cb (Process *process, SeatUnity *seat)
133 /* Make input non-blocking */
134 fd = open ("/dev/null", O_RDONLY);
135 dup2 (fd, STDIN_FILENO);
138 /* Redirect output to logfile */
139 if (seat->priv->log_file)
143 fd = g_open (seat->priv->log_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
145 g_warning ("Failed to open log file %s: %s", seat->priv->log_file, g_strerror (errno));
148 dup2 (fd, STDOUT_FILENO);
149 dup2 (fd, STDERR_FILENO);
154 if (seat->priv->stopping_plymouth)
156 seat->priv->stopping_plymouth = FALSE;
157 plymouth_quit (TRUE);
162 write_message (SeatUnity *seat, guint16 id, const guint8 *payload, guint16 payload_length)
165 gsize data_length = 4 + payload_length;
167 data = g_malloc (data_length);
170 data[2] = payload_length >> 8;
171 data[3] = payload_length & 0xFF;
172 memcpy (data + 4, payload, payload_length);
175 if (write (seat->priv->to_compositor_pipe[1], data, data_length) != data_length)
176 g_warning ("Failed to write to compositor: %s", strerror (errno));
180 read_cb (GIOChannel *source, GIOCondition condition, gpointer data)
182 SeatUnity *seat = data;
184 guint16 id, payload_length;
187 if (condition == G_IO_HUP)
189 g_debug ("Compositor closed communication channel");
193 /* Work out how much required for a message */
194 if (seat->priv->read_buffer_n_used < 4)
195 n_to_read = 4 - seat->priv->read_buffer_n_used;
198 payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
199 n_to_read = 4 + payload_length - seat->priv->read_buffer_n_used;
202 /* Read from compositor */
205 gsize n_total, n_read = 0;
207 GError *error = NULL;
209 n_total = seat->priv->read_buffer_n_used + n_to_read;
210 if (seat->priv->read_buffer_length < n_total)
211 seat->priv->read_buffer = g_realloc (seat->priv->read_buffer, n_total);
213 status = g_io_channel_read_chars (source,
214 (gchar *)seat->priv->read_buffer + seat->priv->read_buffer_n_used,
219 g_warning ("Failed to read from compositor: %s", error->message);
220 if (status != G_IO_STATUS_NORMAL)
222 g_clear_error (&error);
223 seat->priv->read_buffer_n_used += n_read;
227 if (seat->priv->read_buffer_n_used < 4)
229 id = seat->priv->read_buffer[0] << 8 | seat->priv->read_buffer[1];
230 payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
233 if (seat->priv->read_buffer_n_used < 4 + payload_length)
235 /*payload = seat->priv->read_buffer + 4;*/
239 case USC_MESSAGE_PING:
241 write_message (seat, USC_MESSAGE_PONG, NULL, 0);
243 case USC_MESSAGE_PONG:
246 case USC_MESSAGE_READY:
248 if (!seat->priv->compositor_ready)
250 seat->priv->compositor_ready = TRUE;
251 g_debug ("Compositor ready");
252 g_source_remove (seat->priv->compositor_timeout);
253 seat->priv->compositor_timeout = 0;
254 SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
257 case USC_MESSAGE_SESSION_CONNECTED:
258 g_debug ("SESSION CONNECTED");
261 g_warning ("Ingoring unknown message %d with %d octets from system compositor", id, payload_length);
266 seat->priv->read_buffer_n_used = 0;
272 get_absolute_command (const gchar *command)
275 gchar *absolute_binary, *absolute_command = NULL;
277 tokens = g_strsplit (command, " ", 2);
279 absolute_binary = g_find_program_in_path (tokens[0]);
283 absolute_command = g_strjoin (" ", absolute_binary, tokens[1], NULL);
285 absolute_command = g_strdup (absolute_binary);
286 g_free (absolute_binary);
289 absolute_command = g_strdup (command);
293 return absolute_command;
297 compositor_timeout_cb (gpointer data)
299 SeatUnity *seat = data;
301 /* Stop the compositor - it is not working */
302 process_stop (seat->priv->compositor_process);
308 seat_unity_start (Seat *seat)
310 const gchar *compositor_command;
311 gchar *command, *absolute_command, *dir;
315 /* Replace Plymouth if it is running */
316 if (plymouth_get_is_active () && plymouth_has_active_vt ())
318 gint active_vt = vt_get_active ();
319 if (active_vt >= vt_get_min ())
321 g_debug ("Compositor will replace Plymouth");
322 SEAT_UNITY (seat)->priv->stopping_plymouth = TRUE;
323 SEAT_UNITY (seat)->priv->vt = active_vt;
324 plymouth_deactivate ();
327 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 ());
329 if (SEAT_UNITY (seat)->priv->vt < 0)
330 SEAT_UNITY (seat)->priv->vt = vt_get_unused ();
331 if (SEAT_UNITY (seat)->priv->vt < 0)
333 g_debug ("Failed to get a VT to run on");
336 vt_ref (SEAT_UNITY (seat)->priv->vt);
338 /* Create pipes to talk to compositor */
339 if (pipe (SEAT_UNITY (seat)->priv->to_compositor_pipe) < 0 || pipe (SEAT_UNITY (seat)->priv->from_compositor_pipe) < 0)
341 g_debug ("Failed to create compositor pipes: %s", g_strerror (errno));
345 /* Don't allow the daemon end of the pipes to be accessed in the compositor */
346 fcntl (SEAT_UNITY (seat)->priv->to_compositor_pipe[1], F_SETFD, FD_CLOEXEC);
347 fcntl (SEAT_UNITY (seat)->priv->from_compositor_pipe[0], F_SETFD, FD_CLOEXEC);
349 /* Listen for messages from the compositor */
350 SEAT_UNITY (seat)->priv->from_compositor_channel = g_io_channel_unix_new (SEAT_UNITY (seat)->priv->from_compositor_pipe[0]);
351 g_io_add_watch (SEAT_UNITY (seat)->priv->from_compositor_channel, G_IO_IN | G_IO_HUP, read_cb, seat);
354 dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
355 SEAT_UNITY (seat)->priv->log_file = g_build_filename (dir, "unity-system-compositor.log", NULL);
356 g_debug ("Logging to %s", SEAT_UNITY (seat)->priv->log_file);
359 SEAT_UNITY (seat)->priv->mir_socket_filename = g_strdup ("/tmp/mir_socket"); // FIXME: Use this socket by default as XMir is hardcoded to this
360 timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
361 compositor_command = seat_get_string_property (seat, "unity-compositor-command");
362 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);
364 absolute_command = get_absolute_command (command);
367 /* Start the compositor */
368 process_set_command (SEAT_UNITY (seat)->priv->compositor_process, absolute_command);
369 g_free (absolute_command);
370 g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "stopped", G_CALLBACK (compositor_stopped_cb), seat);
371 g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "run", G_CALLBACK (compositor_run_cb), seat);
372 result = process_start (SEAT_UNITY (seat)->priv->compositor_process, FALSE);
374 /* Close compostor ends of the pipes */
375 close (SEAT_UNITY (seat)->priv->to_compositor_pipe[0]);
376 SEAT_UNITY (seat)->priv->to_compositor_pipe[0] = 0;
377 close (SEAT_UNITY (seat)->priv->from_compositor_pipe[1]);
378 SEAT_UNITY (seat)->priv->from_compositor_pipe[1] = 0;
383 /* Connect to the compositor */
384 timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
387 g_debug ("Waiting for system compositor for %ds", timeout);
388 SEAT_UNITY (seat)->priv->compositor_timeout = g_timeout_add (timeout * 1000, compositor_timeout_cb, seat);
393 static DisplayServer *
394 create_x_server (Seat *seat)
396 XServerLocal *xserver;
397 const gchar *command = NULL, *layout = NULL, *config_file = NULL, *xdmcp_manager = NULL, *key_name = NULL;
402 g_debug ("Starting X server on Unity compositor");
404 xserver = xserver_local_new ();
406 if (!SEAT_UNITY (seat)->priv->use_vt_switching)
408 id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
409 SEAT_UNITY (seat)->priv->next_id++;
410 xserver_local_set_mir_id (xserver, id);
411 xserver_local_set_mir_socket (xserver, SEAT_UNITY (seat)->priv->mir_socket_filename);
415 command = seat_get_string_property (seat, "xserver-command");
417 xserver_local_set_command (xserver, command);
419 layout = seat_get_string_property (seat, "xserver-layout");
421 xserver_local_set_layout (xserver, layout);
423 config_file = seat_get_string_property (seat, "xserver-config");
425 xserver_local_set_config (xserver, config_file);
427 allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
428 xserver_local_set_allow_tcp (xserver, allow_tcp);
430 xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
432 xserver_local_set_xdmcp_server (xserver, xdmcp_manager);
434 port = seat_get_integer_property (seat, "xdmcp-port");
436 xserver_local_set_xdmcp_port (xserver, port);
438 key_name = seat_get_string_property (seat, "xdmcp-key");
444 GError *error = NULL;
446 dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
447 path = g_build_filename (dir, "keys.conf", NULL);
450 keys = g_key_file_new ();
451 result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
453 g_debug ("Error getting key %s", error->message);
454 g_clear_error (&error);
460 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
461 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
463 g_debug ("Key %s not defined", key_name);
466 xserver_local_set_xdmcp_key (xserver, key);
471 g_key_file_free (keys);
474 return DISPLAY_SERVER (xserver);
477 static DisplayServer *
478 seat_unity_create_display_server (Seat *seat, const gchar *session_type)
480 if (strcmp (session_type, "x") == 0)
481 return create_x_server (seat);
482 else if (strcmp (session_type, "mir") == 0)
483 return DISPLAY_SERVER (mir_server_new ());
486 g_warning ("Can't create unsupported display server '%s'", session_type);
492 seat_unity_create_greeter_session (Seat *seat)
494 XGreeter *greeter_session;
496 greeter_session = xgreeter_new ();
497 session_set_env (SESSION (greeter_session), "XDG_SEAT", "seat0");
499 return GREETER (greeter_session);
503 seat_unity_create_session (Seat *seat)
507 session = xsession_new ();
508 session_set_env (SESSION (session), "XDG_SEAT", "seat0");
510 return SESSION (session);
514 seat_unity_set_active_session (Seat *seat, Session *session)
516 DisplayServer *display_server;
518 /* If no compositor, have to use VT switching */
519 if (SEAT_UNITY (seat)->priv->use_vt_switching)
521 gint vt = display_server_get_vt (session_get_display_server (session));
525 SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
529 if (session == SEAT_UNITY (seat)->priv->active_session)
531 SEAT_UNITY (seat)->priv->active_session = g_object_ref (session);
533 display_server = session_get_display_server (session);
534 if (SEAT_UNITY (seat)->priv->active_display_server != display_server)
538 SEAT_UNITY (seat)->priv->active_display_server = g_object_ref (display_server);
539 id = xserver_local_get_mir_id (XSERVER_LOCAL (display_server));
541 g_debug ("Switching to Mir session %s", id);
542 write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_ACTIVE_SESSION, (const guint8 *) id, strlen (id));
545 SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
549 seat_unity_get_active_session (Seat *seat)
551 if (SEAT_UNITY (seat)->priv->use_vt_switching)
555 vt = vt_get_active ();
559 for (link = seat_get_sessions (seat); link; link = link->next)
561 Session *session = link->data;
562 if (display_server_get_vt (session_get_display_server (session)) == vt)
569 return SEAT_UNITY (seat)->priv->active_session;
573 seat_unity_run_script (Seat *seat, DisplayServer *display_server, Process *script)
576 XServerLocal *xserver;
578 xserver = XSERVER_LOCAL (display_server);
579 path = xserver_local_get_authority_file_path (xserver);
580 process_set_env (script, "DISPLAY", xserver_get_address (XSERVER (xserver)));
581 process_set_env (script, "XAUTHORITY", path);
583 SEAT_CLASS (seat_unity_parent_class)->run_script (seat, display_server, script);
587 seat_unity_stop (Seat *seat)
589 /* Stop the compositor first */
590 if (process_get_is_running (SEAT_UNITY (seat)->priv->compositor_process))
592 process_stop (SEAT_UNITY (seat)->priv->compositor_process);
596 SEAT_CLASS (seat_unity_parent_class)->stop (seat);
600 seat_unity_init (SeatUnity *seat)
602 seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_UNITY_TYPE, SeatUnityPrivate);
604 seat->priv->compositor_process = process_new ();
608 seat_unity_finalize (GObject *object)
610 SeatUnity *seat = SEAT_UNITY (object);
612 if (seat->priv->vt >= 0)
613 vt_unref (seat->priv->vt);
614 g_free (seat->priv->log_file);
615 g_free (seat->priv->mir_socket_filename);
616 close (seat->priv->to_compositor_pipe[0]);
617 close (seat->priv->to_compositor_pipe[1]);
618 close (seat->priv->from_compositor_pipe[0]);
619 close (seat->priv->from_compositor_pipe[1]);
620 g_io_channel_unref (seat->priv->from_compositor_channel);
621 g_free (seat->priv->read_buffer);
622 g_object_unref (seat->priv->compositor_process);
623 if (seat->priv->active_session)
624 g_object_unref (seat->priv->active_session);
625 if (seat->priv->active_display_server)
626 g_object_unref (seat->priv->active_display_server);
628 G_OBJECT_CLASS (seat_unity_parent_class)->finalize (object);
632 seat_unity_class_init (SeatUnityClass *klass)
634 GObjectClass *object_class = G_OBJECT_CLASS (klass);
635 SeatClass *seat_class = SEAT_CLASS (klass);
637 object_class->finalize = seat_unity_finalize;
638 seat_class->setup = seat_unity_setup;
639 seat_class->start = seat_unity_start;
640 seat_class->create_display_server = seat_unity_create_display_server;
641 seat_class->create_greeter_session = seat_unity_create_greeter_session;
642 seat_class->create_session = seat_unity_create_session;
643 seat_class->set_active_session = seat_unity_set_active_session;
644 seat_class->get_active_session = seat_unity_get_active_session;
645 seat_class->run_script = seat_unity_run_script;
646 seat_class->stop = seat_unity_stop;
648 g_type_class_add_private (klass, sizeof (SeatUnityPrivate));