commit ab872ccce6c5a9a0f94d7e4125f8b2e092912afa
parent 0937dfc5da37f1931dca7c4e33ed35cd5899b47d
Author: bdmfegys@duck.com <bdmfegys@duck.com>
Date: Sat, 27 Apr 2024 20:47:16 -0400
patch xrandr and main monitor patch
Diffstat:
3 files changed, 60 insertions(+), 14 deletions(-)
diff --git a/Makefile b/Makefile
@@ -9,7 +9,7 @@ config.h: config.def.h
cp config.def.h config.h
herbe: herbe.c config.h
- $(CC) herbe.c $(CFLAGS) -o herbe
+ $(CC) herbe.c $(CFLAGS) -o herbe -lXrandr
install: herbe
mkdir -p ${DESTDIR}${PREFIX}/bin
diff --git a/config.def.h b/config.def.h
@@ -2,18 +2,19 @@ static const char *background_color = "#3e3e3e";
static const char *border_color = "#ececec";
static const char *font_color = "#ececec";
static const char *font_pattern = "monospace:size=10";
-static const unsigned line_spacing = 5;
-static const unsigned int padding = 15;
+static unsigned line_spacing = 5;
+static unsigned int padding = 15;
+static const int use_primary_monitor = 0;
-static const unsigned int width = 450;
-static const unsigned int border_size = 2;
-static const unsigned int pos_x = 30;
-static const unsigned int pos_y = 60;
+static unsigned int width = 450;
+static unsigned int border_size = 2;
+static unsigned int pos_x = 30;
+static unsigned int pos_y = 60;
enum corners { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT };
enum corners corner = TOP_RIGHT;
-static const unsigned int duration = 5; /* in seconds */
+static unsigned int duration = 5; /* in seconds */
#define DISMISS_BUTTON Button1
#define ACTION_BUTTON Button3
diff --git a/herbe.c b/herbe.c
@@ -1,5 +1,7 @@
#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>
+#include <X11/extensions/Xrandr.h>
+#include <X11/Xresource.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
@@ -15,6 +17,13 @@
#define EXIT_FAIL 1
#define EXIT_DISMISS 2
+#define XRES_STR(name) \
+ if (XrmGetResource(db, "herbe." #name, "*", &type, &val)) \
+ name = val.addr
+#define XRES_INT(name) \
+ if (XrmGetResource(db, "herbe." #name, "*", &type, &val)) \
+ name = strtoul(val.addr, 0, 10)
+
Display *display;
Window window;
int exit_code = EXIT_DISMISS;
@@ -107,12 +116,48 @@ int main(int argc, char *argv[])
if (!(display = XOpenDisplay(0)))
die("Cannot open display");
+ XrmInitialize();
+
+ char *res_man = XResourceManagerString(display);
+ XrmDatabase db = XrmGetStringDatabase(res_man);
+
+ char *type;
+ XrmValue val;
+
+ XRES_STR(background_color);
+ XRES_STR(border_color);
+ XRES_STR(font_color);
+ XRES_STR(font_pattern);
+
+ XRES_INT(line_spacing);
+ XRES_INT(padding);
+ XRES_INT(width);
+ XRES_INT(border_size);
+ XRES_INT(pos_x);
+ XRES_INT(pos_y);
+ XRES_INT(corner);
+ XRES_INT(duration);
+
int screen = DefaultScreen(display);
Visual *visual = DefaultVisual(display, screen);
Colormap colormap = DefaultColormap(display, screen);
+ int screen_x = 0;
+ int screen_y = 0;
int screen_width = DisplayWidth(display, screen);
int screen_height = DisplayHeight(display, screen);
+ if(use_primary_monitor) {
+ int nMonitors;
+ XRRMonitorInfo* info = XRRGetMonitors(display, RootWindow(display, screen), 1, &nMonitors);
+ for(int i = 0; i < nMonitors; i++) {
+ if(info[i].primary) {
+ screen_x = info[i].x;
+ screen_y = info[i].y;
+ screen_width = info[i].width;
+ screen_height = info[i].height;
+ }
+ }
+ }
XSetWindowAttributes attributes;
attributes.override_redirect = True;
@@ -151,16 +196,16 @@ int main(int argc, char *argv[])
}
}
- unsigned int x = pos_x;
- unsigned int y = pos_y;
+ unsigned int x = screen_x + pos_x;
+ unsigned int y = screen_y + pos_y;
unsigned int text_height = font->ascent - font->descent;
unsigned int height = (num_of_lines - 1) * line_spacing + num_of_lines * text_height + 2 * padding;
if (corner == TOP_RIGHT || corner == BOTTOM_RIGHT)
- x = screen_width - width - border_size * 2 - pos_x;
+ x = screen_x + screen_width - width - border_size * 2 - pos_x;
if (corner == BOTTOM_LEFT || corner == BOTTOM_RIGHT)
- y = screen_height - height - border_size * 2 - pos_y;
+ y = screen_y + screen_height - height - border_size * 2 - pos_y;
window = XCreateWindow(display, RootWindow(display, screen), x, y, width, height, border_size, DefaultDepth(display, screen),
CopyFromParent, visual, CWOverrideRedirect | CWBackPixel | CWBorderPixel, &attributes);
@@ -214,7 +259,8 @@ int main(int argc, char *argv[])
XftDrawDestroy(draw);
XftColorFree(display, visual, colormap, &color);
XftFontClose(display, font);
+ XrmDestroyDatabase(db);
XCloseDisplay(display);
return exit_code;
-}
-\ No newline at end of file
+}