From 73665bd50a63ce3e37363c338da05e94ea311bf4 Mon Sep 17 00:00:00 2001
From: Laurent Gomila <laurent.gom@gmail.com>
Date: Sun, 17 Jul 2011 11:26:28 +0200
Subject: [PATCH] Fixed error in Mouse::Set/GetPosition when the given window
 was already closed

---
 src/SFML/Window/Linux/InputImpl.cpp | 32 ++++++++++++++++++++---------
 src/SFML/Window/Win32/InputImpl.cpp | 26 ++++++++++++++++-------
 2 files changed, 41 insertions(+), 17 deletions(-)

diff --git a/src/SFML/Window/Linux/InputImpl.cpp b/src/SFML/Window/Linux/InputImpl.cpp
index 44ef05ae..83eb50a1 100644
--- a/src/SFML/Window/Linux/InputImpl.cpp
+++ b/src/SFML/Window/Linux/InputImpl.cpp
@@ -228,16 +228,24 @@ Vector2i InputImpl::GetMousePosition()
 ////////////////////////////////////////////////////////////
 Vector2i InputImpl::GetMousePosition(const Window& relativeTo)
 {
-    // we don't care about these but they are required
-    ::Window root, child;
-    int gx, gy;
-    unsigned int buttons;
+    WindowHandle handle = relativeTo.GetSystemHandle();
+    if (handle)
+    {
+        // we don't care about these but they are required
+        ::Window root, child;
+        int gx, gy;
+        unsigned int buttons;
 
-    int x = 0;
-    int y = 0;
-    XQueryPointer(global.display, relativeTo.GetSystemHandle(), &root, &child, &gx, &gy, &x, &y, &buttons);
+        int x = 0;
+        int y = 0;
+        XQueryPointer(global.display, handle, &root, &child, &gx, &gy, &x, &y, &buttons);
 
-    return Vector2i(x, y);
+        return Vector2i(x, y);
+    }
+    else
+    {
+        return Vector2i();
+    }
 }
 
 
@@ -252,8 +260,12 @@ void InputImpl::SetMousePosition(const Vector2i& position)
 ////////////////////////////////////////////////////////////
 void InputImpl::SetMousePosition(const Vector2i& position, const Window& relativeTo)
 {
-    XWarpPointer(global.display, None, relativeTo.GetSystemHandle(), 0, 0, 0, 0, position.x, position.y);
-    XFlush(global.display);
+    WindowHandle handle = relativeTo.GetSystemHandle();
+    if (handle)
+    {
+        XWarpPointer(global.display, None, handle, 0, 0, 0, 0, position.x, position.y);
+        XFlush(global.display);
+    }
 }
 
 } // namespace priv
diff --git a/src/SFML/Window/Win32/InputImpl.cpp b/src/SFML/Window/Win32/InputImpl.cpp
index 2c509173..8122e581 100644
--- a/src/SFML/Window/Win32/InputImpl.cpp
+++ b/src/SFML/Window/Win32/InputImpl.cpp
@@ -178,10 +178,18 @@ Vector2i InputImpl::GetMousePosition()
 ////////////////////////////////////////////////////////////
 Vector2i InputImpl::GetMousePosition(const Window& relativeTo)
 {
-    POINT point;
-    GetCursorPos(&point);
-    ScreenToClient(relativeTo.GetSystemHandle(), &point);
-    return Vector2i(point.x, point.y);
+    WindowHandle handle = relativeTo.GetSystemHandle();
+    if (handle)
+    {
+        POINT point;
+        GetCursorPos(&point);
+        ScreenToClient(handle, &point);
+        return Vector2i(point.x, point.y);
+    }
+    else
+    {
+        return Vector2i();
+    }
 }
 
 
@@ -195,9 +203,13 @@ void InputImpl::SetMousePosition(const Vector2i& position)
 ////////////////////////////////////////////////////////////
 void InputImpl::SetMousePosition(const Vector2i& position, const Window& relativeTo)
 {
-    POINT point = {position.x, position.y};
-    ClientToScreen(relativeTo.GetSystemHandle(), &point);
-    SetCursorPos(point.x, point.y);
+    WindowHandle handle = relativeTo.GetSystemHandle();
+    if (handle)
+    {
+        POINT point = {position.x, position.y};
+        ClientToScreen(handle, &point);
+        SetCursorPos(point.x, point.y);
+    }
 }
 
 } // namespace priv