diff --git a/CMakeLists.txt b/CMakeLists.txt index a4881f1..3cf19ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ pkg_check_modules(GDBUS REQUIRED gio-2.0 dbus-1) find_program(SED sed) add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/org.freedesktop.timedate1.service - COMMAND ${SED} -e s|@LIBEXECDIR@|${CMAKE_INSTALL_LIBEXECDIR}| ${CMAKE_CURRENT_SOURCE_DIR}/org.freedesktop.timedate1.service.in > ${PROJECT_BINARY_DIR}/org.freedesktop.timedate1.service + COMMAND ${SED} -e s|@LIBEXECDIR@|${CMAKE_INSTALL_FULL_LIBEXECDIR}| ${CMAKE_CURRENT_SOURCE_DIR}/org.freedesktop.timedate1.service.in > ${PROJECT_BINARY_DIR}/org.freedesktop.timedate1.service MAIN_DEPENDENCY org.freedesktop.timedate1.service.in VERBATIM) add_custom_target(service ALL DEPENDS ${PROJECT_BINARY_DIR}/org.freedesktop.timedate1.service) diff --git a/slack-timedate.cpp b/slack-timedate.cpp index bde70af..9c07ebe 100644 --- a/slack-timedate.cpp +++ b/slack-timedate.cpp @@ -16,7 +16,6 @@ * */ -#include #include #include #include @@ -30,9 +29,6 @@ static void slack_method_call (GDBusConnection *connection, const gchar *sender, gboolean user_interaction, relative, is_localtime, use_ntp; gint64 usec_utc; - FILE *fp = fopen("/tmp/settime", "w"); - fprintf(fp, "Method %s\n", method_name); - // Set time zone if (g_strcmp0 (method_name, "SetTimezone") == 0) { g_variant_get (parameters, "(&sb)", &timezone, &user_interaction); @@ -48,16 +44,17 @@ static void slack_method_call (GDBusConnection *connection, const gchar *sender, } else if (g_strcmp0 (method_name, "SetTime") == 0) { g_variant_get (parameters, "(xbb)", &usec_utc, &relative, &user_interaction); - fprintf(fp, "Seconds since epoche %lld %llu\n", usec_utc, relative); - // Set time //if (!slack_set_time (usec_utc, slack_get_is_localtime ())) { - if (slack_set_time (usec_utc)) g_dbus_method_invocation_return_value (invocation, - NULL); - else g_dbus_method_invocation_return_error (invocation, - G_IO_ERROR, - G_IO_ERROR_FAILED, - "Failed to set system clock"); + if (slack_set_time(usec_utc, relative)) + { + g_dbus_method_invocation_return_value(invocation, NULL); + } + else + { + g_dbus_method_invocation_return_error(invocation, G_IO_ERROR, G_IO_ERROR_FAILED, + "Failed to set system clock"); + } } else if (g_strcmp0 (method_name, "SetNTP") == 0) { g_variant_get (parameters, "(bb)", &use_ntp, &user_interaction); @@ -69,8 +66,7 @@ static void slack_method_call (GDBusConnection *connection, const gchar *sender, G_IO_ERROR_FAILED, "Error enabling NTP"); } - fclose(fp); - return ; + return; } static GVariant *slack_get_property (GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *prop_name, GError **slack_err, gpointer user_data) { @@ -194,33 +190,31 @@ gboolean slack_set_timezone (gchar *zone) { return TRUE; } -gboolean slack_set_time (gint64 seconds_since_epoch) { - struct timespec ts; -/* gint exit_status; - gboolean spawn_status; - gchar *cmd;*/ +gboolean slack_set_time(gint64 seconds_since_epoch, gboolean relative) +{ + timespec ts; - // Set system clock - ts.tv_sec = (time_t) (seconds_since_epoch / USEC_PER_SEC); - ts.tv_nsec = 0; - if (clock_settime (CLOCK_REALTIME, &ts)) { - return FALSE; - } + if (relative) + { + if (clock_gettime(CLOCK_REALTIME, &ts) == -1) + { + return FALSE; + } + ts.tv_sec += static_cast(seconds_since_epoch / USEC_PER_SEC); + ts.tv_nsec += (seconds_since_epoch % USEC_PER_SEC) * NSEC_PER_USEC; - // Don't save the system time to the hardware clock. The saving takes much - // time. Anyway it is saved automatically on shutdown. - /* Set hardware clock - if (!g_file_test ("/sbin/hwclock", G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_EXECUTABLE)) { - return FALSE; - } - cmd = g_strdup_printf ("/sbin/hwclock %s --systohc", is_localtime ? "--localtime" : "-u"); - spawn_status = g_spawn_command_line_sync (cmd, NULL, NULL, &exit_status, NULL); - g_free (cmd); - if ((WEXITSTATUS (exit_status) != 0) || !spawn_status) { - return FALSE; - }*/ - - return TRUE; + if (ts.tv_nsec < 0) + { + --ts.tv_sec; + ts.tv_nsec += USEC_PER_SEC; + } + } + else + { + ts.tv_sec = static_cast(seconds_since_epoch / USEC_PER_SEC); + ts.tv_nsec = (seconds_since_epoch % USEC_PER_SEC) * NSEC_PER_USEC; + } + return clock_settime (CLOCK_REALTIME, &ts) == 0; } gboolean slack_get_is_localtime () { diff --git a/slack-timedate.h b/slack-timedate.h index c990013..49f0209 100644 --- a/slack-timedate.h +++ b/slack-timedate.h @@ -17,6 +17,7 @@ */ #include +#include #define BUS_NAME "org.freedesktop.timedate1" #define BUS_PATH "/org/freedesktop/timedate1" @@ -50,7 +51,9 @@ "\n" #define DEFAULT_EXIT_SEC 300 -#define USEC_PER_SEC 1000000ULL + +constexpr gint32 USEC_PER_SEC = 1000000ULL; +constexpr gint32 NSEC_PER_USEC = 1000ULL; // Returns the system time zone gchar *slack_get_timezone (); @@ -62,8 +65,7 @@ gboolean slack_set_timezone (gchar *); // Changes the date/time // Takes the amount of seconds since UNIX epoche and // Returns true on success, false otherwise -gboolean slack_set_time (gint64); -//gboolean slack_set_time (gint64, gboolean); +gboolean slack_set_time(gint64 seconds_since_epoch, gboolean relative); // Returns if the hardware clock is set to local time or not gboolean slack_get_is_localtime ();