push 014043c4937c940c54cd1214c96e33a3b3c8cf7d
[wine/hacks.git] / dlls / dinput / effect_linuxinput.c
blob7d603bb594092b28edf072696c23c621e7763261
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 const void *lpVtbl;
53 LONG ref;
54 GUID guid;
56 /* Effect data */
57 struct ff_effect effect;
59 /* Parent device */
60 int* fd;
64 /******************************************************************************
65 * DirectInputEffect Functional Helper
68 static DWORD _typeFromGUID(REFGUID guid)
70 if (IsEqualGUID(guid, &GUID_ConstantForce)) {
71 return DIEFT_CONSTANTFORCE;
72 } else if (IsEqualGUID(guid, &GUID_Square)
73 || IsEqualGUID(guid, &GUID_Sine)
74 || IsEqualGUID(guid, &GUID_Triangle)
75 || IsEqualGUID(guid, &GUID_SawtoothUp)
76 || IsEqualGUID(guid, &GUID_SawtoothDown)) {
77 return DIEFT_PERIODIC;
78 } else if (IsEqualGUID(guid, &GUID_RampForce)) {
79 return DIEFT_RAMPFORCE;
80 } else if (IsEqualGUID(guid, &GUID_Spring)
81 || IsEqualGUID(guid, &GUID_Damper)
82 || IsEqualGUID(guid, &GUID_Inertia)
83 || IsEqualGUID(guid, &GUID_Friction)) {
84 return DIEFT_CONDITION;
85 } else if (IsEqualGUID(guid, &GUID_CustomForce)) {
86 return DIEFT_CUSTOMFORCE;
87 } else {
88 WARN("GUID (%s) is not a known force type\n", _dump_dinput_GUID(guid));
89 return 0;
94 /******************************************************************************
95 * DirectInputEffect debug helpers
98 static void _dump_DIEFFECT_flags(DWORD dwFlags)
100 if (TRACE_ON(dinput)) {
101 unsigned int i;
102 static const struct {
103 DWORD mask;
104 const char *name;
105 } flags[] = {
106 #define FE(x) { x, #x}
107 FE(DIEFF_CARTESIAN),
108 FE(DIEFF_OBJECTIDS),
109 FE(DIEFF_OBJECTOFFSETS),
110 FE(DIEFF_POLAR),
111 FE(DIEFF_SPHERICAL)
112 #undef FE
114 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
115 if (flags[i].mask & dwFlags)
116 TRACE("%s ", flags[i].name);
117 TRACE("\n");
121 static void _dump_DIENVELOPE(LPCDIENVELOPE env)
123 if (env->dwSize != sizeof(DIENVELOPE)) {
124 WARN("Non-standard DIENVELOPE structure size (%d instead of %d).\n",
125 env->dwSize, sizeof(DIENVELOPE));
127 TRACE("Envelope has attack (level: %d time: %d), fade (level: %d time: %d)\n",
128 env->dwAttackLevel, env->dwAttackTime, env->dwFadeLevel, env->dwFadeTime);
131 static void _dump_DICONSTANTFORCE(LPCDICONSTANTFORCE frc)
133 TRACE("Constant force has magnitude %d\n", frc->lMagnitude);
136 static void _dump_DIPERIODIC(LPCDIPERIODIC frc)
138 TRACE("Periodic force has magnitude %d, offset %d, phase %d, period %d\n",
139 frc->dwMagnitude, frc->lOffset, frc->dwPhase, frc->dwPeriod);
142 static void _dump_DIRAMPFORCE(LPCDIRAMPFORCE frc)
144 TRACE("Ramp force has start %d, end %d\n",
145 frc->lStart, frc->lEnd);
148 static void _dump_DICONDITION(LPCDICONDITION frc)
150 TRACE("Condition has offset %d, pos/neg coefficients %d and %d, pos/neg saturations %d and %d, deadband %d\n",
151 frc->lOffset, frc->lPositiveCoefficient, frc->lNegativeCoefficient,
152 frc->dwPositiveSaturation, frc->dwNegativeSaturation, frc->lDeadBand);
155 static void _dump_DICUSTOMFORCE(LPCDICUSTOMFORCE frc)
157 unsigned int i;
158 TRACE("Custom force uses %d channels, sample period %d. Has %d samples at %p.\n",
159 frc->cChannels, frc->dwSamplePeriod, frc->cSamples, frc->rglForceData);
160 if (frc->cSamples % frc->cChannels != 0)
161 WARN("Custom force has a non-integral samples-per-channel count!\n");
162 if (TRACE_ON(dinput)) {
163 TRACE("Custom force data (time aligned, axes in order):\n");
164 for (i = 1; i <= frc->cSamples; ++i) {
165 TRACE("%d ", frc->rglForceData[i]);
166 if (i % frc->cChannels == 0)
167 TRACE("\n");
172 static void _dump_DIEFFECT(LPCDIEFFECT eff, REFGUID guid)
174 unsigned int i;
175 DWORD type = _typeFromGUID(guid);
177 TRACE("Dumping DIEFFECT structure:\n");
178 TRACE(" - dwSize: %d\n", eff->dwSize);
179 if ((eff->dwSize != sizeof(DIEFFECT)) && (eff->dwSize != sizeof(DIEFFECT_DX5))) {
180 WARN("Non-standard DIEFFECT structure size (%d instead of %d or %d).\n",
181 eff->dwSize, sizeof(DIEFFECT), sizeof(DIEFFECT_DX5));
183 TRACE(" - dwFlags: %d\n", eff->dwFlags);
184 TRACE(" ");
185 _dump_DIEFFECT_flags(eff->dwFlags);
186 TRACE(" - dwDuration: %d\n", eff->dwDuration);
187 TRACE(" - dwGain: %d\n", eff->dwGain);
188 if (eff->dwGain > 10000)
189 WARN("dwGain is out of range (>10,000)\n");
190 TRACE(" - dwTriggerButton: %d\n", eff->dwTriggerButton);
191 TRACE(" - dwTriggerRepeatInterval: %d\n", eff->dwTriggerRepeatInterval);
192 TRACE(" - cAxes: %d\n", eff->cAxes);
193 TRACE(" - rgdwAxes: %p\n", eff->rgdwAxes);
194 if (TRACE_ON(dinput)) {
195 TRACE(" ");
196 for (i = 0; i < eff->cAxes; ++i)
197 TRACE("%d ", eff->rgdwAxes[i]);
198 TRACE("\n");
200 TRACE(" - rglDirection: %p\n", eff->rglDirection);
201 TRACE(" - lpEnvelope: %p\n", eff->lpEnvelope);
202 TRACE(" - cbTypeSpecificParams: %d\n", eff->cbTypeSpecificParams);
203 TRACE(" - lpvTypeSpecificParams: %p\n", eff->lpvTypeSpecificParams);
204 if (eff->dwSize > sizeof(DIEFFECT_DX5))
205 TRACE(" - dwStartDelay: %d\n", eff->dwStartDelay);
206 if (eff->lpEnvelope != NULL)
207 _dump_DIENVELOPE(eff->lpEnvelope);
208 if (type == DIEFT_CONSTANTFORCE) {
209 if (eff->cbTypeSpecificParams != sizeof(DICONSTANTFORCE)) {
210 WARN("Effect claims to be a constant force but the type-specific params are the wrong size!\n");
211 } else {
212 _dump_DICONSTANTFORCE(eff->lpvTypeSpecificParams);
214 } else if (type == DIEFT_PERIODIC) {
215 if (eff->cbTypeSpecificParams != sizeof(DIPERIODIC)) {
216 WARN("Effect claims to be a periodic force but the type-specific params are the wrong size!\n");
217 } else {
218 _dump_DIPERIODIC(eff->lpvTypeSpecificParams);
220 } else if (type == DIEFT_RAMPFORCE) {
221 if (eff->cbTypeSpecificParams != sizeof(DIRAMPFORCE)) {
222 WARN("Effect claims to be a ramp force but the type-specific params are the wrong size!\n");
223 } else {
224 _dump_DIRAMPFORCE(eff->lpvTypeSpecificParams);
226 } else if (type == DIEFT_CONDITION) {
227 if (eff->cbTypeSpecificParams != sizeof(DICONDITION)) {
228 WARN("Effect claims to be a condition but the type-specific params are the wrong size!\n");
229 } else {
230 _dump_DICONDITION(eff->lpvTypeSpecificParams);
232 } else if (type == DIEFT_CUSTOMFORCE) {
233 if (eff->cbTypeSpecificParams != sizeof(DICUSTOMFORCE)) {
234 WARN("Effect claims to be a custom force but the type-specific params are the wrong size!\n");
235 } else {
236 _dump_DICUSTOMFORCE(eff->lpvTypeSpecificParams);
242 /******************************************************************************
243 * LinuxInputEffectImpl
246 static ULONG WINAPI LinuxInputEffectImpl_AddRef(
247 LPDIRECTINPUTEFFECT iface)
249 LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
250 return InterlockedIncrement(&(This->ref));
253 static HRESULT WINAPI LinuxInputEffectImpl_Download(
254 LPDIRECTINPUTEFFECT iface)
256 LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
258 TRACE("(this=%p)\n", This);
260 if (ioctl(*(This->fd), EVIOCSFF, &This->effect) == -1) {
261 if (errno == ENOMEM) {
262 return DIERR_DEVICEFULL;
263 } else {
264 FIXME("Could not upload effect. Assuming a disconnected device.\n");
265 return DIERR_INPUTLOST;
269 return DI_OK;
272 static HRESULT WINAPI LinuxInputEffectImpl_Escape(
273 LPDIRECTINPUTEFFECT iface,
274 LPDIEFFESCAPE pesc)
276 WARN("(this=%p,%p): invalid: no hardware-specific escape codes in this"
277 " driver!\n", iface, pesc);
279 return DI_OK;
282 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectGuid(
283 LPDIRECTINPUTEFFECT iface,
284 LPGUID pguid)
286 LinuxInputEffectImpl *This = (LinuxInputEffectImpl*)iface;
288 TRACE("(this=%p,%p)\n", This, pguid);
290 pguid = &This->guid;
292 return DI_OK;
295 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectStatus(
296 LPDIRECTINPUTEFFECT iface,
297 LPDWORD pdwFlags)
299 TRACE("(this=%p,%p)\n", iface, pdwFlags);
301 /* linux sends the effect status through an event.
302 * that event is trapped by our parent joystick driver
303 * and there is no clean way to pass it back to us. */
304 FIXME("Not enough information to provide a status.\n");
306 (*pdwFlags) = 0;
308 return DI_OK;
311 static HRESULT WINAPI LinuxInputEffectImpl_GetParameters(
312 LPDIRECTINPUTEFFECT iface,
313 LPDIEFFECT peff,
314 DWORD dwFlags)
316 HRESULT diErr = DI_OK;
317 LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
318 TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
320 /* Major conversion factors are:
321 * times: millisecond (linux) -> microsecond (windows) (x * 1000)
322 * forces: scale 0x7FFF (linux) -> scale 10000 (windows) approx ((x / 33) * 10)
323 * angles: scale 0x7FFF (linux) -> scale 35999 (windows) approx ((x / 33) * 36)
324 * angle bases: 0 -> -y (down) (linux) -> 0 -> +x (right) (windows)
327 if (dwFlags & DIEP_AXES) {
328 if (peff->cAxes < 2 /* linuxinput effects always use 2 axes, x and y */)
329 diErr = DIERR_MOREDATA;
330 peff->cAxes = 2;
331 if (diErr)
332 return diErr;
333 else {
334 peff->rgdwAxes[0] = DIJOFS_X;
335 peff->rgdwAxes[1] = DIJOFS_Y;
339 if (dwFlags & DIEP_DIRECTION) {
340 if (peff->cAxes < 2)
341 diErr = DIERR_MOREDATA;
342 peff->cAxes = 2;
343 if (diErr)
344 return diErr;
345 else {
346 if (peff->dwFlags & DIEFF_CARTESIAN) {
347 peff->rglDirection[0] = (long)(sin(M_PI * 3 * This->effect.direction / 0x7FFF) * 1000);
348 peff->rglDirection[1] = (long)(cos(M_PI * 3 * This->effect.direction / 0x7FFF) * 1000);
349 } else {
350 /* Polar and spherical coordinates are the same for two or less
351 * axes.
352 * Note that we also use this case if NO flags are marked.
353 * According to MSDN, we should return the direction in the
354 * format that it was specified in, if no flags are marked.
356 peff->rglDirection[0] = (This->effect.direction / 33) * 36 + 9000;
357 if (peff->rglDirection[0] > 35999)
358 peff->rglDirection[0] -= 35999;
363 if (dwFlags & DIEP_DURATION) {
364 peff->dwDuration = (DWORD)This->effect.replay.length * 1000;
367 if (dwFlags & DIEP_ENVELOPE) {
368 struct ff_envelope* env;
369 if (This->effect.type == FF_CONSTANT) env = &This->effect.u.constant.envelope;
370 else if (This->effect.type == FF_PERIODIC) env = &This->effect.u.periodic.envelope;
371 else if (This->effect.type == FF_RAMP) env = &This->effect.u.ramp.envelope;
372 else env = NULL;
373 if (env == NULL) {
374 peff->lpEnvelope = NULL;
375 } else if (peff->lpEnvelope == NULL) {
376 return DIERR_INVALIDPARAM;
377 } else {
378 peff->lpEnvelope->dwAttackLevel = (env->attack_level / 33) * 10;
379 peff->lpEnvelope->dwAttackTime = env->attack_length * 1000;
380 peff->lpEnvelope->dwFadeLevel = (env->fade_level / 33) * 10;
381 peff->lpEnvelope->dwFadeTime = env->fade_length * 1000;
385 if (dwFlags & DIEP_GAIN) {
386 /* the linux input ff driver apparently has no support
387 * for setting the device's gain. */
388 peff->dwGain = DI_FFNOMINALMAX;
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 int 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 = (LPDIPERIODIC)(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 = (LPDICONSTANTFORCE)(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 = (LPDICONDITION)(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 = (LPDIRAMPFORCE)(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 = (LinuxInputEffectImpl*)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 = (LinuxInputEffectImpl*)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 = (LinuxInputEffectImpl*)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;
558 /* some of this may look funky, but it's 'cause the linux driver and directx have
559 * different opinions about which way direction "0" is. directx has 0 along the x
560 * axis (left), linux has it along the y axis (down). */
561 if (dwFlags & DIEP_DIRECTION) {
562 if (peff->cAxes == 1) {
563 if (peff->dwFlags & DIEFF_CARTESIAN) {
564 if (dwFlags & DIEP_AXES) {
565 if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] >= 0)
566 This->effect.direction = 0x4000;
567 else if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] < 0)
568 This->effect.direction = 0xC000;
569 else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] >= 0)
570 This->effect.direction = 0;
571 else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] < 0)
572 This->effect.direction = 0x8000;
574 } else {
575 /* one-axis effects must use cartesian coords */
576 return DIERR_INVALIDPARAM;
578 } else { /* two axes */
579 if (peff->dwFlags & DIEFF_CARTESIAN) {
580 /* avoid divide-by-zero */
581 if (peff->rglDirection[1] == 0) {
582 if (peff->rglDirection[0] >= 0)
583 This->effect.direction = 0x4000;
584 else if (peff->rglDirection[0] < 0)
585 This->effect.direction = 0xC000;
586 } else {
587 This->effect.direction = (int)(atan(peff->rglDirection[0] / peff->rglDirection[1]) * 0x7FFF / (3 * M_PI));
589 } else {
590 /* Polar and spherical are the same for 2 axes */
591 /* Precision is important here, so we do double math with exact constants */
592 This->effect.direction = (int)(((double)peff->rglDirection[0] - 90) / 35999) * 0x7FFF;
597 if (dwFlags & DIEP_DURATION)
598 This->effect.replay.length = peff->dwDuration / 1000;
600 if (dwFlags & DIEP_ENVELOPE) {
601 struct ff_envelope* env;
602 if (This->effect.type == FF_CONSTANT) env = &This->effect.u.constant.envelope;
603 else if (This->effect.type == FF_PERIODIC) env = &This->effect.u.periodic.envelope;
604 else if (This->effect.type == FF_RAMP) env = &This->effect.u.ramp.envelope;
605 else env = NULL;
607 if (peff->lpEnvelope == NULL) {
608 /* if this type had an envelope, reset it
609 * note that length can never be zero, so we set it to something minuscule */
610 if (env) {
611 env->attack_length = 0x10;
612 env->attack_level = 0x7FFF;
613 env->fade_length = 0x10;
614 env->fade_level = 0x7FFF;
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 TRACE("Gain requested but no gain functionality present.\n");
632 if (dwFlags & DIEP_SAMPLEPERIOD)
633 TRACE("Sample period requested but no sample period functionality present.\n");
635 if (dwFlags & DIEP_STARTDELAY)
636 This->effect.replay.delay = peff->dwStartDelay / 1000;
638 if (dwFlags & DIEP_TRIGGERBUTTON) {
639 if (peff->dwTriggerButton != -1) {
640 FIXME("Linuxinput button mapping needs redoing, assuming we're using a joystick.\n");
641 FIXME("Trigger button translation not yet implemented!\n");
643 This->effect.trigger.button = 0;
646 if (dwFlags & DIEP_TRIGGERREPEATINTERVAL)
647 This->effect.trigger.interval = peff->dwTriggerRepeatInterval / 1000;
649 if (dwFlags & DIEP_TYPESPECIFICPARAMS) {
650 if (!(peff->lpvTypeSpecificParams))
651 return DIERR_INCOMPLETEEFFECT;
652 if (type == DIEFT_PERIODIC) {
653 LPCDIPERIODIC tsp;
654 if (peff->cbTypeSpecificParams != sizeof(DIPERIODIC))
655 return DIERR_INVALIDPARAM;
656 tsp = (LPCDIPERIODIC)(peff->lpvTypeSpecificParams);
657 This->effect.u.periodic.magnitude = (tsp->dwMagnitude / 10) * 32;
658 This->effect.u.periodic.offset = (tsp->lOffset / 10) * 32;
659 This->effect.u.periodic.phase = (tsp->dwPhase / 9) * 8; /* == (/ 36 * 32) */
660 This->effect.u.periodic.period = tsp->dwPeriod / 1000;
661 } else if (type == DIEFT_CONSTANTFORCE) {
662 LPCDICONSTANTFORCE tsp;
663 if (peff->cbTypeSpecificParams != sizeof(DICONSTANTFORCE))
664 return DIERR_INVALIDPARAM;
665 tsp = (LPCDICONSTANTFORCE)(peff->lpvTypeSpecificParams);
666 This->effect.u.constant.level = (tsp->lMagnitude / 10) * 32;
667 } else if (type == DIEFT_RAMPFORCE) {
668 LPCDIRAMPFORCE tsp;
669 if (peff->cbTypeSpecificParams != sizeof(DIRAMPFORCE))
670 return DIERR_INVALIDPARAM;
671 tsp = (LPCDIRAMPFORCE)(peff->lpvTypeSpecificParams);
672 This->effect.u.ramp.start_level = (tsp->lStart / 10) * 32;
673 This->effect.u.ramp.end_level = (tsp->lStart / 10) * 32;
674 } else if (type == DIEFT_CONDITION) {
675 LPCDICONDITION tsp = (LPCDICONDITION)(peff->lpvTypeSpecificParams);
676 if (peff->cbTypeSpecificParams == sizeof(DICONDITION)) {
677 /* One condition block. This needs to be rotated to direction,
678 * and expanded to separate x and y conditions. */
679 int i;
680 double factor[2];
681 factor[0] = asin((This->effect.direction * 3.0 * M_PI) / 0x7FFF);
682 factor[1] = acos((This->effect.direction * 3.0 * M_PI) / 0x7FFF);
683 for (i = 0; i < 2; ++i) {
684 This->effect.u.condition[i].center = (int)(factor[i] * (tsp->lOffset / 10) * 32);
685 This->effect.u.condition[i].right_coeff = (int)(factor[i] * (tsp->lPositiveCoefficient / 10) * 32);
686 This->effect.u.condition[i].left_coeff = (int)(factor[i] * (tsp->lNegativeCoefficient / 10) * 32);
687 This->effect.u.condition[i].right_saturation = (int)(factor[i] * (tsp->dwPositiveSaturation / 10) * 32);
688 This->effect.u.condition[i].left_saturation = (int)(factor[i] * (tsp->dwNegativeSaturation / 10) * 32);
689 This->effect.u.condition[i].deadband = (int)(factor[i] * (tsp->lDeadBand / 10) * 32);
691 } else if (peff->cbTypeSpecificParams == 2 * sizeof(DICONDITION)) {
692 /* Two condition blocks. Direct parameter copy. */
693 int i;
694 for (i = 0; i < 2; ++i) {
695 This->effect.u.condition[i].center = (tsp[i].lOffset / 10) * 32;
696 This->effect.u.condition[i].right_coeff = (tsp[i].lPositiveCoefficient / 10) * 32;
697 This->effect.u.condition[i].left_coeff = (tsp[i].lNegativeCoefficient / 10) * 32;
698 This->effect.u.condition[i].right_saturation = (tsp[i].dwPositiveSaturation / 10) * 32;
699 This->effect.u.condition[i].left_saturation = (tsp[i].dwNegativeSaturation / 10) * 32;
700 This->effect.u.condition[i].deadband = (tsp[i].lDeadBand / 10) * 32;
702 } else {
703 return DIERR_INVALIDPARAM;
705 } else {
706 FIXME("Custom force types are not supported\n");
707 return DIERR_INVALIDPARAM;
711 if (!(dwFlags & DIEP_NODOWNLOAD))
712 retval = LinuxInputEffectImpl_Download(iface);
713 if (retval != DI_OK)
714 return DI_DOWNLOADSKIPPED;
716 if (dwFlags & DIEP_NORESTART)
717 TRACE("DIEP_NORESTART: not handled (we have no control of that).\n");
719 if (dwFlags & DIEP_START)
720 retval = LinuxInputEffectImpl_Start(iface, 1, 0);
721 if (retval != DI_OK)
722 return retval;
724 return DI_OK;
727 static ULONG WINAPI LinuxInputEffectImpl_Release(
728 LPDIRECTINPUTEFFECT iface)
730 LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
731 ULONG ref = InterlockedDecrement(&(This->ref));
733 if (ref == 0)
734 HeapFree(GetProcessHeap(), 0, This);
735 return ref;
738 static HRESULT WINAPI LinuxInputEffectImpl_Stop(
739 LPDIRECTINPUTEFFECT iface)
741 struct input_event event;
742 LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
744 TRACE("(this=%p)\n", This);
746 event.type = EV_FF;
747 event.code = This->effect.id;
748 event.value = 0;
749 /* we don't care about the success or failure of this call */
750 write(*(This->fd), &event, sizeof(event));
752 return DI_OK;
755 static HRESULT WINAPI LinuxInputEffectImpl_Unload(
756 LPDIRECTINPUTEFFECT iface)
758 LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
759 TRACE("(this=%p)\n", This);
761 /* Erase the downloaded effect */
762 if (ioctl(*(This->fd), EVIOCRMFF, This->effect.id) == -1)
763 return DIERR_INVALIDPARAM;
765 /* Mark the effect as deallocated */
766 This->effect.id = -1;
768 return DI_OK;
771 /******************************************************************************
772 * LinuxInputEffect
775 HRESULT linuxinput_create_effect(
776 int* fd,
777 REFGUID rguid,
778 LPDIRECTINPUTEFFECT* peff)
780 LinuxInputEffectImpl* newEffect = HeapAlloc(GetProcessHeap(),
781 HEAP_ZERO_MEMORY, sizeof(LinuxInputEffectImpl));
782 DWORD type = _typeFromGUID(rguid);
784 newEffect->lpVtbl = &LinuxInputEffectVtbl;
785 newEffect->ref = 1;
786 newEffect->guid = *rguid;
787 newEffect->fd = fd;
789 /* set the type. this cannot be changed over the effect's life. */
790 switch (type) {
791 case DIEFT_PERIODIC:
792 newEffect->effect.type = FF_PERIODIC;
793 if (IsEqualGUID(rguid, &GUID_Sine)) {
794 newEffect->effect.u.periodic.waveform = FF_SINE;
795 } else if (IsEqualGUID(rguid, &GUID_Triangle)) {
796 newEffect->effect.u.periodic.waveform = FF_TRIANGLE;
797 } else if (IsEqualGUID(rguid, &GUID_Square)) {
798 newEffect->effect.u.periodic.waveform = FF_SQUARE;
799 } else if (IsEqualGUID(rguid, &GUID_SawtoothUp)) {
800 newEffect->effect.u.periodic.waveform = FF_SAW_UP;
801 } else if (IsEqualGUID(rguid, &GUID_SawtoothDown)) {
802 newEffect->effect.u.periodic.waveform = FF_SAW_DOWN;
804 break;
805 case DIEFT_CONSTANTFORCE:
806 newEffect->effect.type = FF_CONSTANT;
807 break;
808 case DIEFT_RAMPFORCE:
809 newEffect->effect.type = FF_RAMP;
810 break;
811 case DIEFT_CONDITION:
812 if (IsEqualGUID(rguid, &GUID_Spring)) {
813 newEffect->effect.type = FF_SPRING;
814 } else if (IsEqualGUID(rguid, &GUID_Friction)) {
815 newEffect->effect.type = FF_FRICTION;
816 } else if (IsEqualGUID(rguid, &GUID_Inertia)) {
817 newEffect->effect.type = FF_INERTIA;
818 } else if (IsEqualGUID(rguid, &GUID_Damper)) {
819 newEffect->effect.type = FF_DAMPER;
821 break;
822 case DIEFT_CUSTOMFORCE:
823 FIXME("Custom forces are not supported.\n");
824 HeapFree(GetProcessHeap(), 0, newEffect);
825 return DIERR_INVALIDPARAM;
826 default:
827 FIXME("Unknown force type.\n");
828 HeapFree(GetProcessHeap(), 0, newEffect);
829 return DIERR_INVALIDPARAM;
832 /* mark as non-uploaded */
833 newEffect->effect.id = -1;
835 *peff = (LPDIRECTINPUTEFFECT)newEffect;
837 TRACE("Creating linux input system effect (%p) with guid %s\n",
838 *peff, _dump_dinput_GUID(rguid));
840 return DI_OK;
843 HRESULT linuxinput_get_info_A(
844 int fd,
845 REFGUID rguid,
846 LPDIEFFECTINFOA info)
848 DWORD type = _typeFromGUID(rguid);
850 TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
852 if (!info) return E_POINTER;
854 if (info->dwSize != sizeof(DIEFFECTINFOA)) return DIERR_INVALIDPARAM;
856 info->guid = *rguid;
858 info->dwEffType = type;
859 /* the event device API does not support querying for all these things
860 * therefore we assume that we have support for them
861 * that's not as dangerous as it sounds, since drivers are allowed to
862 * ignore parameters they claim to support anyway */
863 info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE
864 | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
865 | DIEFT_SATURATION | DIEFT_STARTDELAY;
867 /* again, assume we have support for everything */
868 info->dwStaticParams = DIEP_ALLPARAMS;
869 info->dwDynamicParams = info->dwStaticParams;
871 /* yes, this is windows behavior (print the GUID_Name for name) */
872 strcpy((char*)&(info->tszName), _dump_dinput_GUID(rguid));
874 return DI_OK;
877 HRESULT linuxinput_get_info_W(
878 int fd,
879 REFGUID rguid,
880 LPDIEFFECTINFOW info)
882 DWORD type = _typeFromGUID(rguid);
884 TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
886 if (!info) return E_POINTER;
888 if (info->dwSize != sizeof(DIEFFECTINFOW)) return DIERR_INVALIDPARAM;
890 info->guid = *rguid;
892 info->dwEffType = type;
893 /* the event device API does not support querying for all these things
894 * therefore we assume that we have support for them
895 * that's not as dangerous as it sounds, since drivers are allowed to
896 * ignore parameters they claim to support anyway */
897 info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE
898 | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
899 | DIEFT_SATURATION | DIEFT_STARTDELAY;
901 /* again, assume we have support for everything */
902 info->dwStaticParams = DIEP_ALLPARAMS;
903 info->dwDynamicParams = info->dwStaticParams;
905 /* yes, this is windows behavior (print the GUID_Name for name) */
906 MultiByteToWideChar(CP_ACP, 0, _dump_dinput_GUID(rguid), -1,
907 (WCHAR*)&(info->tszName), MAX_PATH);
909 return DI_OK;
912 static const IDirectInputEffectVtbl LinuxInputEffectVtbl = {
913 LinuxInputEffectImpl_QueryInterface,
914 LinuxInputEffectImpl_AddRef,
915 LinuxInputEffectImpl_Release,
916 LinuxInputEffectImpl_Initialize,
917 LinuxInputEffectImpl_GetEffectGuid,
918 LinuxInputEffectImpl_GetParameters,
919 LinuxInputEffectImpl_SetParameters,
920 LinuxInputEffectImpl_Start,
921 LinuxInputEffectImpl_Stop,
922 LinuxInputEffectImpl_GetEffectStatus,
923 LinuxInputEffectImpl_Download,
924 LinuxInputEffectImpl_Unload,
925 LinuxInputEffectImpl_Escape
928 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */