]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat-unity.c
742b11adbf9ad19fce0148d941a44457e0a7d394
[sojka/lightdm.git] / src / seat-unity.c
1 /*
2  * Copyright (C) 2012-2013 Robert Ancell.
3  * Author: Robert Ancell <robert.ancell@canonical.com>
4  *
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
9  * license.
10  */
11
12 #include <string.h>
13 #include <fcntl.h>
14 #include <errno.h>
15 #include <glib/gstdio.h>
16
17 #include "seat-unity.h"
18 #include "configuration.h"
19 #include "x-server-local.h"
20 #include "mir-server.h"
21 #include "vt.h"
22 #include "plymouth.h"
23
24 typedef enum
25 {
26    USC_MESSAGE_PING = 0,
27    USC_MESSAGE_PONG = 1,
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,
32 } USCMessageID;
33
34 struct SeatUnityPrivate
35 {
36     /* VT we are running on */
37     gint vt;
38
39     /* File to log to */
40     gchar *log_file;
41
42     /* Filename of Mir socket */
43     gchar *mir_socket_filename;
44
45     /* Pipes to communicate with compositor */
46     int to_compositor_pipe[2];
47     int from_compositor_pipe[2];
48
49     /* IO channel listening on for messages from the compositor */
50     GIOChannel *from_compositor_channel;
51     guint from_compositor_watch;
52
53     /* TRUE when the compositor indicates it is ready */
54     gboolean compositor_ready;
55
56     /* Buffer reading from channel */
57     guint8 *read_buffer;
58     gsize read_buffer_length;
59     gsize read_buffer_n_used;
60
61     /* Compositor process */
62     Process *compositor_process;
63
64     /* Timeout when waiting for compositor to start */
65     guint compositor_timeout;
66
67     /* Next Mir ID to use for a compositor client */
68     gint next_id;
69
70     /* TRUE if using VT switching fallback */
71     gboolean use_vt_switching;
72
73     /* The currently visible session */
74     Session *active_session;
75     DisplayServer *active_display_server;
76 };
77
78 G_DEFINE_TYPE (SeatUnity, seat_unity, SEAT_TYPE);
79
80 static gboolean
81 seat_unity_get_start_local_sessions (Seat *seat)
82 {
83     return !seat_get_string_property (seat, "xdmcp-manager");
84 }
85
86 static void
87 seat_unity_setup (Seat *seat)
88 {
89     seat_set_can_switch (seat, TRUE);
90     SEAT_CLASS (seat_unity_parent_class)->setup (seat);
91 }
92
93 static void
94 compositor_stopped_cb (Process *process, SeatUnity *seat)
95 {
96     if (seat->priv->compositor_timeout != 0)
97         g_source_remove (seat->priv->compositor_timeout);
98     seat->priv->compositor_timeout = 0;
99
100     /* Finished with the VT */
101     vt_unref (seat->priv->vt);
102     seat->priv->vt = -1;
103
104     if (seat_get_is_stopping (SEAT (seat)))
105     {
106         SEAT_CLASS (seat_unity_parent_class)->stop (SEAT (seat));
107         return;
108     }
109
110     /* If stopped before it was ready, then revert to VT mode */
111     if (!seat->priv->compositor_ready)
112     {
113         l_debug (seat, "Compositor failed to start, switching to VT mode");
114         seat->priv->use_vt_switching = TRUE;
115         SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
116         return;
117     }
118
119     l_debug (seat, "Stopping Unity seat, compositor terminated");
120
121     seat_stop (SEAT (seat));
122 }
123
124 static void
125 compositor_run_cb (Process *process, SeatUnity *seat)
126 {
127     int fd;
128
129     /* Make input non-blocking */
130     fd = open ("/dev/null", O_RDONLY);
131     dup2 (fd, STDIN_FILENO);
132     close (fd);
133
134     /* Redirect output to logfile */
135     if (seat->priv->log_file)
136     {
137          int fd;
138          gchar *old_filename;
139
140          /* Move old file out of the way */
141          old_filename = g_strdup_printf ("%s.old", seat->priv->log_file);
142          rename (seat->priv->log_file, old_filename);
143          g_free (old_filename);
144
145          /* Create new file and log to it */
146          fd = g_open (seat->priv->log_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
147          if (fd < 0)
148              l_warning (seat, "Failed to open log file %s: %s", seat->priv->log_file, g_strerror (errno));
149          else
150          {
151              dup2 (fd, STDOUT_FILENO);
152              dup2 (fd, STDERR_FILENO);
153              close (fd);
154          }
155     }
156 }
157
158 static void
159 write_message (SeatUnity *seat, guint16 id, const guint8 *payload, guint16 payload_length)
160 {
161     guint8 *data;
162     gsize data_length = 4 + payload_length;
163
164     data = g_malloc (data_length);
165     data[0] = id >> 8;
166     data[1] = id & 0xFF;
167     data[2] = payload_length >> 8;
168     data[3] = payload_length & 0xFF;
169     memcpy (data + 4, payload, payload_length);
170
171     errno = 0;
172     if (write (seat->priv->to_compositor_pipe[1], data, data_length) != data_length)
173         l_warning (seat, "Failed to write to compositor: %s", strerror (errno));
174 }
175
176 static gboolean
177 read_cb (GIOChannel *source, GIOCondition condition, gpointer data)
178 {
179     SeatUnity *seat = data;
180     gsize n_to_read = 0;
181     guint16 id, payload_length;
182     /*guint8 *payload;*/
183   
184     if (condition == G_IO_HUP)
185     {
186         l_debug (seat, "Compositor closed communication channel");
187         return FALSE;
188     }
189
190     /* Work out how much required for a message */
191     if (seat->priv->read_buffer_n_used < 4)
192         n_to_read = 4 - seat->priv->read_buffer_n_used;
193     else
194     {
195         payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
196         n_to_read = 4 + payload_length - seat->priv->read_buffer_n_used;
197     }
198
199     /* Read from compositor */
200     if (n_to_read > 0)
201     {
202         gsize n_total, n_read = 0;
203         GIOStatus status;
204         GError *error = NULL;
205
206         n_total = seat->priv->read_buffer_n_used + n_to_read;
207         if (seat->priv->read_buffer_length < n_total)
208             seat->priv->read_buffer = g_realloc (seat->priv->read_buffer, n_total);
209
210         status = g_io_channel_read_chars (source,
211                                           (gchar *)seat->priv->read_buffer + seat->priv->read_buffer_n_used,
212                                           n_to_read,
213                                           &n_read,
214                                           &error);
215         if (error)
216             l_warning (seat, "Failed to read from compositor: %s", error->message);
217         if (status != G_IO_STATUS_NORMAL)
218             return TRUE;
219         g_clear_error (&error);
220         seat->priv->read_buffer_n_used += n_read;
221     }
222
223     /* Read header */
224     if (seat->priv->read_buffer_n_used < 4)
225          return TRUE;
226     id = seat->priv->read_buffer[0] << 8 | seat->priv->read_buffer[1];
227     payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
228
229     /* Read payload */
230     if (seat->priv->read_buffer_n_used < 4 + payload_length)
231         return TRUE;
232     /*payload = seat->priv->read_buffer + 4;*/
233
234     switch (id)
235     {
236     case USC_MESSAGE_PING:
237         l_debug (seat, "PING!");
238         write_message (seat, USC_MESSAGE_PONG, NULL, 0);
239         break;
240     case USC_MESSAGE_PONG:
241         l_debug (seat, "PONG!");
242         break;
243     case USC_MESSAGE_READY:
244         l_debug (seat, "READY");
245         if (!seat->priv->compositor_ready)
246         {
247             seat->priv->compositor_ready = TRUE;
248             l_debug (seat, "Compositor ready");
249             g_source_remove (seat->priv->compositor_timeout);
250             seat->priv->compositor_timeout = 0;
251             SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
252         }
253         break;
254     case USC_MESSAGE_SESSION_CONNECTED:
255         l_debug (seat, "SESSION CONNECTED");
256         break;
257     default:
258         l_warning (seat, "Ignoring unknown message %d with %d octets from system compositor", id, payload_length);
259         break;
260     }
261
262     /* Clear buffer */
263     seat->priv->read_buffer_n_used = 0;
264
265     return TRUE;
266 }
267
268 static gchar *
269 get_absolute_command (const gchar *command)
270 {
271     gchar **tokens;
272     gchar *absolute_binary, *absolute_command = NULL;
273
274     tokens = g_strsplit (command, " ", 2);
275
276     absolute_binary = g_find_program_in_path (tokens[0]);
277     if (absolute_binary)
278     {
279         if (tokens[1])
280             absolute_command = g_strjoin (" ", absolute_binary, tokens[1], NULL);
281         else
282             absolute_command = g_strdup (absolute_binary);
283         g_free (absolute_binary);
284     }
285     else
286         absolute_command = g_strdup (command);
287
288     g_strfreev (tokens);
289
290     return absolute_command;
291 }
292
293 static gboolean
294 compositor_timeout_cb (gpointer data)
295 {
296     SeatUnity *seat = data;
297
298     /* Stop the compositor - it is not working */
299     process_stop (seat->priv->compositor_process);
300
301     return TRUE;
302 }
303
304 static gboolean
305 seat_unity_start (Seat *seat)
306 {
307     const gchar *compositor_command;
308     gchar *command, *absolute_command, *dir, *value;
309     gboolean result;
310     int timeout;
311
312     /* Replace Plymouth if it is running */
313     if (plymouth_get_is_active () && plymouth_has_active_vt ())
314     {
315         gint active_vt = vt_get_active ();
316         if (active_vt >= vt_get_min ())
317         {
318             SEAT_UNITY (seat)->priv->vt = active_vt;
319             plymouth_quit (TRUE);
320         }
321         else
322             l_debug (seat, "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 ());
323     }
324     if (plymouth_get_is_active ())
325         plymouth_quit (FALSE);
326     if (SEAT_UNITY (seat)->priv->vt < 0)
327         SEAT_UNITY (seat)->priv->vt = vt_get_unused ();
328     if (SEAT_UNITY (seat)->priv->vt < 0)
329     {
330         l_debug (seat, "Failed to get a VT to run on");
331         return FALSE;
332     }
333     vt_ref (SEAT_UNITY (seat)->priv->vt);
334
335     /* Create pipes to talk to compositor */
336     if (pipe (SEAT_UNITY (seat)->priv->to_compositor_pipe) < 0 || pipe (SEAT_UNITY (seat)->priv->from_compositor_pipe) < 0)
337     {
338         l_debug (seat, "Failed to create compositor pipes: %s", g_strerror (errno));
339         return FALSE;
340     }
341
342     /* Don't allow the daemon end of the pipes to be accessed in the compositor */
343     fcntl (SEAT_UNITY (seat)->priv->to_compositor_pipe[1], F_SETFD, FD_CLOEXEC);
344     fcntl (SEAT_UNITY (seat)->priv->from_compositor_pipe[0], F_SETFD, FD_CLOEXEC);
345
346     /* Listen for messages from the compositor */
347     SEAT_UNITY (seat)->priv->from_compositor_channel = g_io_channel_unix_new (SEAT_UNITY (seat)->priv->from_compositor_pipe[0]);
348     SEAT_UNITY (seat)->priv->from_compositor_watch = g_io_add_watch (SEAT_UNITY (seat)->priv->from_compositor_channel, G_IO_IN | G_IO_HUP, read_cb, seat);
349
350     /* Setup logging */
351     dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
352     SEAT_UNITY (seat)->priv->log_file = g_build_filename (dir, "unity-system-compositor.log", NULL);
353     l_debug (seat, "Logging to %s", SEAT_UNITY (seat)->priv->log_file);
354     g_free (dir);
355
356     /* Setup environment */
357     process_set_clear_environment (SEAT_UNITY (seat)->priv->compositor_process, TRUE);
358     process_set_env (SEAT_UNITY (seat)->priv->compositor_process, "XDG_SEAT", "seat0");
359     value = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->vt);
360     process_set_env (SEAT_UNITY (seat)->priv->compositor_process, "XDG_VTNR", value);
361     g_free (value);
362     /* Variable required for regression tests */
363     if (g_getenv ("LIGHTDM_TEST_ROOT"))
364     {
365         process_set_env (SEAT_UNITY (seat)->priv->compositor_process, "LIGHTDM_TEST_ROOT", g_getenv ("LIGHTDM_TEST_ROOT"));
366         process_set_env (SEAT_UNITY (seat)->priv->compositor_process, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
367         process_set_env (SEAT_UNITY (seat)->priv->compositor_process, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
368     }
369
370     SEAT_UNITY (seat)->priv->mir_socket_filename = g_strdup ("/tmp/mir_socket"); // FIXME: Use this socket by default as XMir is hardcoded to this
371     timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
372     compositor_command = seat_get_string_property (seat, "unity-compositor-command");
373     command = g_strdup_printf ("%s --file '%s' --from-dm-fd %d --to-dm-fd %d --vt %d", compositor_command, SEAT_UNITY (seat)->priv->mir_socket_filename, SEAT_UNITY (seat)->priv->to_compositor_pipe[0], SEAT_UNITY (seat)->priv->from_compositor_pipe[1], SEAT_UNITY (seat)->priv->vt);
374
375     absolute_command = get_absolute_command (command);
376     g_free (command);
377
378     /* Start the compositor */
379     process_set_command (SEAT_UNITY (seat)->priv->compositor_process, absolute_command);
380     g_free (absolute_command);
381     g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "stopped", G_CALLBACK (compositor_stopped_cb), seat);
382     g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "run", G_CALLBACK (compositor_run_cb), seat);
383     result = process_start (SEAT_UNITY (seat)->priv->compositor_process, FALSE);
384
385     /* Close compostor ends of the pipes */
386     close (SEAT_UNITY (seat)->priv->to_compositor_pipe[0]);
387     SEAT_UNITY (seat)->priv->to_compositor_pipe[0] = 0;
388     close (SEAT_UNITY (seat)->priv->from_compositor_pipe[1]);
389     SEAT_UNITY (seat)->priv->from_compositor_pipe[1] = 0;
390
391     if (!result)
392         return FALSE;
393
394     /* Connect to the compositor */
395     timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
396     if (timeout <= 0)
397         timeout = 60;
398     l_debug (seat, "Waiting for system compositor for %ds", timeout);
399     SEAT_UNITY (seat)->priv->compositor_timeout = g_timeout_add (timeout * 1000, compositor_timeout_cb, seat);
400
401     return TRUE;
402 }
403
404 static DisplayServer *
405 create_x_server (Seat *seat)
406 {
407     XServerLocal *x_server;
408     const gchar *command = NULL, *layout = NULL, *config_file = NULL, *xdmcp_manager = NULL, *key_name = NULL, *xdg_seat = NULL;
409     gboolean allow_tcp;
410     gint port = 0;
411
412     l_debug (seat, "Starting X server on Unity compositor");
413
414     x_server = x_server_local_new ();
415
416     command = seat_get_string_property (seat, "xserver-command");
417     if (command)
418         x_server_local_set_command (x_server, command);
419
420     if (SEAT_UNITY (seat)->priv->use_vt_switching)
421         x_server_local_set_vt (x_server, vt_get_unused ());
422     else
423     {
424         gchar *id;
425
426         id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
427         SEAT_UNITY (seat)->priv->next_id++;
428         x_server_local_set_mir_id (x_server, id);
429         x_server_local_set_mir_socket (x_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
430         g_free (id);
431     }
432
433     layout = seat_get_string_property (seat, "xserver-layout");
434     if (layout)
435         x_server_local_set_layout (x_server, layout);
436     
437     xdg_seat = seat_get_string_property (seat, "xdg-seat");
438     if (xdg_seat)
439         x_server_local_set_xdg_seat (x_server, xdg_seat);
440
441     config_file = seat_get_string_property (seat, "xserver-config");
442     if (config_file)
443         x_server_local_set_config (x_server, config_file);
444
445     allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
446     x_server_local_set_allow_tcp (x_server, allow_tcp);
447
448     xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
449     if (xdmcp_manager)
450         x_server_local_set_xdmcp_server (x_server, xdmcp_manager);
451
452     port = seat_get_integer_property (seat, "xdmcp-port");
453     if (port > 0)
454         x_server_local_set_xdmcp_port (x_server, port);
455
456     key_name = seat_get_string_property (seat, "xdmcp-key");
457     if (key_name)
458     {
459         gchar *dir, *path;
460         GKeyFile *keys;
461         gboolean result;
462         GError *error = NULL;
463
464         dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
465         path = g_build_filename (dir, "keys.conf", NULL);
466         g_free (dir);
467
468         keys = g_key_file_new ();
469         result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
470         if (error)
471             l_debug (seat, "Error getting key %s", error->message);
472         g_clear_error (&error);
473
474         if (result)
475         {
476             gchar *key = NULL;
477
478             if (g_key_file_has_key (keys, "keyring", key_name, NULL))
479                 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
480             else
481                 l_debug (seat, "Key %s not defined", key_name);
482
483             if (key)
484                 x_server_local_set_xdmcp_key (x_server, key);
485             g_free (key);
486         }
487
488         g_free (path);
489         g_key_file_free (keys);
490     }
491
492     return DISPLAY_SERVER (x_server);
493 }
494
495 static DisplayServer *
496 create_mir_server (Seat *seat, gboolean is_greeter)
497 {
498     MirServer *mir_server;
499
500     mir_server = mir_server_new ();
501     mir_server_set_parent_socket (mir_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
502
503     if (SEAT_UNITY (seat)->priv->use_vt_switching)
504         mir_server_set_vt (mir_server, vt_get_unused ());
505     else
506     {
507         gchar *id;
508
509         // If we're a greeter, mark our ID that way, so USC can treat it specially
510         id = g_strdup_printf ("%s%d", is_greeter ? "greeter-" : "", SEAT_UNITY (seat)->priv->next_id);
511         mir_server_set_id (mir_server, id);
512         g_free (id);
513
514         // Now set display name for logging and internal use
515         id = g_strdup_printf ("mir-%s", SEAT_UNITY (seat)->priv->next_id);
516         display_server_set_name (DISPLAY_SERVER (mir_server), id);
517         g_free (id);
518
519         SEAT_UNITY (seat)->priv->next_id++;
520     }   
521
522     return DISPLAY_SERVER (mir_server);
523 }
524
525 static DisplayServer *
526 seat_unity_create_display_server (Seat *seat, const gchar *session_type, gboolean is_greeter)
527 {  
528     if (strcmp (session_type, "x") == 0)
529         return create_x_server (seat);
530     else if (strcmp (session_type, "mir") == 0)
531         return create_mir_server (seat, is_greeter);
532     else
533     {
534         l_warning (seat, "Can't create unsupported display server '%s'", session_type);
535         return NULL;
536     }
537 }
538
539 static Greeter *
540 seat_unity_create_greeter_session (Seat *seat)
541 {
542     Greeter *greeter_session;
543     const gchar *xdg_seat;
544     gint vt = -1;
545
546     greeter_session = SEAT_CLASS (seat_unity_parent_class)->create_greeter_session (seat);
547     xdg_seat = seat_get_string_property (seat, "xdg-seat");
548     if (!xdg_seat)
549         xdg_seat = "seat0";
550     l_debug (seat, "Setting XDG_SEAT=%s", xdg_seat);
551     session_set_env (SESSION (greeter_session), "XDG_SEAT", xdg_seat);
552
553     if (!SEAT_UNITY (seat)->priv->use_vt_switching)
554         vt = SEAT_UNITY (seat)->priv->vt;
555
556     if (vt >= 0)
557     {
558         gchar *value = g_strdup_printf ("%d", vt);
559         l_debug (seat, "Setting XDG_VTNR=%s", value);
560         session_set_env (SESSION (greeter_session), "XDG_VTNR", value);
561         g_free (value);
562     }
563     else
564         l_debug (seat, "Not setting XDG_VTNR");
565
566     return greeter_session;
567 }
568
569 static Session *
570 seat_unity_create_session (Seat *seat)
571 {
572     Session *session;
573     const gchar *xdg_seat;
574     gint vt = -1;
575
576     session = SEAT_CLASS (seat_unity_parent_class)->create_session (seat);
577     xdg_seat = seat_get_string_property (seat, "xdg-seat");
578     if (!xdg_seat)
579         xdg_seat = "seat0";
580     l_debug (seat, "Setting XDG_SEAT=%s", xdg_seat);
581     session_set_env (session, "XDG_SEAT", xdg_seat);
582
583     if (!SEAT_UNITY (seat)->priv->use_vt_switching)
584         vt = SEAT_UNITY (seat)->priv->vt;
585
586     if (vt >= 0)
587     {
588         gchar *value = g_strdup_printf ("%d", vt);
589         l_debug (seat, "Setting XDG_VTNR=%s", value);
590         session_set_env (SESSION (session), "XDG_VTNR", value);
591         g_free (value);
592     }
593     else
594         l_debug (seat, "Not setting XDG_VTNR");
595
596     return session;
597 }
598
599 static void
600 seat_unity_set_active_session (Seat *seat, Session *session)
601 {
602     DisplayServer *display_server;
603
604     /* If no compositor, have to use VT switching */
605     if (SEAT_UNITY (seat)->priv->use_vt_switching)
606     {
607         gint vt = display_server_get_vt (session_get_display_server (session));
608         if (vt >= 0)
609             vt_set_active (vt);
610
611         SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
612         return;
613     }
614
615     if (session == SEAT_UNITY (seat)->priv->active_session)
616         return;
617     SEAT_UNITY (seat)->priv->active_session = g_object_ref (session);
618
619     display_server = session_get_display_server (session);
620     if (SEAT_UNITY (seat)->priv->active_display_server != display_server)
621     {
622         const gchar *id = NULL;
623
624         SEAT_UNITY (seat)->priv->active_display_server = g_object_ref (display_server);
625
626         if (IS_X_SERVER_LOCAL (display_server))
627             id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
628         else if (IS_MIR_SERVER (display_server))
629             id = mir_server_get_id (MIR_SERVER (display_server));
630
631         if (id)
632         {
633             l_debug (seat, "Switching to Mir session %s", id);
634             write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_ACTIVE_SESSION, (const guint8 *) id, strlen (id));
635         }
636         else
637             l_warning (seat, "Failed to work out session ID");
638     }
639
640     SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
641 }
642
643 static Session *
644 seat_unity_get_active_session (Seat *seat)
645 {
646     if (SEAT_UNITY (seat)->priv->use_vt_switching)
647     {
648         gint vt;
649         GList *link;
650         vt = vt_get_active ();
651         if (vt < 0)
652             return NULL;
653
654         for (link = seat_get_sessions (seat); link; link = link->next)
655         {
656             Session *session = link->data;
657             if (display_server_get_vt (session_get_display_server (session)) == vt)
658                 return session;
659         }
660
661         return NULL;
662     }
663
664     return SEAT_UNITY (seat)->priv->active_session;
665 }
666
667 static void
668 seat_unity_set_next_session (Seat *seat, Session *session)
669 {
670     DisplayServer *display_server;
671     const gchar *id = NULL;
672
673     if (!session)
674         return;
675
676     /* If no compositor, don't worry about it */
677     if (SEAT_UNITY (seat)->priv->use_vt_switching)
678         return;
679
680     display_server = session_get_display_server (session);
681
682     if (IS_X_SERVER_LOCAL (display_server))
683         id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
684     else if (IS_MIR_SERVER (display_server))
685         id = mir_server_get_id (MIR_SERVER (display_server));
686
687     if (id)
688     {
689         l_debug (seat, "Marking Mir session %s as the next session", id);
690         write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_NEXT_SESSION, (const guint8 *) id, strlen (id));
691     }
692     else
693     {
694         l_debug (seat, "Failed to work out session ID to mark");
695     }
696
697     SEAT_CLASS (seat_unity_parent_class)->set_next_session (seat, session);
698 }
699
700 static void
701 seat_unity_run_script (Seat *seat, DisplayServer *display_server, Process *script)
702 {
703     const gchar *path;
704     XServerLocal *x_server;
705
706     x_server = X_SERVER_LOCAL (display_server);
707     path = x_server_local_get_authority_file_path (x_server);
708     process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
709     process_set_env (script, "XAUTHORITY", path);
710
711     SEAT_CLASS (seat_unity_parent_class)->run_script (seat, display_server, script);
712 }
713
714 static void
715 seat_unity_stop (Seat *seat)
716 {
717     /* Stop the compositor first */
718     if (process_get_is_running (SEAT_UNITY (seat)->priv->compositor_process))
719     {
720         process_stop (SEAT_UNITY (seat)->priv->compositor_process);
721         return;
722     }
723
724     SEAT_CLASS (seat_unity_parent_class)->stop (seat);
725 }
726
727 static void
728 seat_unity_init (SeatUnity *seat)
729 {
730     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_UNITY_TYPE, SeatUnityPrivate);
731     seat->priv->vt = -1;
732     seat->priv->compositor_process = process_new ();
733 }
734
735 static void
736 seat_unity_finalize (GObject *object)
737 {
738     SeatUnity *seat = SEAT_UNITY (object);
739
740     if (seat->priv->vt >= 0)
741         vt_unref (seat->priv->vt);
742     g_free (seat->priv->log_file);
743     g_free (seat->priv->mir_socket_filename);
744     close (seat->priv->to_compositor_pipe[0]);
745     close (seat->priv->to_compositor_pipe[1]);
746     close (seat->priv->from_compositor_pipe[0]);
747     close (seat->priv->from_compositor_pipe[1]);
748     g_io_channel_unref (seat->priv->from_compositor_channel);
749     g_source_remove (seat->priv->from_compositor_watch);
750     g_free (seat->priv->read_buffer);
751     g_object_unref (seat->priv->compositor_process);
752     if (seat->priv->active_session)
753         g_object_unref (seat->priv->active_session);
754     if (seat->priv->active_display_server)
755         g_object_unref (seat->priv->active_display_server);
756
757     G_OBJECT_CLASS (seat_unity_parent_class)->finalize (object);
758 }
759
760 static void
761 seat_unity_class_init (SeatUnityClass *klass)
762 {
763     GObjectClass *object_class = G_OBJECT_CLASS (klass);
764     SeatClass *seat_class = SEAT_CLASS (klass);
765
766     object_class->finalize = seat_unity_finalize;
767     seat_class->get_start_local_sessions = seat_unity_get_start_local_sessions;
768     seat_class->setup = seat_unity_setup;
769     seat_class->start = seat_unity_start;
770     seat_class->create_display_server = seat_unity_create_display_server;
771     seat_class->create_greeter_session = seat_unity_create_greeter_session;
772     seat_class->create_session = seat_unity_create_session;
773     seat_class->set_active_session = seat_unity_set_active_session;
774     seat_class->get_active_session = seat_unity_get_active_session;
775     seat_class->set_next_session = seat_unity_set_next_session;
776     seat_class->run_script = seat_unity_run_script;
777     seat_class->stop = seat_unity_stop;
778
779     g_type_class_add_private (klass, sizeof (SeatUnityPrivate));
780 }