1 /*
2  * Copyright (C) 2010-2011 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 <stdlib.h>
13 #include <sys/wait.h>
14 
15 #include "plymouth.h"
16 
17 static gboolean have_pinged = FALSE;
18 static gboolean have_checked_active_vt = FALSE;
19 
20 static gboolean is_running = FALSE;
21 static gboolean is_active = FALSE;
22 static gboolean has_active_vt = FALSE;
23 
24 static gboolean
plymouth_run_command(const gchar * command,gint * exit_status)25 plymouth_run_command (const gchar *command, gint *exit_status)
26 {
27     g_autofree gchar *command_line = g_strdup_printf ("plymouth %s", command);
28     g_autoptr(GError) error = NULL;
29     gboolean result = g_spawn_command_line_sync (command_line, NULL, NULL, exit_status, &error);
30 
31     if (error)
32         g_debug ("Could not run %s: %s", command_line, error->message);
33 
34     return result;
35 }
36 
37 static gboolean
plymouth_command_returns_true(gchar * command)38 plymouth_command_returns_true (gchar *command)
39 {
40     gint exit_status;
41     if (!plymouth_run_command (command, &exit_status))
42         return FALSE;
43     return WIFEXITED (exit_status) && WEXITSTATUS (exit_status) == 0;
44 }
45 
46 gboolean
plymouth_get_is_running(void)47 plymouth_get_is_running (void)
48 {
49     if (!have_pinged)
50     {
51         have_pinged = TRUE;
52         is_running = plymouth_command_returns_true ("--ping");
53         is_active = is_running;
54     }
55 
56     return is_running;
57 }
58 
59 gboolean
plymouth_get_is_active(void)60 plymouth_get_is_active (void)
61 {
62     return plymouth_get_is_running () && is_active;
63 }
64 
65 gboolean
plymouth_has_active_vt(void)66 plymouth_has_active_vt (void)
67 {
68     if (!have_checked_active_vt)
69     {
70         have_checked_active_vt = TRUE;
71         has_active_vt = plymouth_command_returns_true ("--has-active-vt");
72     }
73 
74     return has_active_vt;
75 }
76 
77 void
plymouth_deactivate(void)78 plymouth_deactivate (void)
79 {
80     g_debug ("Deactivating Plymouth");
81     is_active = FALSE;
82     plymouth_run_command ("deactivate", NULL);
83 }
84 
85 void
plymouth_quit(gboolean retain_splash)86 plymouth_quit (gboolean retain_splash)
87 {
88     if (retain_splash)
89         g_debug ("Quitting Plymouth; retaining splash");
90     else
91         g_debug ("Quitting Plymouth");
92 
93     have_pinged = TRUE;
94     is_running = FALSE;
95     if (retain_splash)
96         plymouth_run_command ("quit --retain-splash", NULL);
97     else
98         plymouth_run_command ("quit", NULL);
99 }
100