<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Plexescor (Abhijot Singh)</title>
    <description>The latest articles on DEV Community by Plexescor (Abhijot Singh) (@plexescor).</description>
    <link>https://dev.to/plexescor</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3932680%2F32a73a5a-85da-4124-a0d9-146ebeac7e93.png</url>
      <title>DEV Community: Plexescor (Abhijot Singh)</title>
      <link>https://dev.to/plexescor</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/plexescor"/>
    <language>en</language>
    <item>
      <title>How I built a scriptable Lua extension engine inside my C++ activity tracker (and almost lost my mind to deadlocks)</title>
      <dc:creator>Plexescor (Abhijot Singh)</dc:creator>
      <pubDate>Mon, 01 Jun 2026 11:46:27 +0000</pubDate>
      <link>https://dev.to/plexescor/how-i-built-a-scriptable-lua-extension-engine-inside-my-c-activity-tracker-and-almost-lost-my-4cne</link>
      <guid>https://dev.to/plexescor/how-i-built-a-scriptable-lua-extension-engine-inside-my-c-activity-tracker-and-almost-lost-my-4cne</guid>
      <description>&lt;h1&gt;
  
  
  Hey!
&lt;/h1&gt;

&lt;p&gt;I've been building HPR, an open source activity tracker and window tracker written in C++23. It watches your active window, builds a history of where your time went, and runs locally with zero accounts and zero telemetry. Think of it as a lightweight ActivityWatch alternative with native Wayland support.&lt;/p&gt;

&lt;p&gt;At some point I decided it wasn't enough to just track windows. I wanted people to be able to extend it. So I built a Lua extension engine into it. Here's how that went.&lt;/p&gt;




&lt;h2&gt;
  
  
  The basic idea
&lt;/h2&gt;

&lt;p&gt;Every &lt;code&gt;.lua&lt;/code&gt; file you drop into the extensions folder gets picked up automatically. HPR scans the directory recursively, loads each file into its own &lt;code&gt;sol::state&lt;/code&gt;, registers the API, and spins up a dedicated thread for it. Each extension runs completely isolated from everything else. A slow or broken extension can't freeze the UI or corrupt the tracking loop.&lt;/p&gt;

&lt;p&gt;The lifecycle is simple. You define three functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight lua"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt; &lt;span class="c1"&gt;-- tick interval in ms&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;onTick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;delta&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;-- called every 500ms&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;onExit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="c1"&gt;-- cleanup&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;init&lt;/code&gt; returns how often you want &lt;code&gt;onTick&lt;/code&gt; to fire. That's it. Everything else is opt-in through the API.&lt;/p&gt;




&lt;h2&gt;
  
  
  The API
&lt;/h2&gt;

&lt;p&gt;This is where it gets actually useful. From Lua, an extension can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read the currently focused window with &lt;code&gt;HPR.getCurrentWindow_E()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Query and write to the SQLite database directly with &lt;code&gt;HPR.dbQuery_E()&lt;/code&gt; and &lt;code&gt;HPR.dbExecute_E()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Subscribe to internal events like &lt;code&gt;WINDOW_CHANGED&lt;/code&gt;, &lt;code&gt;MIDNIGHT_ROLLOVER&lt;/code&gt;, &lt;code&gt;HISTORY_LOADED_SINGULAR&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Register completely custom window detection backends for compositors HPR doesn't natively support&lt;/li&gt;
&lt;li&gt;Set UI properties and register callbacks on the Slint UI from Lua&lt;/li&gt;
&lt;li&gt;Make HTTP requests, start an HTTP server, parse JSON, read and write CSV files&lt;/li&gt;
&lt;li&gt;Stop and resume the window tracking loop entirely&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That last one sounds weird but its how the AFK detection works in one of the extensions I built, more on that in a second.&lt;/p&gt;




&lt;h2&gt;
  
  
  The hardest parts
&lt;/h2&gt;

&lt;p&gt;Honestly the code itself wasnt the worst part. The worst part was deadlocks.&lt;/p&gt;

&lt;p&gt;The Lua state is not thread safe. The Slint UI is also not thread safe and must only be touched from its event loop thread. The shared app state has its own mutex. So you have extension threads, the Slint event loop thread, the window poller thread, and the database writer thread all needing to talk to each other without stepping on each other.&lt;/p&gt;

&lt;p&gt;The solution was a &lt;code&gt;recursive_mutex&lt;/code&gt; per extension that wraps every Lua call, and &lt;code&gt;slint::invoke_from_event_loop&lt;/code&gt; for anything that touches the UI. Callbacks registered from Lua that get fired by Slint acquire the Lua mutex before calling back into Lua. It took a while to get right.&lt;/p&gt;

&lt;p&gt;The other fun one was cyclic table detection. If a Lua extension pushes a self referential table into a UI property, without protection you get a stack overflow and a segfault. I added a visited set that tracks table pointers during the conversion and bails out with an error message if it sees a cycle instead of recursing forever.&lt;/p&gt;




&lt;h2&gt;
  
  
  A real example: impersonating ActivityWatch
&lt;/h2&gt;

&lt;p&gt;One of the extensions I wrote is called AW Parasite. It starts an HTTP server on port 5600, which is the default ActivityWatch port. &lt;code&gt;aw-watcher-web&lt;/code&gt; and &lt;code&gt;aw-watcher-afk&lt;/code&gt; both think they're talking to a real ActivityWatch instance. They send heartbeats, the extension processes them, accumulates URL time and AFK status, writes everything into the HPR SQLite database, and updates the UI through &lt;code&gt;HPR.setUiProperty_E&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When the user goes AFK, the extension calls &lt;code&gt;HPR.stopTracking_E()&lt;/code&gt; to pause the main activity window tracker. When they come back it calls &lt;code&gt;HPR.startTracking_E()&lt;/code&gt;. The whole thing is about 200 lines of Lua and it works on both Wayland and Windows because its just HTTP.&lt;/p&gt;




&lt;h2&gt;
  
  
  Hot reload
&lt;/h2&gt;

&lt;p&gt;You can load, unload and reload individual extensions from the UI without restarting HPR. There's also a rescan button that picks up new &lt;code&gt;.lua&lt;/code&gt; files you dropped in after launch. Unloading sets &lt;code&gt;running = false&lt;/code&gt; on the extension's thread and gives it 450ms to exit cleanly. If it doesnt, the thread gets detached and a warning is logged. Native extensions (&lt;code&gt;.dll&lt;/code&gt; / &lt;code&gt;.so&lt;/code&gt;) are also supported for cases where Lua isnt enough, though those run outside the sandbox.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxkyllptoif8ek66ryr13.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxkyllptoif8ek66ryr13.png" alt="Extension UI" width="800" height="749"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;HPR is fully free and open source. If any of this sounds interesting the repo is at &lt;a href="https://github.com/plexescor/HPR" rel="noopener noreferrer"&gt;Github&lt;/a&gt;. Would appreciate a star if you find it useful.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>opensource</category>
      <category>programming</category>
      <category>extensions</category>
    </item>
    <item>
      <title>How I built native Wayland window tracking across Hyprland, GNOME and KDE in C++23</title>
      <dc:creator>Plexescor (Abhijot Singh)</dc:creator>
      <pubDate>Sat, 16 May 2026 11:24:22 +0000</pubDate>
      <link>https://dev.to/plexescor/how-i-built-native-wayland-window-tracking-across-hyprland-gnome-and-kde-in-c23-2lk8</link>
      <guid>https://dev.to/plexescor/how-i-built-native-wayland-window-tracking-across-hyprland-gnome-and-kde-in-c23-2lk8</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsnpmpc4lj1h1y3hl0kia.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsnpmpc4lj1h1y3hl0kia.png" alt=" " width="800" height="599"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you've ever tried to get the currently focused window on Wayland, you know it's a mess. There's no unified API. Every compositor does it differently. X11 had &lt;code&gt;_NET_ACTIVE_WINDOW&lt;/code&gt; and everyone just used that. Wayland deliberately doesn't have an equivalent for security reasons, which means every tracker, every productivity tool, every anything that needs to know what you're looking at has to implement a different solution per compositor.&lt;/p&gt;

&lt;p&gt;I ran into this while building HPR, an activity tracker that watches your active window every 50ms and logs time per app. Here's how I solved it for the three most common Wayland compositors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hyprland
&lt;/h2&gt;

&lt;p&gt;Easiest of the three. Hyprland exposes an IPC socket and a CLI tool called &lt;code&gt;hyprctl&lt;/code&gt;. One command gives you the active window as JSON:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;hyprctl activewindow &lt;span class="nt"&gt;-j&lt;/span&gt; | jq &lt;span class="nt"&gt;-r&lt;/span&gt; &lt;span class="s1"&gt;'.class'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it. No extension, no setup, no workarounds. Just works on first launch. The &lt;code&gt;.class&lt;/code&gt; field gives you the application class which is consistent and clean. I call this every 50ms from a background thread and it holds up fine.&lt;/p&gt;

&lt;h2&gt;
  
  
  GNOME
&lt;/h2&gt;

&lt;p&gt;GNOME on Wayland is the painful one. There's no official API for getting the active window from outside the compositor. The only working solution I found is a shell extension called &lt;code&gt;window-calls-extended&lt;/code&gt; which exposes a DBus interface you can query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gdbus call &lt;span class="nt"&gt;--session&lt;/span&gt; &lt;span class="nt"&gt;--dest&lt;/span&gt; org.gnome.Shell &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--object-path&lt;/span&gt; /org/gnome/Shell/Extensions/WindowsExt &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--method&lt;/span&gt; org.gnome.Shell.Extensions.WindowsExt.FocusClass
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This returns the window class wrapped in some dirty output you have to parse. HPR checks on startup whether the extension is active. If it isn't, it tells the user exactly what to do instead of silently returning garbage. Because GNOME can't hot-reload shell extensions you log out and back in once after installing. Every launch after that is automatic.&lt;/p&gt;

&lt;p&gt;One thing worth knowing: GNOME shell extensions are sandboxed differently across GNOME versions and some distributions patch the shell in ways that break extension APIs. If you're supporting GNOME you need to test across versions.&lt;/p&gt;

&lt;h2&gt;
  
  
  KDE Plasma
&lt;/h2&gt;

&lt;p&gt;KDE was the one I expected to be easy and wasn't. KDE exposes KWin scripting via &lt;code&gt;qdbus6&lt;/code&gt; which lets you inject JavaScript into the compositor and read the output. The active window class comes from &lt;code&gt;workspace.activeWindow.resourceClass&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The problem is KWin doesn't give you a clean return value. The script runs, prints output to the system journal with a &lt;code&gt;js:&lt;/code&gt; prefix, and you have to scrape it back out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'print(workspace.activeWindow.resourceClass);'&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /tmp/kwin_active.js
&lt;span class="nv"&gt;S&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;qdbus6 org.kde.KWin /Scripting org.kde.kwin.Scripting.loadScript /tmp/kwin_active.js kwin_tmp_&lt;span class="nv"&gt;$$&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nv"&gt;T&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; &lt;span class="s1"&gt;'+%Y-%m-%d %H:%M:%S'&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
qdbus6 org.kde.KWin /Scripting/Script&lt;span class="nv"&gt;$S&lt;/span&gt; org.kde.kwin.Script.run &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /dev/null 2&amp;gt;&amp;amp;1
&lt;span class="nb"&gt;sleep &lt;/span&gt;0.1
journalctl &lt;span class="nt"&gt;--since&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$T&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; &lt;span class="nb"&gt;cat&lt;/span&gt; | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s1"&gt;'^js:'&lt;/span&gt; | &lt;span class="nb"&gt;tail&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt; 1 | &lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="s1"&gt;'s/^js: //'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes this is a hack. It forks a shell, injects JS, scrapes the journal, and cleans up after itself every 50ms. Somehow it lands at around 1% CPU. I tested every other approach I could find. None of them worked reliably across KDE configurations. This one does.&lt;/p&gt;

&lt;p&gt;One side effect: during the JS injection, KWin's own runtime briefly appears as the active window. If you don't filter it out, strings like &lt;code&gt;js::kwin_tmp_1234&lt;/code&gt; silently accumulate time in your logs. HPR filters anything containing &lt;code&gt;js::&lt;/code&gt; before it ever touches the data.&lt;/p&gt;

&lt;h2&gt;
  
  
  The normalization layer
&lt;/h2&gt;

&lt;p&gt;Each backend returns a raw string. Raw strings from OS window APIs are inconsistent and noisy. HPR runs every return value through a shared normalization function before doing anything with it. This filters out compositor artifacts, plasma shell entries, null strings, and KWin JS runtime noise. New backends inherit this filtering automatically.&lt;/p&gt;

&lt;h2&gt;
  
  
  Platform detection
&lt;/h2&gt;

&lt;p&gt;HPR reads &lt;code&gt;$XDG_CURRENT_DESKTOP&lt;/code&gt; and matches substrings. Simple, works for 99% of setups. Non-standard session variables or nested compositors can confuse it but that's an edge case worth documenting rather than engineering around.&lt;/p&gt;

&lt;h2&gt;
  
  
  The project
&lt;/h2&gt;

&lt;p&gt;HPR is open source, C++23, Slint UI, SQLite3 bundled. If you're on Wayland and want an activity tracker that actually works without a Python runtime or an embedded web server, give it a try.&lt;/p&gt;

&lt;p&gt;GitHub: github.com/plexescor/HPR&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdlz7bb33h9kal4i9awd3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdlz7bb33h9kal4i9awd3.png" alt=" " width="800" height="599"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you've solved Wayland window detection in a cleaner way especially on KDE, I'd genuinely like to know.&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>linux</category>
      <category>wayland</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
