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 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include "../include/GL/freeglut.h"
33 #include "freeglut_internal.h"
34 
35 #if TARGET_HOST_UNIX_X11
36   #include <X11/cursorfont.h>
37 #endif
38 
39 /*
40  * TODO BEFORE THE STABLE RELEASE:
41  *  glutSetCursor()     -- Win32 mappings are incomplete.
42  *
43  * It would be good to use custom mouse cursor shapes, and introduce
44  * an option to display them using glBitmap() and/or texture mapping,
45  * apart from the windowing system version.
46  */
47 
48 /* -- INTERNAL FUNCTIONS --------------------------------------------------- */
49 
50 #if TARGET_HOST_UNIX_X11
51 
fgGetCursorError(Cursor cursor)52 int fgGetCursorError( Cursor cursor )
53 {
54     int ret = 0;
55     char buf[ 256 ];
56 
57     switch( cursor )
58     {
59     case BadAlloc:
60     case BadFont:
61     case BadMatch:
62     case BadPixmap:
63     case BadValue:
64         XGetErrorText( fgDisplay.Display, cursor, buf, sizeof buf );
65         fgWarning( "Error in setting cursor:\n %s.", buf );
66         ret = cursor;
67         break;
68     default:
69         /* no error */
70         break;
71     }
72 
73     return ret;
74 }
75 
76 #endif
77 
78 
79 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
80 
81 /*
82  * Set the cursor image to be used for the current window
83  */
glutSetCursor(int cursorID)84 void  glutSetCursor( int cursorID )
85 {
86     freeglut_assert_ready;  /* XXX WHY do we need the timer active for this? */
87     freeglut_assert_window;
88 
89 #if TARGET_HOST_UNIX_X11
90     /*
91      * Open issues:
92      * (a) Partial error checking.  Is that a problem?
93      *     Is fgGetCursorError() correct?  Should we abort on errors?
94      *     Should there be a freeglut-wide X error handler?  Should
95      *     we use the X error-handler mechanism?
96      * (b) FULL_CROSSHAIR demotes to plain CROSSHAIR.  Old GLUT allows
97      *     for this, but if there is a system that easily supports a full-
98      *     window (or full-screen) crosshair, we might consider it.
99      * (c) Out-of-range cursor-types generate warnings.  Should we abort?
100      */
101     {
102         Cursor cursor = None;
103         Pixmap no_cursor = None ;  /* Used for GLUT_CURSOR_NONE */
104         int error = 0;
105 
106 #define MAP_CURSOR(a,b)                                     \
107     case a:                                                 \
108         cursor = XCreateFontCursor( fgDisplay.Display, b ); \
109         break;
110 
111         if( GLUT_CURSOR_FULL_CROSSHAIR == cursorID )
112             cursorID = GLUT_CURSOR_CROSSHAIR;
113 
114         switch( cursorID )
115         {
116             MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, XC_right_ptr);
117             MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW,  XC_left_ptr);
118             MAP_CURSOR( GLUT_CURSOR_INFO,        XC_hand1);
119             MAP_CURSOR( GLUT_CURSOR_DESTROY,     XC_pirate);
120             MAP_CURSOR( GLUT_CURSOR_HELP,        XC_question_arrow);
121             MAP_CURSOR( GLUT_CURSOR_CYCLE,       XC_exchange);
122             MAP_CURSOR( GLUT_CURSOR_SPRAY,       XC_spraycan);
123             MAP_CURSOR( GLUT_CURSOR_WAIT,        XC_watch);
124             MAP_CURSOR( GLUT_CURSOR_TEXT,        XC_xterm);
125             MAP_CURSOR( GLUT_CURSOR_CROSSHAIR,   XC_crosshair);
126             MAP_CURSOR( GLUT_CURSOR_UP_DOWN,     XC_sb_v_double_arrow);
127             MAP_CURSOR( GLUT_CURSOR_LEFT_RIGHT,  XC_sb_h_double_arrow);
128             MAP_CURSOR( GLUT_CURSOR_TOP_SIDE,    XC_top_side);
129             MAP_CURSOR( GLUT_CURSOR_BOTTOM_SIDE, XC_bottom_side);
130             MAP_CURSOR( GLUT_CURSOR_LEFT_SIDE,   XC_left_side);
131             MAP_CURSOR( GLUT_CURSOR_RIGHT_SIDE,  XC_right_side);
132             MAP_CURSOR( GLUT_CURSOR_TOP_LEFT_CORNER,     XC_top_left_corner);
133             MAP_CURSOR( GLUT_CURSOR_TOP_RIGHT_CORNER,    XC_top_right_corner);
134             MAP_CURSOR( GLUT_CURSOR_BOTTOM_RIGHT_CORNER,
135                         XC_bottom_right_corner);
136             MAP_CURSOR( GLUT_CURSOR_BOTTOM_LEFT_CORNER, XC_bottom_left_corner);
137             /* MAP_CURSOR( GLUT_CURSOR_NONE,        XC_bogosity); */
138 
139         case GLUT_CURSOR_NONE:
140         {
141             /*
142              * Note that we *never* change {no_cursor_bits} from anything
143              * but all-zeros.  It is our image and mask.  We also apparently
144              * need to pick a color for foreground/background---but what
145              * one we pick doesn't matter for GLUT_CURSOR_NONE.
146              */
147             static unsigned char no_cursor_bits[ 32 ];
148             XColor black;
149             no_cursor = XCreatePixmapFromBitmapData( fgDisplay.Display,
150                                                      fgDisplay.RootWindow,
151                                                      no_cursor_bits,
152                                                      16, 16,
153                                                      1, 0, 1 );
154             XParseColor( fgDisplay.Display,
155                          DefaultColormap( fgDisplay.Display,
156                                           DefaultScreen( fgDisplay.Display ) ),
157                          "black",
158                          &black );
159             cursor = XCreatePixmapCursor( fgDisplay.Display,
160                                           no_cursor, no_cursor,
161                                           &black, &black,
162                                           0, 0 );
163             break;
164         }
165 
166         case GLUT_CURSOR_INHERIT:
167             break;
168 
169         default:
170             fgWarning( "Unknown cursor type: %d\n", cursorID );
171             return;
172         }
173 
174         error = fgGetCursorError( cursor );
175 
176         if( GLUT_CURSOR_INHERIT == cursorID )
177             XUndefineCursor( fgDisplay.Display,
178                              fgStructure.Window->Window.Handle );
179         else
180         {
181             XDefineCursor( fgDisplay.Display,
182                            fgStructure.Window->Window.Handle, cursor );
183             XFreeCursor( fgDisplay.Display, cursor );
184             if( GLUT_CURSOR_NONE == cursorID )
185                 XFreePixmap( fgDisplay.Display, no_cursor );
186         }
187     }
188 
189 #elif TARGET_HOST_WIN32
190 
191     /*
192      * This is a temporary solution only...
193      */
194     /* Set the cursor AND change it for this window class. */
195 #       define MAP_CURSOR(a,b)                                   \
196         case a:                                                  \
197             SetCursor( LoadCursor( NULL, b ) );                  \
198             SetClassLong( fgStructure.Window->Window.Handle,     \
199                           GCL_HCURSOR,                           \
200                           ( LONG )LoadCursor( NULL, b ) );       \
201         break;
202 
203     /* Nuke the cursor AND change it for this window class. */
204 #       define ZAP_CURSOR(a,b)                                   \
205         case a:                                                  \
206             SetCursor( NULL );                                   \
207             SetClassLong( fgStructure.Window->Window.Handle,     \
208                           GCL_HCURSOR, ( LONG )NULL );           \
209         break;
210 
211     switch( cursorID )
212     {
213         MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW     );
214         MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW,  IDC_ARROW     );
215         MAP_CURSOR( GLUT_CURSOR_INFO,        IDC_HELP      );
216         MAP_CURSOR( GLUT_CURSOR_DESTROY,     IDC_CROSS     );
217         MAP_CURSOR( GLUT_CURSOR_HELP,        IDC_HELP      );
218         MAP_CURSOR( GLUT_CURSOR_CYCLE,       IDC_SIZEALL   );
219         MAP_CURSOR( GLUT_CURSOR_SPRAY,       IDC_CROSS     );
220         MAP_CURSOR( GLUT_CURSOR_WAIT,        IDC_WAIT      );
221         MAP_CURSOR( GLUT_CURSOR_TEXT,        IDC_UPARROW   );
222         MAP_CURSOR( GLUT_CURSOR_CROSSHAIR,   IDC_CROSS     );
223         /* MAP_CURSOR( GLUT_CURSOR_NONE,        IDC_NO        ); */
224         ZAP_CURSOR( GLUT_CURSOR_NONE,        NULL           );
225 
226     default:
227         MAP_CURSOR( GLUT_CURSOR_UP_DOWN,     IDC_ARROW     );
228     }
229 #endif
230 
231     fgStructure.Window->State.Cursor = cursorID;
232 }
233 
234 /*
235  * Moves the mouse pointer to given window coordinates
236  */
glutWarpPointer(int x,int y)237 void  glutWarpPointer( int x, int y )
238 {
239     freeglut_assert_ready; /* XXX WHY do we need the timer active for this? */
240     freeglut_assert_window;
241 
242 #if TARGET_HOST_UNIX_X11
243 
244     XWarpPointer(
245         fgDisplay.Display,
246         None,
247         fgStructure.Window->Window.Handle,
248         0, 0, 0, 0,
249         x, y
250     );
251     XFlush( fgDisplay.Display ); /* XXX Is this really necessary? */
252 
253 #elif TARGET_HOST_WIN32
254 
255     {
256         POINT coords = { x, y };
257         /*
258          * ClientToScreen() translates {coords} for us.
259          */
260         ClientToScreen( fgStructure.Window->Window.Handle, &coords );
261         SetCursorPos( coords.x, coords.y );
262     }
263 
264 #endif
265 }
266 
267 /*** END OF FILE ***/
268