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 #elif defined(HAVE_ANDROID)
28 # include <X11/Xlib.h>
30 #endif /* !HAVE_COCOA */
34 #endif /* HAVE_JWZGLES */
36 # define Button4 4 /* WTF */
41 #include "trackball.h"
42 #include "gltrackball.h"
44 /* Bah, copied from ../fps.h */
46 extern double current_device_rotation (void);
48 # define current_device_rotation() (0)
52 struct trackball_state {
55 double dx, dy, ddx, ddy;
58 int ignore_device_rotation_p;
61 /* Returns a trackball_state object, which encapsulates the stuff necessary
62 to make dragging the mouse on the window of a GL program do the right thing.
65 gltrackball_init (int ignore_device_rotation_p)
67 trackball_state *ts = (trackball_state *) calloc (1, sizeof (*ts));
69 ts->ignore_device_rotation_p = ignore_device_rotation_p;
70 trackball (ts->q, 0, 0, 0, 0);
74 /* Reset the trackball to the default unrotated state.
77 gltrackball_reset (trackball_state *ts)
79 int bd = ts->button_down_p;
80 int ig = ts->ignore_device_rotation_p;
81 memset (ts, 0, sizeof(*ts));
82 ts->button_down_p = bd;
83 ts->ignore_device_rotation_p = ig;
84 trackball (ts->q, 0, 0, 0, 0);
88 /* Device rotation interacts very strangely with mouse positions.
89 I'm not entirely sure this is the right fix.
92 adjust_for_device_rotation (trackball_state *ts,
93 double *x, double *y, double *w, double *h)
95 int rot = (int) current_device_rotation();
98 if (ts->ignore_device_rotation_p) return;
100 while (rot <= -180) rot += 360;
101 while (rot > 180) rot -= 360;
103 if (rot > 135 || rot < -135) /* 180 */
108 else if (rot > 45) /* 90 */
110 swap = *x; *x = *y; *y = swap;
111 swap = *w; *w = *h; *h = swap;
114 else if (rot < -45) /* 270 */
116 swap = *x; *x = *y; *y = swap;
117 swap = *w; *w = *h; *h = swap;
123 /* Begin tracking the mouse: Call this when the mouse button goes down.
124 x and y are the mouse position relative to the window.
125 w and h are the size of the window.
128 gltrackball_start (trackball_state *ts, int x, int y, int w, int h)
132 ts->button_down_p = 1;
133 ts->dx = ts->ddx = 0;
134 ts->dy = ts->ddy = 0;
137 /* Stop tracking the mouse: Call this when the mouse button goes up.
140 gltrackball_stop (trackball_state *ts)
142 ts->button_down_p = 0;
146 gltrackball_track_1 (trackball_state *ts,
152 double W = w, W2 = w;
153 double H = h, H2 = h;
161 adjust_for_device_rotation (ts, &ox, &oy, &W, &H);
162 adjust_for_device_rotation (ts, &X, &Y, &W2, &H2);
170 add_quats (q2, ts->q, ts->q);
174 /* Track the mouse: Call this each time the mouse moves with the button down.
175 x and y are the new mouse position relative to the window.
176 w and h are the size of the window.
179 gltrackball_track (trackball_state *ts, int x, int y, int w, int h)
181 double dampen = 0.01; /* This keeps it going for about 3 sec */
184 ts->ddx = ts->dx * dampen;
185 ts->ddy = ts->dy * dampen;
188 gltrackball_track_1 (ts, x, y, w, h);
193 gltrackball_dampen (double *n, double *dn)
202 /* Execute the rotations current encapsulated in the trackball_state:
203 this does something analagous to glRotatef().
206 gltrackball_rotate (trackball_state *ts)
209 if (!ts->button_down_p &&
213 /* Apply inertia: keep moving in the same direction as the last move. */
214 gltrackball_track_1 (ts,
219 /* Dampen inertia: gradually stop spinning. */
220 gltrackball_dampen (&ts->dx, &ts->ddx);
221 gltrackball_dampen (&ts->dy, &ts->ddy);
224 build_rotmatrix (m, ts->q);
225 glMultMatrixf (&m[0][0]);
229 /* Call this when a mouse-wheel click is detected.
230 Clicks act like horizontal or vertical drags.
231 Percent is the length of the drag as a percentage of the screen size.
232 Button is 'Button4' or 'Button5' (for the vertical wheel)
233 or 'Button5' or 'Button6' (for the horizontal wheel).
234 If `flip_p' is true, swap the horizontal and vertical axes.
237 gltrackball_mousewheel (trackball_state *ts,
238 int button, int percent, int flip_p)
242 int mx, my, move, scale;
245 flip_p = 0; /* MacOS has already handled this. */
249 case Button4: up_p = 1; horizontal_p = 0; break;
250 case Button5: up_p = 0; horizontal_p = 0; break;
251 case Button6: up_p = 1; horizontal_p = 1; break;
252 case Button7: up_p = 0; horizontal_p = 1; break;
253 default: abort(); break;
258 horizontal_p = !horizontal_p;
262 scale = mx = my = 1000;
264 ? floor (scale * (1.0 - (percent / 100.0)))
265 : ceil (scale * (1.0 + (percent / 100.0))));
266 if (horizontal_p) mx = move;
268 gltrackball_start (ts, scale, scale, scale*2, scale*2);
269 gltrackball_track (ts, mx, my, scale*2, scale*2);
273 gltrackball_get_quaternion (trackball_state *ts, float q[4])
281 /* A utility function for event-handler functions:
282 Handles the various motion and click events related to trackballs.
283 Returns True if the event was handled.
286 gltrackball_event_handler (XEvent *event,
288 int window_width, int window_height,
291 if (event->xany.type == ButtonPress &&
292 event->xbutton.button == Button1)
294 *button_down_p = True;
295 gltrackball_start (ts,
296 event->xbutton.x, event->xbutton.y,
297 window_width, window_height);
300 else if (event->xany.type == ButtonRelease &&
301 event->xbutton.button == Button1)
303 *button_down_p = False;
304 gltrackball_stop (ts);
307 else if (event->xany.type == ButtonPress &&
308 (event->xbutton.button == Button4 ||
309 event->xbutton.button == Button5 ||
310 event->xbutton.button == Button6 ||
311 event->xbutton.button == Button7))
313 gltrackball_mousewheel (ts, event->xbutton.button, 10,
314 !!event->xbutton.state);
317 else if (event->xany.type == MotionNotify &&
320 gltrackball_track (ts,
321 event->xmotion.x, event->xmotion.y,
322 window_width, window_height);