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