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
|
/*
* Copyright (C) 2022 Purism SPC
*
* SPDX-License-Identifier: GPL-3-or-later
*
* Author: Guido Günther <agx@sigxcpu.org>
*/
/* Wait for n seconds taking suspend/resume into account */
#define GMOBILE_USE_UNSTABLE_API
#include "gmobile.h"
#include <glib/gprintf.h>
static void
print_version (void)
{
g_printf ("gm-timeout %s - Wait a bit\n", GM_VERSION);
exit (0);
}
static void
print_now (void)
{
g_autoptr (GDateTime) now = g_date_time_new_now_local ();
g_message ("Now %.2d:%.2d:%.2d",
g_date_time_get_hour (now),
g_date_time_get_minute (now),
g_date_time_get_second (now));
}
static void
on_timeout (gpointer data)
{
g_main_loop_quit (data);
g_message ("Exiting main loop");
}
int
main (int argc, char **argv)
{
g_autoptr(GOptionContext) opt_context = NULL;
g_autoptr (GMainLoop) loop = NULL;
g_autoptr (GError) err = NULL;
int seconds = 60;
gboolean version = FALSE, wakeup = FALSE;
const GOptionEntry options [] = {
{"seconds", 's', 0, G_OPTION_ARG_INT, &seconds,
"Sleep for that many seconds", NULL},
{"wakeup", 'w', 0, G_OPTION_ARG_NONE, &wakeup,
"Wakeup system if needed", NULL},
{"version", 0, 0, G_OPTION_ARG_NONE, &version,
"Show version information", NULL},
{ NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
};
opt_context = g_option_context_new ("- Wait a bit");
g_option_context_add_main_entries (opt_context, options, NULL);
if (!g_option_context_parse (opt_context, &argc, &argv, &err)) {
g_warning ("%s", err->message);
g_clear_error (&err);
return 1;
}
if (version)
print_version ();
loop = g_main_loop_new (NULL, FALSE);
if (wakeup) {
int id;
g_message ("Arming wakeup timer with %d seconds", seconds);
id = gm_wakeup_timeout_add_seconds_once (seconds,
on_timeout,
loop,
&err);
if (!id) {
g_warning ("Failed to create timer: %s", err->message);
return EXIT_FAILURE;
}
} else {
g_message ("Arming timer with %d seconds", seconds);
gm_timeout_add_seconds_once (seconds, on_timeout, loop);
}
print_now ();
g_main_loop_run (loop);
print_now ();
return EXIT_SUCCESS;
}
|