diff --git a/include/GL/glfw3.h b/include/GL/glfw3.h index e49654d1..421adb4a 100644 --- a/include/GL/glfw3.h +++ b/include/GL/glfw3.h @@ -474,6 +474,8 @@ typedef void (* GLFWwindowfocusfun)(GLFWwindow,int); typedef void (* GLFWwindowiconifyfun)(GLFWwindow,int); typedef void (* GLFWmousebuttonfun)(GLFWwindow,int,int); typedef void (* GLFWmouseposfun)(GLFWwindow,int,int); +typedef void (* GLFWcursorenterfun)(GLFWwindow); +typedef void (* GLFWcursorleavefun)(GLFWwindow); typedef void (* GLFWscrollfun)(GLFWwindow,int,int); typedef void (* GLFWkeyfun)(GLFWwindow,int,int); typedef void (* GLFWcharfun)(GLFWwindow,int); @@ -574,6 +576,8 @@ GLFWAPI void glfwSetCharCallback(GLFWcharfun cbfun); GLFWAPI void glfwSetMouseButtonCallback(GLFWmousebuttonfun cbfun); GLFWAPI void glfwSetMousePosCallback(GLFWmouseposfun cbfun); GLFWAPI void glfwSetScrollCallback(GLFWscrollfun cbfun); +GLFWAPI void glfwSetCursorEnterCallback(GLFWcursorenterfun cbfun); +GLFWAPI void glfwSetCursorLeaveCallback(GLFWcursorleavefun cbfun); /* Joystick input */ GLFWAPI int glfwGetJoystickParam(int joy, int param); diff --git a/src/input.c b/src/input.c index 531bcc7e..2c480424 100644 --- a/src/input.c +++ b/src/input.c @@ -436,3 +436,35 @@ GLFWAPI void glfwSetScrollCallback(GLFWscrollfun cbfun) _glfwLibrary.scrollCallback = cbfun; } + +//======================================================================== +// Set callback function for cursor enter events +//======================================================================== + +GLFWAPI void glfwSetCursorEnterCallback(GLFWcursorenterfun cbfun) +{ + if (!_glfwInitialized) + { + _glfwSetError(GLFW_NOT_INITIALIZED, NULL); + return; + } + + _glfwLibrary.cursorEnterCallback = cbfun; +} + + +//======================================================================== +// Set callback function for cursor enter events +//======================================================================== + +GLFWAPI void glfwSetCursorLeaveCallback(GLFWcursorleavefun cbfun) +{ + if (!_glfwInitialized) + { + _glfwSetError(GLFW_NOT_INITIALIZED, NULL); + return; + } + + _glfwLibrary.cursorLeaveCallback = cbfun; +} + diff --git a/src/internal.h b/src/internal.h index 5992a0f4..a15dbfc6 100644 --- a/src/internal.h +++ b/src/internal.h @@ -240,6 +240,8 @@ struct _GLFWlibrary GLFWscrollfun scrollCallback; GLFWkeyfun keyCallback; GLFWcharfun charCallback; + GLFWcursorenterfun cursorEnterCallback; + GLFWcursorleavefun cursorLeaveCallback; GLFWthreadmodel threading; GLFWallocator allocator; @@ -352,6 +354,8 @@ void _glfwInputChar(_GLFWwindow* window, int character); void _glfwInputScroll(_GLFWwindow* window, int x, int y); void _glfwInputMouseClick(_GLFWwindow* window, int button, int action); void _glfwInputCursorMotion(_GLFWwindow* window, int x, int y); +void _glfwInputCursorEnter(_GLFWwindow* window); +void _glfwInputCursorLeave(_GLFWwindow* window); // OpenGL context helpers (opengl.c) int _glfwStringInExtensionString(const char* string, const GLubyte* extensions); diff --git a/src/window.c b/src/window.c index 88b3dc18..567ec340 100644 --- a/src/window.c +++ b/src/window.c @@ -205,6 +205,25 @@ void _glfwInputWindowDamage(_GLFWwindow* window) _glfwLibrary.windowRefreshCallback(window); } +//======================================================================== +// Register cursor enter events +//======================================================================== + +void _glfwInputCursorEnter(_GLFWwindow* window) +{ + if (_glfwLibrary.cursorEnterCallback) + _glfwLibrary.cursorEnterCallback(window); +} + +//======================================================================== +// Register cursor leave events +//======================================================================== + +void _glfwInputCursorLeave(_GLFWwindow* window) +{ + if (_glfwLibrary.cursorLeaveCallback) + _glfwLibrary.cursorLeaveCallback(window); +} ////////////////////////////////////////////////////////////////////////// ////// GLFW public API ////// diff --git a/src/x11_window.c b/src/x11_window.c index 641b3c84..cbd844d3 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -679,7 +679,8 @@ static GLboolean createWindow(_GLFWwindow* window, wa.border_pixel = 0; wa.event_mask = StructureNotifyMask | KeyPressMask | KeyReleaseMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask | - ExposureMask | FocusChangeMask | VisibilityChangeMask; + ExposureMask | FocusChangeMask | VisibilityChangeMask | + EnterWindowMask | LeaveWindowMask; if (wndconfig->mode == GLFW_WINDOWED) { @@ -1180,6 +1181,37 @@ static void processSingleEvent(void) break; } + case EnterNotify: + { + // The mouse cursor enters the Window + window = findWindow(event.xcrossing.window); + if (window == NULL) + { + fprintf(stderr, "Cannot find GLFW window structure for EnterNotify event\n"); + return; + } + if(window->cursorMode == GLFW_CURSOR_HIDDEN) + { + hideMouseCursor(window); + } + _glfwInputCursorEnter(window); + break; + } + + case LeaveNotify: + { + // The mouse cursor leave the Window + window = findWindow(event.xcrossing.window); + if (window == NULL) + { + fprintf(stderr, "Cannot find GLFW window structure for LeaveNotify event\n"); + return; + } + showMouseCursor(window); + _glfwInputCursorLeave(window); + break; + } + case MotionNotify: { // The mouse cursor was moved