]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat-unity.c
Update environment variables that we pass to Mir.
[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 --standalone --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)
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         id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
510         SEAT_UNITY (seat)->priv->next_id++;
511         mir_server_set_id (mir_server, id);
512         g_free (id);
513     }   
514
515     return DISPLAY_SERVER (mir_server);
516 }
517
518 static DisplayServer *
519 seat_unity_create_display_server (Seat *seat, const gchar *session_type)
520 {  
521     if (strcmp (session_type, "x") == 0)
522         return create_x_server (seat);
523     else if (strcmp (session_type, "mir") == 0)
524         return create_mir_server (seat);
525     else
526     {
527         l_warning (seat, "Can't create unsupported display server '%s'", session_type);
528         return NULL;
529     }
530 }
531
532 static Greeter *
533 seat_unity_create_greeter_session (Seat *seat)
534 {
535     Greeter *greeter_session;
536     const gchar *xdg_seat;
537     gint vt = -1;
538
539     greeter_session = SEAT_CLASS (seat_unity_parent_class)->create_greeter_session (seat);
540     xdg_seat = seat_get_string_property (seat, "xdg-seat");
541     if (!xdg_seat)
542         xdg_seat = "seat0";
543     l_debug (seat, "Setting XDG_SEAT=%s", xdg_seat);
544     session_set_env (SESSION (greeter_session), "XDG_SEAT", xdg_seat);
545
546     if (!SEAT_UNITY (seat)->priv->use_vt_switching)
547         vt = SEAT_UNITY (seat)->priv->vt;
548
549     if (vt >= 0)
550     {
551         gchar *value = g_strdup_printf ("%d", vt);
552         l_debug (seat, "Setting XDG_VTNR=%s", value);
553         session_set_env (SESSION (greeter_session), "XDG_VTNR", value);
554         g_free (value);
555     }
556     else
557         l_debug (seat, "Not setting XDG_VTNR");
558
559     return greeter_session;
560 }
561
562 static Session *
563 seat_unity_create_session (Seat *seat)
564 {
565     Session *session;
566     const gchar *xdg_seat;
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     if (!SEAT_UNITY (seat)->priv->use_vt_switching)
577         vt = SEAT_UNITY (seat)->priv->vt;
578
579     if (vt >= 0)
580     {
581         gchar *value = g_strdup_printf ("%d", vt);
582         l_debug (seat, "Setting XDG_VTNR=%s", value);
583         session_set_env (SESSION (session), "XDG_VTNR", value);
584         g_free (value);
585     }
586     else
587         l_debug (seat, "Not setting XDG_VTNR");
588
589     return session;
590 }
591
592 static void
593 seat_unity_set_active_session (Seat *seat, Session *session)
594 {
595     DisplayServer *display_server;
596
597     /* If no compositor, have to use VT switching */
598     if (SEAT_UNITY (seat)->priv->use_vt_switching)
599     {
600         gint vt = display_server_get_vt (session_get_display_server (session));
601         if (vt >= 0)
602             vt_set_active (vt);
603
604         SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
605         return;
606     }
607
608     if (session == SEAT_UNITY (seat)->priv->active_session)
609         return;
610     SEAT_UNITY (seat)->priv->active_session = g_object_ref (session);
611
612     display_server = session_get_display_server (session);
613     if (SEAT_UNITY (seat)->priv->active_display_server != display_server)
614     {
615         const gchar *id = NULL;
616
617         SEAT_UNITY (seat)->priv->active_display_server = g_object_ref (display_server);
618
619         if (IS_X_SERVER_LOCAL (display_server))
620             id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
621         else if (IS_MIR_SERVER (display_server))
622             id = mir_server_get_id (MIR_SERVER (display_server));
623
624         if (id)
625         {
626             l_debug (seat, "Switching to Mir session %s", id);
627             write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_ACTIVE_SESSION, (const guint8 *) id, strlen (id));
628         }
629         else
630             l_warning (seat, "Failed to work out session ID");
631     }
632
633     SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
634 }
635
636 static Session *
637 seat_unity_get_active_session (Seat *seat)
638 {
639     if (SEAT_UNITY (seat)->priv->use_vt_switching)
640     {
641         gint vt;
642         GList *link;
643         vt = vt_get_active ();
644         if (vt < 0)
645             return NULL;
646
647         for (link = seat_get_sessions (seat); link; link = link->next)
648         {
649             Session *session = link->data;
650             if (display_server_get_vt (session_get_display_server (session)) == vt)
651                 return session;
652         }
653
654         return NULL;
655     }
656
657     return SEAT_UNITY (seat)->priv->active_session;
658 }
659
660 static void
661 seat_unity_set_next_session (Seat *seat, Session *session)
662 {
663     DisplayServer *display_server;
664     const gchar *id = NULL;
665
666     if (!session)
667         return;
668
669     /* If no compositor, don't worry about it */
670     if (SEAT_UNITY (seat)->priv->use_vt_switching)
671         return;
672
673     display_server = session_get_display_server (session);
674
675     if (IS_X_SERVER_LOCAL (display_server))
676         id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
677     else if (IS_MIR_SERVER (display_server))
678         id = mir_server_get_id (MIR_SERVER (display_server));
679
680     if (id)
681     {
682         l_debug (seat, "Marking Mir session %s as the next session", id);
683         write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_NEXT_SESSION, (const guint8 *) id, strlen (id));
684     }
685     else
686     {
687         l_debug (seat, "Failed to work out session ID to mark");
688     }
689
690     SEAT_CLASS (seat_unity_parent_class)->set_next_session (seat, session);
691 }
692
693 static void
694 seat_unity_run_script (Seat *seat, DisplayServer *display_server, Process *script)
695 {
696     const gchar *path;
697     XServerLocal *x_server;
698
699     x_server = X_SERVER_LOCAL (display_server);
700     path = x_server_local_get_authority_file_path (x_server);
701     process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
702     process_set_env (script, "XAUTHORITY", path);
703
704     SEAT_CLASS (seat_unity_parent_class)->run_script (seat, display_server, script);
705 }
706
707 static void
708 seat_unity_stop (Seat *seat)
709 {
710     /* Stop the compositor first */
711     if (process_get_is_running (SEAT_UNITY (seat)->priv->compositor_process))
712     {
713         process_stop (SEAT_UNITY (seat)->priv->compositor_process);
714         return;
715     }
716
717     SEAT_CLASS (seat_unity_parent_class)->stop (seat);
718 }
719
720 static void
721 seat_unity_init (SeatUnity *seat)
722 {
723     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_UNITY_TYPE, SeatUnityPrivate);
724     seat->priv->vt = -1;
725     seat->priv->compositor_process = process_new ();
726 }
727
728 static void
729 seat_unity_finalize (GObject *object)
730 {
731     SeatUnity *seat = SEAT_UNITY (object);
732
733     if (seat->priv->vt >= 0)
734         vt_unref (seat->priv->vt);
735     g_free (seat->priv->log_file);
736     g_free (seat->priv->mir_socket_filename);
737     close (seat->priv->to_compositor_pipe[0]);
738     close (seat->priv->to_compositor_pipe[1]);
739     close (seat->priv->from_compositor_pipe[0]);
740     close (seat->priv->from_compositor_pipe[1]);
741     g_io_channel_unref (seat->priv->from_compositor_channel);
742     g_source_remove (seat->priv->from_compositor_watch);
743     g_free (seat->priv->read_buffer);
744     g_object_unref (seat->priv->compositor_process);
745     if (seat->priv->active_session)
746         g_object_unref (seat->priv->active_session);
747     if (seat->priv->active_display_server)
748         g_object_unref (seat->priv->active_display_server);
749
750     G_OBJECT_CLASS (seat_unity_parent_class)->finalize (object);
751 }
752
753 static void
754 seat_unity_class_init (SeatUnityClass *klass)
755 {
756     GObjectClass *object_class = G_OBJECT_CLASS (klass);
757     SeatClass *seat_class = SEAT_CLASS (klass);
758
759     object_class->finalize = seat_unity_finalize;
760     seat_class->get_start_local_sessions = seat_unity_get_start_local_sessions;
761     seat_class->setup = seat_unity_setup;
762     seat_class->start = seat_unity_start;
763     seat_class->create_display_server = seat_unity_create_display_server;
764     seat_class->create_greeter_session = seat_unity_create_greeter_session;
765     seat_class->create_session = seat_unity_create_session;
766     seat_class->set_active_session = seat_unity_set_active_session;
767     seat_class->get_active_session = seat_unity_get_active_session;
768     seat_class->set_next_session = seat_unity_set_next_session;
769     seat_class->run_script = seat_unity_run_script;
770     seat_class->stop = seat_unity_stop;
771
772     g_type_class_add_private (klass, sizeof (SeatUnityPrivate));
773 }