]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat-unity.c
Update env vars
[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     process_set_env (SEAT_UNITY (seat)->priv->compositor_process, "MIR_SERVER_FILE", SEAT_UNITY (seat)->priv->mir_socket_filename);
372
373     timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
374     compositor_command = seat_get_string_property (seat, "unity-compositor-command");
375     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);
376
377     absolute_command = get_absolute_command (command);
378     g_free (command);
379
380     /* Start the compositor */
381     process_set_command (SEAT_UNITY (seat)->priv->compositor_process, absolute_command);
382     g_free (absolute_command);
383     g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "stopped", G_CALLBACK (compositor_stopped_cb), seat);
384     g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "run", G_CALLBACK (compositor_run_cb), seat);
385     result = process_start (SEAT_UNITY (seat)->priv->compositor_process, FALSE);
386
387     /* Close compostor ends of the pipes */
388     close (SEAT_UNITY (seat)->priv->to_compositor_pipe[0]);
389     SEAT_UNITY (seat)->priv->to_compositor_pipe[0] = 0;
390     close (SEAT_UNITY (seat)->priv->from_compositor_pipe[1]);
391     SEAT_UNITY (seat)->priv->from_compositor_pipe[1] = 0;
392
393     if (!result)
394         return FALSE;
395
396     /* Connect to the compositor */
397     timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
398     if (timeout <= 0)
399         timeout = 60;
400     l_debug (seat, "Waiting for system compositor for %ds", timeout);
401     SEAT_UNITY (seat)->priv->compositor_timeout = g_timeout_add (timeout * 1000, compositor_timeout_cb, seat);
402
403     return TRUE;
404 }
405
406 static DisplayServer *
407 create_x_server (Seat *seat)
408 {
409     XServerLocal *x_server;
410     const gchar *command = NULL, *layout = NULL, *config_file = NULL, *xdmcp_manager = NULL, *key_name = NULL, *xdg_seat = NULL;
411     gboolean allow_tcp;
412     gint port = 0;
413
414     l_debug (seat, "Starting X server on Unity compositor");
415
416     x_server = x_server_local_new ();
417
418     command = seat_get_string_property (seat, "xserver-command");
419     if (command)
420         x_server_local_set_command (x_server, command);
421
422     if (SEAT_UNITY (seat)->priv->use_vt_switching)
423         x_server_local_set_vt (x_server, vt_get_unused ());
424     else
425     {
426         gchar *id;
427
428         id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
429         SEAT_UNITY (seat)->priv->next_id++;
430         x_server_local_set_mir_id (x_server, id);
431         x_server_local_set_mir_socket (x_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
432         g_free (id);
433     }
434
435     layout = seat_get_string_property (seat, "xserver-layout");
436     if (layout)
437         x_server_local_set_layout (x_server, layout);
438     
439     xdg_seat = seat_get_string_property (seat, "xdg-seat");
440     if (xdg_seat)
441         x_server_local_set_xdg_seat (x_server, xdg_seat);
442
443     config_file = seat_get_string_property (seat, "xserver-config");
444     if (config_file)
445         x_server_local_set_config (x_server, config_file);
446
447     allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
448     x_server_local_set_allow_tcp (x_server, allow_tcp);
449
450     xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
451     if (xdmcp_manager)
452         x_server_local_set_xdmcp_server (x_server, xdmcp_manager);
453
454     port = seat_get_integer_property (seat, "xdmcp-port");
455     if (port > 0)
456         x_server_local_set_xdmcp_port (x_server, port);
457
458     key_name = seat_get_string_property (seat, "xdmcp-key");
459     if (key_name)
460     {
461         gchar *dir, *path;
462         GKeyFile *keys;
463         gboolean result;
464         GError *error = NULL;
465
466         dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
467         path = g_build_filename (dir, "keys.conf", NULL);
468         g_free (dir);
469
470         keys = g_key_file_new ();
471         result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
472         if (error)
473             l_debug (seat, "Error getting key %s", error->message);
474         g_clear_error (&error);
475
476         if (result)
477         {
478             gchar *key = NULL;
479
480             if (g_key_file_has_key (keys, "keyring", key_name, NULL))
481                 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
482             else
483                 l_debug (seat, "Key %s not defined", key_name);
484
485             if (key)
486                 x_server_local_set_xdmcp_key (x_server, key);
487             g_free (key);
488         }
489
490         g_free (path);
491         g_key_file_free (keys);
492     }
493
494     return DISPLAY_SERVER (x_server);
495 }
496
497 static DisplayServer *
498 create_mir_server (Seat *seat)
499 {
500     MirServer *mir_server;
501
502     mir_server = mir_server_new ();
503     mir_server_set_parent_socket (mir_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
504
505     if (SEAT_UNITY (seat)->priv->use_vt_switching)
506         mir_server_set_vt (mir_server, vt_get_unused ());
507     else
508     {
509         gchar *id;
510
511         id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
512         SEAT_UNITY (seat)->priv->next_id++;
513         mir_server_set_id (mir_server, id);
514         g_free (id);
515     }   
516
517     return DISPLAY_SERVER (mir_server);
518 }
519
520 static DisplayServer *
521 seat_unity_create_display_server (Seat *seat, const gchar *session_type)
522 {  
523     if (strcmp (session_type, "x") == 0)
524         return create_x_server (seat);
525     else if (strcmp (session_type, "mir") == 0)
526         return create_mir_server (seat);
527     else
528     {
529         l_warning (seat, "Can't create unsupported display server '%s'", session_type);
530         return NULL;
531     }
532 }
533
534 static Greeter *
535 seat_unity_create_greeter_session (Seat *seat)
536 {
537     Greeter *greeter_session;
538     const gchar *xdg_seat;
539     gint vt = -1;
540
541     greeter_session = SEAT_CLASS (seat_unity_parent_class)->create_greeter_session (seat);
542     xdg_seat = seat_get_string_property (seat, "xdg-seat");
543     if (!xdg_seat)
544         xdg_seat = "seat0";
545     l_debug (seat, "Setting XDG_SEAT=%s", xdg_seat);
546     session_set_env (SESSION (greeter_session), "XDG_SEAT", xdg_seat);
547
548     if (!SEAT_UNITY (seat)->priv->use_vt_switching)
549         vt = SEAT_UNITY (seat)->priv->vt;
550     else if (vt_get_active () < 0)
551         vt = 1; /* If even VTs aren't working, just fake it */
552
553     if (vt >= 0)
554     {
555         gchar *value = g_strdup_printf ("%d", vt);
556         l_debug (seat, "Setting XDG_VTNR=%s", value);
557         session_set_env (SESSION (greeter_session), "XDG_VTNR", value);
558         g_free (value);
559     }
560     else
561         l_debug (seat, "Not setting XDG_VTNR");
562
563     return greeter_session;
564 }
565
566 static Session *
567 seat_unity_create_session (Seat *seat)
568 {
569     Session *session;
570     const gchar *xdg_seat;
571     gint vt = -1;
572
573     session = SEAT_CLASS (seat_unity_parent_class)->create_session (seat);
574     xdg_seat = seat_get_string_property (seat, "xdg-seat");
575     if (!xdg_seat)
576         xdg_seat = "seat0";
577     l_debug (seat, "Setting XDG_SEAT=%s", xdg_seat);
578     session_set_env (session, "XDG_SEAT", xdg_seat);
579
580     if (!SEAT_UNITY (seat)->priv->use_vt_switching)
581         vt = SEAT_UNITY (seat)->priv->vt;
582     else if (vt_get_active () < 0)
583         vt = 1; /* If even VTs aren't working, just fake it */
584
585     if (vt >= 0)
586     {
587         gchar *value = g_strdup_printf ("%d", vt);
588         l_debug (seat, "Setting XDG_VTNR=%s", value);
589         session_set_env (SESSION (session), "XDG_VTNR", value);
590         g_free (value);
591     }
592     else
593         l_debug (seat, "Not setting XDG_VTNR");
594
595     return session;
596 }
597
598 static void
599 seat_unity_set_active_session (Seat *seat, Session *session)
600 {
601     DisplayServer *display_server;
602
603     /* If no compositor, have to use VT switching */
604     if (SEAT_UNITY (seat)->priv->use_vt_switching)
605     {
606         gint vt = display_server_get_vt (session_get_display_server (session));
607         if (vt >= 0)
608             vt_set_active (vt);
609
610         SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
611         return;
612     }
613
614     if (session == SEAT_UNITY (seat)->priv->active_session)
615         return;
616     SEAT_UNITY (seat)->priv->active_session = g_object_ref (session);
617
618     display_server = session_get_display_server (session);
619     if (SEAT_UNITY (seat)->priv->active_display_server != display_server)
620     {
621         const gchar *id = NULL;
622
623         SEAT_UNITY (seat)->priv->active_display_server = g_object_ref (display_server);
624
625         if (IS_X_SERVER_LOCAL (display_server))
626             id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
627         else if (IS_MIR_SERVER (display_server))
628             id = mir_server_get_id (MIR_SERVER (display_server));
629
630         if (id)
631         {
632             l_debug (seat, "Switching to Mir session %s", id);
633             write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_ACTIVE_SESSION, (const guint8 *) id, strlen (id));
634         }
635         else
636             l_warning (seat, "Failed to work out session ID");
637     }
638
639     SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
640 }
641
642 static Session *
643 seat_unity_get_active_session (Seat *seat)
644 {
645     if (SEAT_UNITY (seat)->priv->use_vt_switching)
646     {
647         gint vt;
648         GList *link;
649         vt = vt_get_active ();
650         if (vt < 0)
651             return NULL;
652
653         for (link = seat_get_sessions (seat); link; link = link->next)
654         {
655             Session *session = link->data;
656             if (display_server_get_vt (session_get_display_server (session)) == vt)
657                 return session;
658         }
659
660         return NULL;
661     }
662
663     return SEAT_UNITY (seat)->priv->active_session;
664 }
665
666 static void
667 seat_unity_set_next_session (Seat *seat, Session *session)
668 {
669     DisplayServer *display_server;
670     const gchar *id = NULL;
671
672     if (!session)
673         return;
674
675     /* If no compositor, don't worry about it */
676     if (SEAT_UNITY (seat)->priv->use_vt_switching)
677         return;
678
679     display_server = session_get_display_server (session);
680
681     if (IS_X_SERVER_LOCAL (display_server))
682         id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
683     else if (IS_MIR_SERVER (display_server))
684         id = mir_server_get_id (MIR_SERVER (display_server));
685
686     if (id)
687     {
688         l_debug (seat, "Marking Mir session %s as the next session", id);
689         write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_NEXT_SESSION, (const guint8 *) id, strlen (id));
690     }
691     else
692     {
693         l_debug (seat, "Failed to work out session ID to mark");
694     }
695
696     SEAT_CLASS (seat_unity_parent_class)->set_next_session (seat, session);
697 }
698
699 static void
700 seat_unity_run_script (Seat *seat, DisplayServer *display_server, Process *script)
701 {
702     const gchar *path;
703     XServerLocal *x_server;
704
705     x_server = X_SERVER_LOCAL (display_server);
706     path = x_server_local_get_authority_file_path (x_server);
707     process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
708     process_set_env (script, "XAUTHORITY", path);
709
710     SEAT_CLASS (seat_unity_parent_class)->run_script (seat, display_server, script);
711 }
712
713 static void
714 seat_unity_stop (Seat *seat)
715 {
716     /* Stop the compositor first */
717     if (process_get_is_running (SEAT_UNITY (seat)->priv->compositor_process))
718     {
719         process_stop (SEAT_UNITY (seat)->priv->compositor_process);
720         return;
721     }
722
723     SEAT_CLASS (seat_unity_parent_class)->stop (seat);
724 }
725
726 static void
727 seat_unity_init (SeatUnity *seat)
728 {
729     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_UNITY_TYPE, SeatUnityPrivate);
730     seat->priv->vt = -1;
731     seat->priv->compositor_process = process_new ();
732 }
733
734 static void
735 seat_unity_finalize (GObject *object)
736 {
737     SeatUnity *seat = SEAT_UNITY (object);
738
739     if (seat->priv->vt >= 0)
740         vt_unref (seat->priv->vt);
741     g_free (seat->priv->log_file);
742     g_free (seat->priv->mir_socket_filename);
743     close (seat->priv->to_compositor_pipe[0]);
744     close (seat->priv->to_compositor_pipe[1]);
745     close (seat->priv->from_compositor_pipe[0]);
746     close (seat->priv->from_compositor_pipe[1]);
747     g_io_channel_unref (seat->priv->from_compositor_channel);
748     g_source_remove (seat->priv->from_compositor_watch);
749     g_free (seat->priv->read_buffer);
750     g_object_unref (seat->priv->compositor_process);
751     if (seat->priv->active_session)
752         g_object_unref (seat->priv->active_session);
753     if (seat->priv->active_display_server)
754         g_object_unref (seat->priv->active_display_server);
755
756     G_OBJECT_CLASS (seat_unity_parent_class)->finalize (object);
757 }
758
759 static void
760 seat_unity_class_init (SeatUnityClass *klass)
761 {
762     GObjectClass *object_class = G_OBJECT_CLASS (klass);
763     SeatClass *seat_class = SEAT_CLASS (klass);
764
765     object_class->finalize = seat_unity_finalize;
766     seat_class->get_start_local_sessions = seat_unity_get_start_local_sessions;
767     seat_class->setup = seat_unity_setup;
768     seat_class->start = seat_unity_start;
769     seat_class->create_display_server = seat_unity_create_display_server;
770     seat_class->create_greeter_session = seat_unity_create_greeter_session;
771     seat_class->create_session = seat_unity_create_session;
772     seat_class->set_active_session = seat_unity_set_active_session;
773     seat_class->get_active_session = seat_unity_get_active_session;
774     seat_class->set_next_session = seat_unity_set_next_session;
775     seat_class->run_script = seat_unity_run_script;
776     seat_class->stop = seat_unity_stop;
777
778     g_type_class_add_private (klass, sizeof (SeatUnityPrivate));
779 }