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