1  /* GIO - GLib Input, Output and Streaming Library
       2   *
       3   * Copyright 2021 Igalia S.L.
       4   *
       5   * SPDX-License-Identifier: LGPL-2.1-or-later
       6   *
       7   * This library is free software; you can redistribute it and/or
       8   * modify it under the terms of the GNU Lesser General Public
       9   * License as published by the Free Software Foundation; either
      10   * version 2.1 of the License, or (at your option) any later version.
      11   *
      12   * This library is distributed in the hope that it will be useful,
      13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15   * Lesser General Public License for more details.
      16   *
      17   * You should have received a copy of the GNU Lesser General
      18   * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19   */
      20  
      21  #include <gio/gio.h>
      22  
      23  static void
      24  test_dup_default (void)
      25  {
      26    GPowerProfileMonitor *monitor;
      27  
      28    monitor = g_power_profile_monitor_dup_default ();
      29    g_assert_nonnull (monitor);
      30    g_object_unref (monitor);
      31  }
      32  
      33  static void
      34  power_saver_enabled_cb (GPowerProfileMonitor *monitor,
      35                          GParamSpec           *pspec,
      36                          gpointer              user_data)
      37  {
      38    gboolean enabled;
      39  
      40    enabled = g_power_profile_monitor_get_power_saver_enabled (monitor);
      41    g_debug ("Power Saver %s (%d)", enabled ? "enabled" : "disabled", enabled);
      42  }
      43  
      44  static void
      45  do_watch_power_profile (void)
      46  {
      47    GPowerProfileMonitor *monitor;
      48    GMainLoop *loop;
      49    gulong signal_id;
      50  
      51    monitor = g_power_profile_monitor_dup_default ();
      52    signal_id = g_signal_connect (G_OBJECT (monitor), "notify::power-saver-enabled",
      53  		                G_CALLBACK (power_saver_enabled_cb), NULL);
      54  
      55    loop = g_main_loop_new (NULL, TRUE);
      56    g_main_loop_run (loop);
      57  
      58    g_signal_handler_disconnect (monitor, signal_id);
      59    g_object_unref (monitor);
      60    g_main_loop_unref (loop);
      61  }
      62  
      63  int
      64  main (int argc, char **argv)
      65  {
      66    int ret;
      67  
      68    if (argc == 2 && !strcmp (argv[1], "--watch"))
      69      {
      70        do_watch_power_profile ();
      71        return 0;
      72      }
      73  
      74    g_test_init (&argc, &argv, NULL);
      75  
      76    g_test_add_func ("/power-profile-monitor/default", test_dup_default);
      77  
      78    ret = g_test_run ();
      79  
      80    return ret;
      81  }