1 /* gltrackball, Copyright (c) 2002-2014 Jamie Zawinski <jwz@jwz.org>
2 * GL-flavored wrapper for trackball.c
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
23 #else /* !HAVE_COCOA -- real X11 */
25 # include <X11/Xlib.h>
27 #endif /* !HAVE_COCOA */
31 #endif /* HAVE_JWZGLES */
33 # define Button4 4 /* WTF */
38 #include "trackball.h"
39 #include "gltrackball.h"
41 /* Bah, copied from ../fps.h */
43 extern double current_device_rotation (void);
45 # define current_device_rotation() (0)
49 struct trackball_state {
52 double dx, dy, ddx, ddy;
55 int ignore_device_rotation_p;
58 /* Returns a trackball_state object, which encapsulates the stuff necessary
59 to make dragging the mouse on the window of a GL program do the right thing.
62 gltrackball_init (int ignore_device_rotation_p)
64 trackball_state *ts = (trackball_state *) calloc (1, sizeof (*ts));
66 ts->ignore_device_rotation_p = ignore_device_rotation_p;
67 trackball (ts->q, 0, 0, 0, 0);
71 /* Reset the trackball to the default unrotated state.
74 gltrackball_reset (trackball_state *ts)
76 memset (ts, 0, sizeof(*ts));
77 trackball (ts->q, 0, 0, 0, 0);
81 /* Device rotation interacts very strangely with mouse positions.
82 I'm not entirely sure this is the right fix.
85 adjust_for_device_rotation (trackball_state *ts,
86 double *x, double *y, double *w, double *h)
88 int rot = (int) current_device_rotation();
91 if (ts->ignore_device_rotation_p) return;
93 while (rot <= -180) rot += 360;
94 while (rot > 180) rot -= 360;
96 if (rot > 135 || rot < -135) /* 180 */
101 else if (rot > 45) /* 90 */
103 swap = *x; *x = *y; *y = swap;
104 swap = *w; *w = *h; *h = swap;
107 else if (rot < -45) /* 270 */
109 swap = *x; *x = *y; *y = swap;
110 swap = *w; *w = *h; *h = swap;
116 /* Begin tracking the mouse: Call this when the mouse button goes down.
117 x and y are the mouse position relative to the window.
118 w and h are the size of the window.
121 gltrackball_start (trackball_state *ts, int x, int y, int w, int h)
125 ts->button_down_p = 1;
126 ts->dx = ts->ddx = 0;
127 ts->dy = ts->ddy = 0;
130 /* Stop tracking the mouse: Call this when the mouse button goes up.
133 gltrackball_stop (trackball_state *ts)
135 ts->button_down_p = 0;
139 gltrackball_track_1 (trackball_state *ts,
145 double W = w, W2 = w;
146 double H = h, H2 = h;
154 adjust_for_device_rotation (ts, &ox, &oy, &W, &H);
155 adjust_for_device_rotation (ts, &X, &Y, &W2, &H2);
163 add_quats (q2, ts->q, ts->q);
167 /* Track the mouse: Call this each time the mouse moves with the button down.
168 x and y are the new mouse position relative to the window.
169 w and h are the size of the window.
172 gltrackball_track (trackball_state *ts, int x, int y, int w, int h)
174 double dampen = 0.01; /* This keeps it going for about 3 sec */
177 ts->ddx = ts->dx * dampen;
178 ts->ddy = ts->dy * dampen;
181 gltrackball_track_1 (ts, x, y, w, h);
186 gltrackball_dampen (double *n, double *dn)
195 /* Execute the rotations current encapsulated in the trackball_state:
196 this does something analagous to glRotatef().
199 gltrackball_rotate (trackball_state *ts)
202 if (!ts->button_down_p &&
206 /* Apply inertia: keep moving in the same direction as the last move. */
207 gltrackball_track_1 (ts,
212 /* Dampen inertia: gradually stop spinning. */
213 gltrackball_dampen (&ts->dx, &ts->ddx);
214 gltrackball_dampen (&ts->dy, &ts->ddy);
217 build_rotmatrix (m, ts->q);
218 glMultMatrixf (&m[0][0]);
222 /* Call this when a mouse-wheel click is detected.
223 Clicks act like horizontal or vertical drags.
224 Percent is the length of the drag as a percentage of the screen size.
225 Button is 'Button4' or 'Button5' (for the vertical wheel)
226 or 'Button5' or 'Button6' (for the horizontal wheel).
227 If `flip_p' is true, swap the horizontal and vertical axes.
230 gltrackball_mousewheel (trackball_state *ts,
231 int button, int percent, int flip_p)
235 int mx, my, move, scale;
238 flip_p = 0; /* MacOS has already handled this. */
242 case Button4: up_p = 1; horizontal_p = 0; break;
243 case Button5: up_p = 0; horizontal_p = 0; break;
244 case Button6: up_p = 1; horizontal_p = 1; break;
245 case Button7: up_p = 0; horizontal_p = 1; break;
246 default: abort(); break;
251 horizontal_p = !horizontal_p;
255 scale = mx = my = 1000;
257 ? floor (scale * (1.0 - (percent / 100.0)))
258 : ceil (scale * (1.0 + (percent / 100.0))));
259 if (horizontal_p) mx = move;
261 gltrackball_start (ts, scale, scale, scale*2, scale*2);
262 gltrackball_track (ts, mx, my, scale*2, scale*2);
266 gltrackball_get_quaternion (trackball_state *ts, float q[4])
274 /* A utility function for event-handler functions:
275 Handles the various motion and click events related to trackballs.
276 Returns True if the event was handled.
279 gltrackball_event_handler (XEvent *event,
281 int window_width, int window_height,
284 if (event->xany.type == ButtonPress &&
285 event->xbutton.button == Button1)
287 *button_down_p = True;
288 gltrackball_start (ts,
289 event->xbutton.x, event->xbutton.y,
290 window_width, window_height);
293 else if (event->xany.type == ButtonRelease &&
294 event->xbutton.button == Button1)
296 *button_down_p = False;
297 gltrackball_stop (ts);
300 else if (event->xany.type == ButtonPress &&
301 (event->xbutton.button == Button4 ||
302 event->xbutton.button == Button5 ||
303 event->xbutton.button == Button6 ||
304 event->xbutton.button == Button7))
306 gltrackball_mousewheel (ts, event->xbutton.button, 10,
307 !!event->xbutton.state);
310 else if (event->xany.type == MotionNotify &&
313 gltrackball_track (ts,
314 event->xmotion.x, event->xmotion.y,
315 window_width, window_height);