Added support for sRGB framebuffers via the GLX_ARB_framebuffer_sRGB /
[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_POSIX_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_POSIX_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_POSIX_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_MS_WINDOWS
160
161     /*
162      * Joe Krahn is re-writing the following code.
163      */
164     /* Set the cursor AND change it for this window class. */
165 #if _MSC_VER <= 1200
166 #       define MAP_CURSOR(a,b)                                   \
167         case a:                                                  \
168             SetCursor( LoadCursor( NULL, b ) );                  \
169             SetClassLong( window->Window.Handle,                 \
170                           GCL_HCURSOR,                           \
171                           ( LONG )LoadCursor( NULL, b ) );       \
172         break;
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 #else
181 #       define MAP_CURSOR(a,b)                                   \
182         case a:                                                  \
183             SetCursor( LoadCursor( NULL, b ) );                  \
184             SetClassLongPtr( window->Window.Handle,              \
185                           GCLP_HCURSOR,                          \
186                           ( LONG )( LONG_PTR )LoadCursor( NULL, b ) );       \
187         break;
188     /* Nuke the cursor AND change it for this window class. */
189 #       define ZAP_CURSOR(a,b)                                   \
190         case a:                                                  \
191             SetCursor( NULL );                                   \
192             SetClassLongPtr( window->Window.Handle,              \
193                           GCLP_HCURSOR, ( LONG )( LONG_PTR )NULL );          \
194         break;
195 #endif
196
197     switch( cursorID )
198     {
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 */
222
223     default:
224         fgError( "Unknown cursor type: %d", cursorID );
225         break;
226     }
227 #endif
228
229     window->State.Cursor = cursorID;
230 }
231
232 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
233
234 /*
235  * Set the cursor image to be used for the current window
236  */
237 void FGAPIENTRY glutSetCursor( int cursorID )
238 {
239     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetCursor" );
240     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetCursor" );
241
242     fgSetCursor ( fgStructure.CurrentWindow, cursorID );
243 }
244
245 /*
246  * Moves the mouse pointer to given window coordinates
247  */
248 void FGAPIENTRY glutWarpPointer( int x, int y )
249 {
250     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutWarpPointer" );
251     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutWarpPointer" );
252
253 #if TARGET_HOST_POSIX_X11
254
255     XWarpPointer(
256         fgDisplay.Display,
257         None,
258         fgStructure.CurrentWindow->Window.Handle,
259         0, 0, 0, 0,
260         x, y
261     );
262     /* Make the warp visible immediately. */
263     XFlush( fgDisplay.Display );
264
265 #elif TARGET_HOST_MS_WINDOWS
266
267     {
268         POINT coords;
269         coords.x = x;
270         coords.y = y;
271
272         /* ClientToScreen() translates {coords} for us. */
273         ClientToScreen( fgStructure.CurrentWindow->Window.Handle, &coords );
274         SetCursorPos( coords.x, coords.y );
275     }
276
277 #endif
278 }
279
280 /*** END OF FILE ***/