]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat-unity.c
33d5ffda802a41f41b961017ee61da0af6d75b24
[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 Mir sessions, X server and greeters */
68     gint next_session_id;
69     gint next_x_server_id;
70     gint next_greeter_id;
71
72     /* TRUE if using VT switching fallback */
73     gboolean use_vt_switching;
74
75     /* The currently visible session */
76     Session *active_session;
77     DisplayServer *active_display_server;
78 };
79
80 G_DEFINE_TYPE (SeatUnity, seat_unity, SEAT_TYPE);
81
82 static gboolean
83 seat_unity_get_start_local_sessions (Seat *seat)
84 {
85     return !seat_get_string_property (seat, "xdmcp-manager");
86 }
87
88 static void
89 seat_unity_setup (Seat *seat)
90 {
91     seat_set_can_switch (seat, TRUE);
92     SEAT_CLASS (seat_unity_parent_class)->setup (seat);
93 }
94
95 static void
96 compositor_stopped_cb (Process *process, SeatUnity *seat)
97 {
98     if (seat->priv->compositor_timeout != 0)
99         g_source_remove (seat->priv->compositor_timeout);
100     seat->priv->compositor_timeout = 0;
101
102     /* Finished with the VT */
103     vt_unref (seat->priv->vt);
104     seat->priv->vt = -1;
105
106     if (seat_get_is_stopping (SEAT (seat)))
107     {
108         SEAT_CLASS (seat_unity_parent_class)->stop (SEAT (seat));
109         return;
110     }
111
112     /* If stopped before it was ready, then revert to VT mode */
113     if (!seat->priv->compositor_ready)
114     {
115         l_debug (seat, "Compositor failed to start, switching to VT mode");
116         seat->priv->use_vt_switching = TRUE;
117         SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
118         return;
119     }
120
121     l_debug (seat, "Stopping Unity seat, compositor terminated");
122
123     seat_stop (SEAT (seat));
124 }
125
126 static void
127 compositor_run_cb (Process *process, SeatUnity *seat)
128 {
129     int fd;
130
131     /* Make input non-blocking */
132     fd = open ("/dev/null", O_RDONLY);
133     dup2 (fd, STDIN_FILENO);
134     close (fd);
135
136     /* Redirect output to logfile */
137     if (seat->priv->log_file)
138     {
139          int fd;
140          gchar *old_filename;
141
142          /* Move old file out of the way */
143          old_filename = g_strdup_printf ("%s.old", seat->priv->log_file);
144          rename (seat->priv->log_file, old_filename);
145          g_free (old_filename);
146
147          /* Create new file and log to it */
148          fd = g_open (seat->priv->log_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
149          if (fd < 0)
150              l_warning (seat, "Failed to open log file %s: %s", seat->priv->log_file, g_strerror (errno));
151          else
152          {
153              dup2 (fd, STDOUT_FILENO);
154              dup2 (fd, STDERR_FILENO);
155              close (fd);
156          }
157     }
158 }
159
160 static void
161 write_message (SeatUnity *seat, guint16 id, const guint8 *payload, guint16 payload_length)
162 {
163     guint8 *data;
164     gsize data_length = 4 + payload_length;
165
166     data = g_malloc (data_length);
167     data[0] = id >> 8;
168     data[1] = id & 0xFF;
169     data[2] = payload_length >> 8;
170     data[3] = payload_length & 0xFF;
171     memcpy (data + 4, payload, payload_length);
172
173     errno = 0;
174     if (write (seat->priv->to_compositor_pipe[1], data, data_length) != data_length)
175         l_warning (seat, "Failed to write to compositor: %s", strerror (errno));
176 }
177
178 static gboolean
179 read_cb (GIOChannel *source, GIOCondition condition, gpointer data)
180 {
181     SeatUnity *seat = data;
182     gsize n_to_read = 0;
183     guint16 id, payload_length;
184     /*guint8 *payload;*/
185   
186     if (condition == G_IO_HUP)
187     {
188         l_debug (seat, "Compositor closed communication channel");
189         return FALSE;
190     }
191
192     /* Work out how much required for a message */
193     if (seat->priv->read_buffer_n_used < 4)
194         n_to_read = 4 - seat->priv->read_buffer_n_used;
195     else
196     {
197         payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
198         n_to_read = 4 + payload_length - seat->priv->read_buffer_n_used;
199     }
200
201     /* Read from compositor */
202     if (n_to_read > 0)
203     {
204         gsize n_total, n_read = 0;
205         GIOStatus status;
206         GError *error = NULL;
207
208         n_total = seat->priv->read_buffer_n_used + n_to_read;
209         if (seat->priv->read_buffer_length < n_total)
210             seat->priv->read_buffer = g_realloc (seat->priv->read_buffer, n_total);
211
212         status = g_io_channel_read_chars (source,
213                                           (gchar *)seat->priv->read_buffer + seat->priv->read_buffer_n_used,
214                                           n_to_read,
215                                           &n_read,
216                                           &error);
217         if (error)
218             l_warning (seat, "Failed to read from compositor: %s", error->message);
219         if (status != G_IO_STATUS_NORMAL)
220             return TRUE;
221         g_clear_error (&error);
222         seat->priv->read_buffer_n_used += n_read;
223     }
224
225     /* Read header */
226     if (seat->priv->read_buffer_n_used < 4)
227          return TRUE;
228     id = seat->priv->read_buffer[0] << 8 | seat->priv->read_buffer[1];
229     payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
230
231     /* Read payload */
232     if (seat->priv->read_buffer_n_used < 4 + payload_length)
233         return TRUE;
234     /*payload = seat->priv->read_buffer + 4;*/
235
236     switch (id)
237     {
238     case USC_MESSAGE_PING:
239         l_debug (seat, "PING!");
240         write_message (seat, USC_MESSAGE_PONG, NULL, 0);
241         break;
242     case USC_MESSAGE_PONG:
243         l_debug (seat, "PONG!");
244         break;
245     case USC_MESSAGE_READY:
246         l_debug (seat, "READY");
247         if (!seat->priv->compositor_ready)
248         {
249             seat->priv->compositor_ready = TRUE;
250             l_debug (seat, "Compositor ready");
251             g_source_remove (seat->priv->compositor_timeout);
252             seat->priv->compositor_timeout = 0;
253             SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
254         }
255         break;
256     case USC_MESSAGE_SESSION_CONNECTED:
257         l_debug (seat, "SESSION CONNECTED");
258         break;
259     default:
260         l_warning (seat, "Ignoring unknown message %d with %d octets from system compositor", id, payload_length);
261         break;
262     }
263
264     /* Clear buffer */
265     seat->priv->read_buffer_n_used = 0;
266
267     return TRUE;
268 }
269
270 static gchar *
271 get_absolute_command (const gchar *command)
272 {
273     gchar **tokens;
274     gchar *absolute_binary, *absolute_command = NULL;
275
276     tokens = g_strsplit (command, " ", 2);
277
278     absolute_binary = g_find_program_in_path (tokens[0]);
279     if (absolute_binary)
280     {
281         if (tokens[1])
282             absolute_command = g_strjoin (" ", absolute_binary, tokens[1], NULL);
283         else
284             absolute_command = g_strdup (absolute_binary);
285         g_free (absolute_binary);
286     }
287     else
288         absolute_command = g_strdup (command);
289
290     g_strfreev (tokens);
291
292     return absolute_command;
293 }
294
295 static gboolean
296 compositor_timeout_cb (gpointer data)
297 {
298     SeatUnity *seat = data;
299
300     /* Stop the compositor - it is not working */
301     process_stop (seat->priv->compositor_process);
302
303     return TRUE;
304 }
305
306 static gboolean
307 seat_unity_start (Seat *seat)
308 {
309     const gchar *compositor_command;
310     gchar *command, *absolute_command, *dir, *value;
311     gboolean result;
312     int timeout;
313
314     /* Replace Plymouth if it is running */
315     if (plymouth_get_is_active () && plymouth_has_active_vt ())
316     {
317         gint active_vt = vt_get_active ();
318         if (active_vt >= vt_get_min ())
319         {
320             SEAT_UNITY (seat)->priv->vt = active_vt;
321             plymouth_quit (TRUE);
322         }
323         else
324             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 ());
325     }
326     if (plymouth_get_is_active ())
327         plymouth_quit (FALSE);
328     if (SEAT_UNITY (seat)->priv->vt < 0)
329         SEAT_UNITY (seat)->priv->vt = vt_get_unused ();
330     if (SEAT_UNITY (seat)->priv->vt < 0)
331     {
332         l_debug (seat, "Failed to get a VT to run on");
333         return FALSE;
334     }
335     vt_ref (SEAT_UNITY (seat)->priv->vt);
336
337     /* Create pipes to talk to compositor */
338     if (pipe (SEAT_UNITY (seat)->priv->to_compositor_pipe) < 0 || pipe (SEAT_UNITY (seat)->priv->from_compositor_pipe) < 0)
339     {
340         l_debug (seat, "Failed to create compositor pipes: %s", g_strerror (errno));
341         return FALSE;
342     }
343
344     /* Don't allow the daemon end of the pipes to be accessed in the compositor */
345     fcntl (SEAT_UNITY (seat)->priv->to_compositor_pipe[1], F_SETFD, FD_CLOEXEC);
346     fcntl (SEAT_UNITY (seat)->priv->from_compositor_pipe[0], F_SETFD, FD_CLOEXEC);
347
348     /* Listen for messages from the compositor */
349     SEAT_UNITY (seat)->priv->from_compositor_channel = g_io_channel_unix_new (SEAT_UNITY (seat)->priv->from_compositor_pipe[0]);
350     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);
351
352     /* Setup logging */
353     dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
354     SEAT_UNITY (seat)->priv->log_file = g_build_filename (dir, "unity-system-compositor.log", NULL);
355     l_debug (seat, "Logging to %s", SEAT_UNITY (seat)->priv->log_file);
356     g_free (dir);
357
358     /* Setup environment */
359     process_set_clear_environment (SEAT_UNITY (seat)->priv->compositor_process, TRUE);
360     process_set_env (SEAT_UNITY (seat)->priv->compositor_process, "XDG_SEAT", "seat0");
361     value = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->vt);
362     process_set_env (SEAT_UNITY (seat)->priv->compositor_process, "XDG_VTNR", value);
363     g_free (value);
364     /* Variable required for regression tests */
365     if (g_getenv ("LIGHTDM_TEST_ROOT"))
366     {
367         process_set_env (SEAT_UNITY (seat)->priv->compositor_process, "LIGHTDM_TEST_ROOT", g_getenv ("LIGHTDM_TEST_ROOT"));
368         process_set_env (SEAT_UNITY (seat)->priv->compositor_process, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
369         process_set_env (SEAT_UNITY (seat)->priv->compositor_process, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
370     }
371
372     SEAT_UNITY (seat)->priv->mir_socket_filename = g_strdup ("/tmp/mir_socket"); // FIXME: Use this socket by default as XMir is hardcoded to this
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 --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);
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 ("x-%d", SEAT_UNITY (seat)->priv->next_x_server_id);
429         SEAT_UNITY (seat)->priv->next_x_server_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
508     return DISPLAY_SERVER (mir_server);
509 }
510
511 static DisplayServer *
512 seat_unity_create_display_server (Seat *seat, const gchar *session_type)
513 {  
514     if (strcmp (session_type, "x") == 0)
515         return create_x_server (seat);
516     else if (strcmp (session_type, "mir") == 0)
517         return create_mir_server (seat);
518     else
519     {
520         l_warning (seat, "Can't create unsupported display server '%s'", session_type);
521         return NULL;
522     }
523 }
524
525 static Greeter *
526 seat_unity_create_greeter_session (Seat *seat)
527 {
528     Greeter *greeter_session;
529     const gchar *xdg_seat;
530     gchar *id;
531     gint vt = -1;
532
533     greeter_session = SEAT_CLASS (seat_unity_parent_class)->create_greeter_session (seat);
534     xdg_seat = seat_get_string_property (seat, "xdg-seat");
535     if (!xdg_seat)
536         xdg_seat = "seat0";
537     l_debug (seat, "Setting XDG_SEAT=%s", xdg_seat);
538     session_set_env (SESSION (greeter_session), "XDG_SEAT", xdg_seat);
539
540     id = g_strdup_printf ("greeter-%d", SEAT_UNITY (seat)->priv->next_greeter_id);
541     SEAT_UNITY (seat)->priv->next_greeter_id++;
542     session_set_env (SESSION (greeter_session), "MIR_SERVER_NAME", id);
543     g_free (id);
544
545     if (!SEAT_UNITY (seat)->priv->use_vt_switching)
546         vt = SEAT_UNITY (seat)->priv->vt;
547
548     if (vt >= 0)
549     {
550         gchar *value = g_strdup_printf ("%d", vt);
551         l_debug (seat, "Setting XDG_VTNR=%s", value);
552         session_set_env (SESSION (greeter_session), "XDG_VTNR", value);
553         g_free (value);
554     }
555     else
556         l_debug (seat, "Not setting XDG_VTNR");
557
558     return greeter_session;
559 }
560
561 static Session *
562 seat_unity_create_session (Seat *seat)
563 {
564     Session *session;
565     const gchar *xdg_seat;
566     gchar *id;
567     gint vt = -1;
568
569     session = SEAT_CLASS (seat_unity_parent_class)->create_session (seat);
570     xdg_seat = seat_get_string_property (seat, "xdg-seat");
571     if (!xdg_seat)
572         xdg_seat = "seat0";
573     l_debug (seat, "Setting XDG_SEAT=%s", xdg_seat);
574     session_set_env (session, "XDG_SEAT", xdg_seat);
575
576     id = g_strdup_printf ("session-%d", SEAT_UNITY (seat)->priv->next_session_id);
577     SEAT_UNITY (seat)->priv->next_session_id++;
578     session_set_env (session, "MIR_SERVER_NAME", id);
579     g_free (id);
580
581     if (!SEAT_UNITY (seat)->priv->use_vt_switching)
582         vt = SEAT_UNITY (seat)->priv->vt;
583
584     if (vt >= 0)
585     {
586         gchar *value = g_strdup_printf ("%d", vt);
587         l_debug (seat, "Setting XDG_VTNR=%s", value);
588         session_set_env (SESSION (session), "XDG_VTNR", value);
589         g_free (value);
590     }
591     else
592         l_debug (seat, "Not setting XDG_VTNR");
593
594     return session;
595 }
596
597 static void
598 seat_unity_set_active_session (Seat *seat, Session *session)
599 {
600     DisplayServer *display_server;
601
602     /* If no compositor, have to use VT switching */
603     if (SEAT_UNITY (seat)->priv->use_vt_switching)
604     {
605         gint vt = display_server_get_vt (session_get_display_server (session));
606         if (vt >= 0)
607             vt_set_active (vt);
608
609         SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
610         return;
611     }
612
613     if (session == SEAT_UNITY (seat)->priv->active_session)
614         return;
615     SEAT_UNITY (seat)->priv->active_session = g_object_ref (session);
616
617     display_server = session_get_display_server (session);
618     if (SEAT_UNITY (seat)->priv->active_display_server != display_server)
619     {
620         const gchar *id = NULL;
621
622         SEAT_UNITY (seat)->priv->active_display_server = g_object_ref (display_server);
623
624         if (IS_X_SERVER_LOCAL (display_server))
625             id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
626         else
627             id = session_get_env (session, "MIR_SERVER_NAME");
628
629         if (id)
630         {
631             l_debug (seat, "Switching to Mir session %s", id);
632             write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_ACTIVE_SESSION, (const guint8 *) id, strlen (id));
633         }
634         else
635             l_warning (seat, "Failed to work out session ID");
636     }
637
638     SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
639 }
640
641 static Session *
642 seat_unity_get_active_session (Seat *seat)
643 {
644     if (SEAT_UNITY (seat)->priv->use_vt_switching)
645     {
646         gint vt;
647         GList *link;
648         vt = vt_get_active ();
649         if (vt < 0)
650             return NULL;
651
652         for (link = seat_get_sessions (seat); link; link = link->next)
653         {
654             Session *session = link->data;
655             if (display_server_get_vt (session_get_display_server (session)) == vt)
656                 return session;
657         }
658
659         return NULL;
660     }
661
662     return SEAT_UNITY (seat)->priv->active_session;
663 }
664
665 static void
666 seat_unity_set_next_session (Seat *seat, Session *session)
667 {
668     DisplayServer *display_server;
669     const gchar *id = NULL;
670
671     if (!session)
672         return;
673
674     /* If no compositor, don't worry about it */
675     if (SEAT_UNITY (seat)->priv->use_vt_switching)
676         return;
677
678     display_server = session_get_display_server (session);
679
680     if (IS_X_SERVER_LOCAL (display_server))
681         id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
682     else
683         id = session_get_env (session, "MIR_SESSION_NAME");
684
685     if (id)
686     {
687         l_debug (seat, "Marking Mir session %s as the next session", id);
688         write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_NEXT_SESSION, (const guint8 *) id, strlen (id));
689     }
690     else
691     {
692         l_debug (seat, "Failed to work out session ID to mark");
693     }
694
695     SEAT_CLASS (seat_unity_parent_class)->set_next_session (seat, session);
696 }
697
698 static void
699 seat_unity_run_script (Seat *seat, DisplayServer *display_server, Process *script)
700 {
701     const gchar *path;
702     XServerLocal *x_server;
703
704     x_server = X_SERVER_LOCAL (display_server);
705     path = x_server_local_get_authority_file_path (x_server);
706     process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
707     process_set_env (script, "XAUTHORITY", path);
708
709     SEAT_CLASS (seat_unity_parent_class)->run_script (seat, display_server, script);
710 }
711
712 static void
713 seat_unity_stop (Seat *seat)
714 {
715     /* Stop the compositor first */
716     if (process_get_is_running (SEAT_UNITY (seat)->priv->compositor_process))
717     {
718         process_stop (SEAT_UNITY (seat)->priv->compositor_process);
719         return;
720     }
721
722     SEAT_CLASS (seat_unity_parent_class)->stop (seat);
723 }
724
725 static void
726 seat_unity_init (SeatUnity *seat)
727 {
728     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_UNITY_TYPE, SeatUnityPrivate);
729     seat->priv->vt = -1;
730     seat->priv->compositor_process = process_new ();
731 }
732
733 static void
734 seat_unity_finalize (GObject *object)
735 {
736     SeatUnity *seat = SEAT_UNITY (object);
737
738     if (seat->priv->vt >= 0)
739         vt_unref (seat->priv->vt);
740     g_free (seat->priv->log_file);
741     g_free (seat->priv->mir_socket_filename);
742     close (seat->priv->to_compositor_pipe[0]);
743     close (seat->priv->to_compositor_pipe[1]);
744     close (seat->priv->from_compositor_pipe[0]);
745     close (seat->priv->from_compositor_pipe[1]);
746     g_io_channel_unref (seat->priv->from_compositor_channel);
747     g_source_remove (seat->priv->from_compositor_watch);
748     g_free (seat->priv->read_buffer);
749     g_object_unref (seat->priv->compositor_process);
750     if (seat->priv->active_session)
751         g_object_unref (seat->priv->active_session);
752     if (seat->priv->active_display_server)
753         g_object_unref (seat->priv->active_display_server);
754
755     G_OBJECT_CLASS (seat_unity_parent_class)->finalize (object);
756 }
757
758 static void
759 seat_unity_class_init (SeatUnityClass *klass)
760 {
761     GObjectClass *object_class = G_OBJECT_CLASS (klass);
762     SeatClass *seat_class = SEAT_CLASS (klass);
763
764     object_class->finalize = seat_unity_finalize;
765     seat_class->get_start_local_sessions = seat_unity_get_start_local_sessions;
766     seat_class->setup = seat_unity_setup;
767     seat_class->start = seat_unity_start;
768     seat_class->create_display_server = seat_unity_create_display_server;
769     seat_class->create_greeter_session = seat_unity_create_greeter_session;
770     seat_class->create_session = seat_unity_create_session;
771     seat_class->set_active_session = seat_unity_set_active_session;
772     seat_class->get_active_session = seat_unity_get_active_session;
773     seat_class->set_next_session = seat_unity_set_next_session;
774     seat_class->run_script = seat_unity_run_script;
775     seat_class->stop = seat_unity_stop;
776
777     g_type_class_add_private (klass, sizeof (SeatUnityPrivate));
778 }