Replace gettimeofday with monotonic time

let's be independent of NTP updates, daylight savings time
and syscalls.
This commit is contained in:
Robin Gareus 2024-12-02 23:43:47 +01:00
parent 4e032f7469
commit ecaeea94e5
No known key found for this signature in database
GPG key ID: A090BCE02CF57F04
2 changed files with 6 additions and 9 deletions

View file

@ -323,7 +323,7 @@ Editor::Editor ()
, pending_keyboard_selection_start (0) , pending_keyboard_selection_start (0)
, ignore_gui_changes (false) , ignore_gui_changes (false)
, lock_dialog (0) , lock_dialog (0)
, last_event_time { 0, 0 } , _last_event_time (g_get_monotonic_time ())
, _dragging_playhead (false) , _dragging_playhead (false)
, ignore_map_change (false) , ignore_map_change (false)
, _stationary_playhead (false) , _stationary_playhead (false)
@ -1047,7 +1047,7 @@ Editor::generic_event_handler (GdkEvent* ev)
case GDK_KEY_PRESS: case GDK_KEY_PRESS:
case GDK_KEY_RELEASE: case GDK_KEY_RELEASE:
if (contents().get_mapped()) { if (contents().get_mapped()) {
gettimeofday (&last_event_time, 0); _last_event_time = g_get_monotonic_time ();
} }
break; break;
@ -1078,13 +1078,9 @@ Editor::generic_event_handler (GdkEvent* ev)
bool bool
Editor::lock_timeout_callback () Editor::lock_timeout_callback ()
{ {
struct timeval now, delta; int64_t dt = g_get_monotonic_time () - _last_event_time;
gettimeofday (&now, 0); if (dt * 1e-6 > UIConfiguration::instance().get_lock_gui_after_seconds()) {
timersub (&now, &last_event_time, &delta);
if (delta.tv_sec > (time_t) UIConfiguration::instance().get_lock_gui_after_seconds()) {
lock (); lock ();
/* don't call again. Returning false will effectively /* don't call again. Returning false will effectively
disconnect us from the timer callback. disconnect us from the timer callback.

View file

@ -1453,7 +1453,8 @@ private:
void unlock (); void unlock ();
Gtk::Dialog* lock_dialog; Gtk::Dialog* lock_dialog;
struct timeval last_event_time; int64_t _last_event_time;
bool generic_event_handler (GdkEvent*); bool generic_event_handler (GdkEvent*);
bool lock_timeout_callback (); bool lock_timeout_callback ();
void start_lock_event_timing (); void start_lock_event_timing ();