Linux下如何获取当前/Alt+Tab切换后的应用程序名称?
CGWindowListCopyWindowInfo for Active Window Tracking Got it, let's break down how to get the active window name and track Alt+Tab switches on Linux—since unlike macOS, Linux relies on two main display servers (X11 and Wayland) with distinct approaches, we'll cover both scenarios.
For X11 (Traditional Desktops: Ubuntu <22.04, XFCE, KDE Plasma X11)
X11 has well-established tools and low-level APIs for window interaction, similar to macOS's CG Graphics framework.
Quick Command-Line Solutions
If you just need a fast way to fetch the active window name, use these pre-built tools:
- xdotool: Run
xdotool getactivewindow getwindownameto directly print the active window's title. - wmctrl: Use
wmctrl -l | grep $(xdotool getactivewindow)to filter the active window from the full list of open windows.
Programmatic Approach with Xlib (C/C++)
This is the low-level equivalent to macOS's CGWindowListCopyWindowInfo. Here's a minimal example to retrieve the active window name:
#include <X11/Xlib.h> #include <X11/Xutil.h> #include <stdio.h> int main() { Display *display = XOpenDisplay(NULL); if (!display) { fprintf(stderr, "Failed to connect to X display\n"); return 1; } Window root = DefaultRootWindow(display); Window activeWindow; int revertMode; XGetInputFocus(display, &activeWindow, &revertMode); char *windowName; if (XFetchName(display, activeWindow, &windowName)) { printf("Current Active Window: %s\n", windowName); XFree(windowName); } else { printf("No title found for active window\n"); } XCloseDisplay(display); return 0; }
Compile with: gcc -o active_window active_window.c -lX11
Tracking Alt+Tab Window Switches
To detect when the active window changes (like after an Alt+Tab press), listen for FocusIn events using Xlib:
- Enable event tracking on the root window:
XSelectInput(display, root, FocusChangeMask) - Enter an event loop, and whenever a
FocusInevent triggers, re-fetch the active window name using the code above.
For Wayland (Modern Desktops: GNOME 40+, KDE Plasma Wayland, Sway)
Wayland prioritizes security, so there's no universal "list all windows" API like X11. Instead, use desktop-specific tools or framework APIs:
GNOME Wayland
Use D-Bus to query and monitor window activity:
- Get current active window name:
gdbus call --session --dest org.gnome.Shell --object-path /org/gnome/Shell --method org.gnome.Shell.Eval "global.display.focus_window.get_title()" - Track window switches: Monitor D-Bus signals from GNOME Shell, or use the GNOME Shell Extension API if building a GUI app.
KDE Plasma Wayland
Leverage KDE's KWindowSystem library (part of KDE Frameworks) or command-line D-Bus calls:
# Get active window ID qdbus org.kde.KWin /KWin activeWindow # Get window title from ID qdbus org.kde.KWin /KWin getWindowTitle $(qdbus org.kde.KWin /KWin activeWindow)
Wlroots-Based Compositors (Sway, Hyprland)
Use compositor-specific tools:
- Get active window name in Sway:
swaymsg -t get_tree | jq '.. | select(.focused == true) | .name' - Track switches: Listen to Sway's IPC events with
swaymsg -t subscribe '["window"]'
Cross-Desktop GUI Frameworks
If you're building a Qt/GTK app, use their built-in APIs to abstract X11/Wayland differences:
- Qt:
QGuiApplication::activeWindow()->windowTitle() - GTK:
gtk_window_get_title(gtk_window_get_active())
内容的提问来源于stack exchange,提问作者Nikhil Nilawar

