usp10: Add Cherokee script.
[wine/multimedia.git] / dlls / dinput / effect_linuxinput.c
blobc224bd8ce24f0f19f4670722074f37e857950e61
1 /* DirectInput Linux Event Device Effect
3 * Copyright 2005 Daniel Remenak
5 * Thanks to Google's Summer of Code Program (2005)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
26 #include <stdarg.h>
27 #include <string.h>
28 #ifdef HAVE_LINUX_INPUT_H
29 # include <linux/input.h>
30 # undef SW_MAX
31 #endif
32 #include <errno.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include <math.h>
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winerror.h"
42 #include "dinput.h"
44 #include "device_private.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
48 static const IDirectInputEffectVtbl LinuxInputEffectVtbl;
49 typedef struct LinuxInputEffectImpl LinuxInputEffectImpl;
50 struct LinuxInputEffectImpl
52 IDirectInputEffect IDirectInputEffect_iface;
53 LONG ref;
54 GUID guid;
56 struct ff_effect effect; /* Effect data */
57 int gain; /* Effect gain */
58 int first_axis_is_x;
59 int* fd; /* Parent device */
60 struct list *entry; /* Entry into the parent's list of effects */
63 static inline LinuxInputEffectImpl *impl_from_IDirectInputEffect(IDirectInputEffect *iface)
65 return CONTAINING_RECORD(iface, LinuxInputEffectImpl, IDirectInputEffect_iface);
68 /******************************************************************************
69 * DirectInputEffect Functional Helper
72 static DWORD _typeFromGUID(REFGUID guid)
74 if (IsEqualGUID(guid, &GUID_ConstantForce)) {
75 return DIEFT_CONSTANTFORCE;
76 } else if (IsEqualGUID(guid, &GUID_Square)
77 || IsEqualGUID(guid, &GUID_Sine)
78 || IsEqualGUID(guid, &GUID_Triangle)
79 || IsEqualGUID(guid, &GUID_SawtoothUp)
80 || IsEqualGUID(guid, &GUID_SawtoothDown)) {
81 return DIEFT_PERIODIC;
82 } else if (IsEqualGUID(guid, &GUID_RampForce)) {
83 return DIEFT_RAMPFORCE;
84 } else if (IsEqualGUID(guid, &GUID_Spring)
85 || IsEqualGUID(guid, &GUID_Damper)
86 || IsEqualGUID(guid, &GUID_Inertia)
87 || IsEqualGUID(guid, &GUID_Friction)) {
88 return DIEFT_CONDITION;
89 } else if (IsEqualGUID(guid, &GUID_CustomForce)) {
90 return DIEFT_CUSTOMFORCE;
91 } else {
92 WARN("GUID (%s) is not a known force type\n", _dump_dinput_GUID(guid));
93 return 0;
98 /******************************************************************************
99 * DirectInputEffect debug helpers
102 static void _dump_DIEFFECT_flags(DWORD dwFlags)
104 if (TRACE_ON(dinput)) {
105 unsigned int i;
106 static const struct {
107 DWORD mask;
108 const char *name;
109 } flags[] = {
110 #define FE(x) { x, #x}
111 FE(DIEFF_CARTESIAN),
112 FE(DIEFF_OBJECTIDS),
113 FE(DIEFF_OBJECTOFFSETS),
114 FE(DIEFF_POLAR),
115 FE(DIEFF_SPHERICAL)
116 #undef FE
118 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
119 if (flags[i].mask & dwFlags)
120 TRACE("%s ", flags[i].name);
121 TRACE("\n");
125 static void _dump_DIENVELOPE(LPCDIENVELOPE env)
127 if (env->dwSize != sizeof(DIENVELOPE)) {
128 WARN("Non-standard DIENVELOPE structure size %d.\n", env->dwSize);
130 TRACE("Envelope has attack (level: %d time: %d), fade (level: %d time: %d)\n",
131 env->dwAttackLevel, env->dwAttackTime, env->dwFadeLevel, env->dwFadeTime);
134 static void _dump_DICONSTANTFORCE(LPCDICONSTANTFORCE frc)
136 TRACE("Constant force has magnitude %d\n", frc->lMagnitude);
139 static void _dump_DIPERIODIC(LPCDIPERIODIC frc)
141 TRACE("Periodic force has magnitude %d, offset %d, phase %d, period %d\n",
142 frc->dwMagnitude, frc->lOffset, frc->dwPhase, frc->dwPeriod);
145 static void _dump_DIRAMPFORCE(LPCDIRAMPFORCE frc)
147 TRACE("Ramp force has start %d, end %d\n",
148 frc->lStart, frc->lEnd);
151 static void _dump_DICONDITION(LPCDICONDITION frc)
153 TRACE("Condition has offset %d, pos/neg coefficients %d and %d, pos/neg saturations %d and %d, deadband %d\n",
154 frc->lOffset, frc->lPositiveCoefficient, frc->lNegativeCoefficient,
155 frc->dwPositiveSaturation, frc->dwNegativeSaturation, frc->lDeadBand);
158 static void _dump_DICUSTOMFORCE(LPCDICUSTOMFORCE frc)
160 unsigned int i;
161 TRACE("Custom force uses %d channels, sample period %d. Has %d samples at %p.\n",
162 frc->cChannels, frc->dwSamplePeriod, frc->cSamples, frc->rglForceData);
163 if (frc->cSamples % frc->cChannels != 0)
164 WARN("Custom force has a non-integral samples-per-channel count!\n");
165 if (TRACE_ON(dinput)) {
166 TRACE("Custom force data (time aligned, axes in order):\n");
167 for (i = 1; i <= frc->cSamples; ++i) {
168 TRACE("%d ", frc->rglForceData[i]);
169 if (i % frc->cChannels == 0)
170 TRACE("\n");
175 static void _dump_DIEFFECT(LPCDIEFFECT eff, REFGUID guid)
177 unsigned int i;
178 DWORD type = _typeFromGUID(guid);
180 TRACE("Dumping DIEFFECT structure:\n");
181 TRACE(" - dwSize: %d\n", eff->dwSize);
182 if ((eff->dwSize != sizeof(DIEFFECT)) && (eff->dwSize != sizeof(DIEFFECT_DX5))) {
183 WARN("Non-standard DIEFFECT structure size %d\n", eff->dwSize);
185 TRACE(" - dwFlags: %d\n", eff->dwFlags);
186 TRACE(" ");
187 _dump_DIEFFECT_flags(eff->dwFlags);
188 TRACE(" - dwDuration: %d\n", eff->dwDuration);
189 TRACE(" - dwGain: %d\n", eff->dwGain);
190 if (eff->dwGain > 10000)
191 WARN("dwGain is out of range (>10,000)\n");
192 TRACE(" - dwTriggerButton: %d\n", eff->dwTriggerButton);
193 TRACE(" - dwTriggerRepeatInterval: %d\n", eff->dwTriggerRepeatInterval);
194 TRACE(" - cAxes: %d\n", eff->cAxes);
195 TRACE(" - rgdwAxes: %p\n", eff->rgdwAxes);
196 if (TRACE_ON(dinput) && eff->rgdwAxes) {
197 TRACE(" ");
198 for (i = 0; i < eff->cAxes; ++i)
199 TRACE("%d ", eff->rgdwAxes[i]);
200 TRACE("\n");
202 TRACE(" - rglDirection: %p\n", eff->rglDirection);
203 TRACE(" - lpEnvelope: %p\n", eff->lpEnvelope);
204 TRACE(" - cbTypeSpecificParams: %d\n", eff->cbTypeSpecificParams);
205 TRACE(" - lpvTypeSpecificParams: %p\n", eff->lpvTypeSpecificParams);
206 if (eff->dwSize > sizeof(DIEFFECT_DX5))
207 TRACE(" - dwStartDelay: %d\n", eff->dwStartDelay);
208 if (eff->lpEnvelope != NULL)
209 _dump_DIENVELOPE(eff->lpEnvelope);
210 if (type == DIEFT_CONSTANTFORCE) {
211 if (eff->cbTypeSpecificParams != sizeof(DICONSTANTFORCE)) {
212 WARN("Effect claims to be a constant force but the type-specific params are the wrong size!\n");
213 } else {
214 _dump_DICONSTANTFORCE(eff->lpvTypeSpecificParams);
216 } else if (type == DIEFT_PERIODIC) {
217 if (eff->cbTypeSpecificParams != sizeof(DIPERIODIC)) {
218 WARN("Effect claims to be a periodic force but the type-specific params are the wrong size!\n");
219 } else {
220 _dump_DIPERIODIC(eff->lpvTypeSpecificParams);
222 } else if (type == DIEFT_RAMPFORCE) {
223 if (eff->cbTypeSpecificParams != sizeof(DIRAMPFORCE)) {
224 WARN("Effect claims to be a ramp force but the type-specific params are the wrong size!\n");
225 } else {
226 _dump_DIRAMPFORCE(eff->lpvTypeSpecificParams);
228 } else if (type == DIEFT_CONDITION) {
229 if (eff->cbTypeSpecificParams != sizeof(DICONDITION)) {
230 WARN("Effect claims to be a condition but the type-specific params are the wrong size!\n");
231 } else {
232 _dump_DICONDITION(eff->lpvTypeSpecificParams);
234 } else if (type == DIEFT_CUSTOMFORCE) {
235 if (eff->cbTypeSpecificParams != sizeof(DICUSTOMFORCE)) {
236 WARN("Effect claims to be a custom force but the type-specific params are the wrong size!\n");
237 } else {
238 _dump_DICUSTOMFORCE(eff->lpvTypeSpecificParams);
244 /******************************************************************************
245 * LinuxInputEffectImpl
248 static ULONG WINAPI LinuxInputEffectImpl_AddRef(
249 LPDIRECTINPUTEFFECT iface)
251 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
252 return InterlockedIncrement(&(This->ref));
255 static HRESULT WINAPI LinuxInputEffectImpl_Download(
256 LPDIRECTINPUTEFFECT iface)
258 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
260 TRACE("(this=%p)\n", This);
262 if (ioctl(*(This->fd), EVIOCSFF, &This->effect) == -1) {
263 if (errno == ENOMEM) {
264 return DIERR_DEVICEFULL;
265 } else {
266 FIXME("Could not upload effect. Assuming a disconnected device %d \"%s\".\n", *This->fd, strerror(errno));
267 return DIERR_INPUTLOST;
271 return DI_OK;
274 static HRESULT WINAPI LinuxInputEffectImpl_Escape(
275 LPDIRECTINPUTEFFECT iface,
276 LPDIEFFESCAPE pesc)
278 WARN("(this=%p,%p): invalid: no hardware-specific escape codes in this"
279 " driver!\n", iface, pesc);
281 return DI_OK;
284 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectGuid(
285 LPDIRECTINPUTEFFECT iface,
286 LPGUID pguid)
288 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
290 TRACE("(this=%p,%p)\n", This, pguid);
292 *pguid = This->guid;
294 return DI_OK;
297 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectStatus(
298 LPDIRECTINPUTEFFECT iface,
299 LPDWORD pdwFlags)
301 TRACE("(this=%p,%p)\n", iface, pdwFlags);
303 /* linux sends the effect status through an event.
304 * that event is trapped by our parent joystick driver
305 * and there is no clean way to pass it back to us. */
306 FIXME("Not enough information to provide a status.\n");
308 (*pdwFlags) = 0;
310 return DI_OK;
313 static HRESULT WINAPI LinuxInputEffectImpl_GetParameters(
314 LPDIRECTINPUTEFFECT iface,
315 LPDIEFFECT peff,
316 DWORD dwFlags)
318 HRESULT diErr = DI_OK;
319 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
320 TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
322 /* Major conversion factors are:
323 * times: millisecond (linux) -> microsecond (windows) (x * 1000)
324 * forces: scale 0x7FFF (linux) -> scale 10000 (windows) approx ((x / 33) * 10)
325 * angles: scale 0x7FFF (linux) -> scale 35999 (windows) approx ((x / 33) * 36)
326 * angle bases: 0 -> -y (down) (linux) -> 0 -> +x (right) (windows)
329 if (dwFlags & DIEP_AXES) {
330 if (peff->cAxes < 2 /* linuxinput effects always use 2 axes, x and y */)
331 diErr = DIERR_MOREDATA;
332 peff->cAxes = 2;
333 if (diErr)
334 return diErr;
335 else {
336 peff->rgdwAxes[0] = DIJOFS_X;
337 peff->rgdwAxes[1] = DIJOFS_Y;
341 if (dwFlags & DIEP_DIRECTION) {
342 if (peff->cAxes < 2)
343 diErr = DIERR_MOREDATA;
344 peff->cAxes = 2;
345 if (diErr)
346 return diErr;
347 else {
348 if (peff->dwFlags & DIEFF_CARTESIAN) {
349 peff->rglDirection[0] = sin(M_PI * 3 * This->effect.direction / 0x7FFF) * 1000;
350 peff->rglDirection[1] = cos(M_PI * 3 * This->effect.direction / 0x7FFF) * 1000;
351 } else {
352 /* Polar and spherical coordinates are the same for two or less
353 * axes.
354 * Note that we also use this case if NO flags are marked.
355 * According to MSDN, we should return the direction in the
356 * format that it was specified in, if no flags are marked.
358 peff->rglDirection[0] = (This->effect.direction / 33) * 36 + 9000;
359 if (peff->rglDirection[0] > 35999)
360 peff->rglDirection[0] -= 35999;
365 if (dwFlags & DIEP_DURATION) {
366 peff->dwDuration = (DWORD)This->effect.replay.length * 1000;
369 if (dwFlags & DIEP_ENVELOPE) {
370 struct ff_envelope* env;
371 if (This->effect.type == FF_CONSTANT) env = &This->effect.u.constant.envelope;
372 else if (This->effect.type == FF_PERIODIC) env = &This->effect.u.periodic.envelope;
373 else if (This->effect.type == FF_RAMP) env = &This->effect.u.ramp.envelope;
374 else env = NULL;
375 if (env == NULL) {
376 peff->lpEnvelope = NULL;
377 } else if (peff->lpEnvelope == NULL) {
378 return DIERR_INVALIDPARAM;
379 } else {
380 peff->lpEnvelope->dwAttackLevel = (env->attack_level / 33) * 10;
381 peff->lpEnvelope->dwAttackTime = env->attack_length * 1000;
382 peff->lpEnvelope->dwFadeLevel = (env->fade_level / 33) * 10;
383 peff->lpEnvelope->dwFadeTime = env->fade_length * 1000;
387 if (dwFlags & DIEP_GAIN) {
388 peff->dwGain = This->gain * 10000 / 0xFFFF;
391 if (dwFlags & DIEP_SAMPLEPERIOD) {
392 /* the linux input ff driver has no support for setting
393 * the playback sample period. 0 means default. */
394 peff->dwSamplePeriod = 0;
397 if (dwFlags & DIEP_STARTDELAY) {
398 peff->dwStartDelay = This->effect.replay.delay * 1000;
401 if (dwFlags & DIEP_TRIGGERBUTTON) {
402 FIXME("LinuxInput button mapping needs redoing; for now, assuming we're using an actual joystick.\n");
403 peff->dwTriggerButton = DIJOFS_BUTTON(This->effect.trigger.button - BTN_JOYSTICK);
406 if (dwFlags & DIEP_TRIGGERREPEATINTERVAL) {
407 peff->dwTriggerRepeatInterval = This->effect.trigger.interval * 1000;
410 if (dwFlags & DIEP_TYPESPECIFICPARAMS) {
411 DWORD expectedsize = 0;
412 if (This->effect.type == FF_PERIODIC) {
413 expectedsize = sizeof(DIPERIODIC);
414 } else if (This->effect.type == FF_CONSTANT) {
415 expectedsize = sizeof(DICONSTANTFORCE);
416 } else if (This->effect.type == FF_SPRING
417 || This->effect.type == FF_FRICTION
418 || This->effect.type == FF_INERTIA
419 || This->effect.type == FF_DAMPER) {
420 expectedsize = sizeof(DICONDITION) * 2;
421 } else if (This->effect.type == FF_RAMP) {
422 expectedsize = sizeof(DIRAMPFORCE);
424 if (expectedsize > peff->cbTypeSpecificParams)
425 diErr = DIERR_MOREDATA;
426 peff->cbTypeSpecificParams = expectedsize;
427 if (diErr)
428 return diErr;
429 else {
430 if (This->effect.type == FF_PERIODIC) {
431 LPDIPERIODIC tsp = peff->lpvTypeSpecificParams;
432 tsp->dwMagnitude = (This->effect.u.periodic.magnitude / 33) * 10;
433 tsp->lOffset = (This->effect.u.periodic.offset / 33) * 10;
434 tsp->dwPhase = (This->effect.u.periodic.phase / 33) * 36;
435 tsp->dwPeriod = (This->effect.u.periodic.period * 1000);
436 } else if (This->effect.type == FF_CONSTANT) {
437 LPDICONSTANTFORCE tsp = peff->lpvTypeSpecificParams;
438 tsp->lMagnitude = (This->effect.u.constant.level / 33) * 10;
439 } else if (This->effect.type == FF_SPRING
440 || This->effect.type == FF_FRICTION
441 || This->effect.type == FF_INERTIA
442 || This->effect.type == FF_DAMPER) {
443 LPDICONDITION tsp = peff->lpvTypeSpecificParams;
444 int i;
445 for (i = 0; i < 2; ++i) {
446 tsp[i].lOffset = (This->effect.u.condition[i].center / 33) * 10;
447 tsp[i].lPositiveCoefficient = (This->effect.u.condition[i].right_coeff / 33) * 10;
448 tsp[i].lNegativeCoefficient = (This->effect.u.condition[i].left_coeff / 33) * 10;
449 tsp[i].dwPositiveSaturation = (This->effect.u.condition[i].right_saturation / 33) * 10;
450 tsp[i].dwNegativeSaturation = (This->effect.u.condition[i].left_saturation / 33) * 10;
451 tsp[i].lDeadBand = (This->effect.u.condition[i].deadband / 33) * 10;
453 } else if (This->effect.type == FF_RAMP) {
454 LPDIRAMPFORCE tsp = peff->lpvTypeSpecificParams;
455 tsp->lStart = (This->effect.u.ramp.start_level / 33) * 10;
456 tsp->lEnd = (This->effect.u.ramp.end_level / 33) * 10;
461 return diErr;
464 static HRESULT WINAPI LinuxInputEffectImpl_Initialize(
465 LPDIRECTINPUTEFFECT iface,
466 HINSTANCE hinst,
467 DWORD dwVersion,
468 REFGUID rguid)
470 FIXME("(this=%p,%p,%d,%s): stub!\n",
471 iface, hinst, dwVersion, debugstr_guid(rguid));
473 return DI_OK;
476 static HRESULT WINAPI LinuxInputEffectImpl_QueryInterface(
477 LPDIRECTINPUTEFFECT iface,
478 REFIID riid,
479 void **ppvObject)
481 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
483 TRACE("(this=%p,%s,%p)\n", This, debugstr_guid(riid), ppvObject);
485 if (IsEqualGUID(&IID_IUnknown, riid) ||
486 IsEqualGUID(&IID_IDirectInputEffect, riid)) {
487 LinuxInputEffectImpl_AddRef(iface);
488 *ppvObject = This;
489 return 0;
492 TRACE("Unsupported interface!\n");
493 return E_FAIL;
496 static HRESULT WINAPI LinuxInputEffectImpl_Start(
497 LPDIRECTINPUTEFFECT iface,
498 DWORD dwIterations,
499 DWORD dwFlags)
501 struct input_event event;
502 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
504 TRACE("(this=%p,%d,%d)\n", This, dwIterations, dwFlags);
506 if (!(dwFlags & DIES_NODOWNLOAD)) {
507 /* Download the effect if necessary */
508 if (This->effect.id == -1) {
509 HRESULT res = LinuxInputEffectImpl_Download(iface);
510 if (res != DI_OK)
511 return res;
515 if (dwFlags & DIES_SOLO) {
516 FIXME("Solo mode requested: should be stopping all effects here!\n");
519 event.type = EV_FF;
520 event.code = This->effect.id;
521 event.value = dwIterations;
522 if (write(*(This->fd), &event, sizeof(event)) == -1) {
523 FIXME("Unable to write event. Assuming device disconnected.\n");
524 return DIERR_INPUTLOST;
527 return DI_OK;
530 static HRESULT WINAPI LinuxInputEffectImpl_SetParameters(
531 LPDIRECTINPUTEFFECT iface,
532 LPCDIEFFECT peff,
533 DWORD dwFlags)
535 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
536 DWORD type = _typeFromGUID(&This->guid);
537 HRESULT retval = DI_OK;
539 TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
541 _dump_DIEFFECT(peff, &This->guid);
543 if ((dwFlags & ~DIEP_NORESTART & ~DIEP_NODOWNLOAD & ~DIEP_START) == 0) {
544 /* set everything */
545 dwFlags = DIEP_AXES | DIEP_DIRECTION | DIEP_DURATION | DIEP_ENVELOPE |
546 DIEP_GAIN | DIEP_SAMPLEPERIOD | DIEP_STARTDELAY | DIEP_TRIGGERBUTTON |
547 DIEP_TRIGGERREPEATINTERVAL | DIEP_TYPESPECIFICPARAMS;
550 if (dwFlags & DIEP_AXES) {
551 /* the linux input effect system only supports one or two axes */
552 if (peff->cAxes > 2)
553 return DIERR_INVALIDPARAM;
554 else if (peff->cAxes < 1)
555 return DIERR_INCOMPLETEEFFECT;
556 This->first_axis_is_x = peff->rgdwAxes[0] == DIJOFS_X;
559 /* some of this may look funky, but it's 'cause the linux driver and directx have
560 * different opinions about which way direction "0" is. directx has 0 along the x
561 * axis (left), linux has it along the y axis (down). */
562 if (dwFlags & DIEP_DIRECTION) {
563 if (peff->cAxes == 1) {
564 if (peff->dwFlags & DIEFF_CARTESIAN) {
565 if (dwFlags & DIEP_AXES) {
566 if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] >= 0)
567 This->effect.direction = 0x4000;
568 else if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] < 0)
569 This->effect.direction = 0xC000;
570 else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] >= 0)
571 This->effect.direction = 0;
572 else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] < 0)
573 This->effect.direction = 0x8000;
575 } else {
576 /* one-axis effects must use cartesian coords */
577 return DIERR_INVALIDPARAM;
579 } else { /* two axes */
580 if (peff->dwFlags & DIEFF_CARTESIAN) {
581 LONG x, y;
582 if (This->first_axis_is_x) {
583 x = peff->rglDirection[0];
584 y = peff->rglDirection[1];
585 } else {
586 x = peff->rglDirection[1];
587 y = peff->rglDirection[0];
589 This->effect.direction = (int)((3 * M_PI / 2 - atan2(y, x)) * -0x7FFF / M_PI);
590 } else {
591 /* Polar and spherical are the same for 2 axes */
592 /* Precision is important here, so we do double math with exact constants */
593 This->effect.direction = (int)(((double)peff->rglDirection[0] - 90) / 35999) * 0x7FFF;
598 if (dwFlags & DIEP_DURATION)
599 This->effect.replay.length = peff->dwDuration / 1000;
601 if (dwFlags & DIEP_ENVELOPE) {
602 struct ff_envelope* env;
603 if (This->effect.type == FF_CONSTANT) env = &This->effect.u.constant.envelope;
604 else if (This->effect.type == FF_PERIODIC) env = &This->effect.u.periodic.envelope;
605 else if (This->effect.type == FF_RAMP) env = &This->effect.u.ramp.envelope;
606 else env = NULL;
608 if (peff->lpEnvelope == NULL) {
609 /* if this type had an envelope, reset it */
610 if (env) {
611 env->attack_length = 0;
612 env->attack_level = 0;
613 env->fade_length = 0;
614 env->fade_level = 0;
616 } else {
617 /* did we get passed an envelope for a type that doesn't even have one? */
618 if (!env) return DIERR_INVALIDPARAM;
619 /* copy the envelope */
620 env->attack_length = peff->lpEnvelope->dwAttackTime / 1000;
621 env->attack_level = (peff->lpEnvelope->dwAttackLevel / 10) * 32;
622 env->fade_length = peff->lpEnvelope->dwFadeTime / 1000;
623 env->fade_level = (peff->lpEnvelope->dwFadeLevel / 10) * 32;
627 /* Gain and Sample Period settings are not supported by the linux
628 * event system */
629 if (dwFlags & DIEP_GAIN) {
630 This->gain = 0xFFFF * peff->dwGain / 10000;
631 TRACE("Effect gain requested but no effect gain functionality present.\n");
634 if (dwFlags & DIEP_SAMPLEPERIOD)
635 TRACE("Sample period requested but no sample period functionality present.\n");
637 if (dwFlags & DIEP_STARTDELAY)
638 This->effect.replay.delay = peff->dwStartDelay / 1000;
640 if (dwFlags & DIEP_TRIGGERBUTTON) {
641 if (peff->dwTriggerButton != -1) {
642 FIXME("Linuxinput button mapping needs redoing, assuming we're using a joystick.\n");
643 FIXME("Trigger button translation not yet implemented!\n");
645 This->effect.trigger.button = 0;
648 if (dwFlags & DIEP_TRIGGERREPEATINTERVAL)
649 This->effect.trigger.interval = peff->dwTriggerRepeatInterval / 1000;
651 if (dwFlags & DIEP_TYPESPECIFICPARAMS) {
652 if (!(peff->lpvTypeSpecificParams))
653 return DIERR_INCOMPLETEEFFECT;
654 if (type == DIEFT_PERIODIC) {
655 LPCDIPERIODIC tsp;
656 if (peff->cbTypeSpecificParams != sizeof(DIPERIODIC))
657 return DIERR_INVALIDPARAM;
658 tsp = peff->lpvTypeSpecificParams;
659 This->effect.u.periodic.magnitude = (tsp->dwMagnitude / 10) * 32;
660 This->effect.u.periodic.offset = (tsp->lOffset / 10) * 32;
661 This->effect.u.periodic.phase = (tsp->dwPhase / 9) * 8; /* == (/ 36 * 32) */
662 This->effect.u.periodic.period = tsp->dwPeriod / 1000;
663 } else if (type == DIEFT_CONSTANTFORCE) {
664 LPCDICONSTANTFORCE tsp;
665 if (peff->cbTypeSpecificParams != sizeof(DICONSTANTFORCE))
666 return DIERR_INVALIDPARAM;
667 tsp = peff->lpvTypeSpecificParams;
668 This->effect.u.constant.level = (max(min(tsp->lMagnitude, 10000), -10000) / 10) * 32;
669 } else if (type == DIEFT_RAMPFORCE) {
670 LPCDIRAMPFORCE tsp;
671 if (peff->cbTypeSpecificParams != sizeof(DIRAMPFORCE))
672 return DIERR_INVALIDPARAM;
673 tsp = peff->lpvTypeSpecificParams;
674 This->effect.u.ramp.start_level = (tsp->lStart / 10) * 32;
675 This->effect.u.ramp.end_level = (tsp->lEnd / 10) * 32;
676 } else if (type == DIEFT_CONDITION) {
677 LPCDICONDITION tsp = peff->lpvTypeSpecificParams;
678 if (peff->cbTypeSpecificParams == sizeof(DICONDITION)) {
679 /* One condition block. This needs to be rotated to direction,
680 * and expanded to separate x and y conditions. */
681 int i;
682 double factor[2];
683 factor[0] = asin((This->effect.direction * 3.0 * M_PI) / 0x7FFF);
684 factor[1] = acos((This->effect.direction * 3.0 * M_PI) / 0x7FFF);
685 for (i = 0; i < 2; ++i) {
686 This->effect.u.condition[i].center = (int)(factor[i] * (tsp->lOffset / 10) * 32);
687 This->effect.u.condition[i].right_coeff = (int)(factor[i] * (tsp->lPositiveCoefficient / 10) * 32);
688 This->effect.u.condition[i].left_coeff = (int)(factor[i] * (tsp->lNegativeCoefficient / 10) * 32);
689 This->effect.u.condition[i].right_saturation = (int)(factor[i] * (tsp->dwPositiveSaturation / 10) * 32);
690 This->effect.u.condition[i].left_saturation = (int)(factor[i] * (tsp->dwNegativeSaturation / 10) * 32);
691 This->effect.u.condition[i].deadband = (int)(factor[i] * (tsp->lDeadBand / 10) * 32);
693 } else if (peff->cbTypeSpecificParams == 2 * sizeof(DICONDITION)) {
694 /* Two condition blocks. Direct parameter copy. */
695 int i;
696 for (i = 0; i < 2; ++i) {
697 This->effect.u.condition[i].center = (tsp[i].lOffset / 10) * 32;
698 This->effect.u.condition[i].right_coeff = (tsp[i].lPositiveCoefficient / 10) * 32;
699 This->effect.u.condition[i].left_coeff = (tsp[i].lNegativeCoefficient / 10) * 32;
700 This->effect.u.condition[i].right_saturation = (tsp[i].dwPositiveSaturation / 10) * 32;
701 This->effect.u.condition[i].left_saturation = (tsp[i].dwNegativeSaturation / 10) * 32;
702 This->effect.u.condition[i].deadband = (tsp[i].lDeadBand / 10) * 32;
704 } else {
705 return DIERR_INVALIDPARAM;
707 } else {
708 FIXME("Custom force types are not supported\n");
709 return DIERR_INVALIDPARAM;
713 if (!(dwFlags & DIEP_NODOWNLOAD))
714 retval = LinuxInputEffectImpl_Download(iface);
715 if (retval != DI_OK)
716 return DI_DOWNLOADSKIPPED;
718 if (dwFlags & DIEP_NORESTART)
719 TRACE("DIEP_NORESTART: not handled (we have no control of that).\n");
721 if (dwFlags & DIEP_START)
722 retval = LinuxInputEffectImpl_Start(iface, 1, 0);
723 if (retval != DI_OK)
724 return retval;
726 return DI_OK;
729 static HRESULT WINAPI LinuxInputEffectImpl_Stop(
730 LPDIRECTINPUTEFFECT iface)
732 struct input_event event;
733 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
735 TRACE("(this=%p)\n", This);
737 event.type = EV_FF;
738 event.code = This->effect.id;
739 event.value = 0;
740 /* we don't care about the success or failure of this call */
741 write(*(This->fd), &event, sizeof(event));
743 return DI_OK;
746 static HRESULT WINAPI LinuxInputEffectImpl_Unload(
747 LPDIRECTINPUTEFFECT iface)
749 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
750 TRACE("(this=%p)\n", This);
752 /* Erase the downloaded effect */
753 if (ioctl(*(This->fd), EVIOCRMFF, This->effect.id) == -1)
754 return DIERR_INVALIDPARAM;
756 /* Mark the effect as deallocated */
757 This->effect.id = -1;
759 return DI_OK;
762 static ULONG WINAPI LinuxInputEffectImpl_Release(LPDIRECTINPUTEFFECT iface)
764 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
765 ULONG ref = InterlockedDecrement(&(This->ref));
767 if (ref == 0)
769 LinuxInputEffectImpl_Stop(iface);
770 LinuxInputEffectImpl_Unload(iface);
771 list_remove(This->entry);
772 HeapFree(GetProcessHeap(), 0, LIST_ENTRY(This->entry, effect_list_item, entry));
773 HeapFree(GetProcessHeap(), 0, This);
775 return ref;
778 /******************************************************************************
779 * LinuxInputEffect
782 DECLSPEC_HIDDEN HRESULT linuxinput_create_effect(
783 int* fd,
784 REFGUID rguid,
785 struct list *parent_list_entry,
786 LPDIRECTINPUTEFFECT* peff)
788 LinuxInputEffectImpl* newEffect = HeapAlloc(GetProcessHeap(),
789 HEAP_ZERO_MEMORY, sizeof(LinuxInputEffectImpl));
790 DWORD type = _typeFromGUID(rguid);
792 newEffect->IDirectInputEffect_iface.lpVtbl = &LinuxInputEffectVtbl;
793 newEffect->ref = 1;
794 newEffect->guid = *rguid;
795 newEffect->fd = fd;
796 newEffect->gain = 0xFFFF;
798 /* set the type. this cannot be changed over the effect's life. */
799 switch (type) {
800 case DIEFT_PERIODIC:
801 newEffect->effect.type = FF_PERIODIC;
802 if (IsEqualGUID(rguid, &GUID_Sine)) {
803 newEffect->effect.u.periodic.waveform = FF_SINE;
804 } else if (IsEqualGUID(rguid, &GUID_Triangle)) {
805 newEffect->effect.u.periodic.waveform = FF_TRIANGLE;
806 } else if (IsEqualGUID(rguid, &GUID_Square)) {
807 newEffect->effect.u.periodic.waveform = FF_SQUARE;
808 } else if (IsEqualGUID(rguid, &GUID_SawtoothUp)) {
809 newEffect->effect.u.periodic.waveform = FF_SAW_UP;
810 } else if (IsEqualGUID(rguid, &GUID_SawtoothDown)) {
811 newEffect->effect.u.periodic.waveform = FF_SAW_DOWN;
813 break;
814 case DIEFT_CONSTANTFORCE:
815 newEffect->effect.type = FF_CONSTANT;
816 break;
817 case DIEFT_RAMPFORCE:
818 newEffect->effect.type = FF_RAMP;
819 break;
820 case DIEFT_CONDITION:
821 if (IsEqualGUID(rguid, &GUID_Spring)) {
822 newEffect->effect.type = FF_SPRING;
823 } else if (IsEqualGUID(rguid, &GUID_Friction)) {
824 newEffect->effect.type = FF_FRICTION;
825 } else if (IsEqualGUID(rguid, &GUID_Inertia)) {
826 newEffect->effect.type = FF_INERTIA;
827 } else if (IsEqualGUID(rguid, &GUID_Damper)) {
828 newEffect->effect.type = FF_DAMPER;
830 break;
831 case DIEFT_CUSTOMFORCE:
832 FIXME("Custom forces are not supported.\n");
833 HeapFree(GetProcessHeap(), 0, newEffect);
834 return DIERR_INVALIDPARAM;
835 default:
836 FIXME("Unknown force type 0x%x.\n", type);
837 HeapFree(GetProcessHeap(), 0, newEffect);
838 return DIERR_INVALIDPARAM;
841 /* mark as non-uploaded */
842 newEffect->effect.id = -1;
844 newEffect->entry = parent_list_entry;
846 *peff = &newEffect->IDirectInputEffect_iface;
848 TRACE("Creating linux input system effect (%p) with guid %s\n",
849 *peff, _dump_dinput_GUID(rguid));
851 return DI_OK;
854 DECLSPEC_HIDDEN HRESULT linuxinput_get_info_A(
855 int fd,
856 REFGUID rguid,
857 LPDIEFFECTINFOA info)
859 DWORD type = _typeFromGUID(rguid);
861 TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
863 if (!info) return E_POINTER;
865 if (info->dwSize != sizeof(DIEFFECTINFOA)) return DIERR_INVALIDPARAM;
867 info->guid = *rguid;
869 info->dwEffType = type;
870 /* the event device API does not support querying for all these things
871 * therefore we assume that we have support for them
872 * that's not as dangerous as it sounds, since drivers are allowed to
873 * ignore parameters they claim to support anyway */
874 info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE
875 | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
876 | DIEFT_SATURATION | DIEFT_STARTDELAY;
878 /* again, assume we have support for everything */
879 info->dwStaticParams = DIEP_ALLPARAMS;
880 info->dwDynamicParams = info->dwStaticParams;
882 /* yes, this is windows behavior (print the GUID_Name for name) */
883 strcpy(info->tszName, _dump_dinput_GUID(rguid));
885 return DI_OK;
888 DECLSPEC_HIDDEN HRESULT linuxinput_get_info_W(
889 int fd,
890 REFGUID rguid,
891 LPDIEFFECTINFOW info)
893 DWORD type = _typeFromGUID(rguid);
895 TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
897 if (!info) return E_POINTER;
899 if (info->dwSize != sizeof(DIEFFECTINFOW)) return DIERR_INVALIDPARAM;
901 info->guid = *rguid;
903 info->dwEffType = type;
904 /* the event device API does not support querying for all these things
905 * therefore we assume that we have support for them
906 * that's not as dangerous as it sounds, since drivers are allowed to
907 * ignore parameters they claim to support anyway */
908 info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE
909 | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
910 | DIEFT_SATURATION | DIEFT_STARTDELAY;
912 /* again, assume we have support for everything */
913 info->dwStaticParams = DIEP_ALLPARAMS;
914 info->dwDynamicParams = info->dwStaticParams;
916 /* yes, this is windows behavior (print the GUID_Name for name) */
917 MultiByteToWideChar(CP_ACP, 0, _dump_dinput_GUID(rguid), -1,
918 info->tszName, MAX_PATH);
920 return DI_OK;
923 static const IDirectInputEffectVtbl LinuxInputEffectVtbl = {
924 LinuxInputEffectImpl_QueryInterface,
925 LinuxInputEffectImpl_AddRef,
926 LinuxInputEffectImpl_Release,
927 LinuxInputEffectImpl_Initialize,
928 LinuxInputEffectImpl_GetEffectGuid,
929 LinuxInputEffectImpl_GetParameters,
930 LinuxInputEffectImpl_SetParameters,
931 LinuxInputEffectImpl_Start,
932 LinuxInputEffectImpl_Stop,
933 LinuxInputEffectImpl_GetEffectStatus,
934 LinuxInputEffectImpl_Download,
935 LinuxInputEffectImpl_Unload,
936 LinuxInputEffectImpl_Escape
939 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */