]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/unity-system-compositor.c
Launchpad automatic translations update.
[sojka/lightdm.git] / src / unity-system-compositor.c
1 /*
2  * Copyright (C) 2013 Canonical Ltd.
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 <config.h>
13 #include <string.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <sys/stat.h>
17 #include <errno.h>
18 #include <glib/gstdio.h>
19 #include <stdlib.h>
20
21 #include "unity-system-compositor.h"
22 #include "configuration.h"
23 #include "process.h"
24 #include "greeter.h"
25 #include "vt.h"
26
27 struct UnitySystemCompositorPrivate
28 {
29     /* Compositor process */
30     Process *process;
31
32     /* Command to run the compositor */
33     gchar *command;
34
35     /* Socket to communicate on */
36     gchar *socket;
37
38     /* VT to run on */
39     gint vt;
40     gboolean have_vt_ref;
41
42     /* TRUE if should show hardware cursor */
43     gboolean enable_hardware_cursor;
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     /* Buffer reading from channel */
54     guint8 *read_buffer;
55     gsize read_buffer_length;
56     gsize read_buffer_n_used;
57
58     /* Timeout when waiting for compositor to start */
59     gint timeout;
60     guint timeout_source;
61
62     /* TRUE when received ready signal */
63     gboolean is_ready;
64 };
65
66 G_DEFINE_TYPE (UnitySystemCompositor, unity_system_compositor, DISPLAY_SERVER_TYPE);
67
68 typedef enum
69 {
70    USC_MESSAGE_PING = 0,
71    USC_MESSAGE_PONG = 1,
72    USC_MESSAGE_READY = 2,
73    USC_MESSAGE_SESSION_CONNECTED = 3,
74    USC_MESSAGE_SET_ACTIVE_SESSION = 4,
75    USC_MESSAGE_SET_NEXT_SESSION = 5,
76 } USCMessageID;
77
78 UnitySystemCompositor *
79 unity_system_compositor_new (void)
80 {
81     return g_object_new (UNITY_SYSTEM_COMPOSITOR_TYPE, NULL);
82 }
83
84 void
85 unity_system_compositor_set_command (UnitySystemCompositor *compositor, const gchar *command)
86 {
87     g_return_if_fail (compositor != NULL);
88     g_free (compositor->priv->command);
89     compositor->priv->command = g_strdup (command);
90 }
91
92 void
93 unity_system_compositor_set_socket (UnitySystemCompositor *compositor, const gchar *socket)
94 {
95     g_return_if_fail (compositor != NULL);
96     g_free (compositor->priv->socket);
97     compositor->priv->socket = g_strdup (socket);
98 }
99
100 const gchar *
101 unity_system_compositor_get_socket (UnitySystemCompositor *compositor)
102 {
103     g_return_val_if_fail (compositor != NULL, NULL);
104     return compositor->priv->socket;
105 }
106
107 void
108 unity_system_compositor_set_vt (UnitySystemCompositor *compositor, gint vt)
109 {
110     g_return_if_fail (compositor != NULL);
111
112     if (compositor->priv->have_vt_ref)
113         vt_unref (compositor->priv->vt);
114     compositor->priv->have_vt_ref = FALSE;
115     compositor->priv->vt = vt;
116     if (vt > 0)
117     {
118         vt_ref (vt);
119         compositor->priv->have_vt_ref = TRUE;
120     }
121 }
122
123 void
124 unity_system_compositor_set_enable_hardware_cursor (UnitySystemCompositor *compositor, gboolean enable_cursor)
125 {
126     g_return_if_fail (compositor != NULL);
127     compositor->priv->enable_hardware_cursor = enable_cursor;
128 }
129
130 void
131 unity_system_compositor_set_timeout (UnitySystemCompositor *compositor, gint timeout)
132 {
133     g_return_if_fail (compositor != NULL);
134     compositor->priv->timeout = timeout;
135 }
136
137 static void
138 write_message (UnitySystemCompositor *compositor, guint16 id, const guint8 *payload, guint16 payload_length)
139 {
140     guint8 *data;
141     gsize data_length = 4 + payload_length;
142
143     data = g_malloc (data_length);
144     data[0] = id >> 8;
145     data[1] = id & 0xFF;
146     data[2] = payload_length >> 8;
147     data[3] = payload_length & 0xFF;
148     memcpy (data + 4, payload, payload_length);
149
150     errno = 0;
151     if (write (compositor->priv->to_compositor_pipe[1], data, data_length) != data_length)
152         l_warning (compositor, "Failed to write to compositor: %s", strerror (errno));
153 }
154
155 void
156 unity_system_compositor_set_active_session (UnitySystemCompositor *compositor, const gchar *id)
157 {
158     g_return_if_fail (compositor != NULL);
159     write_message (compositor, USC_MESSAGE_SET_ACTIVE_SESSION, (const guint8 *) id, strlen (id));
160 }
161
162 void
163 unity_system_compositor_set_next_session (UnitySystemCompositor *compositor, const gchar *id)
164 {
165     g_return_if_fail (compositor != NULL);
166     write_message (compositor, USC_MESSAGE_SET_NEXT_SESSION, (const guint8 *) id, strlen (id));
167 }
168
169 static gint
170 unity_system_compositor_get_vt (DisplayServer *server)
171 {
172     g_return_val_if_fail (server != NULL, 0);
173     return UNITY_SYSTEM_COMPOSITOR (server)->priv->vt;
174 }
175
176 static void
177 unity_system_compositor_connect_session (DisplayServer *display_server, Session *session)
178 {
179     UnitySystemCompositor *compositor = UNITY_SYSTEM_COMPOSITOR (display_server);
180     const gchar *name;
181
182     session_set_env (session, "XDG_SESSION_TYPE", "mir");
183
184     if (compositor->priv->socket)
185         session_set_env (session, "MIR_SOCKET", compositor->priv->socket);
186     if (IS_GREETER (session))
187         name = "greeter-0";
188     else
189         name = "session-0";
190     session_set_env (session, "MIR_SERVER_NAME", name);
191
192     if (compositor->priv->vt >= 0)
193     {
194         gchar *value = g_strdup_printf ("%d", compositor->priv->vt);
195         session_set_env (session, "XDG_VTNR", value);
196         g_free (value);
197     }
198 }
199
200 static void
201 unity_system_compositor_disconnect_session (DisplayServer *display_server, Session *session)
202 {
203     session_unset_env (session, "XDG_SESSION_TYPE");
204     session_unset_env (session, "MIR_SOCKET");
205     session_unset_env (session, "MIR_SERVER_NAME");
206     session_unset_env (session, "XDG_VTNR");
207 }
208
209 static gchar *
210 get_absolute_command (const gchar *command)
211 {
212     gchar **tokens;
213     gchar *absolute_binary, *absolute_command = NULL;
214
215     tokens = g_strsplit (command, " ", 2);
216
217     absolute_binary = g_find_program_in_path (tokens[0]);
218     if (absolute_binary)
219     {
220         if (tokens[1])
221             absolute_command = g_strjoin (" ", absolute_binary, tokens[1], NULL);
222         else
223             absolute_command = g_strdup (absolute_binary);
224     }
225     g_free (absolute_binary);
226
227     g_strfreev (tokens);
228
229     return absolute_command;
230 }
231
232 static gboolean
233 read_cb (GIOChannel *source, GIOCondition condition, gpointer data)
234 {
235     UnitySystemCompositor *compositor = data;
236     gsize n_to_read = 0;
237     guint16 id, payload_length;
238     /*guint8 *payload;*/
239   
240     if (condition == G_IO_HUP)
241     {
242         l_debug (compositor, "Compositor closed communication channel");
243         compositor->priv->from_compositor_watch = 0;
244         return FALSE;
245     }
246
247     /* Work out how much required for a message */
248     if (compositor->priv->read_buffer_n_used < 4)
249         n_to_read = 4 - compositor->priv->read_buffer_n_used;
250     else
251     {
252         payload_length = compositor->priv->read_buffer[2] << 8 | compositor->priv->read_buffer[3];
253         n_to_read = 4 + payload_length - compositor->priv->read_buffer_n_used;
254     }
255
256     /* Read from compositor */
257     if (n_to_read > 0)
258     {
259         gsize n_total, n_read = 0;
260         GIOStatus status;
261         GError *error = NULL;
262
263         n_total = compositor->priv->read_buffer_n_used + n_to_read;
264         if (compositor->priv->read_buffer_length < n_total)
265             compositor->priv->read_buffer = g_realloc (compositor->priv->read_buffer, n_total);
266
267         status = g_io_channel_read_chars (source,
268                                           (gchar *)compositor->priv->read_buffer + compositor->priv->read_buffer_n_used,
269                                           n_to_read,
270                                           &n_read,
271                                           &error);
272         if (error)
273             l_warning (compositor, "Failed to read from compositor: %s", error->message);
274         if (status != G_IO_STATUS_NORMAL)
275             return TRUE;
276         g_clear_error (&error);
277         compositor->priv->read_buffer_n_used += n_read;
278     }
279
280     /* Read header */
281     if (compositor->priv->read_buffer_n_used < 4)
282          return TRUE;
283     id = compositor->priv->read_buffer[0] << 8 | compositor->priv->read_buffer[1];
284     payload_length = compositor->priv->read_buffer[2] << 8 | compositor->priv->read_buffer[3];
285
286     /* Read payload */
287     if (compositor->priv->read_buffer_n_used < 4 + payload_length)
288         return TRUE;
289     /*payload = compositor->priv->read_buffer + 4;*/
290
291     switch (id)
292     {
293     case USC_MESSAGE_PING:
294         l_debug (compositor, "PING!");
295         write_message (compositor, USC_MESSAGE_PONG, NULL, 0);
296         break;
297     case USC_MESSAGE_PONG:
298         l_debug (compositor, "PONG!");
299         break;
300     case USC_MESSAGE_READY:
301         l_debug (compositor, "READY");
302         if (!compositor->priv->is_ready)
303         {
304             compositor->priv->is_ready = TRUE;
305             l_debug (compositor, "Compositor ready");
306             g_source_remove (compositor->priv->timeout_source);
307             compositor->priv->timeout_source = 0;
308             DISPLAY_SERVER_CLASS (unity_system_compositor_parent_class)->start (DISPLAY_SERVER (compositor));
309         }
310         break;
311     case USC_MESSAGE_SESSION_CONNECTED:
312         l_debug (compositor, "SESSION CONNECTED");
313         break;
314     default:
315         l_warning (compositor, "Ignoring unknown message %d with %d octets from system compositor", id, payload_length);
316         break;
317     }
318
319     /* Clear buffer */
320     compositor->priv->read_buffer_n_used = 0;
321
322     return TRUE;
323 }
324
325 static void
326 run_cb (Process *process, gpointer user_data)
327 {
328     int fd;
329
330     /* Make input non-blocking */
331     fd = open ("/dev/null", O_RDONLY);
332     dup2 (fd, STDIN_FILENO);
333     close (fd);
334 }
335
336 static gboolean
337 timeout_cb (gpointer data)
338 {
339     UnitySystemCompositor *compositor = data;
340
341     /* Stop the compositor - it is not working */
342     display_server_stop (DISPLAY_SERVER (compositor));
343
344     compositor->priv->timeout_source = 0;
345
346     return TRUE;
347 }
348
349 static void
350 stopped_cb (Process *process, UnitySystemCompositor *compositor)
351 {
352     l_debug (compositor, "Unity system compositor stopped");
353
354     if (compositor->priv->timeout_source != 0)
355         g_source_remove (compositor->priv->timeout_source);
356     compositor->priv->timeout_source = 0;
357
358     /* Release VT and display number for re-use */
359     if (compositor->priv->have_vt_ref)
360     {
361         vt_unref (compositor->priv->vt);
362         compositor->priv->have_vt_ref = FALSE;
363     }
364
365     DISPLAY_SERVER_CLASS (unity_system_compositor_parent_class)->stop (DISPLAY_SERVER (compositor));
366 }
367
368 static gboolean
369 unity_system_compositor_start (DisplayServer *server)
370 {
371     UnitySystemCompositor *compositor = UNITY_SYSTEM_COMPOSITOR (server);
372     gboolean result;
373     GString *command;
374     gchar *dir, *log_file, *absolute_command, *value;
375
376     g_return_val_if_fail (compositor->priv->process == NULL, FALSE);
377
378     compositor->priv->is_ready = FALSE;
379
380     g_return_val_if_fail (compositor->priv->command != NULL, FALSE);
381
382     /* Create pipes to talk to compositor */
383     if (pipe (compositor->priv->to_compositor_pipe) < 0 || pipe (compositor->priv->from_compositor_pipe) < 0)
384     {
385         l_debug (compositor, "Failed to create compositor pipes: %s", g_strerror (errno));
386         return FALSE;
387     }
388
389     /* Don't allow the daemon end of the pipes to be accessed in the compositor */
390     fcntl (compositor->priv->to_compositor_pipe[1], F_SETFD, FD_CLOEXEC);
391     fcntl (compositor->priv->from_compositor_pipe[0], F_SETFD, FD_CLOEXEC);
392
393     /* Listen for messages from the compositor */
394     compositor->priv->from_compositor_channel = g_io_channel_unix_new (compositor->priv->from_compositor_pipe[0]);
395     compositor->priv->from_compositor_watch = g_io_add_watch (compositor->priv->from_compositor_channel, G_IO_IN | G_IO_HUP, read_cb, compositor);
396
397     /* Setup logging */
398     dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
399     log_file = g_build_filename (dir, "unity-system-compositor.log", NULL);
400     l_debug (compositor, "Logging to %s", log_file);
401     g_free (dir);
402
403     /* Setup environment */
404     compositor->priv->process = process_new (run_cb, compositor);
405     process_set_log_file (compositor->priv->process, log_file, TRUE);
406     g_free (log_file);
407     process_set_clear_environment (compositor->priv->process, TRUE);
408     process_set_env (compositor->priv->process, "XDG_SEAT", "seat0");
409     value = g_strdup_printf ("%d", compositor->priv->vt);
410     process_set_env (compositor->priv->process, "XDG_VTNR", value);
411     g_free (value);
412     /* Variable required for regression tests */
413     if (g_getenv ("LIGHTDM_TEST_ROOT"))
414     {
415         process_set_env (compositor->priv->process, "LIGHTDM_TEST_ROOT", g_getenv ("LIGHTDM_TEST_ROOT"));
416         process_set_env (compositor->priv->process, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
417         process_set_env (compositor->priv->process, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
418     }
419
420     /* Generate command line to run */
421     absolute_command = get_absolute_command (compositor->priv->command);
422     if (!absolute_command)
423     {
424         l_debug (compositor, "Can't launch compositor %s, not found in path", compositor->priv->command);
425         return FALSE;
426     }
427     command = g_string_new (absolute_command);
428     g_free (absolute_command);
429     g_string_append_printf (command, " --file '%s'", compositor->priv->socket);
430     g_string_append_printf (command, " --from-dm-fd %d --to-dm-fd %d", compositor->priv->to_compositor_pipe[0], compositor->priv->from_compositor_pipe[1]);
431     if (compositor->priv->vt > 0)
432         g_string_append_printf (command, " --vt %d", compositor->priv->vt);
433     if (compositor->priv->enable_hardware_cursor)
434         g_string_append (command, " --enable-hardware-cursor=true");
435     process_set_command (compositor->priv->process, command->str);
436     g_string_free (command, TRUE);
437
438     /* Start the compositor */
439     g_signal_connect (compositor->priv->process, "stopped", G_CALLBACK (stopped_cb), compositor);
440     result = process_start (compositor->priv->process, FALSE);
441
442     /* Close compostor ends of the pipes */
443     close (compositor->priv->to_compositor_pipe[0]);
444     compositor->priv->to_compositor_pipe[0] = 0;
445     close (compositor->priv->from_compositor_pipe[1]);
446     compositor->priv->from_compositor_pipe[1] = 0;
447
448     if (!result)
449         return FALSE;
450
451     /* Connect to the compositor */
452     if (compositor->priv->timeout > 0)
453     {
454         l_debug (compositor, "Waiting for system compositor for %ds", compositor->priv->timeout);
455         compositor->priv->timeout_source = g_timeout_add (compositor->priv->timeout * 1000, timeout_cb, compositor);
456     }
457
458     return TRUE;
459 }
460  
461 static void
462 unity_system_compositor_stop (DisplayServer *server)
463 {
464     process_stop (UNITY_SYSTEM_COMPOSITOR (server)->priv->process);
465 }
466
467 static void
468 unity_system_compositor_init (UnitySystemCompositor *compositor)
469 {
470     compositor->priv = G_TYPE_INSTANCE_GET_PRIVATE (compositor, UNITY_SYSTEM_COMPOSITOR_TYPE, UnitySystemCompositorPrivate);
471     compositor->priv->vt = -1;
472     compositor->priv->command = g_strdup ("unity-system-compositor");
473     compositor->priv->socket = g_strdup ("/run/mir_socket");
474     compositor->priv->timeout = -1;
475 }
476
477 static void
478 unity_system_compositor_finalize (GObject *object)
479 {
480     UnitySystemCompositor *self;
481
482     self = UNITY_SYSTEM_COMPOSITOR (object);  
483
484     if (self->priv->process)
485     {
486         g_signal_handlers_disconnect_matched (self->priv->process, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
487         g_object_unref (self->priv->process);
488     }
489     g_free (self->priv->command);
490     g_free (self->priv->socket);
491     if (self->priv->have_vt_ref)
492         vt_unref (self->priv->vt);
493     close (self->priv->to_compositor_pipe[0]);
494     close (self->priv->to_compositor_pipe[1]);
495     close (self->priv->from_compositor_pipe[0]);
496     close (self->priv->from_compositor_pipe[1]);
497     g_io_channel_unref (self->priv->from_compositor_channel);
498     if (self->priv->from_compositor_watch)      
499         g_source_remove (self->priv->from_compositor_watch);
500     g_free (self->priv->read_buffer);
501     if (self->priv->timeout_source)
502         g_source_remove (self->priv->timeout_source);
503
504     G_OBJECT_CLASS (unity_system_compositor_parent_class)->finalize (object);
505 }
506
507 static void
508 unity_system_compositor_class_init (UnitySystemCompositorClass *klass)
509 {
510     GObjectClass *object_class = G_OBJECT_CLASS (klass);
511     DisplayServerClass *display_server_class = DISPLAY_SERVER_CLASS (klass);
512
513     display_server_class->get_vt = unity_system_compositor_get_vt;
514     display_server_class->connect_session = unity_system_compositor_connect_session;
515     display_server_class->disconnect_session = unity_system_compositor_disconnect_session;
516     display_server_class->start = unity_system_compositor_start;
517     display_server_class->stop = unity_system_compositor_stop;
518     object_class->finalize = unity_system_compositor_finalize;
519
520     g_type_class_add_private (klass, sizeof (UnitySystemCompositorPrivate));
521 }