2
* Copyright (C) 2017 Canonical Ltd
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Marco Trevisan <marco.trevisan@canonical.com>
22
#define COMPIZ_CONFIG_PROFILE_ENV "COMPIZ_CONFIG_PROFILE"
23
#define COMPIZ_CONFIG_DEFAULT_PROFILE "ubuntu"
25
extern const CCSInterfaceTable ccsDefaultInterfaceTable;
28
get_ccs_profile_env_from_env_list (const gchar **env_vars)
32
for (; env_vars && *env_vars; ++env_vars)
34
if (g_str_has_prefix (*env_vars, COMPIZ_CONFIG_PROFILE_ENV "="))
36
var = g_strdup (*env_vars + G_N_ELEMENTS (COMPIZ_CONFIG_PROFILE_ENV));
45
get_ccs_profile_env_from_session_manager ()
48
GVariant *environment_prop, *environment_prop_list;
49
const gchar **env_vars;
53
bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
55
environment_prop = g_dbus_connection_call_sync (bus,
56
"org.freedesktop.systemd1",
57
"/org/freedesktop/systemd1",
58
"org.freedesktop.DBus.Properties",
60
g_variant_new ("(ss)",
61
"org.freedesktop.systemd1.Manager",
63
G_VARIANT_TYPE ("(v)"),
64
G_DBUS_CALL_FLAGS_NONE,
69
if (!environment_prop)
72
g_variant_get (environment_prop, "(v)", &environment_prop_list, NULL);
73
env_vars = g_variant_get_strv (environment_prop_list, NULL);
74
profile = get_ccs_profile_env_from_env_list (env_vars);
76
g_clear_pointer (&environment_prop, g_variant_unref);
77
g_clear_pointer (&environment_prop_list, g_variant_unref);
79
if (!profile && g_getenv ("UPSTART_SESSION"))
81
const gchar * const * empty_array[] = {0};
82
environment_prop_list = g_dbus_connection_call_sync (bus,
84
"/com/ubuntu/Upstart",
85
"com.ubuntu.Upstart0_6",
87
g_variant_new("(@as)",
88
g_variant_new_strv (NULL, 0)),
90
G_DBUS_CALL_FLAGS_NONE,
94
if (!environment_prop_list)
97
g_variant_get (environment_prop_list, "(^a&s)", &env_vars);
98
profile = get_ccs_profile_env_from_env_list (env_vars);
100
g_variant_unref (environment_prop_list);
104
g_object_unref (bus);
110
is_compiz_profile_available (const gchar *profile)
112
gboolean is_available;
115
profile_path = g_strdup_printf ("%s/compiz-1/compizconfig/%s.ini",
116
g_get_user_config_dir (), profile);
117
is_available = g_file_test (profile_path, G_FILE_TEST_EXISTS);
118
g_free (profile_path);
122
profile_path = g_strdup_printf ("/etc/compizconfig/%s.ini", profile);
123
is_available = g_file_test (profile_path, G_FILE_TEST_EXISTS);
124
g_free (profile_path);
131
set_compiz_profile (CCSContext *ccs_context, const gchar *profile_name)
133
CCSPluginList plugins;
134
const char *ccs_backend;
136
ccs_backend = ccsGetBackend (ccs_context);
138
ccsSetProfile (ccs_context, profile_name);
139
ccsReadSettings (ccs_context);
140
ccsWriteSettings (ccs_context);
142
if (g_strcmp0 (ccs_backend, "gsettings") == 0)
145
plugins = ccsContextGetPlugins (ccs_context);
147
for (CCSPluginList p = plugins; p; p = p->next)
149
CCSPlugin* plugin = p->data;
150
ccsReadPluginSettings (plugin);
156
int main(int argc, char *argv[])
159
const gchar *profile, *current_profile, *ccs_profile_env;
160
gchar *session_manager_ccs_profile;
164
g_warning ("You need to pass a valid profile as argument\n");
168
session_manager_ccs_profile = get_ccs_profile_env_from_session_manager ();
170
if (session_manager_ccs_profile)
172
g_setenv (COMPIZ_CONFIG_PROFILE_ENV, session_manager_ccs_profile, TRUE);
173
g_clear_pointer (&session_manager_ccs_profile, g_free);
177
ccs_profile_env = g_getenv (COMPIZ_CONFIG_PROFILE_ENV);
179
if (!ccs_profile_env || ccs_profile_env[0] == '\0')
181
g_setenv (COMPIZ_CONFIG_PROFILE_ENV, COMPIZ_CONFIG_DEFAULT_PROFILE, TRUE);
187
if (!is_compiz_profile_available (profile))
189
g_warning ("Compiz profile '%s' not found", profile);
193
context = ccsContextNew (0, &ccsDefaultInterfaceTable);
197
g_warning ("Impossible to get Compiz config context\n");
201
g_debug ("Setting profile to '%s' (for environment '%s')",
202
profile, g_getenv (COMPIZ_CONFIG_PROFILE_ENV));
204
current_profile = ccsGetProfile (context);
206
if (g_strcmp0 (current_profile, profile) == 0)
208
g_print("We're already using profile '%s', no need to switch\n", profile);
212
if (!set_compiz_profile (context, profile))
214
ccsFreeContext (context);
218
ccsFreeContext (context);
220
g_print ("Switched to profile '%s' (for environment '%s')\n",
221
profile, g_getenv (COMPIZ_CONFIG_PROFILE_ENV));
223
/* This is for printing debug informations */
224
context = ccsContextNew (0, &ccsDefaultInterfaceTable);
225
ccsFreeContext (context);