]> 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 9202cffff1a7a066c8be57ba15874c6d7a9797db..342e52dbdd855fc8ef4907979edaca98d8a0b097 100644 (file)
--- a/vid_glx.c
+++ b/vid_glx.c
@@ -39,6 +39,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #endif
 #include <X11/extensions/xf86vmode.h>
 
+// get the Uchar type
+#include "utf8lib.h"
+#include "image.h"
+
 #include "nexuiz.xpm"
 #include "darkplaces.xpm"
 
@@ -83,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 | \
@@ -122,16 +128,57 @@ static Visual *vidx11_visual;
 static Colormap vidx11_colormap;
 
 /*-----------------------------------------------------------------------*/
+//
+
+long keysym2ucs(KeySym keysym);
+void DP_Xutf8LookupString(XKeyEvent * ev,
+                        Uchar *uch,
+                        KeySym * keysym_return,
+                        Status * status_return)
+{
+       int rc;
+       KeySym keysym;
+       int codepoint;
+       char buffer[64];
+       int nbytes = sizeof(buffer);
+
+       rc = XLookupString(ev, buffer, nbytes, &keysym, NULL);
+
+       if (rc > 0) {
+               codepoint = buffer[0] & 0xFF;
+       } else {
+               codepoint = keysym2ucs(keysym);
+       }
+
+       if (codepoint < 0) {
+               if (keysym == None) {
+                       *status_return = XLookupNone;
+               } else {
+                       *status_return = XLookupKeySym;
+                       *keysym_return = keysym;
+               }
+               *uch = 0;
+               return;
+       }
+
+       *uch = codepoint;
 
-static int XLateKey(XKeyEvent *ev, char *ascii)
+       if (keysym != None) {
+               *keysym_return = keysym;
+               *status_return = XLookupBoth;
+       } else {
+               *status_return = XLookupChars;
+       }
+}
+static int XLateKey(XKeyEvent *ev, Uchar *ascii)
 {
        int key = 0;
-       char buf[64];
+       //char buf[64];
        KeySym keysym, shifted;
+       Status status;
 
        keysym = XLookupKeysym (ev, 0);
-       XLookupString(ev, buf, sizeof buf, &shifted, 0);
-       *ascii = buf[0];
+       DP_Xutf8LookupString(ev, ascii, &shifted, &status);
 
        switch(keysym)
        {
@@ -398,7 +445,7 @@ static void HandleEvents(void)
 {
        XEvent event;
        int key;
-       char ascii;
+       Uchar unicode;
        qboolean dowarp = false;
 
        if (!vidx11_display)
@@ -412,14 +459,14 @@ static void HandleEvents(void)
                {
                case KeyPress:
                        // key pressed
-                       key = XLateKey (&event.xkey, &ascii);
-                       Key_Event(key, ascii, true);
+                       key = XLateKey (&event.xkey, &unicode);
+                       Key_Event(key, unicode, true);
                        break;
 
                case KeyRelease:
                        // key released
-                       key = XLateKey (&event.xkey, &ascii);
-                       Key_Event(key, ascii, false);
+                       key = XLateKey (&event.xkey, &unicode);
+                       Key_Event(key, unicode, false);
                        break;
 
                case MotionNotify:
@@ -761,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;
@@ -771,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;
@@ -806,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);
@@ -953,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");
@@ -972,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);
@@ -998,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");
@@ -1109,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