]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - vid_glx.c
PROPERLY load the icon in SDL/X11
[xonotic/darkplaces.git] / vid_glx.c
index da669873ce25f6baa6ca2a6fa93e0ab6989330cc..342e52dbdd855fc8ef4907979edaca98d8a0b097 100644 (file)
--- a/vid_glx.c
+++ b/vid_glx.c
@@ -41,6 +41,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 // get the Uchar type
 #include "utf8lib.h"
+#include "image.h"
 
 #include "nexuiz.xpm"
 #include "darkplaces.xpm"
@@ -86,6 +87,8 @@ Atom wm_delete_window_atom;
 Atom net_wm_state_atom;
 Atom net_wm_state_hidden_atom;
 Atom net_wm_state_fullscreen_atom;
+Atom net_wm_icon;
+Atom cardinal;
 
 #define KEY_MASK (KeyPressMask | KeyReleaseMask)
 #define MOUSE_MASK (ButtonPressMask | ButtonReleaseMask | \
@@ -128,7 +131,7 @@ static Colormap vidx11_colormap;
 //
 
 long keysym2ucs(KeySym keysym);
-int DP_Xutf8LookupString(XKeyEvent * ev,
+void DP_Xutf8LookupString(XKeyEvent * ev,
                         Uchar *uch,
                         KeySym * keysym_return,
                         Status * status_return)
@@ -136,7 +139,6 @@ int DP_Xutf8LookupString(XKeyEvent * ev,
        int rc;
        KeySym keysym;
        int codepoint;
-       int len;
        char buffer[64];
        int nbytes = sizeof(buffer);
 
@@ -155,7 +157,8 @@ int DP_Xutf8LookupString(XKeyEvent * ev,
                        *status_return = XLookupKeySym;
                        *keysym_return = keysym;
                }
-               return 0;
+               *uch = 0;
+               return;
        }
 
        *uch = codepoint;
@@ -166,7 +169,6 @@ int DP_Xutf8LookupString(XKeyEvent * ev,
        } else {
                *status_return = XLookupChars;
        }
-       return len;
 }
 static int XLateKey(XKeyEvent *ev, Uchar *ascii)
 {
@@ -806,7 +808,7 @@ void VID_BuildGLXAttrib(int *attrib, qboolean stencil, qboolean stereobuffer, in
 
 qboolean VID_InitMode(viddef_mode_t *mode)
 {
-       int i;
+       int i, j;
        int attrib[32];
        XSetWindowAttributes attr;
        XClassHint *clshints;
@@ -816,6 +818,9 @@ qboolean VID_InitMode(viddef_mode_t *mode)
        XVisualInfo *visinfo;
        int MajorVersion, MinorVersion;
        const char *drivername;
+       char *xpm;
+       char **idata;
+       unsigned char *data;
 
        vid_isfullscreen = false;
        vid_isnetwmfullscreen = false;
@@ -851,6 +856,8 @@ qboolean VID_InitMode(viddef_mode_t *mode)
        net_wm_state_atom = XInternAtom(vidx11_display, "_NET_WM_STATE", false);
        net_wm_state_fullscreen_atom = XInternAtom(vidx11_display, "_NET_WM_STATE_FULLSCREEN", false);
        net_wm_state_hidden_atom = XInternAtom(vidx11_display, "_NET_WM_STATE_HIDDEN", false);
+       net_wm_icon = XInternAtom(vidx11_display, "_NET_WM_ICON", false);
+       cardinal = XInternAtom(vidx11_display, "CARDINAL", false);
 
        // make autorepeat send keypress/keypress/.../keyrelease instead of intervening keyrelease
        XkbSetDetectableAutoRepeat(vidx11_display, true, NULL);
@@ -998,12 +1005,52 @@ qboolean VID_InitMode(viddef_mode_t *mode)
 
        win = XCreateWindow(vidx11_display, root, 0, 0, mode->width, mode->height, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
 
+       data = loadimagepixelsbgra("darkplaces-icon", false, false, false, NULL);
+       if(data)
+       {
+               // use _NET_WM_ICON too
+               static long netwm_icon[MAX_NETWM_ICON];
+               int pos = 0;
+               int i = 1;
+
+               while(data)
+               {
+                       if(pos + 2 * image_width * image_height < MAX_NETWM_ICON)
+                       {
+                               netwm_icon[pos++] = image_width;
+                               netwm_icon[pos++] = image_height;
+                               for(i = 0; i < image_height; ++i)
+                                       for(j = 0; j < image_width; ++j)
+                                               netwm_icon[pos++] = BuffLittleLong(&data[(i*image_width+j)*4]);
+                       }
+                       else
+                       {
+                               Con_Printf("Skipping NETWM icon #%d because there is no space left\n", i);
+                       }
+                       ++i;
+                       Mem_Free(data);
+                       data = loadimagepixelsbgra(va("darkplaces-icon%d", i), false, false, false, NULL);
+               }
+               XChangeProperty(vidx11_display, win, net_wm_icon, cardinal, 32, PropModeReplace, (const unsigned char *) netwm_icon, pos);
+       }
+
+       // fallthrough for old window managers
+       xpm = (char *) FS_LoadFile("darkplaces-icon.xpm", tempmempool, false, NULL);
+       idata = NULL;
+       if(xpm)
+               idata = XPM_DecodeString(xpm);
+       if(!idata)
+               idata = ENGINE_ICON;
+
        wmhints = XAllocWMHints();
        if(XpmCreatePixmapFromData(vidx11_display, win,
-               (gamemode == GAME_NEXUIZ) ? nexuiz_xpm : darkplaces_xpm,
+               idata,
                &wmhints->icon_pixmap, &wmhints->icon_mask, NULL) == XpmSuccess)
                wmhints->flags |= IconPixmapHint | IconMaskHint;
 
+       if(xpm)
+               Mem_Free(xpm);
+
        clshints = XAllocClassHint();
        clshints->res_name = strdup(gamename);
        clshints->res_class = strdup("DarkPlaces");
@@ -1017,6 +1064,9 @@ qboolean VID_InitMode(viddef_mode_t *mode)
        }
 
        XmbSetWMProperties(vidx11_display, win, gamename, gamename, (char **) com_argv, com_argc, szhints, wmhints, clshints);
+       // strdup() allocates using malloc(), should be freed with free()
+       free(clshints->res_name);
+       free(clshints->res_class);
        XFree(clshints);
        XFree(wmhints);
        XFree(szhints);
@@ -1043,6 +1093,7 @@ qboolean VID_InitMode(viddef_mode_t *mode)
        //XSync(vidx11_display, False);
 
        ctx = qglXCreateContext(vidx11_display, visinfo, NULL, True);
+       XFree(visinfo); // glXChooseVisual man page says to use XFree to free visinfo
        if (!ctx)
        {
                Con_Printf ("glXCreateContext failed\n");
@@ -1154,6 +1205,8 @@ size_t VID_ListModes(vid_mode_t *modes, size_t maxcount)
                                ++k;
                        }
                }
+               // manpage of XF86VidModeGetAllModeLines says it should be freed by the caller
+               XFree(vidmodes);
                return k;
        }
        return 0; // FIXME implement this