~mterry/lightdm/no-dmrc

« back to all changes in this revision

Viewing changes to utils/dm-tool.c

  • Committer: Robert Ancell
  • Date: 2011-08-17 02:16:56 UTC
  • Revision ID: robert.ancell@canonical.com-20110817021656-4mtq0ufnaoteuqbb
Added a dm-tool program that allows user switching and adding seats

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <config.h>
 
13
 
 
14
#include <stdlib.h>
 
15
#include <glib.h>
 
16
#include <glib/gi18n.h>
 
17
#include <gio/gio.h>
 
18
 
 
19
static void
 
20
usage ()
 
21
{
 
22
    g_printerr (/* Text printed out when an unknown command-line argument provided */
 
23
                _("Run 'dm-tool --help' to see a full list of available command line options."));
 
24
    g_printerr ("\n");
 
25
}
 
26
 
 
27
int
 
28
main (int argc, char **argv)
 
29
{
 
30
    gchar *command;
 
31
    gint n_options;
 
32
    gchar **options;
 
33
    GDBusProxy *dm_proxy, *seat_proxy;
 
34
    GError *error = NULL;
 
35
    gint arg_index;
 
36
 
 
37
    g_type_init ();
 
38
 
 
39
    for (arg_index = 1; arg_index < argc; arg_index++)
 
40
    {
 
41
        gchar *arg = argv[arg_index];
 
42
 
 
43
        if (!g_str_has_prefix (arg, "-"))
 
44
            break;
 
45
      
 
46
        if (strcmp (arg, "-h") == 0 || strcmp (arg, "--help") == 0)
 
47
        {
 
48
            g_printerr ("Usage:\n"
 
49
                        "  dm-tool [OPTION...] COMMAND [ARGS...] - Display Manager tool\n"
 
50
                        "\n"
 
51
                        "Options:\n"
 
52
                        "  -h, --help        Show help options\n"
 
53
                        "  -v, --version     Show release version\n"
 
54
                        "\n"
 
55
                        "Commands:\n"
 
56
                        "  switch-to-greeter                   Switch to the greeter\n"
 
57
                        "  switch-to-user USERNAME [SESSION]   Switch to a user session\n"
 
58
                        "  switch-to-guest [SESSION]           Switch to a guest session\n"
 
59
                        "  add-seat TYPE [NAME=VALUE...]       Add a dynamic seat\n");
 
60
            return EXIT_SUCCESS;
 
61
        }
 
62
        else if (strcmp (arg, "-v") == 0 || strcmp (arg, "--version") == 0)
 
63
        {
 
64
            /* NOTE: Is not translated so can be easily parsed */
 
65
            g_printerr ("lightdm %s\n", VERSION);
 
66
            return EXIT_SUCCESS;
 
67
        }
 
68
        else
 
69
        {
 
70
            g_printerr ("Unknown option %s\n", arg);
 
71
            usage ();
 
72
            return EXIT_FAILURE;
 
73
        }
 
74
    }
 
75
 
 
76
    if (arg_index >= argc)
 
77
    {
 
78
        g_printerr ("Missing command\n");
 
79
        usage ();
 
80
        return EXIT_FAILURE;
 
81
    }
 
82
 
 
83
    dm_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
 
84
                                              G_DBUS_PROXY_FLAGS_NONE,
 
85
                                              NULL,
 
86
                                              "org.freedesktop.DisplayManager",
 
87
                                              "/org/freedesktop/DisplayManager",
 
88
                                              "org.freedesktop.DisplayManager",
 
89
                                              NULL,
 
90
                                              &error);
 
91
    if (!dm_proxy)
 
92
    {
 
93
        g_printerr ("Unable to contact display manager: %s\n", error->message);
 
94
        return EXIT_FAILURE;
 
95
    }
 
96
    g_clear_error (&error);
 
97
 
 
98
    seat_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
 
99
                                                G_DBUS_PROXY_FLAGS_NONE,
 
100
                                                NULL,
 
101
                                                "org.freedesktop.DisplayManager",
 
102
                                                g_getenv ("XDG_SEAT_PATH"),
 
103
                                                "org.freedesktop.DisplayManager.Seat",
 
104
                                                NULL,
 
105
                                                &error);
 
106
    if (!seat_proxy)
 
107
    {
 
108
        g_printerr ("Unable to contact display manager: %s\n", error->message);
 
109
        return EXIT_FAILURE;
 
110
    }
 
111
    g_clear_error (&error);
 
112
 
 
113
    command = argv[arg_index];
 
114
    arg_index++;
 
115
    n_options = argc - arg_index;
 
116
    options = argv + arg_index;
 
117
    if (strcmp (command, "switch-to-greeter") == 0)
 
118
    {
 
119
        if (n_options != 0)
 
120
        {
 
121
            g_printerr ("Usage switch-to-greeter\n");
 
122
            usage ();
 
123
            return EXIT_FAILURE;
 
124
        }
 
125
 
 
126
        if (!g_dbus_proxy_call_sync (seat_proxy,
 
127
                                     "SwitchToGreeter",
 
128
                                     g_variant_new ("()"),
 
129
                                     G_DBUS_CALL_FLAGS_NONE,
 
130
                                     -1,
 
131
                                     NULL,
 
132
                                     &error))
 
133
        {
 
134
            g_printerr ("Unable to switch to greeter: %s\n", error->message);
 
135
            return EXIT_FAILURE;
 
136
        }
 
137
        return EXIT_SUCCESS;
 
138
    }
 
139
    else if (strcmp (command, "switch-to-user") == 0)
 
140
    {
 
141
        gchar *username, *session = "";
 
142
 
 
143
        if (n_options > 1)
 
144
        {
 
145
            g_printerr ("Usage switch-to-user USERNAME [SESSION]\n");
 
146
            usage ();
 
147
            return EXIT_FAILURE;
 
148
        }
 
149
 
 
150
        username = options[0];
 
151
        if (n_options == 2)
 
152
            session = options[1];
 
153
 
 
154
        if (!g_dbus_proxy_call_sync (seat_proxy,
 
155
                                     "SwitchToUser",
 
156
                                     g_variant_new ("(ss)", username, session),
 
157
                                     G_DBUS_CALL_FLAGS_NONE,
 
158
                                     -1,
 
159
                                     NULL,
 
160
                                     &error))
 
161
        {
 
162
            g_printerr ("Unable to switch to user %s: %s\n", username, error->message);
 
163
            return EXIT_FAILURE;
 
164
        }
 
165
        return EXIT_SUCCESS;
 
166
    }
 
167
    else if (strcmp (command, "switch-to-guest") == 0)
 
168
    {
 
169
        gchar *session = "";
 
170
 
 
171
        if (n_options > 1)
 
172
        {
 
173
            g_printerr ("Usage switch-to-guest [SESSION]\n");
 
174
            usage ();
 
175
            return EXIT_FAILURE;
 
176
        }
 
177
 
 
178
        if (n_options == 1)
 
179
            session = options[0];
 
180
 
 
181
        if (!g_dbus_proxy_call_sync (seat_proxy,
 
182
                                     "SwitchToGuest",
 
183
                                     g_variant_new ("(s)", session),
 
184
                                     G_DBUS_CALL_FLAGS_NONE,
 
185
                                     -1,
 
186
                                     NULL,
 
187
                                     &error))
 
188
        {
 
189
            g_printerr ("Unable to switch to guest: %s\n", error->message);
 
190
            return EXIT_FAILURE;
 
191
        }
 
192
        return EXIT_SUCCESS;
 
193
    }
 
194
    else if (strcmp (command, "add-seat") == 0)
 
195
    {
 
196
        GVariant *result;
 
197
        gchar *type, *path;
 
198
        GVariantBuilder *properties;
 
199
        gint i;
 
200
 
 
201
        if (n_options < 1)
 
202
        {
 
203
            g_printerr ("Usage add-seat TYPE [NAME=VALUE...]\n");
 
204
            usage ();
 
205
            return EXIT_FAILURE;
 
206
        }
 
207
 
 
208
        type = options[0];
 
209
        properties = g_variant_builder_new (G_VARIANT_TYPE ("a(ss)"));
 
210
      
 
211
        for (i = 0; i < n_options; i++)
 
212
        {
 
213
            gchar *property, *name, *value;
 
214
 
 
215
            property = g_strdup (options[i]);
 
216
            name = property;
 
217
            value = strchr (property, '=');
 
218
            if (value)
 
219
            {
 
220
                *value = '\0';
 
221
                value++;
 
222
            }
 
223
            else
 
224
               value = "";
 
225
 
 
226
            g_variant_builder_add_value (properties, g_variant_new ("(ss)", "name", "value"));
 
227
            g_free (property);
 
228
        }
 
229
 
 
230
        result = g_dbus_proxy_call_sync (dm_proxy,
 
231
                                         "AddSeat",
 
232
                                         g_variant_new ("(sa(ss))", type, properties),
 
233
                                         G_DBUS_CALL_FLAGS_NONE,
 
234
                                         -1,
 
235
                                         NULL,
 
236
                                         &error);
 
237
        {
 
238
            g_printerr ("Unable to add seat: %s\n", error->message);
 
239
            return EXIT_FAILURE;
 
240
        }
 
241
 
 
242
        if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(o)")))
 
243
        {
 
244
            g_printerr ("Unexpected response to AddSeat: %s\n", g_variant_get_type_string (result));
 
245
            return EXIT_FAILURE;
 
246
        }
 
247
 
 
248
        g_variant_get (result, "(&o)", path);
 
249
        g_print ("%s\n", path);
 
250
 
 
251
        return EXIT_SUCCESS;
 
252
    }
 
253
 
 
254
    g_printerr ("Unknown command %s\n", command);
 
255
    usage ();
 
256
    return EXIT_FAILURE;
 
257
}