4 * The mouse cursor related stuff.
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8 * Creation date: Thu Dec 16 1999
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
31 #if TARGET_HOST_POSIX_X11
32 #include <X11/cursorfont.h>
36 * TODO BEFORE THE STABLE RELEASE:
37 * glutSetCursor() -- Win32 mappings are incomplete.
39 * It would be good to use custom mouse cursor shapes, and introduce
40 * an option to display them using glBitmap() and/or texture mapping,
41 * apart from the windowing system version.
44 /* -- PRIVATE FUNCTIONS --------------------------------------------------- */
46 #if TARGET_HOST_POSIX_X11
48 * A factory method for an empty cursor
50 static Cursor getEmptyCursor( void )
52 static Cursor cursorNone = None;
53 if( cursorNone == None ) {
54 char cursorNoneBits[ 32 ];
56 Pixmap cursorNonePixmap;
57 memset( cursorNoneBits, 0, sizeof( cursorNoneBits ) );
58 memset( &dontCare, 0, sizeof( dontCare ) );
59 cursorNonePixmap = XCreateBitmapFromData ( fgDisplay.Display,
61 cursorNoneBits, 16, 16 );
62 if( cursorNonePixmap != None ) {
63 cursorNone = XCreatePixmapCursor( fgDisplay.Display,
64 cursorNonePixmap, cursorNonePixmap,
65 &dontCare, &dontCare, 0, 0 );
66 XFreePixmap( fgDisplay.Display, cursorNonePixmap );
72 typedef struct tag_cursorCacheEntry cursorCacheEntry;
73 struct tag_cursorCacheEntry {
74 unsigned int cursorShape; /* an XC_foo value */
75 Cursor cachedCursor; /* None if the corresponding cursor has
76 not been created yet */
80 * Note: The arrangement of the table below depends on the fact that
81 * the "normal" GLUT_CURSOR_* values start a 0 and are consecutive.
83 static cursorCacheEntry cursorCache[] = {
84 { XC_arrow, None }, /* GLUT_CURSOR_RIGHT_ARROW */
85 { XC_top_left_arrow, None }, /* GLUT_CURSOR_LEFT_ARROW */
86 { XC_hand1, None }, /* GLUT_CURSOR_INFO */
87 { XC_pirate, None }, /* GLUT_CURSOR_DESTROY */
88 { XC_question_arrow, None }, /* GLUT_CURSOR_HELP */
89 { XC_exchange, None }, /* GLUT_CURSOR_CYCLE */
90 { XC_spraycan, None }, /* GLUT_CURSOR_SPRAY */
91 { XC_watch, None }, /* GLUT_CURSOR_WAIT */
92 { XC_xterm, None }, /* GLUT_CURSOR_TEXT */
93 { XC_crosshair, None }, /* GLUT_CURSOR_CROSSHAIR */
94 { XC_sb_v_double_arrow, None }, /* GLUT_CURSOR_UP_DOWN */
95 { XC_sb_h_double_arrow, None }, /* GLUT_CURSOR_LEFT_RIGHT */
96 { XC_top_side, None }, /* GLUT_CURSOR_TOP_SIDE */
97 { XC_bottom_side, None }, /* GLUT_CURSOR_BOTTOM_SIDE */
98 { XC_left_side, None }, /* GLUT_CURSOR_LEFT_SIDE */
99 { XC_right_side, None }, /* GLUT_CURSOR_RIGHT_SIDE */
100 { XC_top_left_corner, None }, /* GLUT_CURSOR_TOP_LEFT_CORNER */
101 { XC_top_right_corner, None }, /* GLUT_CURSOR_TOP_RIGHT_CORNER */
102 { XC_bottom_right_corner, None }, /* GLUT_CURSOR_BOTTOM_RIGHT_CORNER */
103 { XC_bottom_left_corner, None } /* GLUT_CURSOR_BOTTOM_LEFT_CORNER */
107 /* -- INTERNAL FUNCTIONS ---------------------------------------------------- */
110 * Set the cursor image to be used for the current window
112 void fgSetCursor ( SFG_Window *window, int cursorID )
114 #if TARGET_HOST_POSIX_X11
118 * XXX FULL_CROSSHAIR demotes to plain CROSSHAIR. Old GLUT allows
119 * for this, but if there is a system that easily supports a full-
120 * window (or full-screen) crosshair, we might consider it.
123 ( cursorID == GLUT_CURSOR_FULL_CROSSHAIR ) ? GLUT_CURSOR_CROSSHAIR : cursorID;
125 if( ( cursorIDToUse >= 0 ) &&
126 ( cursorIDToUse < sizeof( cursorCache ) / sizeof( cursorCache[0] ) ) ) {
127 cursorCacheEntry *entry = &cursorCache[ cursorIDToUse ];
128 if( entry->cachedCursor == None ) {
129 entry->cachedCursor =
130 XCreateFontCursor( fgDisplay.Display, entry->cursorShape );
132 cursor = entry->cachedCursor;
134 switch( cursorIDToUse )
136 case GLUT_CURSOR_NONE:
137 cursor = getEmptyCursor( );
140 case GLUT_CURSOR_INHERIT:
145 fgError( "Unknown cursor type: %d", cursorIDToUse );
150 if ( cursorIDToUse == GLUT_CURSOR_INHERIT ) {
151 XUndefineCursor( fgDisplay.Display, window->Window.Handle );
152 } else if ( cursor != None ) {
153 XDefineCursor( fgDisplay.Display, window->Window.Handle, cursor );
154 } else if ( cursorIDToUse != GLUT_CURSOR_NONE ) {
155 fgError( "Failed to create cursor" );
159 #elif TARGET_HOST_MS_WINDOWS
162 * Joe Krahn is re-writing the following code.
164 /* Set the cursor AND change it for this window class. */
166 # define MAP_CURSOR(a,b) \
168 SetCursor( LoadCursor( NULL, b ) ); \
169 SetClassLong( window->Window.Handle, \
171 ( LONG )LoadCursor( NULL, b ) ); \
173 /* Nuke the cursor AND change it for this window class. */
174 # define ZAP_CURSOR(a,b) \
177 SetClassLong( window->Window.Handle, \
178 GCL_HCURSOR, ( LONG )NULL ); \
181 # define MAP_CURSOR(a,b) \
183 SetCursor( LoadCursor( NULL, b ) ); \
184 SetClassLongPtr( window->Window.Handle, \
186 ( LONG )LoadCursor( NULL, b ) ); \
188 /* Nuke the cursor AND change it for this window class. */
189 # define ZAP_CURSOR(a,b) \
192 SetClassLong( window->Window.Handle, \
193 GCLP_HCURSOR, ( LONG )NULL ); \
199 MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW );
200 MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW, IDC_ARROW );
201 MAP_CURSOR( GLUT_CURSOR_INFO, IDC_HELP );
202 MAP_CURSOR( GLUT_CURSOR_DESTROY, IDC_CROSS );
203 MAP_CURSOR( GLUT_CURSOR_HELP, IDC_HELP );
204 MAP_CURSOR( GLUT_CURSOR_CYCLE, IDC_SIZEALL );
205 MAP_CURSOR( GLUT_CURSOR_SPRAY, IDC_CROSS );
206 MAP_CURSOR( GLUT_CURSOR_WAIT, IDC_WAIT );
207 MAP_CURSOR( GLUT_CURSOR_TEXT, IDC_IBEAM );
208 MAP_CURSOR( GLUT_CURSOR_CROSSHAIR, IDC_CROSS );
209 MAP_CURSOR( GLUT_CURSOR_UP_DOWN, IDC_SIZENS );
210 MAP_CURSOR( GLUT_CURSOR_LEFT_RIGHT, IDC_SIZEWE );
211 MAP_CURSOR( GLUT_CURSOR_TOP_SIDE, IDC_ARROW ); /* XXX ToDo */
212 MAP_CURSOR( GLUT_CURSOR_BOTTOM_SIDE, IDC_ARROW ); /* XXX ToDo */
213 MAP_CURSOR( GLUT_CURSOR_LEFT_SIDE, IDC_ARROW ); /* XXX ToDo */
214 MAP_CURSOR( GLUT_CURSOR_RIGHT_SIDE, IDC_ARROW ); /* XXX ToDo */
215 MAP_CURSOR( GLUT_CURSOR_TOP_LEFT_CORNER, IDC_SIZENWSE );
216 MAP_CURSOR( GLUT_CURSOR_TOP_RIGHT_CORNER, IDC_SIZENESW );
217 MAP_CURSOR( GLUT_CURSOR_BOTTOM_RIGHT_CORNER, IDC_SIZENWSE );
218 MAP_CURSOR( GLUT_CURSOR_BOTTOM_LEFT_CORNER, IDC_SIZENESW );
219 MAP_CURSOR( GLUT_CURSOR_INHERIT, IDC_ARROW ); /* XXX ToDo */
220 ZAP_CURSOR( GLUT_CURSOR_NONE, NULL );
221 MAP_CURSOR( GLUT_CURSOR_FULL_CROSSHAIR, IDC_CROSS ); /* XXX ToDo */
224 fgError( "Unknown cursor type: %d", cursorID );
229 window->State.Cursor = cursorID;
232 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
235 * Set the cursor image to be used for the current window
237 void FGAPIENTRY glutSetCursor( int cursorID )
239 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetCursor" );
240 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetCursor" );
242 fgSetCursor ( fgStructure.CurrentWindow, cursorID );
246 * Moves the mouse pointer to given window coordinates
248 void FGAPIENTRY glutWarpPointer( int x, int y )
250 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWarpPointer" );
251 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutWarpPointer" );
253 #if TARGET_HOST_POSIX_X11
258 fgStructure.CurrentWindow->Window.Handle,
262 /* Make the warp visible immediately. */
263 XFlush( fgDisplay.Display );
265 #elif TARGET_HOST_MS_WINDOWS
272 /* ClientToScreen() translates {coords} for us. */
273 ClientToScreen( fgStructure.CurrentWindow->Window.Handle, &coords );
274 SetCursorPos( coords.x, coords.y );
280 /*** END OF FILE ***/