[PATCH 4/8] Draw osd only on the primary monitor if randr extensions are available

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

---
 wmix/Makefile       |  4 ++--
 wmix/include/ui_x.h |  6 ++++--
 wmix/ui_x.c         | 51 ++++++++++++++++++++++++++++++++++++++++++++++-----
 wmix/wmix.c         | 22 ++++++++++++++++++++--
 4 files changed, 72 insertions(+), 11 deletions(-)

diff --git a/wmix/Makefile b/wmix/Makefile
index 6e94a34..d62273c 100644
--- a/wmix/Makefile
+++ b/wmix/Makefile
@@ -1,7 +1,7 @@
 CC		= gcc
-CFLAGS		= -std=gnu99 -O3 -W -Wall `pkg-config --cflags alsa`
+CFLAGS		= -std=gnu99 -O3 -W -Wall `pkg-config --cflags alsa xrandr`
 LDFLAGS		= -L/usr/X11R6/lib
-LIBS		= -lXpm -lXext -lX11 -lm `pkg-config --libs alsa`
+LIBS		= -lXpm -lXext -lX11 -lm `pkg-config --libs alsa xrandr`
 OBJECTS		= misc.o config.o mixer-alsa.o mixer-oss.o ui_x.o mmkeys.o wmix.o
 
 # where to install this program (also for packaging stuff)
diff --git a/wmix/include/ui_x.h b/wmix/include/ui_x.h
index 087d473..22f1f9b 100644
--- a/wmix/include/ui_x.h
+++ b/wmix/include/ui_x.h
@@ -18,11 +18,11 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-void		dockapp_init	(Display *x_display);
+void		dockapp_init	(Display *x_display, Bool randr);
 
 void		new_window	(char *name, int width, int height);
 
-void		new_osd		(int width, int height);
+void		new_osd		(int height);
 void		update_osd	(float volume, bool up);
 void		map_osd		(void);
 void		unmap_osd	(void);
@@ -38,3 +38,5 @@ void		knob_turn	(float delta);
 void		slider_move	(float delta);
 
 unsigned long	get_color	(Display *display, char *color_name);
+
+void            ui_rrnotify     (void);
diff --git a/wmix/ui_x.c b/wmix/ui_x.c
index ef034c5..eb1123a 100644
--- a/wmix/ui_x.c
+++ b/wmix/ui_x.c
@@ -35,6 +35,7 @@
 #include <X11/extensions/shape.h>
 #include <X11/xpm.h>
 #include <X11/cursorfont.h>
+#include <X11/extensions/Xrandr.h>
 
 #include "include/master.xpm"
 #include "include/led-on.xpm"
@@ -70,6 +71,8 @@ struct _Dockapp {
     Window osd;
     GC osd_gc;
     int osd_width;
+    int osd_x;
+    int osd_y;
     bool osd_mapped;
 
 };
@@ -103,11 +106,15 @@ static Cursor hand_cursor;
 static Cursor null_cursor;
 static Cursor norm_cursor;
 static Cursor bar_cursor;
+static Bool have_randr;
 
 /* public methods */
-void dockapp_init(Display *x_display)
+void dockapp_init(Display *x_display, Bool randr)
 {
     display = x_display;
+    have_randr = randr;
+    dockapp.osd = 0;
+    dockapp.gc = 0;
 }
 
 void redraw_window(void)
@@ -328,7 +335,7 @@ void new_window(char *name, int width, int height)
     XMapWindow(display, win);
 }
 
-void new_osd(int width, int height)
+void new_osd(int height)
 {
     Window osd;
     Pixel fg, bg;
@@ -338,20 +345,45 @@ void new_osd(int width, 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]);
+        width = crtc_info->width - 200;
+        x = crtc_info->x + 100;
+        y = crtc_info->y + crtc_info->height - 120;
+        if (dockapp.osd &&
+            width == dockapp.osd_width &&
+            x == dockapp.osd_x &&
+            y == dockapp.osd_y) {
+            // Nothing important has changed.
+            return;
+        }
+    } else {
+        width = DisplayWidth(display, DefaultScreen(display)) - 200;
+        x = 100;
+        y = DisplayHeight(display, 0) - 120;
+    }
 
     sizehints.flags = USSize | USPosition;
-    sizehints.x = (DisplayWidth(display, 0) - width) / 2;
-    sizehints.y = (DisplayHeight(display, 0) - 120);
+    sizehints.x = x;
+    sizehints.y = y;
     sizehints.width = width;
     sizehints.height = height;
     xattributes.save_under = True;
     xattributes.override_redirect = True;
     xattributes.cursor = None;
 
-
     fg = WhitePixel(display, DefaultScreen(display));
     bg = BlackPixel(display, DefaultScreen(display));
 
+    if (dockapp.osd)
+        XDestroyWindow(display, dockapp.osd);
     osd = XCreateSimpleWindow(display, DefaultRootWindow(display),
 			      sizehints.x, sizehints.y, width, height,
 			      0, fg, bg);
@@ -398,6 +430,8 @@ void new_osd(int width, int height)
 	}
     }
 
+    if (dockapp.osd_gc)
+        XFreeGC(display, dockapp.osd_gc);
     gc =
 	XCreateGC(display, osd,
 		  GCForeground | GCBackground | GCGraphicsExposures,
@@ -407,6 +441,8 @@ void new_osd(int width, int height)
     dockapp.osd = osd;
     dockapp.osd_gc = gc;
     dockapp.osd_width = width;
+    dockapp.osd_x = x;
+    dockapp.osd_y = y;
     dockapp.osd_mapped = false;
 }
 
@@ -613,3 +649,8 @@ unsigned long get_color(Display *display, char *color_name)
 
     return color.pixel;
 }
+
+void ui_rrnotify()
+{
+    new_osd(60);
+}
diff --git a/wmix/wmix.c b/wmix/wmix.c
index 13d1593..c0ef6ba 100644
--- a/wmix/wmix.c
+++ b/wmix/wmix.c
@@ -30,6 +30,7 @@
 #include <X11/X.h>
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
+#include <X11/extensions/Xrandr.h>
 
 #include "include/common.h"
 #include "include/mixer.h"
@@ -64,6 +65,8 @@ static void choose_api(int api);
 int main(int argc, char **argv)
 {
     XEvent event;
+    int rr_event_base, rr_error_base;
+    Bool have_randr;
 
     config_init();
     parse_cli_options(argc, argv);
@@ -89,12 +92,21 @@ int main(int argc, char **argv)
 	fprintf(stderr, "wmix:error: Unable to open display \"%s\"\n", name);
 	return EXIT_FAILURE;
     }
+
+    have_randr = XRRQueryExtension(display, &rr_event_base, &rr_error_base);
+    if (have_randr) {
+        int rr_mask = RRScreenChangeNotifyMask;
+        XRRSelectInput(display,
+                       RootWindow(display, DefaultScreen(display)),
+                       rr_mask);
+    }
+
     display_width = (float)DisplayWidth(display, DefaultScreen(display)) / 4.0;
     display_height = (float)DisplayHeight(display, DefaultScreen(display)) / 2.0;
 
-    dockapp_init(display);
+    dockapp_init(display, have_randr);
     new_window("wmix", 64, 64);
-    new_osd(DisplayWidth(display, DefaultScreen(display)) - 200, 60);
+    new_osd(60);
 
     if (config.mmkeys)
 	    mmkey_install(display);
@@ -152,6 +164,12 @@ int main(int argc, char **argv)
 		    XCloseDisplay(display);
 		    return EXIT_SUCCESS;
 		default:
+                    if (have_randr) {
+                        if (event.type == rr_event_base + RRScreenChangeNotify) {
+                            XRRUpdateConfiguration(&event);
+                            ui_rrnotify();
+                        }
+                    }
 		    break;
 	    }
 	} else {
-- 
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.