src/platform/sdl/sdl-events.c (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "sdl-events.h"
7
8#include "debugger/debugger.h"
9#include "gba/io.h"
10#include "gba/supervisor/rr.h"
11#include "gba/serialize.h"
12#include "gba/video.h"
13#include "gba/renderers/video-software.h"
14#include "util/configuration.h"
15#include "util/vfs.h"
16
17#if SDL_VERSION_ATLEAST(2, 0, 0) && defined(__APPLE__)
18#define GUI_MOD KMOD_GUI
19#else
20#define GUI_MOD KMOD_CTRL
21#endif
22
23#define GYRO_STEPS 100
24
25static void _GBASDLSetRumble(struct GBARumble* rumble, int enable);
26static int32_t _GBASDLReadTiltX(struct GBARotationSource* rumble);
27static int32_t _GBASDLReadTiltY(struct GBARotationSource* rumble);
28static int32_t _GBASDLReadGyroZ(struct GBARotationSource* rumble);
29static void _GBASDLRotationSample(struct GBARotationSource* source);
30
31bool GBASDLInitEvents(struct GBASDLEvents* context) {
32 int subsystem = SDL_INIT_JOYSTICK;
33#if SDL_VERSION_ATLEAST(2, 0, 0)
34 subsystem |= SDL_INIT_HAPTIC;
35#endif
36 if (SDL_InitSubSystem(subsystem) < 0) {
37 return false;
38 }
39
40 SDL_JoystickEventState(SDL_ENABLE);
41 int nJoysticks = SDL_NumJoysticks();
42 if (nJoysticks > 0) {
43 context->nJoysticks = nJoysticks;
44 context->joysticks = calloc(context->nJoysticks, sizeof(SDL_Joystick*));
45#if SDL_VERSION_ATLEAST(2, 0, 0)
46 context->haptic = calloc(context->nJoysticks, sizeof(SDL_Haptic*));
47#endif
48 size_t i;
49 for (i = 0; i < context->nJoysticks; ++i) {
50 context->joysticks[i] = SDL_JoystickOpen(i);
51#if SDL_VERSION_ATLEAST(2, 0, 0)
52 context->haptic[i] = SDL_HapticOpenFromJoystick(context->joysticks[i]);
53#endif
54 }
55 } else {
56 context->nJoysticks = 0;
57 context->joysticks = 0;
58 }
59
60 context->playersAttached = 0;
61
62 size_t i;
63 for (i = 0; i < MAX_PLAYERS; ++i) {
64 context->preferredJoysticks[i] = 0;
65 context->joysticksClaimed[i] = SIZE_MAX;
66 }
67
68#if !SDL_VERSION_ATLEAST(2, 0, 0)
69 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
70#endif
71 return true;
72}
73
74void GBASDLDeinitEvents(struct GBASDLEvents* context) {
75 size_t i;
76 for (i = 0; i < context->nJoysticks; ++i) {
77#if SDL_VERSION_ATLEAST(2, 0, 0)
78 SDL_HapticClose(context->haptic[i]);
79#endif
80 SDL_JoystickClose(context->joysticks[i]);
81 }
82
83 SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
84}
85
86void GBASDLEventsLoadConfig(struct GBASDLEvents* context, const struct Configuration* config) {
87 context->preferredJoysticks[0] = GBAInputGetPreferredDevice(config, SDL_BINDING_BUTTON, 0);
88 context->preferredJoysticks[1] = GBAInputGetPreferredDevice(config, SDL_BINDING_BUTTON, 1);
89 context->preferredJoysticks[2] = GBAInputGetPreferredDevice(config, SDL_BINDING_BUTTON, 2);
90 context->preferredJoysticks[3] = GBAInputGetPreferredDevice(config, SDL_BINDING_BUTTON, 3);
91}
92
93void GBASDLInitBindings(struct GBAInputMap* inputMap) {
94#ifdef BUILD_PANDORA
95 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_PAGEDOWN, GBA_KEY_A);
96 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_END, GBA_KEY_B);
97 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RSHIFT, GBA_KEY_L);
98 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RCTRL, GBA_KEY_R);
99 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LALT, GBA_KEY_START);
100 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LCTRL, GBA_KEY_SELECT);
101 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
102 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
103 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
104 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
105#elif SDL_VERSION_ATLEAST(2, 0, 0)
106 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_X, GBA_KEY_A);
107 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_Z, GBA_KEY_B);
108 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_A, GBA_KEY_L);
109 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_S, GBA_KEY_R);
110 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RETURN, GBA_KEY_START);
111 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_BACKSPACE, GBA_KEY_SELECT);
112 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_UP, GBA_KEY_UP);
113 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_DOWN, GBA_KEY_DOWN);
114 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_LEFT, GBA_KEY_LEFT);
115 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDL_SCANCODE_RIGHT, GBA_KEY_RIGHT);
116#else
117 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_x, GBA_KEY_A);
118 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_z, GBA_KEY_B);
119 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_a, GBA_KEY_L);
120 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_s, GBA_KEY_R);
121 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RETURN, GBA_KEY_START);
122 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_BACKSPACE, GBA_KEY_SELECT);
123 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_UP, GBA_KEY_UP);
124 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_DOWN, GBA_KEY_DOWN);
125 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_LEFT, GBA_KEY_LEFT);
126 GBAInputBindKey(inputMap, SDL_BINDING_KEY, SDLK_RIGHT, GBA_KEY_RIGHT);
127#endif
128
129 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 13, GBA_KEY_A);
130 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 14, GBA_KEY_B);
131 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 10, GBA_KEY_L);
132 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 11, GBA_KEY_R);
133 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 3, GBA_KEY_START);
134 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 0, GBA_KEY_SELECT);
135 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 4, GBA_KEY_UP);
136 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 6, GBA_KEY_DOWN);
137 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 7, GBA_KEY_LEFT);
138 GBAInputBindKey(inputMap, SDL_BINDING_BUTTON, 5, GBA_KEY_RIGHT);
139
140 struct GBAAxis description = { GBA_KEY_RIGHT, GBA_KEY_LEFT, 0x4000, -0x4000 };
141 GBAInputBindAxis(inputMap, SDL_BINDING_BUTTON, 0, &description);
142 description = (struct GBAAxis) { GBA_KEY_DOWN, GBA_KEY_UP, 0x4000, -0x4000 };
143 GBAInputBindAxis(inputMap, SDL_BINDING_BUTTON, 1, &description);
144}
145
146bool GBASDLAttachPlayer(struct GBASDLEvents* events, struct GBASDLPlayer* player) {
147 player->joystick = 0;
148 player->joystickIndex = SIZE_MAX;
149
150#if SDL_VERSION_ATLEAST(2, 0, 0)
151 player->rumble.d.setRumble = _GBASDLSetRumble;
152 player->rumble.p = player;
153#endif
154
155 player->rotation.d.readTiltX = _GBASDLReadTiltX;
156 player->rotation.d.readTiltY = _GBASDLReadTiltY;
157 player->rotation.d.readGyroZ = _GBASDLReadGyroZ;
158 player->rotation.d.sample = _GBASDLRotationSample;
159 player->rotation.axisX = 2;
160 player->rotation.axisY = 3;
161 player->rotation.gyroSensitivity = 2.2e9f;
162 player->rotation.gyroX = 0;
163 player->rotation.gyroY = 1;
164 player->rotation.zDelta = 0;
165 CircleBufferInit(&player->rotation.zHistory, sizeof(float) * GYRO_STEPS);
166 player->rotation.p = player;
167
168 if (events->playersAttached >= MAX_PLAYERS) {
169 return false;
170 }
171
172 player->playerId = events->playersAttached;
173 size_t firstUnclaimed = SIZE_MAX;
174
175 size_t i;
176 for (i = 0; i < events->nJoysticks; ++i) {
177 bool claimed = false;
178
179 int p;
180 for (p = 0; p < events->playersAttached; ++p) {
181 if (events->joysticksClaimed[p] == i) {
182 claimed = true;
183 break;
184 }
185 }
186 if (claimed) {
187 continue;
188 }
189
190 if (firstUnclaimed == SIZE_MAX) {
191 firstUnclaimed = i;
192 }
193
194 const char* joystickName;
195#if SDL_VERSION_ATLEAST(2, 0, 0)
196 joystickName = SDL_JoystickName(events->joysticks[i]);
197#else
198 joystickName = SDL_JoystickName(SDL_JoystickIndex(events->joysticks[i]));
199#endif
200 if (events->preferredJoysticks[player->playerId] && strcmp(events->preferredJoysticks[player->playerId], joystickName) == 0) {
201 player->joystickIndex = i;
202 break;
203 }
204 }
205
206 if (player->joystickIndex == SIZE_MAX && firstUnclaimed != SIZE_MAX) {
207 player->joystickIndex = firstUnclaimed;
208 }
209
210 if (player->joystickIndex != SIZE_MAX) {
211 player->joystick = events->joysticks[player->joystickIndex];
212 events->joysticksClaimed[player->playerId] = player->joystickIndex;
213
214#if SDL_VERSION_ATLEAST(2, 0, 0)
215 player->haptic = events->haptic[player->joystickIndex];
216 if (player->haptic) {
217 SDL_HapticRumbleInit(player->haptic);
218 }
219#endif
220 }
221
222 ++events->playersAttached;
223 return true;
224}
225
226void GBASDLDetachPlayer(struct GBASDLEvents* events, struct GBASDLPlayer* player) {
227 events->joysticksClaimed[player->playerId] = SIZE_MAX;
228 CircleBufferDeinit(&player->rotation.zHistory);
229}
230
231void GBASDLPlayerLoadConfig(struct GBASDLPlayer* context, const struct Configuration* config) {
232 GBAInputMapLoad(context->bindings, SDL_BINDING_KEY, config);
233 if (context->joystick) {
234 GBAInputMapLoad(context->bindings, SDL_BINDING_BUTTON, config);
235#if SDL_VERSION_ATLEAST(2, 0, 0)
236 GBAInputProfileLoad(context->bindings, SDL_BINDING_BUTTON, config, SDL_JoystickName(context->joystick));
237#else
238 GBAInputProfileLoad(context->bindings, SDL_BINDING_BUTTON, config, SDL_JoystickName(SDL_JoystickIndex(context->joystick)));
239#endif
240 }
241}
242
243void GBASDLPlayerChangeJoystick(struct GBASDLEvents* events, struct GBASDLPlayer* player, size_t index) {
244 if (player->playerId > MAX_PLAYERS || index >= events->nJoysticks) {
245 return;
246 }
247 events->joysticksClaimed[player->playerId] = index;
248 player->joystickIndex = index;
249 player->joystick = events->joysticks[index];
250#if SDL_VERSION_ATLEAST(2, 0, 0)
251 player->haptic = events->haptic[index];
252#endif
253}
254
255static void _pauseAfterFrame(struct GBAThread* context) {
256 context->frameCallback = 0;
257 GBAThreadPauseFromThread(context);
258}
259
260static void _GBASDLHandleKeypress(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const struct SDL_KeyboardEvent* event) {
261 enum GBAKey key = GBA_KEY_NONE;
262 if (!event->keysym.mod) {
263#if !defined(BUILD_PANDORA) && SDL_VERSION_ATLEAST(2, 0, 0)
264 key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.scancode);
265#else
266 key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_KEY, event->keysym.sym);
267#endif
268 }
269 if (key != GBA_KEY_NONE) {
270 if (event->type == SDL_KEYDOWN) {
271 context->activeKeys |= 1 << key;
272 } else {
273 context->activeKeys &= ~(1 << key);
274 }
275 return;
276 }
277 if (event->keysym.sym == SDLK_TAB) {
278 context->sync.audioWait = event->type != SDL_KEYDOWN;
279 return;
280 }
281 if (event->type == SDL_KEYDOWN) {
282 switch (event->keysym.sym) {
283 case SDLK_F11:
284 if (context->debugger) {
285 ARMDebuggerEnter(context->debugger, DEBUGGER_ENTER_MANUAL, 0);
286 }
287 return;
288#ifdef USE_PNG
289 case SDLK_F12:
290 GBAThreadInterrupt(context);
291 GBAThreadTakeScreenshot(context);
292 GBAThreadContinue(context);
293 return;
294#endif
295 case SDLK_BACKSLASH:
296 GBAThreadPause(context);
297 context->frameCallback = _pauseAfterFrame;
298 GBAThreadUnpause(context);
299 return;
300 case SDLK_BACKQUOTE:
301 GBAThreadInterrupt(context);
302 GBARewind(context, 10);
303 GBAThreadContinue(context);
304 return;
305#ifdef BUILD_PANDORA
306 case SDLK_ESCAPE:
307 GBAThreadEnd(context);
308 return;
309#endif
310 default:
311 if ((event->keysym.mod & GUI_MOD) && (event->keysym.mod & GUI_MOD) == event->keysym.mod) {
312 switch (event->keysym.sym) {
313#if SDL_VERSION_ATLEAST(2, 0, 0)
314 case SDLK_f:
315 SDL_SetWindowFullscreen(sdlContext->window, sdlContext->fullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
316 sdlContext->fullscreen = !sdlContext->fullscreen;
317 sdlContext->windowUpdated = 1;
318 break;
319#endif
320 case SDLK_p:
321 GBAThreadTogglePause(context);
322 break;
323 case SDLK_n:
324 GBAThreadPause(context);
325 context->frameCallback = _pauseAfterFrame;
326 GBAThreadUnpause(context);
327 break;
328 case SDLK_r:
329 GBAThreadReset(context);
330 break;
331 default:
332 break;
333 }
334 }
335 if (event->keysym.mod & KMOD_SHIFT) {
336 switch (event->keysym.sym) {
337 case SDLK_F1:
338 case SDLK_F2:
339 case SDLK_F3:
340 case SDLK_F4:
341 case SDLK_F5:
342 case SDLK_F6:
343 case SDLK_F7:
344 case SDLK_F8:
345 case SDLK_F9:
346 GBAThreadInterrupt(context);
347 GBASaveState(context, context->stateDir, event->keysym.sym - SDLK_F1 + 1, true);
348 GBAThreadContinue(context);
349 break;
350 default:
351 break;
352 }
353 } else {
354 switch (event->keysym.sym) {
355 case SDLK_F1:
356 case SDLK_F2:
357 case SDLK_F3:
358 case SDLK_F4:
359 case SDLK_F5:
360 case SDLK_F6:
361 case SDLK_F7:
362 case SDLK_F8:
363 case SDLK_F9:
364 GBAThreadInterrupt(context);
365 GBALoadState(context, context->stateDir, event->keysym.sym - SDLK_F1 + 1);
366 GBAThreadContinue(context);
367 break;
368 default:
369 break;
370 }
371 }
372 return;
373 }
374 }
375}
376
377static void _GBASDLHandleJoyButton(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const struct SDL_JoyButtonEvent* event) {
378 enum GBAKey key = 0;
379 key = GBAInputMapKey(sdlContext->bindings, SDL_BINDING_BUTTON, event->button);
380 if (key == GBA_KEY_NONE) {
381 return;
382 }
383
384 if (event->type == SDL_JOYBUTTONDOWN) {
385 context->activeKeys |= 1 << key;
386 } else {
387 context->activeKeys &= ~(1 << key);
388 }
389}
390
391static void _GBASDLHandleJoyHat(struct GBAThread* context, const struct SDL_JoyHatEvent* event) {
392 enum GBAKey key = 0;
393
394 if (event->value & SDL_HAT_UP) {
395 key |= 1 << GBA_KEY_UP;
396 }
397 if (event->value & SDL_HAT_LEFT) {
398 key |= 1 << GBA_KEY_LEFT;
399 }
400 if (event->value & SDL_HAT_DOWN) {
401 key |= 1 << GBA_KEY_DOWN;
402 }
403 if (event->value & SDL_HAT_RIGHT) {
404 key |= 1 << GBA_KEY_RIGHT;
405 }
406
407 context->activeKeys &= ~((1 << GBA_KEY_UP) | (1 << GBA_KEY_LEFT) | (1 << GBA_KEY_DOWN) | (1 << GBA_KEY_RIGHT));
408 context->activeKeys |= key;
409}
410
411static void _GBASDLHandleJoyAxis(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const struct SDL_JoyAxisEvent* event) {
412 int keys = context->activeKeys;
413
414 keys = GBAInputClearAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, keys);
415 enum GBAKey key = GBAInputMapAxis(sdlContext->bindings, SDL_BINDING_BUTTON, event->axis, event->value);
416 if (key != GBA_KEY_NONE) {
417 keys |= 1 << key;
418 }
419
420 context->activeKeys = keys;
421}
422
423#if SDL_VERSION_ATLEAST(2, 0, 0)
424static void _GBASDLHandleWindowEvent(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const struct SDL_WindowEvent* event) {
425 UNUSED(context);
426 switch (event->event) {
427 case SDL_WINDOWEVENT_SIZE_CHANGED:
428 sdlContext->windowUpdated = 1;
429 break;
430 }
431}
432#endif
433
434void GBASDLHandleEvent(struct GBAThread* context, struct GBASDLPlayer* sdlContext, const union SDL_Event* event) {
435 switch (event->type) {
436 case SDL_QUIT:
437 GBAThreadEnd(context);
438 break;
439#if SDL_VERSION_ATLEAST(2, 0, 0)
440 case SDL_WINDOWEVENT:
441 _GBASDLHandleWindowEvent(context, sdlContext, &event->window);
442 break;
443#endif
444 case SDL_KEYDOWN:
445 case SDL_KEYUP:
446 _GBASDLHandleKeypress(context, sdlContext, &event->key);
447 break;
448 case SDL_JOYBUTTONDOWN:
449 case SDL_JOYBUTTONUP:
450 _GBASDLHandleJoyButton(context, sdlContext, &event->jbutton);
451 break;
452 case SDL_JOYHATMOTION:
453 _GBASDLHandleJoyHat(context, &event->jhat);
454 break;
455 case SDL_JOYAXISMOTION:
456 _GBASDLHandleJoyAxis(context, sdlContext, &event->jaxis);
457 break;
458 }
459}
460
461#if SDL_VERSION_ATLEAST(2, 0, 0)
462static void _GBASDLSetRumble(struct GBARumble* rumble, int enable) {
463 struct GBASDLRumble* sdlRumble = (struct GBASDLRumble*) rumble;
464 if (!sdlRumble->p->haptic || !SDL_HapticRumbleSupported(sdlRumble->p->haptic)) {
465 return;
466 }
467 if (enable) {
468 SDL_HapticRumblePlay(sdlRumble->p->haptic, 1.0f, 20);
469 } else {
470 SDL_HapticRumbleStop(sdlRumble->p->haptic);
471 }
472}
473#endif
474
475static int32_t _readTilt(struct GBASDLPlayer* player, int axis) {
476 return SDL_JoystickGetAxis(player->joystick, axis) * 0x3800;
477}
478
479static int32_t _GBASDLReadTiltX(struct GBARotationSource* source) {
480 struct GBASDLRotation* rotation = (struct GBASDLRotation*) source;
481 return _readTilt(rotation->p, rotation->axisX);
482}
483
484static int32_t _GBASDLReadTiltY(struct GBARotationSource* source) {
485 struct GBASDLRotation* rotation = (struct GBASDLRotation*) source;
486 return _readTilt(rotation->p, rotation->axisY);
487}
488
489static int32_t _GBASDLReadGyroZ(struct GBARotationSource* source) {
490 struct GBASDLRotation* rotation = (struct GBASDLRotation*) source;
491 float z = rotation->zDelta;
492 return z * rotation->gyroSensitivity;
493}
494
495static void _GBASDLRotationSample(struct GBARotationSource* source) {
496 struct GBASDLRotation* rotation = (struct GBASDLRotation*) source;
497 SDL_JoystickUpdate();
498
499 int x = SDL_JoystickGetAxis(rotation->p->joystick, rotation->gyroX);
500 int y = SDL_JoystickGetAxis(rotation->p->joystick, rotation->gyroY);
501 union {
502 float f;
503 int32_t i;
504 } theta = { .f = atan2f(y, x) - atan2f(rotation->oldY, rotation->oldX) };
505 if (isnan(theta.f)) {
506 theta.f = 0.0f;
507 } else if (theta.f > M_PI) {
508 theta.f -= 2.0f * M_PI;
509 } else if (theta.f < -M_PI) {
510 theta.f += 2.0f * M_PI;
511 }
512 rotation->oldX = x;
513 rotation->oldY = y;
514
515 float oldZ = 0;
516 if (CircleBufferSize(&rotation->zHistory) == GYRO_STEPS * sizeof(float)) {
517 CircleBufferRead32(&rotation->zHistory, (int32_t*) &oldZ);
518 }
519 CircleBufferWrite32(&rotation->zHistory, theta.i);
520 rotation->zDelta += theta.f - oldZ;
521}