Further normalization of the code's style. No substantial changes.
[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 #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 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
51
52 /*
53  * Set the cursor image to be used for the current window
54  */
55 void FGAPIENTRY glutSetCursor( int cursorID )
56 {
57     freeglut_assert_ready;  /* XXX WHY do we need the timer active for this? */
58     freeglut_assert_window;
59
60 #if TARGET_HOST_UNIX_X11
61     /*
62      * Open issues:
63      * (X) GLUT_CURSOR_NONE doesn't do what it should.  We can probably
64      *     build an empty pixmap for it, though, quite painlessly.
65      * (X) Are we allocating resources, or causing X to do so?
66      *     If yes, we should arrange to deallocate!
67      * (c) No error checking.  Is that a problem?
68      * (d) FULL_CROSSHAIR demotes to plain CROSSHAIR.  Old GLUT allows
69      *     for this, but if there is a system that easily supports a full-
70      *     window (or full-screen) crosshair, we might consider it.
71      * (e) Out-of-range cursor-types are ignored.  Should we abort?
72      *     Print a warning message?
73      */
74     {
75         Cursor cursor;
76         Pixmap no_cursor;  /* Used for GLUT_CURSOR_NONE */
77
78 #define MAP_CURSOR(a,b)                                     \
79     case a:                                                 \
80         cursor = XCreateFontCursor( fgDisplay.Display, b ); \
81         break;
82
83         if( GLUT_CURSOR_FULL_CROSSHAIR == cursorID )
84             cursorID = GLUT_CURSOR_CROSSHAIR;
85         
86         switch( cursorID )
87         {
88             MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, XC_right_ptr);
89             MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW,  XC_left_ptr);
90             MAP_CURSOR( GLUT_CURSOR_INFO,        XC_hand1);
91             MAP_CURSOR( GLUT_CURSOR_DESTROY,     XC_pirate);
92             MAP_CURSOR( GLUT_CURSOR_HELP,        XC_question_arrow);
93             MAP_CURSOR( GLUT_CURSOR_CYCLE,       XC_exchange);
94             MAP_CURSOR( GLUT_CURSOR_SPRAY,       XC_spraycan);
95             MAP_CURSOR( GLUT_CURSOR_WAIT,        XC_watch);
96             MAP_CURSOR( GLUT_CURSOR_TEXT,        XC_xterm);
97             MAP_CURSOR( GLUT_CURSOR_CROSSHAIR,   XC_crosshair);
98             MAP_CURSOR( GLUT_CURSOR_UP_DOWN,     XC_sb_v_double_arrow);
99             MAP_CURSOR( GLUT_CURSOR_LEFT_RIGHT,  XC_sb_h_double_arrow);
100             MAP_CURSOR( GLUT_CURSOR_TOP_SIDE,    XC_top_side);
101             MAP_CURSOR( GLUT_CURSOR_BOTTOM_SIDE, XC_bottom_side);
102             MAP_CURSOR( GLUT_CURSOR_LEFT_SIDE,   XC_left_side);
103             MAP_CURSOR( GLUT_CURSOR_RIGHT_SIDE,  XC_right_side);
104             MAP_CURSOR( GLUT_CURSOR_TOP_LEFT_CORNER,     XC_top_left_corner);
105             MAP_CURSOR( GLUT_CURSOR_TOP_RIGHT_CORNER,    XC_top_right_corner);
106             MAP_CURSOR( GLUT_CURSOR_BOTTOM_RIGHT_CORNER,
107                         XC_bottom_right_corner);
108             MAP_CURSOR( GLUT_CURSOR_BOTTOM_LEFT_CORNER, XC_bottom_left_corner);
109             /* MAP_CURSOR( GLUT_CURSOR_NONE,        XC_bogosity); */
110         case GLUT_CURSOR_NONE:
111         {
112             static unsigned char no_cursor_bits[ 32 ];
113             XColor black;
114             no_cursor = XCreatePixmapFromBitmapData( fgDisplay.Display,
115                                                      fgDisplay.RootWindow,
116                                                      no_cursor_bits,
117                                                      16, 16,
118                                                      1, 0, 1 );
119             XParseColor( fgDisplay.Display,
120                          DefaultColormap( fgDisplay.Display,
121                                           DefaultScreen( fgDisplay.Display ) ),
122                          "black",
123                          &black );
124             cursor = XCreatePixmapCursor( fgDisplay.Display,
125                                           no_cursor, no_cursor,
126                                           &black, &black,
127                                           0, 0 );
128             break;
129         }
130         
131         case GLUT_CURSOR_INHERIT:
132             break;
133         default:
134             return;
135         }
136
137         if( GLUT_CURSOR_INHERIT == cursorID )
138             XUndefineCursor( fgDisplay.Display,
139                              fgStructure.Window->Window.Handle );
140         else
141         {
142             XDefineCursor( fgDisplay.Display,
143                            fgStructure.Window->Window.Handle, cursor );
144             XFreeCursor( fgDisplay.Display, cursor );
145             if( GLUT_CURSOR_NONE == cursorID )
146                 XFreePixmap( fgDisplay.Display, no_cursor );
147         }
148     }
149
150 #elif TARGET_HOST_WIN32
151
152     /*
153      * This is a temporary solution only...
154      */
155     /* Set the cursor AND change it for this window class. */
156 #       define MAP_CURSOR(a,b)                                   \
157         case a:                                                  \
158             SetCursor( LoadCursor( NULL, b ) );                  \
159             SetClassLong( fgStructure.Window->Window.Handle,     \
160                           GCL_HCURSOR,                           \
161                           ( LONG )LoadCursor( NULL, b ) );       \
162         break;
163
164         /* Nuke the cursor AND change it for this window class. */
165 #       define ZAP_CURSOR(a,b)                                   \
166         case a:                                                  \
167             SetCursor( NULL );                                   \
168             SetClassLong( fgStructure.Window->Window.Handle,     \
169                           GCL_HCURSOR, ( LONG )NULL );           \
170         break;
171
172         switch( cursorID )
173         {
174             MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW     );
175             MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW,  IDC_ARROW     );
176             MAP_CURSOR( GLUT_CURSOR_INFO,        IDC_HELP      );
177             MAP_CURSOR( GLUT_CURSOR_DESTROY,     IDC_CROSS     );
178             MAP_CURSOR( GLUT_CURSOR_HELP,        IDC_HELP      );
179             MAP_CURSOR( GLUT_CURSOR_CYCLE,       IDC_SIZEALL   );
180             MAP_CURSOR( GLUT_CURSOR_SPRAY,       IDC_CROSS     );
181             MAP_CURSOR( GLUT_CURSOR_WAIT,        IDC_WAIT      );
182             MAP_CURSOR( GLUT_CURSOR_TEXT,        IDC_UPARROW   );
183             MAP_CURSOR( GLUT_CURSOR_CROSSHAIR,   IDC_CROSS     );
184             /* MAP_CURSOR( GLUT_CURSOR_NONE,        IDC_NO        ); */
185             ZAP_CURSOR( GLUT_CURSOR_NONE,        NULL           );
186             
187         default:
188             MAP_CURSOR( GLUT_CURSOR_UP_DOWN,     IDC_ARROW     );
189         }
190 #endif
191
192   fgStructure.Window->State.Cursor = cursorID;
193 }
194
195 /*
196  * Moves the mouse pointer to given window coordinates
197  */
198 void FGAPIENTRY glutWarpPointer( int x, int y )
199 {
200     freeglut_assert_ready; /* XXX WHY do we need the timer active for this? */
201     freeglut_assert_window;
202
203 #if TARGET_HOST_UNIX_X11
204
205     XWarpPointer(
206         fgDisplay.Display,
207         None,
208         fgStructure.Window->Window.Handle,
209         0, 0, 0, 0,
210         x, y
211     );
212     XFlush( fgDisplay.Display ); /* XXX Is this really necessary? */
213
214 #elif TARGET_HOST_WIN32
215
216     {
217         POINT coords = { x, y };
218         /*
219          * ClientToScreen() translates {coords} for us.
220          */
221         ClientToScreen( fgStructure.Window->Window.Handle, &coords );
222         SetCursorPos( coords.x, coords.y );
223     }
224
225 #endif
226 }
227
228 /*** END OF FILE ***/