Push events to an asynchronous queue, timestamped

This commit is contained in:
Přemysl Eric Janouch 2020-09-22 11:48:24 +02:00
parent c9795fe01a
commit 906b45982c
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 30 additions and 2 deletions

View File

@ -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<Event?> 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<Event?> ();
X.init_threads ();
if (null == (dpy = new X.Display ()))
exit_fatal ("cannot open display");