[PATCH 5/8] Add a command line option for choosing a monitor for osd display

Doug Torrance <[email protected]>
Newsgroups gmane.compw.window-managers.windowmaker.devel
Message-ID <[email protected]>
From: Johannes Holmberg <[email protected]>

---
 wmix/config.c         | 25 ++++++++++++++++++++++++-
 wmix/include/config.h |  2 ++
 wmix/ui_x.c           | 44 +++++++++++++++++++++++++++++++++++++++++---
 3 files changed, 67 insertions(+), 4 deletions(-)

diff --git a/wmix/config.c b/wmix/config.c
index c11c2fe..22de1b3 100644
--- a/wmix/config.c
+++ b/wmix/config.c
@@ -25,6 +25,7 @@
 #include <string.h>
 #include <ctype.h>
 #include <getopt.h>
+#include <limits.h>
 
 #include <sys/soundcard.h>
 
@@ -45,6 +46,8 @@
 	"  -k        disable grabing volume control keys\n" \
 	"  -m <dev>  oss mixer device [/dev/mixer]\n" \
 	"            or alsa card name [default]\n" \
+	"  -o <num>  display osd on this monitor number or name [0]\n" \
+	"            use -1 to disable osd\n" \
 	"  -v        verbose -> id, long name, name\n" \
 
 /* The global configuration */
@@ -73,6 +76,8 @@ void config_init(void)
 	config.scrollstep = 0.03;
 	config.osd = 1;
 	config.osd_color = (char *) default_osd_color;
+	config.osd_monitor_number = 0;
+	config.osd_monitor_name = NULL;
 }
 
 /*
@@ -122,7 +127,7 @@ void parse_cli_options(int argc, char **argv)
 	config.verbose = false;
 	error_found = false;
 	for (;;) {
-		opt = getopt(argc, argv, ":a:d:e:f:hkm:v");
+		opt = getopt(argc, argv, ":a:d:e:f:hkm:o:v");
 		if (opt == -1)
 			break;
 
@@ -178,6 +183,24 @@ void parse_cli_options(int argc, char **argv)
 			config.mixer_device = strdup(optarg);
 			break;
 
+		case 'o': ;
+			char *end;
+			long mon = strtol(optarg, &end, 10);
+			if (end == optarg + strlen(optarg)) {
+				if ((mon > INT_MAX) || (mon < -1)) {
+					fprintf(stderr, "wmix:error: unreasonable monitor number provided\n");
+					error_found = true;
+				} else {
+					if (mon == -1)
+						config.osd = 0;
+					else
+						config.osd_monitor_number = (int)mon;
+				}
+			} else {
+				config.osd_monitor_name = strdup(optarg);
+			}
+			break;
+
 		case 'v':
 			config.verbose = true;
 			break;
diff --git a/wmix/include/config.h b/wmix/include/config.h
index 609336d..9d73668 100644
--- a/wmix/include/config.h
+++ b/wmix/include/config.h
@@ -40,6 +40,8 @@ extern struct _Config {
 
 	float        scrollstep;		/* scroll mouse step adjustment */
 	char        *osd_color;			/* osd color */
+	char        *osd_monitor_name;		/* monitor name to display osd on */
+	int          osd_monitor_number;	/* monitor number to display osd on */
 
 	char        *exclude_channel[EXCLUDE_MAX_COUNT + 1];	/* Devices to exclude from GUI's list */
 } config;
diff --git a/wmix/ui_x.c b/wmix/ui_x.c
index eb1123a..72d1349 100644
--- a/wmix/ui_x.c
+++ b/wmix/ui_x.c
@@ -335,6 +335,35 @@ void new_window(char *name, int width, int height)
     XMapWindow(display, win);
 }
 
+XRRCrtcInfo *crtc_info_by_output_name(char *monitor)
+{
+    XRRScreenResources *screen = XRRGetScreenResources(display, DefaultRootWindow(display));
+    for (int i = 0; i < screen->noutput; i++) {
+        XRROutputInfo *output_info = XRRGetOutputInfo(display, screen, screen->outputs[i]);
+        if (!strcmp(monitor, output_info->name)) {
+	    XRRCrtcInfo *crtc_info = XRRGetCrtcInfo(display, screen, output_info->crtc);
+	    XRRFreeOutputInfo(output_info);
+	    XRRFreeScreenResources(screen);
+            return crtc_info;
+	}
+	XRRFreeOutputInfo(output_info);
+    }
+    XRRFreeScreenResources(screen);
+    return NULL;
+}
+
+XRRCrtcInfo *crtc_info_by_output_number(int monitor)
+{
+    XRRScreenResources *screen = XRRGetScreenResources(display, DefaultRootWindow(display));
+    if (monitor >= screen->ncrtc) {
+	fprintf(stderr, "wmix:warning: Requested osd monitor number is out of range, clamping\n");
+	monitor = screen->ncrtc - 1;
+    }
+    XRRCrtcInfo *crtc_info = XRRGetCrtcInfo(display, screen, screen->crtcs[monitor]);
+    XRRFreeScreenResources(screen);
+    return crtc_info;
+}
+
 void new_osd(int height)
 {
     Window osd;
@@ -345,18 +374,27 @@ void new_osd(int height)
     XSetWindowAttributes xattributes;
     int win_layer = 6;
     XFontStruct *fs = NULL;
-    XRRScreenResources *screen;
     XRRCrtcInfo *crtc_info;
     int width;
     int x;
     int y;
 
     if (have_randr) {
-        screen = XRRGetScreenResources(display, DefaultRootWindow(display));
-        crtc_info = XRRGetCrtcInfo(display, screen, screen->crtcs[0]);
+        if (config.osd_monitor_name) {
+            crtc_info = crtc_info_by_output_name(config.osd_monitor_name);
+	    if (crtc_info == NULL) {
+		fprintf(stderr, "wmix:warning: Requested osd monitor not found, falling back to default\n");
+		crtc_info = crtc_info_by_output_number(0);
+	    }
+	}
+        else {
+	    crtc_info = crtc_info_by_output_number(config.osd_monitor_number);
+	}
+
         width = crtc_info->width - 200;
         x = crtc_info->x + 100;
         y = crtc_info->y + crtc_info->height - 120;
+	XRRFreeCrtcInfo(crtc_info);
         if (dockapp.osd &&
             width == dockapp.osd_width &&
             x == dockapp.osd_x &&
-- 
2.1.4


-- 
To unsubscribe, send mail to [email protected].
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.