Fixed the GLUT_CURSOR_INHERIT logic once again...
[freeglut] / src / freeglut_cursor.c
1 /*
2  * freeglut_cursor.c
3  *
4  * The mouse cursor related stuff.
5  *
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
9  *
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:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
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.
26  */
27
28 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
30
31 #if TARGET_HOST_UNIX_X11
32   #include <X11/cursorfont.h>
33 #endif
34
35 /*
36  * TODO BEFORE THE STABLE RELEASE:
37  *  glutSetCursor()     -- Win32 mappings are incomplete.
38  *
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.
42  */
43
44 /* -- PRIVATE FUNCTIONS --------------------------------------------------- */
45
46 #if TARGET_HOST_UNIX_X11
47 /*
48  * A factory method for an empty cursor
49  */
50 static Cursor getEmptyCursor( void )
51 {
52     static Cursor cursorNone = None;
53     if( cursorNone == None ) {
54         char cursorNoneBits[ 32 ];
55         XColor dontCare;
56         Pixmap cursorNonePixmap;
57         memset( cursorNoneBits, 0, sizeof( cursorNoneBits ) );
58         memset( &dontCare, 0, sizeof( dontCare ) );
59         cursorNonePixmap = XCreateBitmapFromData ( fgDisplay.Display,
60                                                    fgDisplay.RootWindow,
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 );
67         }
68     }
69     return cursorNone;
70 }
71
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 */
77 };
78
79 /*
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.
82  */ 
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 */
104 };
105 #endif
106
107 /* -- INTERNAL FUNCTIONS ---------------------------------------------------- */
108
109 /*
110  * Set the cursor image to be used for the current window
111  */
112 void fgSetCursor ( SFG_Window *window, int cursorID )
113 {
114 #if TARGET_HOST_UNIX_X11
115     {
116         Cursor cursor;
117         /*
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.
121          */
122         int cursorIDToUse =
123             ( cursorID == GLUT_CURSOR_FULL_CROSSHAIR ) ? GLUT_CURSOR_CROSSHAIR : cursorID;
124
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 );
131             }
132             cursor = entry->cachedCursor;
133         } else {
134             switch( cursorIDToUse )
135             {
136             case GLUT_CURSOR_NONE:
137                 cursor = getEmptyCursor( );
138                 break;
139
140             case GLUT_CURSOR_INHERIT:
141                 cursor = None;
142                 break;
143
144             default:
145                 fgError( "Unknown cursor type: %d", cursorIDToUse );
146                 return;
147             }
148         }
149
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" );
156         }
157     }
158
159 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
160
161     /*
162      * This is a temporary solution only...
163      */
164     /* Set the cursor AND change it for this window class. */
165 #       define MAP_CURSOR(a,b)                                   \
166         case a:                                                  \
167             SetCursor( LoadCursor( NULL, b ) );                  \
168             SetClassLong( window->Window.Handle,                 \
169                           GCL_HCURSOR,                           \
170                           ( LONG )LoadCursor( NULL, b ) );       \
171         break;
172
173     /* Nuke the cursor AND change it for this window class. */
174 #       define ZAP_CURSOR(a,b)                                   \
175         case a:                                                  \
176             SetCursor( NULL );                                   \
177             SetClassLong( window->Window.Handle,                 \
178                           GCL_HCURSOR, ( LONG )NULL );           \
179         break;
180
181     switch( cursorID )
182     {
183         MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW,         IDC_ARROW     );
184         MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW,          IDC_ARROW     );
185         MAP_CURSOR( GLUT_CURSOR_INFO,                IDC_HELP      );
186         MAP_CURSOR( GLUT_CURSOR_DESTROY,             IDC_CROSS     );
187         MAP_CURSOR( GLUT_CURSOR_HELP,                IDC_HELP      );
188         MAP_CURSOR( GLUT_CURSOR_CYCLE,               IDC_SIZEALL   );
189         MAP_CURSOR( GLUT_CURSOR_SPRAY,               IDC_CROSS     );
190         MAP_CURSOR( GLUT_CURSOR_WAIT,                IDC_WAIT      );
191         MAP_CURSOR( GLUT_CURSOR_TEXT,                IDC_IBEAM     );
192         MAP_CURSOR( GLUT_CURSOR_CROSSHAIR,           IDC_CROSS     );
193         MAP_CURSOR( GLUT_CURSOR_UP_DOWN,             IDC_SIZENS    );
194         MAP_CURSOR( GLUT_CURSOR_LEFT_RIGHT,          IDC_SIZEWE    );
195         MAP_CURSOR( GLUT_CURSOR_TOP_SIDE,            IDC_ARROW     ); /* XXX ToDo */
196         MAP_CURSOR( GLUT_CURSOR_BOTTOM_SIDE,         IDC_ARROW     ); /* XXX ToDo */
197         MAP_CURSOR( GLUT_CURSOR_LEFT_SIDE,           IDC_ARROW     ); /* XXX ToDo */
198         MAP_CURSOR( GLUT_CURSOR_RIGHT_SIDE,          IDC_ARROW     ); /* XXX ToDo */
199         MAP_CURSOR( GLUT_CURSOR_TOP_LEFT_CORNER,     IDC_SIZENWSE  );
200         MAP_CURSOR( GLUT_CURSOR_TOP_RIGHT_CORNER,    IDC_SIZENESW  );
201         MAP_CURSOR( GLUT_CURSOR_BOTTOM_RIGHT_CORNER, IDC_SIZENWSE  );
202         MAP_CURSOR( GLUT_CURSOR_BOTTOM_LEFT_CORNER,  IDC_SIZENESW  );
203         MAP_CURSOR( GLUT_CURSOR_INHERIT,             IDC_ARROW     ); /* XXX ToDo */
204         ZAP_CURSOR( GLUT_CURSOR_NONE,                NULL          );
205         MAP_CURSOR( GLUT_CURSOR_FULL_CROSSHAIR,      IDC_CROSS     ); /* XXX ToDo */
206
207     default:
208         fgError( "Unknown cursor type: %d", cursorID );
209         break;
210     }
211 #endif
212
213     window->State.Cursor = cursorID;
214 }
215
216 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
217
218 /*
219  * Set the cursor image to be used for the current window
220  */
221 void FGAPIENTRY glutSetCursor( int cursorID )
222 {
223     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetCursor" );
224     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetCursor" );
225
226     fgSetCursor ( fgStructure.CurrentWindow, cursorID );
227 }
228
229 /*
230  * Moves the mouse pointer to given window coordinates
231  */
232 void FGAPIENTRY glutWarpPointer( int x, int y )
233 {
234     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWarpPointer" );
235     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutWarpPointer" );
236
237 #if TARGET_HOST_UNIX_X11
238
239     XWarpPointer(
240         fgDisplay.Display,
241         None,
242         fgStructure.CurrentWindow->Window.Handle,
243         0, 0, 0, 0,
244         x, y
245     );
246     /* Make the warp visible immediately. */
247     XFlush( fgDisplay.Display );
248
249 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
250
251     {
252         POINT coords;
253         coords.x = x;
254         coords.y = y;
255
256         /* ClientToScreen() translates {coords} for us. */
257         ClientToScreen( fgStructure.CurrentWindow->Window.Handle, &coords );
258         SetCursorPos( coords.x, coords.y );
259     }
260
261 #endif
262 }
263
264 /*** END OF FILE ***/