summaryrefslogtreecommitdiff
path: root/mate-session/test-inhibit.c
blob: 2bd65691a79c30d4eb93aafb143760e79bcabcd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008 Red Hat, Inc.
 * Copyright (C) 2012-2021 MATE Developers
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <gtk/gtk.h>
#include <gdk/gdkx.h>

#define SM_DBUS_NAME      "org.gnome.SessionManager"
#define SM_DBUS_PATH      "/org/gnome/SessionManager"
#define SM_DBUS_INTERFACE "org.gnome.SessionManager"

static GDBusProxy      *sm_proxy = NULL;
static guint            cookie = 0;

static gboolean
session_manager_connect (void)
{
        GError *error = NULL;
        if (sm_proxy == NULL) {
                sm_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
                                                          G_DBUS_PROXY_FLAGS_NONE,
                                                          NULL,
                                                          SM_DBUS_NAME,
                                                          SM_DBUS_PATH,
                                                          SM_DBUS_INTERFACE,
                                                          NULL,
                                                          &error);
        }
        return (sm_proxy != NULL);
}

typedef enum {
        GSM_INHIBITOR_FLAG_LOGOUT      = 1 << 0,
        GSM_INHIBITOR_FLAG_SWITCH_USER = 1 << 1,
        GSM_INHIBITOR_FLAG_SUSPEND     = 1 << 2
} GsmInhibitFlag;

static gboolean
do_inhibit_for_window (GdkWindow *window)
{
        GError     *error;
        GVariant   *ret;
        const char *startup_id G_GNUC_UNUSED;
        const char *app_id;
        const char *reason;
        guint       toplevel_xid;
        guint       flags;

        startup_id = g_getenv ("DESKTOP_AUTOSTART_ID");
        app_id = "caja-cd-burner";
        reason = "A CD burn is in progress.";
        toplevel_xid = gdk_x11_window_get_xid (window);

        flags = GSM_INHIBITOR_FLAG_LOGOUT
                | GSM_INHIBITOR_FLAG_SWITCH_USER
                | GSM_INHIBITOR_FLAG_SUSPEND;

        error = NULL;
        ret = g_dbus_proxy_call_sync (sm_proxy,
                                      "Inhibit",
                                      g_variant_new ("(susu)", app_id, toplevel_xid, reason, flags),
                                      G_DBUS_CALL_FLAGS_NONE,
                                      -1,
                                      NULL,
                                      &error);
        if (ret == NULL) {
                g_warning ("Failed to inhibit: %s", error->message);
                g_error_free (error);
                return FALSE;
        }

        g_variant_get (ret, "(u)", &cookie);
        g_variant_unref (ret);
        g_debug ("Inhibiting session manager: %u", cookie);

        return TRUE;
}
static gboolean
session_manager_disconnect (void)
{
        if (sm_proxy != NULL) {
                g_object_unref (sm_proxy);
                sm_proxy = NULL;
        }

        return TRUE;
}

static gboolean
do_uninhibit (void)
{
        GError  *error;
        GVariant *ret;

        error = NULL;
        ret = g_dbus_proxy_call_sync (sm_proxy,
                                      "Uninhibit",
                                      g_variant_new ("(u)", cookie),
                                      G_DBUS_CALL_FLAGS_NONE,
                                      -1,
                                      NULL,
                                      &error);
        if (ret == NULL) {
                g_warning ("Failed to uninhibit: %s", error->message);
                g_error_free (error);
                return FALSE;
        }

        g_variant_unref (ret);
        cookie = 0;

        return TRUE;
}

static void
on_widget_show (GtkWidget *dialog,
                gpointer   data)
{
        gboolean res;

        res = do_inhibit_for_window (gtk_widget_get_window (dialog));
        if (! res) {
                g_warning ("Unable to register client with session manager");
        }
}

int
main (int   argc,
      char *argv[])
{
        gboolean   res;
        GtkWidget *dialog;

        g_log_set_always_fatal (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING);

        gtk_init (&argc, &argv);

        res = session_manager_connect ();
        if (! res) {
                g_warning ("Unable to connect to session manager");
                exit (1);
        }

        g_timeout_add_seconds (30, (GSourceFunc)gtk_main_quit, NULL);

        dialog = gtk_message_dialog_new (NULL,
                                         0,
                                         GTK_MESSAGE_INFO,
                                         GTK_BUTTONS_CANCEL,
                                         "Inhibiting logout, switch user, and suspend.");

        g_signal_connect (dialog, "response", G_CALLBACK (gtk_main_quit), NULL);
        g_signal_connect (dialog, "show", G_CALLBACK (on_widget_show), NULL);
        gtk_widget_show (dialog);

        gtk_main ();

        do_uninhibit ();
        session_manager_disconnect ();

        return 0;
}