<?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 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>
