xdbe replaced with more generic pixmap based buffering but there are 3 major bugs:
[monky] / src / x11.c
index 03ccf25..e933a64 100644 (file)
--- a/src/x11.c
+++ b/src/x11.c
@@ -10,7 +10,7 @@
  * Please see COPYING for details
  *
  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
- * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
+ * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
  *     (see AUTHORS)
  * All rights reserved.
  *
 
 #ifdef XFT
 #include <X11/Xft/Xft.h>
-int use_xft = 0;
+int use_xft;
 #endif
 
 #ifdef HAVE_XDBE
 int use_xdbe;
 #endif
 
+#ifdef USE_ARGB
+int use_argb_visual;
+int have_argb_visual;
+#endif /* USE_ARGB */
+int own_window_argb_value;
+
 /* some basic X11 stuff */
 Display *display = NULL;
 int display_width;
@@ -64,6 +70,7 @@ int workarea[4];
 
 /* Window stuff */
 struct conky_window window;
+char window_created = 0;
 
 /* local prototypes */
 static void update_workarea(void);
@@ -170,10 +177,26 @@ static Window find_desktop_window(Window *p_root, Window *p_desktop)
        return win;
 }
 
-/* sets background to ParentRelative for the Window and all parents */
-void set_transparent_background(Window win)
+static int colour_set = -1;
+/* if no argb visual is configured sets background to ParentRelative for the Window and all parents,
+   else real transparency is used */
+void set_transparent_background(Window win, int alpha)
 {
-       static int colour_set = -1;
+       (void)alpha; /* disable warnings when unused */
+
+#ifdef USE_ARGB
+       if (have_argb_visual) {
+               // real transparency
+               if (set_transparent) {
+                       XSetWindowBackground(display, win, 0x00);
+               } else if (colour_set != background_colour) {
+                       XSetWindowBackground(display, win,
+                               background_colour | (alpha << 24));
+                       colour_set = background_colour;
+               }
+       } else {
+#endif /* USE_ARGB */
+       // pseudo transparency
 
        if (set_transparent) {
                Window parent = win;
@@ -192,17 +215,58 @@ void set_transparent_background(Window win)
                XSetWindowBackground(display, win, background_colour);
                colour_set = background_colour;
        }
+#ifdef USE_ARGB
+       }
+#endif /* USE_ARGB */
 }
 
+#ifdef USE_ARGB
+static int get_argb_visual(Visual** visual, int *depth) {
+       /* code from gtk project, gdk_screen_get_rgba_visual */
+       XVisualInfo visual_template;
+       XVisualInfo *visual_list;
+       int nxvisuals = 0, i;
+
+       visual_template.screen = screen;
+       visual_list = XGetVisualInfo (display, VisualScreenMask,
+                               &visual_template, &nxvisuals);
+       for (i = 0; i < nxvisuals; i++) {
+               if (visual_list[i].depth == 32 &&
+                        (visual_list[i].red_mask   == 0xff0000 &&
+                         visual_list[i].green_mask == 0x00ff00 &&
+                         visual_list[i].blue_mask  == 0x0000ff)) {
+                       *visual = visual_list[i].visual;
+                       *depth = visual_list[i].depth;
+                       DBGP("Found ARGB Visual");
+                       XFree(visual_list);
+                       return 1;
+               }
+       }
+       // no argb visual available
+       DBGP("No ARGB Visual found");
+       XFree(visual_list);
+       return 0;
+}
+#endif /* USE_ARGB */
+
 void destroy_window(void)
 {
+#ifdef XFT
        if(window.xftdraw) {
                XftDrawDestroy(window.xftdraw);
        }
+#endif
        if(window.gc) {
                XFreeGC(display, window.gc);
        }
+       if(window.gc_back) {
+               XFreeGC(display, window.gc_back);
+       }
+       if(window.back_buffer != None) {
+               XFreePixmap(display, window.back_buffer);
+       }
        memset(&window, 0, sizeof(struct conky_window));
+       colour_set = -1;
 }
 
 void init_window(int own_window, int w, int h, int set_trans, int back_colour,
@@ -213,13 +277,33 @@ void init_window(int own_window, int w, int h, int set_trans, int back_colour,
         * happens but I bet the bug is somewhere here. */
        set_transparent = set_trans;
        background_colour = back_colour;
+       window_created = 1;
 
 #ifdef OWN_WINDOW
        if (own_window) {
+               int depth = 0, flags;
+               Visual *visual = NULL;
+
                if (!find_desktop_window(&window.root, &window.desktop)) {
                        return;
                }
 
+#ifdef USE_ARGB
+               if (use_argb_visual && get_argb_visual(&visual, &depth)) {
+                       have_argb_visual = 1;
+                       window.visual = visual;
+                       window.colourmap = XCreateColormap(display,
+                               DefaultRootWindow(display), window.visual, AllocNone);
+               } else {
+#endif /* USE_ARGB */
+                       window.visual = DefaultVisual(display, screen);
+                       window.colourmap = DefaultColormap(display, screen);
+                       depth = CopyFromParent;
+                       visual = CopyFromParent;
+#ifdef USE_ARGB
+               }
+#endif /* USE_ARGB */
+
                if (window.type == TYPE_OVERRIDE) {
 
                        /* An override_redirect True window.
@@ -227,11 +311,21 @@ void init_window(int own_window, int w, int h, int set_trans, int back_colour,
                        XSetWindowAttributes attrs = { ParentRelative, 0L, 0, 0L, 0, 0,
                                Always, 0L, 0L, False, StructureNotifyMask | ExposureMask, 0L,
                                True, 0, 0 };
+#ifdef USE_ARGB
+                       if (have_argb_visual) {
+                               attrs.colormap = window.colourmap;
+                               flags = CWBorderPixel | CWColormap | CWOverrideRedirect;
+                       } else {
+#endif /* USE_ARGB */
+                               flags = CWBackPixel | CWOverrideRedirect;
+#ifdef USE_ARGB
+                       }
+#endif /* USE_ARGB */
 
                        /* Parent is desktop window (which might be a child of root) */
                        window.window = XCreateWindow(display, window.desktop, window.x,
-                                       window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
-                                       CWBackPixel | CWOverrideRedirect, &attrs);
+                                       window.y, w, h, 0, depth, InputOutput, visual,
+                                       flags, &attrs);
 
                        XLowerWindow(display, window.window);
 
@@ -249,13 +343,26 @@ void init_window(int own_window, int w, int h, int set_trans, int back_colour,
                        XWMHints wmHint;
                        Atom xa;
 
+#ifdef USE_ARGB
+                       if (have_argb_visual) {
+                               attrs.colormap = window.colourmap;
+                               flags = CWBorderPixel | CWColormap | CWOverrideRedirect;
+                       } else {
+#endif /* USE_ARGB */
+                               flags = CWBackPixel | CWOverrideRedirect;
+#ifdef USE_ARGB
+                       }
+#endif /* USE_ARGB */
+
                        if (window.type == TYPE_DOCK) {
                                window.x = window.y = 0;
                        }
                        /* Parent is root window so WM can take control */
+
+                       fprintf(stderr, PACKAGE_NAME": creating window of %d x %d x %d\n",w,h,depth);
                        window.window = XCreateWindow(display, window.root, window.x,
-                                       window.y, w, h, 0, CopyFromParent, InputOutput, CopyFromParent,
-                                       CWBackPixel | CWOverrideRedirect, &attrs);
+                                       window.y, w, h, 0, depth, InputOutput, visual,
+                                       flags, &attrs);
 
                        classHint.res_name = window.class_name;
                        classHint.res_class = classHint.res_name;
@@ -270,8 +377,9 @@ void init_window(int own_window, int w, int h, int set_trans, int back_colour,
                                wmHint.initial_state = NormalState;
                        }
 
-                       XmbSetWMProperties(display, window.window, window.title, NULL, argv,
+                       XmbSetWMProperties(display, window.window, NULL, NULL, argv,
                                        argc, NULL, &wmHint, &classHint);
+                       XStoreName(display, window.window, window.title);
 
                        /* Sets an empty WM_PROTOCOLS property */
                        XSetWMProtocols(display, window.window, NULL, 0);
@@ -403,13 +511,13 @@ void init_window(int own_window, int w, int h, int set_trans, int back_colour,
                        }
 
                        /* Skip pager */
-                       if (TEST_HINT(window.hints, HINT_SKIP_PAGER)) {
-                               /* fprintf(stderr, PACKAGE_NAME": hint - skip_pager\n");
-                                  fflush(stderr); */
+                       if (TEST_HINT(window.hints, HINT_FULLSCREEN)) {
+                               fprintf(stderr, PACKAGE_NAME": hint - fullscreen\n");
+                               fflush(stderr);
 
                                xa = ATOM(_NET_WM_STATE);
                                if (xa != None) {
-                                       Atom xa_prop = ATOM(_NET_WM_STATE_SKIP_PAGER);
+                                       Atom xa_prop = ATOM(_NET_WM_STATE_FULLSCREEN);
 
                                        XChangeProperty(display, window.window, xa, XA_ATOM, 32,
                                                        PropModeAppend, (unsigned char *) &xa_prop, 1);
@@ -426,18 +534,23 @@ void init_window(int own_window, int w, int h, int set_trans, int back_colour,
        } else
 #endif /* OWN_WINDOW */
        {
-               XWindowAttributes attrs;
-
                if (!window.window) {
                        window.window = find_desktop_window(&window.root, &window.desktop);
                }
+               window.visual = DefaultVisual(display, screen);
+               window.colourmap = DefaultColormap(display, screen);
+
+               fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
+       }
 
+       {
+               fprintf(stderr, PACKAGE_NAME": getting window attrs\n");
+               XWindowAttributes attrs;
                if (XGetWindowAttributes(display, window.window, &attrs)) {
                        window.width = attrs.width;
                        window.height = attrs.height;
+                       window.depth = attrs.depth;
                }
-
-               fprintf(stderr, PACKAGE_NAME": drawing to desktop window\n");
        }
 
        /* Drawable is same as window. This may be changed by double buffering. */
@@ -445,16 +558,12 @@ void init_window(int own_window, int w, int h, int set_trans, int back_colour,
 
 #ifdef HAVE_XDBE
        if (use_xdbe) {
-               int major, minor;
-
-               if (!XdbeQueryExtension(display, &major, &minor)) {
-                       use_xdbe = 0;
-               } else {
-                       window.back_buffer = XdbeAllocateBackBufferName(display,
-                                       window.window, XdbeBackground);
+               {
+                       window.back_buffer = XCreatePixmap(display, window.window,
+                                                                                          window.width, window.height, window.depth);
                        if (window.back_buffer != None) {
                                window.drawable = window.back_buffer;
-                               fprintf(stderr, PACKAGE_NAME": drawing to double buffer\n");
+                               fprintf(stderr, PACKAGE_NAME": created %d x %d x %d back buffer\n",window.width, window.height, window.depth);
                        } else {
                                use_xdbe = 0;
                        }
@@ -467,8 +576,6 @@ void init_window(int own_window, int w, int h, int set_trans, int back_colour,
                fprintf(stderr, PACKAGE_NAME": drawing to single buffer\n");
        }
 #endif
-       window.visual = DefaultVisual(display, DefaultScreen(display));
-       window.colourmap = DefaultColormap(display, DefaultScreen(display));
 #ifdef IMLIB2
        {
                cimlib_init(display, window.drawable, window.visual, window.colourmap);
@@ -519,32 +626,6 @@ static Window find_subwindow(Window win, int w, int h)
        return win;
 }
 
-long get_x11_color(const char *name)
-{
-       XColor color;
-
-       color.pixel = 0;
-       if (!XParseColor(display, DefaultColormap(display, screen), name, &color)) {
-               /* lets check if it's a hex colour with the # missing in front
-                * if yes, then do something about it */
-               char newname[DEFAULT_TEXT_BUFFER_SIZE];
-
-               newname[0] = '#';
-               strncpy(&newname[1], name, DEFAULT_TEXT_BUFFER_SIZE - 1);
-               /* now lets try again */
-               if (!XParseColor(display, DefaultColormap(display, screen), &newname[0],
-                                       &color)) {
-                       NORM_ERR("can't parse X color '%s'", name);
-                       return 0xFF00FF;
-               }
-       }
-       if (!XAllocColor(display, DefaultColormap(display, screen), &color)) {
-               NORM_ERR("can't allocate X color '%s'", name);
-       }
-
-       return (long) color.pixel;
-}
-
 void create_gc(void)
 {
        XGCValues values;
@@ -553,6 +634,8 @@ void create_gc(void)
        values.function = GXcopy;
        window.gc = XCreateGC(display, window.drawable,
                        GCFunction | GCGraphicsExposures, &values);
+       window.gc_back = XCreateGC(display, window.drawable,
+                       GCFunction | GCGraphicsExposures, &values);
 }
 
 //Get current desktop number
@@ -701,13 +784,14 @@ void get_x11_desktop_info(Display *current_display, Atom atom)
        }
 }
 
-void update_x11info(void)
+int update_x11info(void)
 {
        struct information *current_info = &info;
-       if (!x_initialised == YES)
-               return;
+       if (x_initialised != YES)
+               return 0;
        current_info->x11.monitor.number = XScreenCount(display);
        current_info->x11.monitor.current = XDefaultScreen(display);
+       return 0;
 }
 
 #ifdef OWN_WINDOW
@@ -781,11 +865,16 @@ void set_struts(int sidenum)
 void xdbe_swap_buffers(void)
 {
        if (use_xdbe) {
-               XdbeSwapInfo swap;
-
-               swap.swap_window = window.window;
-               swap.swap_action = XdbeBackground;
-               XdbeSwapBuffers(display, &swap, 1);
+               //XWindowAttributes dest;//leaks
+               //XGetWindowAttributes(display, window.window, &dest);
+               //unsigned int src_height, src_width, src_depth = 0;
+               //XGetGeometry(display, window.back_buffer, NULL, NULL, NULL,src_width,src_height, NULL, src_depth);
+//             fprintf(stderr, PACKAGE_NAME": copy from %d x %d x %d to %d x %d x %d\n",src_width, src_height, src_depth, dest.width, dest.height, dest.depth);
+               XCopyArea(display, window.drawable, window.window,
+                                 window.gc_back, 0, 0, window.width, window.height, 0, 0);
+               /* FIXME should fill w/ window background */
+//             XFillRectangle(display, window.back_buffer, window.gc,
+//                                        0, 0, window.width, window.height);
        }
 }
 #endif /* HAVE_XDBE */