commit 5d227d1abca84be81af16d07d3b9bc7be49f5b19
parent 8a3dd6ce5361c483bad7930f7a8430dee42d68ec
Author: Andrew Laack <andrew@laack.co>
Date: Mon, 21 Sep 2026 09:11:16 -0500
Fix multi-monitor issues by only drawing to the primary screen.
Diffstat:
5 files changed, 37 insertions(+), 13 deletions(-)
diff --git a/README b/README
@@ -6,7 +6,7 @@ as an X11 background.
Requirements
============
-C++ compiler, X11, raylib, make
+C++ compiler, X11, raylib, make, xrandr
Installation
diff --git a/config.mk b/config.mk
@@ -5,7 +5,7 @@ PREFIX = /usr/local
MANPREFIX = ${PREFIX}/share/man
# linking
-LIBS = -lraylib -lX11
+LIBS = -lraylib -lX11 -lXrandr
TLIBS = -lCatch2Main -lCatch2 ${LIBS}
# compiler
diff --git a/include/background.hpp b/include/background.hpp
@@ -3,5 +3,5 @@
#include <cstdint>
#include <string>
#include <vector>
-std::vector<uint32_t> getScreenSize();
-void sendToBg(std::string name);
+std::vector<uint32_t> getScreenSizeAndOffsets();
+void sendToBg(std::string name, uint32_t xOffset, uint32_t yOffset);
diff --git a/src/background.cpp b/src/background.cpp
@@ -1,5 +1,6 @@
#include <X11/Xatom.h>
#include <X11/Xlib.h>
+#include <X11/extensions/Xrandr.h>
#include <unistd.h>
#include <cstddef>
@@ -8,22 +9,44 @@
#include <string>
#include <vector>
-std::vector<uint32_t> getScreenSize() {
+std::vector<uint32_t> getScreenSizeAndOffsets() {
Display* dpy;
- Screen* screen;
dpy = XOpenDisplay(NULL);
+
+ if (dpy == NULL) {
+ throw std::runtime_error{"Unable to open display."};
+ }
+
int count = ScreenCount(dpy);
+
if (count == 0) {
throw std::runtime_error{"Unable to find screens."};
}
- screen = ScreenOfDisplay(dpy, 0);
- std::vector<uint32_t> res{(uint32_t)screen->width,
- (uint32_t)screen->height};
+
+ XRRMonitorInfo* mons =
+ XRRGetMonitors(dpy, DefaultRootWindow(dpy), True, &count);
+
+ if (count == 0) {
+ throw std::runtime_error{"Unable to find monitors."};
+ }
+
+ XRRMonitorInfo* m = &mons[0];
+ for (int i = 0; i < count; ++i) {
+ if (mons[i].primary) {
+ m = &mons[i];
+ break;
+ }
+ }
+
+ std::vector<uint32_t> res{(uint32_t)m->width, (uint32_t)m->height,
+ (uint32_t)m->x, (uint32_t)m->y};
+
+ XRRFreeMonitors(mons);
XCloseDisplay(dpy);
return res;
}
-void sendToBg(std::string name) {
+void sendToBg(std::string name, uint32_t xOffset, uint32_t yOffset) {
Display* d = XOpenDisplay(nullptr);
Window root = DefaultRootWindow(d), r, p, *kids;
uint32_t n;
@@ -51,6 +74,8 @@ void sendToBg(std::string name) {
// have to do unmap / map to make it bg for all tags
XUnmapWindow(d, kids[i]);
+ XMoveWindow(d, kids[i], xOffset, yOffset);
+ XLowerWindow(d, kids[i]);
XMapWindow(d, kids[i]);
XLowerWindow(d, kids[i]);
diff --git a/src/main.cpp b/src/main.cpp
@@ -71,7 +71,7 @@ int main(int argc, char** argv) {
SetTraceLogLevel(LOG_ERROR);
- auto ss = getScreenSize();
+ auto ss = getScreenSizeAndOffsets();
uint32_t xMax = ss[0];
uint32_t yMax = ss[1];
@@ -81,7 +81,7 @@ int main(int argc, char** argv) {
// with Font.
InitWindow(xMax, yMax, "abg");
- sendToBg("abg");
+ sendToBg("abg", ss[2], ss[3]);
while (!WindowShouldClose()) {
Graph g = Graph(edgeCount, vertexCount, xMax, yMax);
std::vector<double> minEdgeToVertex(vertexCount, -1);
@@ -112,7 +112,6 @@ int main(int argc, char** argv) {
BeginDrawing();
DrawTexture(blankGraph.texture, 0, 0, WHITE);
EndDrawing();
-
usleep((int)(sleepTime * 1000000));
oneStepPrim(toVisit, visitedIndices, g, minEdgeToVertex);