summaryrefslogtreecommitdiff
path: root/src/keep-screen-on.c
blob: c9979ac7d931a307a371c2f66c7235308190cf0d (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
/*
 * Copyright 2025 UBports Foundation
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, 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, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *   Lorenzo Torracchi <lorenzotorracchi@mail.com>
 */

#include "keep-screen-on.h"

#include <ayatana/common/utils.h>
#include <gio/gio.h>

#define UNITY_SCREEN_BUS_NAME "com.canonical.Unity.Screen"
#define UNITY_SCREEN_OBJECT_PATH "/com/canonical/Unity/Screen"
#define UNITY_SCREEN_INTERFACE "com.canonical.Unity.Screen"

static guint request_id = 0;

void
toggle_keep_screen_on_action (GAction *action,
                              GVariant *parameter G_GNUC_UNUSED,
                              gpointer data G_GNUC_UNUSED)
{
  GVariant *state = g_action_get_state (action);
  gboolean enabled = g_variant_get_boolean (state);
  GError *error = NULL;
  GDBusProxy *proxy = g_dbus_proxy_new_for_bus_sync (
      G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL, UNITY_SCREEN_BUS_NAME,
      UNITY_SCREEN_OBJECT_PATH, UNITY_SCREEN_INTERFACE, NULL, &error);

  if (!proxy)
    {
      g_warning ("Failed to create D-Bus proxy: %s", error->message);
      g_clear_error (&error);
      return;
    }
  if (!enabled)
    {
      GVariant *result
          = g_dbus_proxy_call_sync (proxy, "keepDisplayOn", NULL,
                                    G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
      if (result)
        {
          g_variant_get (result, "(i)", &request_id);
          g_variant_unref (result);
          g_simple_action_set_state (G_SIMPLE_ACTION (action),
                                     g_variant_new_boolean (!enabled));
        }
      else
        {
          g_warning ("D-Bus call keepDisplayOn failed: %s", error->message);
          g_clear_error (&error);
        }
    }
  else if (request_id != 0)
    {
      GVariant *params = g_variant_new ("(i)", request_id);
      g_dbus_proxy_call_sync (proxy, "removeDisplayOnRequest", params,
                              G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
      if (error)
        {
          g_warning ("D-Bus call removeDisplayOnRequest failed: %s",
                     error->message);
          g_clear_error (&error);
        }
      else
        {
          g_simple_action_set_state (G_SIMPLE_ACTION (action),
                                     g_variant_new_boolean (!enabled));
          request_id = 0;
        }
    }

  g_object_unref (proxy);
}

int
keep_screen_on_supported ()
{
  return ayatana_common_utils_is_lomiri ();
}