From 8c62390ca211e0dbc7b9233f41ba53952b80154c Mon Sep 17 00:00:00 2001
From: binary1248 <binary1248@hotmail.com>
Date: Sat, 26 Nov 2016 15:51:24 +0100
Subject: [PATCH] Fixed Xlib crashing in sf::Window:setIcon because it expects
 the element data type passed to XChangeProperty to be unsigned long
 (architecture dependent 32-bit or 64-bit) instead of sf::Uint32 (architecture
 independent 32-bit) (#1168). Also adjusted other occurrences of wrong types
 passed to XChangeProperty with format set to 32.

[jcowgill: this is upstream PR https://github.com/SFML/SFML/pull/1171]

---
 src/SFML/Window/Unix/WindowImplX11.cpp | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)

diff --git a/src/SFML/Window/Unix/WindowImplX11.cpp b/src/SFML/Window/Unix/WindowImplX11.cpp
index 530bea50..a17d3d31 100644
--- a/src/SFML/Window/Unix/WindowImplX11.cpp
+++ b/src/SFML/Window/Unix/WindowImplX11.cpp
@@ -777,10 +777,10 @@ void WindowImplX11::setIcon(unsigned int width, unsigned int height, const Uint8
     Uint8* iconPixels = static_cast<Uint8*>(std::malloc(width * height * 4));
     for (std::size_t i = 0; i < width * height; ++i)
     {
-        iconPixels[8 + i * 4 + 0] = pixels[i * 4 + 2];
-        iconPixels[8 + i * 4 + 1] = pixels[i * 4 + 1];
-        iconPixels[8 + i * 4 + 2] = pixels[i * 4 + 0];
-        iconPixels[8 + i * 4 + 3] = pixels[i * 4 + 3];
+        iconPixels[i * 4 + 0] = pixels[i * 4 + 2];
+        iconPixels[i * 4 + 1] = pixels[i * 4 + 1];
+        iconPixels[i * 4 + 2] = pixels[i * 4 + 0];
+        iconPixels[i * 4 + 3] = pixels[i * 4 + 3];
     }
 
     // Create the icon pixmap
@@ -835,18 +835,20 @@ void WindowImplX11::setIcon(unsigned int width, unsigned int height, const Uint8
 
     // ICCCM wants BGRA pixels: swap red and blue channels
     // ICCCM also wants the first 2 unsigned 32-bit values to be width and height
-    std::vector<Uint8> icccmIconPixels(8 + width * height * 4, 0);
+    std::vector<unsigned long> icccmIconPixels(2 + width * height, 0);
+    unsigned long* ptr = &icccmIconPixels[0];
+
+    *ptr++ = width;
+    *ptr++ = height;
+
     for (std::size_t i = 0; i < width * height; ++i)
     {
-        icccmIconPixels[8 + i * 4 + 0] = pixels[i * 4 + 2];
-        icccmIconPixels[8 + i * 4 + 1] = pixels[i * 4 + 1];
-        icccmIconPixels[8 + i * 4 + 2] = pixels[i * 4 + 0];
-        icccmIconPixels[8 + i * 4 + 3] = pixels[i * 4 + 3];
+        *ptr++ = (pixels[i * 4 + 2] << 0 ) |
+                 (pixels[i * 4 + 1] << 8 ) |
+                 (pixels[i * 4 + 0] << 16) |
+                 (pixels[i * 4 + 3] << 24);
     }
 
-    reinterpret_cast<Uint32*>(&icccmIconPixels[0])[0] = width;
-    reinterpret_cast<Uint32*>(&icccmIconPixels[0])[1] = height;
-
     Atom netWmIcon = getAtom("_NET_WM_ICON");
 
     XChangeProperty(m_display,
@@ -1147,7 +1149,7 @@ void WindowImplX11::switchToFullscreen()
 
         if (netWmBypassCompositor)
         {
-            static const Uint32 bypassCompositor = 1;
+            static const unsigned long bypassCompositor = 1;
 
             XChangeProperty(m_display,
                             m_window,
@@ -1226,7 +1228,7 @@ void WindowImplX11::setProtocols()
 
     if (netWmPing && netWmPid)
     {
-        uint32_t pid = getpid();
+        const long pid = getpid();
 
         XChangeProperty(m_display,
                         m_window,
-- 
2.11.0

