From 906b45982cf176b4f6fc8bb12b02f82514ea7460 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?=
Date: Tue, 22 Sep 2020 11:48:24 +0200
Subject: [PATCH] Push events to an asynchronous queue, timestamped
---
wdmtg.vala | 32 ++++++++++++++++++++++++++++++--
1 file changed, 30 insertions(+), 2 deletions(-)
diff --git a/wdmtg.vala b/wdmtg.vala
index 30b2ddc..534d76e 100644
--- a/wdmtg.vala
+++ b/wdmtg.vala
@@ -49,6 +49,15 @@ namespace Wdmtg {
string? current_title; ///< Current window title
X.Window current_window; ///< Current window
+ bool current_idle = false; ///< Current idle status
+
+ struct Event {
+ public int64 timestamp; ///< When the event happened
+ public string? title; ///< Current title at the time
+ public bool idle; ///< Whether the user is idle
+ }
+
+ AsyncQueue queue; ///< Async queue
// --- X helpers ---------------------------------------------------------------
@@ -119,6 +128,14 @@ namespace Wdmtg {
return changed;
}
+ void push_event () {
+ queue.push(Event () {
+ timestamp = get_real_time (),
+ title = current_title,
+ idle = current_idle
+ });
+ }
+
void update_current_window () {
var root = dpy.default_root_window ();
X.Atom dummy_type; int dummy_format; ulong nitems, dummy_bytes;
@@ -139,9 +156,11 @@ namespace Wdmtg {
new_title = x_window_title (active_window);
current_window = active_window;
}
- if (update_window_title (new_title))
+ if (update_window_title (new_title)) {
stdout.printf ("Window changed: %s\n",
null != current_title ? current_title : "(none)");
+ push_event ();
+ }
}
void on_x_property_notify (X.PropertyEvent *xproperty) {
@@ -150,8 +169,10 @@ namespace Wdmtg {
update_current_window ();
else if (xproperty.window == current_window
&& xproperty.atom == net_wm_name) {
- if (update_window_title (x_window_title (current_window)))
+ if (update_window_title (x_window_title (current_window))) {
stdout.printf ("Title changed: %s\n", current_title);
+ push_event ();
+ }
}
}
@@ -174,6 +195,8 @@ namespace Wdmtg {
void on_x_alarm_notify (X.Sync.AlarmNotifyEvent *xalarm) {
if (xalarm.alarm == idle_alarm_inactive) {
stdout.printf ("User is inactive\n");
+ current_idle = true;
+ push_event ();
X.Sync.Value one, minus_one;
X.Sync.int_to_value (out one, 1);
@@ -187,6 +210,9 @@ namespace Wdmtg {
X.Sync.TestType.NegativeComparison, minus_one);
} else if (xalarm.alarm == idle_alarm_inactive) {
stdout.printf ("User is active\n");
+ current_idle = false;
+ push_event ();
+
set_idle_alarm (ref idle_alarm_inactive,
X.Sync.TestType.PositiveComparison, idle_timeout);
}
@@ -219,6 +245,8 @@ namespace Wdmtg {
return 0;
}
+ queue = new AsyncQueue ();
+
X.init_threads ();
if (null == (dpy = new X.Display ()))
exit_fatal ("cannot open display");