Added all missing GLUT cursor types for X11.
[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 #define  G_LOG_DOMAIN  "freeglut-cursor"
33
34 #include "../include/GL/freeglut.h"
35 #include "freeglut_internal.h"
36
37 #if TARGET_HOST_UNIX_X11
38     #include <X11/cursorfont.h>
39 #endif
40
41 /*
42  * TODO BEFORE THE STABLE RELEASE:
43  *
44  *  fgDisplayCursor()   -- this waits for better times
45  *  glutSetCursor()     -- both X and Win32 mappings are incomplete
46  *
47  * It would be good to use custom mouse cursor shapes, and introduce
48  * an option to display them using glBitmap() and/or texture mapping,
49  * apart from the windowing system version.
50  */
51
52 /* -- INTERNAL FUNCTIONS --------------------------------------------------- */
53
54 /*
55  * Display the mouse cursor using OpenGL calls
56  */
57 void fgDisplayCursor( void )
58 {
59     /*
60      * Do nothing for the moment
61      */
62 }
63
64
65 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
66
67 /*
68  * Set the cursor image to be used for the current window
69  */
70 void FGAPIENTRY glutSetCursor( int cursorID )
71 {
72     /*
73      * Make sure freeglut is ready and there is a current window set
74      */
75     freeglut_assert_ready; freeglut_assert_window;
76
77 #if TARGET_HOST_UNIX_X11
78     /*
79      * Open issues:
80      * (a) GLUT_CURSOR_NONE doesn't do what it should.
81      * (b) Are we allocating resources, or causing X to do so?
82      *     If yes, we should arrange to deallocate!
83      * (c) No error checking.  Is that a problem?
84      */
85     {
86         Cursor cursor;
87
88         /*
89          * For now we'll limit ourselves to the X cursor fonts...
90          */
91 #define MAP_CURSOR(a,b) case a: cursor = XCreateFontCursor( fgDisplay.Display, b ); break;
92         if( GLUT_CURSOR_FULL_CROSSHAIR == cursorID )
93             cursorID = GLUT_CURSOR_CROSSHAIR;
94         switch( cursorID )
95         {
96             MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, XC_right_ptr);
97             MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW,  XC_left_ptr);
98             MAP_CURSOR( GLUT_CURSOR_INFO,        XC_hand1);
99             MAP_CURSOR( GLUT_CURSOR_DESTROY,     XC_pirate);
100             MAP_CURSOR( GLUT_CURSOR_HELP,        XC_question_arrow);
101             MAP_CURSOR( GLUT_CURSOR_CYCLE,       XC_exchange);
102             MAP_CURSOR( GLUT_CURSOR_SPRAY,       XC_spraycan);
103             MAP_CURSOR( GLUT_CURSOR_WAIT,        XC_watch);
104             MAP_CURSOR( GLUT_CURSOR_TEXT,        XC_xterm);
105             MAP_CURSOR( GLUT_CURSOR_CROSSHAIR,   XC_crosshair);
106             MAP_CURSOR( GLUT_CURSOR_UP_DOWN,     XC_sb_v_double_arrow);
107             MAP_CURSOR( GLUT_CURSOR_LEFT_RIGHT,  XC_sb_h_double_arrow);
108             MAP_CURSOR( GLUT_CURSOR_TOP_SIDE,    XC_top_side);
109             MAP_CURSOR( GLUT_CURSOR_BOTTOM_SIDE, XC_bottom_side);
110             MAP_CURSOR( GLUT_CURSOR_LEFT_SIDE,   XC_left_side);
111             MAP_CURSOR( GLUT_CURSOR_RIGHT_SIDE,  XC_right_side);
112             MAP_CURSOR( GLUT_CURSOR_TOP_LEFT_CORNER,     XC_top_left_corner);
113             MAP_CURSOR( GLUT_CURSOR_TOP_RIGHT_CORNER,    XC_top_right_corner);
114             MAP_CURSOR( GLUT_CURSOR_BOTTOM_RIGHT_CORNER, XC_bottom_right_corner);
115             MAP_CURSOR( GLUT_CURSOR_BOTTOM_LEFT_CORNER, XC_bottom_left_corner);
116             MAP_CURSOR( GLUT_CURSOR_NONE,        XC_bogosity);
117         case GLUT_CURSOR_INHERIT:
118             break;
119         default:
120             return;
121         }
122         
123         /*
124          * Define a window's cursor now
125          */
126         if( GLUT_CURSOR_INHERIT == cursorID )
127             XUndefineCursor( fgDisplay.Display, fgStructure.Window->Window.Handle );
128         else
129             XDefineCursor( fgDisplay.Display, fgStructure.Window->Window.Handle, cursor );
130     }
131
132 #elif TARGET_HOST_WIN32
133         /*
134          * This is a temporary solution only...
135          */
136         /* Set the cursor AND change it for this window class. */
137 #       define MAP_CURSOR(a,b) case a: SetCursor( LoadCursor( NULL, b ) ); \
138         SetClassLong(fgStructure.Window->Window.Handle,GCL_HCURSOR,(LONG)LoadCursor(NULL,b)); \
139         break;
140         /* Nuke the cursor AND change it for this window class. */
141 #       define ZAP_CURSOR(a,b) case a: SetCursor( NULL ); \
142         SetClassLong(fgStructure.Window->Window.Handle,GCL_HCURSOR,(LONG)NULL); \
143         break;
144
145         switch( cursorID )
146         {
147                 MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW     );
148                 MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW,  IDC_ARROW     );
149                 MAP_CURSOR( GLUT_CURSOR_INFO,        IDC_HELP      );
150                 MAP_CURSOR( GLUT_CURSOR_DESTROY,     IDC_CROSS     );
151                 MAP_CURSOR( GLUT_CURSOR_HELP,        IDC_HELP      );
152                 MAP_CURSOR( GLUT_CURSOR_CYCLE,       IDC_SIZEALL   );
153                 MAP_CURSOR( GLUT_CURSOR_SPRAY,       IDC_CROSS     );
154                 MAP_CURSOR( GLUT_CURSOR_WAIT,            IDC_WAIT      );
155                 MAP_CURSOR( GLUT_CURSOR_TEXT,        IDC_UPARROW   );
156                 MAP_CURSOR( GLUT_CURSOR_CROSSHAIR,   IDC_CROSS     );
157                 /* MAP_CURSOR( GLUT_CURSOR_NONE,        IDC_NO             ); */
158                 ZAP_CURSOR( GLUT_CURSOR_NONE,        NULL          );
159
160                 default:
161                 MAP_CURSOR( GLUT_CURSOR_UP_DOWN,     IDC_ARROW     );
162         }
163
164 #endif
165
166     /*
167      * Remember the currently selected cursor
168      */
169     fgStructure.Window->State.Cursor = cursorID;
170 }
171
172 /*
173  * Moves the mouse pointer to given window coordinates
174  */
175 void FGAPIENTRY glutWarpPointer( int x, int y )
176 {
177     freeglut_assert_ready; freeglut_assert_window;
178
179 #if TARGET_HOST_UNIX_X11
180     /*
181      * Move the mouse pointer to given window coordinates
182      */
183     XWarpPointer(
184         fgDisplay.Display,
185         None,
186         fgStructure.Window->Window.Handle,
187         0, 0, 0, 0,
188         x, y
189     );
190
191     XFlush( fgDisplay.Display );
192
193 #elif TARGET_HOST_WIN32
194     {
195         POINT coords = { x, y };
196
197         /*
198          * First of all, we need to find the new screen-relative coordinates of the mouse cursor
199          */
200         ClientToScreen( fgStructure.Window->Window.Handle, &coords );
201
202         /*
203          * Now set the new mouse cursor position...
204          */
205         SetCursorPos( coords.x, coords.y );
206     }
207
208 #endif
209 }
210
211 /*** END OF FILE ***/