Release 2.16.
[wine.git] / dlls / dinput / effect_linuxinput.c
blob7477bc757da621ea63c6a666b50293ef18d59cc2
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 <limits.h>
33 #include <errno.h>
34 #ifdef HAVE_UNISTD_H
35 # include <unistd.h>
36 #endif
37 #include <math.h>
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winerror.h"
43 #include "dinput.h"
45 #include "device_private.h"
46 #include "joystick_private.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
50 static const IDirectInputEffectVtbl LinuxInputEffectVtbl;
51 typedef struct LinuxInputEffectImpl LinuxInputEffectImpl;
52 struct LinuxInputEffectImpl
54 IDirectInputEffect IDirectInputEffect_iface;
55 LONG ref;
56 GUID guid;
58 struct ff_effect effect; /* Effect data */
59 int gain; /* Effect gain */
60 BOOL first_axis_is_x;
61 int* fd; /* Parent device */
62 struct list *entry; /* Entry into the parent's list of effects */
65 static inline LinuxInputEffectImpl *impl_from_IDirectInputEffect(IDirectInputEffect *iface)
67 return CONTAINING_RECORD(iface, LinuxInputEffectImpl, IDirectInputEffect_iface);
70 static double ff_effect_direction_to_rad(unsigned int dir)
72 return (dir & 0xffff) * M_PI / 0x8000;
75 static void ff_dump_effect(struct ff_effect *effect)
77 const char *type = "(Unknown)", *length = "INFINITE";
78 struct ff_envelope *env = NULL;
79 double angle;
80 #define FE(x) case x: type = #x; break
81 switch (effect->type)
83 FE(FF_RUMBLE);
84 FE(FF_PERIODIC);
85 FE(FF_CONSTANT);
86 FE(FF_SPRING);
87 FE(FF_FRICTION);
88 FE(FF_DAMPER);
89 FE(FF_INERTIA);
90 FE(FF_RAMP);
92 #undef FE
94 /* rotate so 0 points right */
95 angle = 360 - ff_effect_direction_to_rad(effect->direction + 0xc000) * 180 / M_PI;
97 if (effect->replay.length)
98 length = wine_dbg_sprintf("%u ms", effect->replay.length);
100 TRACE("type 0x%x %s, id %d, direction 0x%x (source angle %.2f), time length %s, start delay %u ms\n",
101 effect->type, type, effect->id, effect->direction, angle, length, effect->replay.delay);
102 if (effect->trigger.button || effect->trigger.interval)
103 TRACE(" -> trigger button %u, re-trigger interval %u ms\n",
104 effect->trigger.button, effect->trigger.interval);
106 if (effect->type == FF_PERIODIC)
108 struct ff_periodic_effect *per = &effect->u.periodic;
109 const char *wave = "(Unknown)";
110 #define FE(x) case x: wave = #x; break
111 switch (per->waveform)
113 FE(FF_SQUARE);
114 FE(FF_TRIANGLE);
115 FE(FF_SINE);
116 FE(FF_SAW_UP);
117 FE(FF_SAW_DOWN);
118 FE(FF_CUSTOM);
120 #undef FE
121 angle = ff_effect_direction_to_rad(per->phase) * 180 / M_PI;
122 TRACE(" -> waveform 0x%x %s, period %u ms, magnitude %d, offset %d, phase 0x%x (angle %.2f), custom len %d\n",
123 per->waveform, wave, per->period, per->magnitude, per->offset, per->phase, angle, per->custom_len);
124 env = &per->envelope;
126 else if (effect->type == FF_CONSTANT)
128 struct ff_constant_effect *cons = &effect->u.constant;
129 TRACE(" -> level %d\n", cons->level);
130 env = &cons->envelope;
132 else if (effect->type == FF_RAMP)
134 struct ff_ramp_effect *ramp = &effect->u.ramp;
135 TRACE(" -> start/end level %d/%d\n", ramp->start_level, ramp->end_level);
136 env = &ramp->envelope;
138 else if (effect->type == FF_RUMBLE)
140 struct ff_rumble_effect *rumble = &effect->u.rumble;
141 TRACE(" -> strong/weak magnitude %u/%u\n", rumble->strong_magnitude, rumble->weak_magnitude);
143 else if (effect->type == FF_SPRING || effect->type == FF_FRICTION ||
144 effect->type == FF_DAMPER || effect->type == FF_INERTIA)
146 struct ff_condition_effect *cond = effect->u.condition;
147 int i;
148 for (i = 0; i < 2; i++)
150 /* format numbers here to make them align correctly */
151 TRACE(" -> [%d] right/left saturation %5u/%5u, right/left coefficient %5d/%5d,"
152 " deadband %5u, center %5d\n", i, cond[i].right_saturation, cond[i].left_saturation,
153 cond[i].right_coeff, cond[i].left_coeff, cond[i].deadband, cond[i].center);
157 if (env)
158 TRACE(" -> envelope attack length(ms)/level %u/%u, fade length(ms)/level %u/%u\n",
159 env->attack_length, env->attack_level, env->fade_length, env->fade_level);
162 /******************************************************************************
163 * LinuxInputEffectImpl
166 static ULONG WINAPI LinuxInputEffectImpl_AddRef(
167 LPDIRECTINPUTEFFECT iface)
169 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
170 return InterlockedIncrement(&(This->ref));
173 static HRESULT WINAPI LinuxInputEffectImpl_Download(
174 LPDIRECTINPUTEFFECT iface)
176 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
177 int ret, old_effect_id;
179 TRACE("(this=%p)\n", This);
180 ff_dump_effect(&This->effect);
182 old_effect_id = This->effect.id;
183 if (ioctl(*(This->fd), EVIOCSFF, &This->effect) != -1)
184 return DI_OK;
186 /* Linux kernel < 3.14 has a bug that incorrectly assigns an effect ID even
187 * on error, restore it here if that is the case. */
188 This->effect.id = old_effect_id;
190 switch (errno)
192 case EINVAL:
193 ret = DIERR_INVALIDPARAM;
194 break;
195 case ENOSPC:
196 ret = DIERR_DEVICEFULL;
197 break;
198 case ENOMEM:
199 ret = DIERR_OUTOFMEMORY;
200 break;
201 default:
202 ret = DIERR_INPUTLOST;
203 break;
205 TRACE("Could not upload effect to fd %d, errno %d \"%s\", returning 0x%x.\n",
206 *This->fd, errno, strerror(errno), ret);
207 return ret;
210 static HRESULT WINAPI LinuxInputEffectImpl_Escape(
211 LPDIRECTINPUTEFFECT iface,
212 LPDIEFFESCAPE pesc)
214 WARN("(this=%p,%p): invalid: no hardware-specific escape codes in this"
215 " driver!\n", iface, pesc);
217 return DI_OK;
220 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectGuid(
221 LPDIRECTINPUTEFFECT iface,
222 LPGUID pguid)
224 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
226 TRACE("(this=%p,%p)\n", This, pguid);
228 *pguid = This->guid;
230 return DI_OK;
233 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectStatus(
234 LPDIRECTINPUTEFFECT iface,
235 LPDWORD pdwFlags)
237 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
239 TRACE("(this=%p,%p)\n", This, pdwFlags);
241 if (!pdwFlags)
242 return E_POINTER;
244 if (This->effect.id == -1)
245 return DIERR_NOTDOWNLOADED;
247 /* linux sends the effect status through an event.
248 * that event is trapped by our parent joystick driver
249 * and there is no clean way to pass it back to us. */
250 FIXME("Not enough information to provide a status.\n");
252 (*pdwFlags) = 0;
254 return DI_OK;
257 static HRESULT WINAPI LinuxInputEffectImpl_GetParameters(
258 LPDIRECTINPUTEFFECT iface,
259 LPDIEFFECT peff,
260 DWORD dwFlags)
262 HRESULT diErr = DI_OK;
263 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
264 TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
266 /* Major conversion factors are:
267 * times: millisecond (linux) -> microsecond (windows) (x * 1000)
268 * forces: scale 0x7FFF (linux) -> scale 10000 (windows) approx ((x / 33) * 10)
269 * angles: scale 0x7FFF (linux) -> scale 35999 (windows) approx ((x / 33) * 36)
270 * angle bases: 0 -> -y (down) (linux) -> 0 -> +x (right) (windows)
273 if (dwFlags & DIEP_AXES) {
274 if (peff->cAxes < 2 /* linuxinput effects always use 2 axes, x and y */)
275 diErr = DIERR_MOREDATA;
276 peff->cAxes = 2;
277 if (diErr)
278 return diErr;
279 else {
280 peff->rgdwAxes[0] = DIJOFS_X;
281 peff->rgdwAxes[1] = DIJOFS_Y;
285 if (dwFlags & DIEP_DIRECTION) {
286 if (peff->cAxes < 2)
287 diErr = DIERR_MOREDATA;
288 peff->cAxes = 2;
289 if (diErr)
290 return diErr;
291 else {
292 if (peff->dwFlags & DIEFF_CARTESIAN) {
293 /* rotate so 0 points right */
294 double angle = ff_effect_direction_to_rad(This->effect.direction + 0xc000);
295 peff->rglDirection[0] = sin(angle) * 1000;
296 peff->rglDirection[1] = -cos(angle) * 1000;
297 } else {
298 /* Polar and spherical coordinates are the same for two or less
299 * axes.
300 * Note that we also use this case if NO flags are marked.
301 * According to MSDN, we should return the direction in the
302 * format that it was specified in, if no flags are marked.
304 peff->rglDirection[0] = (This->effect.direction / 33) * 36 + 9000;
305 if (peff->rglDirection[0] > 35999)
306 peff->rglDirection[0] -= 35999;
311 if (dwFlags & DIEP_DURATION)
313 if (!This->effect.replay.length) /* infinite for the linux driver */
314 peff->dwDuration = INFINITE;
315 else
316 peff->dwDuration = (DWORD)This->effect.replay.length * 1000;
319 if (dwFlags & DIEP_ENVELOPE) {
320 struct ff_envelope* env;
321 if (This->effect.type == FF_CONSTANT) env = &This->effect.u.constant.envelope;
322 else if (This->effect.type == FF_PERIODIC) env = &This->effect.u.periodic.envelope;
323 else if (This->effect.type == FF_RAMP) env = &This->effect.u.ramp.envelope;
324 else env = NULL;
325 if (env == NULL) {
326 peff->lpEnvelope = NULL;
327 } else if (peff->lpEnvelope == NULL) {
328 return DIERR_INVALIDPARAM;
329 } else {
330 peff->lpEnvelope->dwAttackLevel = (env->attack_level / 33) * 10;
331 peff->lpEnvelope->dwAttackTime = env->attack_length * 1000;
332 peff->lpEnvelope->dwFadeLevel = (env->fade_level / 33) * 10;
333 peff->lpEnvelope->dwFadeTime = env->fade_length * 1000;
337 if (dwFlags & DIEP_GAIN) {
338 peff->dwGain = This->gain * 10000 / 0xFFFF;
341 if (dwFlags & DIEP_SAMPLEPERIOD) {
342 /* the linux input ff driver has no support for setting
343 * the playback sample period. 0 means default. */
344 peff->dwSamplePeriod = 0;
347 if ((dwFlags & DIEP_STARTDELAY) && peff->dwSize > sizeof(DIEFFECT_DX5))
348 peff->dwStartDelay = This->effect.replay.delay * 1000;
350 if (dwFlags & DIEP_TRIGGERBUTTON) {
351 FIXME("LinuxInput button mapping needs redoing; for now, assuming we're using an actual joystick.\n");
352 peff->dwTriggerButton = DIJOFS_BUTTON(This->effect.trigger.button - BTN_JOYSTICK);
355 if (dwFlags & DIEP_TRIGGERREPEATINTERVAL) {
356 peff->dwTriggerRepeatInterval = This->effect.trigger.interval * 1000;
359 if (dwFlags & DIEP_TYPESPECIFICPARAMS) {
360 DWORD expectedsize = 0;
361 if (This->effect.type == FF_PERIODIC) {
362 expectedsize = sizeof(DIPERIODIC);
363 } else if (This->effect.type == FF_CONSTANT) {
364 expectedsize = sizeof(DICONSTANTFORCE);
365 } else if (This->effect.type == FF_SPRING
366 || This->effect.type == FF_FRICTION
367 || This->effect.type == FF_INERTIA
368 || This->effect.type == FF_DAMPER) {
369 expectedsize = sizeof(DICONDITION) * 2;
370 } else if (This->effect.type == FF_RAMP) {
371 expectedsize = sizeof(DIRAMPFORCE);
373 if (expectedsize > peff->cbTypeSpecificParams)
374 diErr = DIERR_MOREDATA;
375 peff->cbTypeSpecificParams = expectedsize;
376 if (diErr)
377 return diErr;
378 else {
379 if (This->effect.type == FF_PERIODIC) {
380 LPDIPERIODIC tsp = peff->lpvTypeSpecificParams;
381 tsp->dwMagnitude = (This->effect.u.periodic.magnitude / 33) * 10;
382 tsp->lOffset = (This->effect.u.periodic.offset / 33) * 10;
383 tsp->dwPhase = (This->effect.u.periodic.phase / 33) * 36;
384 tsp->dwPeriod = (This->effect.u.periodic.period * 1000);
385 } else if (This->effect.type == FF_CONSTANT) {
386 LPDICONSTANTFORCE tsp = peff->lpvTypeSpecificParams;
387 tsp->lMagnitude = (This->effect.u.constant.level / 33) * 10;
388 } else if (This->effect.type == FF_SPRING
389 || This->effect.type == FF_FRICTION
390 || This->effect.type == FF_INERTIA
391 || This->effect.type == FF_DAMPER) {
392 LPDICONDITION tsp = peff->lpvTypeSpecificParams;
393 int i;
394 for (i = 0; i < 2; ++i) {
395 tsp[i].lOffset = (This->effect.u.condition[i].center / 33) * 10;
396 tsp[i].lPositiveCoefficient = (This->effect.u.condition[i].right_coeff / 33) * 10;
397 tsp[i].lNegativeCoefficient = (This->effect.u.condition[i].left_coeff / 33) * 10;
398 tsp[i].dwPositiveSaturation = (This->effect.u.condition[i].right_saturation / 33) * 10;
399 tsp[i].dwNegativeSaturation = (This->effect.u.condition[i].left_saturation / 33) * 10;
400 tsp[i].lDeadBand = (This->effect.u.condition[i].deadband / 33) * 10;
402 } else if (This->effect.type == FF_RAMP) {
403 LPDIRAMPFORCE tsp = peff->lpvTypeSpecificParams;
404 tsp->lStart = (This->effect.u.ramp.start_level / 33) * 10;
405 tsp->lEnd = (This->effect.u.ramp.end_level / 33) * 10;
410 return diErr;
413 static HRESULT WINAPI LinuxInputEffectImpl_Initialize(
414 LPDIRECTINPUTEFFECT iface,
415 HINSTANCE hinst,
416 DWORD dwVersion,
417 REFGUID rguid)
419 FIXME("(this=%p,%p,%d,%s): stub!\n",
420 iface, hinst, dwVersion, debugstr_guid(rguid));
422 return DI_OK;
425 static HRESULT WINAPI LinuxInputEffectImpl_QueryInterface(
426 LPDIRECTINPUTEFFECT iface,
427 REFIID riid,
428 void **ppvObject)
430 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
432 TRACE("(this=%p,%s,%p)\n", This, debugstr_guid(riid), ppvObject);
434 if (IsEqualGUID(&IID_IUnknown, riid) ||
435 IsEqualGUID(&IID_IDirectInputEffect, riid)) {
436 LinuxInputEffectImpl_AddRef(iface);
437 *ppvObject = This;
438 return 0;
441 TRACE("Unsupported interface!\n");
442 return E_FAIL;
445 static HRESULT WINAPI LinuxInputEffectImpl_Start(
446 LPDIRECTINPUTEFFECT iface,
447 DWORD dwIterations,
448 DWORD dwFlags)
450 struct input_event event;
451 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
453 TRACE("(this=%p,%d,%d)\n", This, dwIterations, dwFlags);
455 if (!(dwFlags & DIES_NODOWNLOAD)) {
456 /* Download the effect if necessary */
457 if (This->effect.id == -1) {
458 HRESULT res = LinuxInputEffectImpl_Download(iface);
459 if (res != DI_OK)
460 return res;
464 if (dwFlags & DIES_SOLO) {
465 FIXME("Solo mode requested: should be stopping all effects here!\n");
468 event.type = EV_FF;
469 event.code = This->effect.id;
470 event.value = min( dwIterations, INT_MAX );
471 if (write(*(This->fd), &event, sizeof(event)) == -1) {
472 FIXME("Unable to write event. Assuming device disconnected.\n");
473 return DIERR_INPUTLOST;
476 return DI_OK;
479 static HRESULT WINAPI LinuxInputEffectImpl_SetParameters(
480 LPDIRECTINPUTEFFECT iface,
481 LPCDIEFFECT peff,
482 DWORD dwFlags)
484 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
485 DWORD type = typeFromGUID(&This->guid);
486 HRESULT retval = DI_OK;
488 TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
490 dump_DIEFFECT(peff, &This->guid, dwFlags);
492 if ((dwFlags & ~DIEP_NORESTART & ~DIEP_NODOWNLOAD & ~DIEP_START) == 0) {
493 /* set everything */
494 dwFlags = DIEP_AXES | DIEP_DIRECTION | DIEP_DURATION | DIEP_ENVELOPE |
495 DIEP_GAIN | DIEP_SAMPLEPERIOD | DIEP_STARTDELAY | DIEP_TRIGGERBUTTON |
496 DIEP_TRIGGERREPEATINTERVAL | DIEP_TYPESPECIFICPARAMS;
499 if (dwFlags & DIEP_AXES) {
500 /* the linux input effect system only supports one or two axes */
501 if (peff->cAxes > 2)
502 return DIERR_INVALIDPARAM;
503 else if (peff->cAxes < 1)
504 return DIERR_INCOMPLETEEFFECT;
505 This->first_axis_is_x = peff->rgdwAxes[0] == DIJOFS_X;
508 /* some of this may look funky, but it's 'cause the linux driver and directx have
509 * different opinions about which way direction "0" is. directx has 0 along the x
510 * axis (left), linux has it along the y axis (down). */
511 if (dwFlags & DIEP_DIRECTION) {
512 if (peff->cAxes == 1) {
513 if (peff->dwFlags & DIEFF_CARTESIAN) {
514 if (dwFlags & DIEP_AXES) {
515 if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] >= 0)
516 This->effect.direction = 0x4000;
517 else if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] < 0)
518 This->effect.direction = 0xC000;
519 else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] >= 0)
520 This->effect.direction = 0;
521 else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] < 0)
522 This->effect.direction = 0x8000;
524 } else {
525 /* one-axis effects must use cartesian coords */
526 return DIERR_INVALIDPARAM;
529 /* two axes */
530 else
532 if (peff->dwFlags & DIEFF_CARTESIAN)
534 LONG x, y;
535 if (This->first_axis_is_x)
537 x = peff->rglDirection[0];
538 y = peff->rglDirection[1];
540 else
542 x = peff->rglDirection[1];
543 y = peff->rglDirection[0];
545 This->effect.direction = (unsigned int)((M_PI / 2 + atan2(y, x)) * 0x8000 / M_PI);
547 else
549 /* Polar and spherical are the same for 2 axes */
550 /* Precision is important here, so we do double math with exact constants */
551 This->effect.direction = (unsigned int)(((double)peff->rglDirection[0] / 18000) * 0x8000);
556 if (dwFlags & DIEP_DURATION)
558 if (peff->dwDuration == INFINITE)
559 This->effect.replay.length = 0; /* infinite for the linux driver */
560 else if(peff->dwDuration > 1000)
561 This->effect.replay.length = peff->dwDuration / 1000;
562 else
563 This->effect.replay.length = 1;
566 if (dwFlags & DIEP_ENVELOPE)
568 struct ff_envelope* env;
569 if (This->effect.type == FF_CONSTANT)
570 env = &This->effect.u.constant.envelope;
571 else if (This->effect.type == FF_PERIODIC)
572 env = &This->effect.u.periodic.envelope;
573 else if (This->effect.type == FF_RAMP)
574 env = &This->effect.u.ramp.envelope;
575 else
576 env = NULL;
578 /* copy the envelope if it is present and the linux effect supports it */
579 if (peff->lpEnvelope && env)
581 env->attack_length = peff->lpEnvelope->dwAttackTime / 1000;
582 env->attack_level = (peff->lpEnvelope->dwAttackLevel / 10) * 32;
583 env->fade_length = peff->lpEnvelope->dwFadeTime / 1000;
584 env->fade_level = (peff->lpEnvelope->dwFadeLevel / 10) * 32;
586 /* if the dinput envelope is NULL we will clear the linux envelope */
587 else if (env)
589 env->attack_length = 0;
590 env->attack_level = 0;
591 env->fade_length = 0;
592 env->fade_level = 0;
594 else if(peff->lpEnvelope)
596 if(peff->lpEnvelope->dwAttackTime || peff->lpEnvelope->dwAttackLevel ||
597 peff->lpEnvelope->dwFadeTime || peff->lpEnvelope->dwFadeLevel)
598 WARN("Ignoring dinput envelope not supported in the linux effect\n");
602 /* Gain and Sample Period settings are not supported by the linux
603 * event system */
604 if (dwFlags & DIEP_GAIN) {
605 This->gain = 0xFFFF * peff->dwGain / 10000;
606 TRACE("Effect gain requested but no effect gain functionality present.\n");
609 if (dwFlags & DIEP_SAMPLEPERIOD)
610 TRACE("Sample period requested but no sample period functionality present.\n");
612 if (dwFlags & DIEP_STARTDELAY)
613 if ((dwFlags & DIEP_STARTDELAY) && peff->dwSize > sizeof(DIEFFECT_DX5))
614 This->effect.replay.delay = peff->dwStartDelay / 1000;
616 if (dwFlags & DIEP_TRIGGERBUTTON) {
617 if (peff->dwTriggerButton != -1) {
618 FIXME("Linuxinput button mapping needs redoing, assuming we're using a joystick.\n");
619 FIXME("Trigger button translation not yet implemented!\n");
621 This->effect.trigger.button = 0;
624 if (dwFlags & DIEP_TRIGGERREPEATINTERVAL)
625 This->effect.trigger.interval = peff->dwTriggerRepeatInterval / 1000;
627 if (dwFlags & DIEP_TYPESPECIFICPARAMS)
629 if (!(peff->lpvTypeSpecificParams))
630 return DIERR_INCOMPLETEEFFECT;
632 if (type == DIEFT_PERIODIC)
634 DIPERIODIC *tsp;
635 if (peff->cbTypeSpecificParams != sizeof(DIPERIODIC))
636 return DIERR_INVALIDPARAM;
637 tsp = peff->lpvTypeSpecificParams;
639 This->effect.u.periodic.magnitude = (tsp->dwMagnitude / 10) * 32;
640 This->effect.u.periodic.offset = (tsp->lOffset / 10) * 32;
641 /* phase ranges from 0 - 35999 in dinput and 0 - 65535 on Linux */
642 This->effect.u.periodic.phase = (tsp->dwPhase / 36) * 65;
643 /* dinput uses microseconds, Linux uses milliseconds */
644 if (tsp->dwPeriod <= 1000)
645 This->effect.u.periodic.period = 1;
646 else
647 This->effect.u.periodic.period = tsp->dwPeriod / 1000;
649 else if (type == DIEFT_CONSTANTFORCE)
651 LPCDICONSTANTFORCE tsp;
652 if (peff->cbTypeSpecificParams != sizeof(DICONSTANTFORCE))
653 return DIERR_INVALIDPARAM;
654 tsp = peff->lpvTypeSpecificParams;
655 This->effect.u.constant.level = (max(min(tsp->lMagnitude, 10000), -10000) / 10) * 32;
656 } else if (type == DIEFT_RAMPFORCE) {
657 LPCDIRAMPFORCE tsp;
658 if (peff->cbTypeSpecificParams != sizeof(DIRAMPFORCE))
659 return DIERR_INVALIDPARAM;
660 tsp = peff->lpvTypeSpecificParams;
661 This->effect.u.ramp.start_level = (tsp->lStart / 10) * 32;
662 This->effect.u.ramp.end_level = (tsp->lEnd / 10) * 32;
664 else if (type == DIEFT_CONDITION)
666 DICONDITION *tsp = peff->lpvTypeSpecificParams;
667 struct ff_condition_effect *cond = This->effect.u.condition;
668 int i, j, sources;
669 double factor[2];
671 if (peff->cbTypeSpecificParams == sizeof(DICONDITION))
673 /* One condition block. This needs to be rotated to direction,
674 * and expanded to separate x and y conditions. Ensures 0 points right */
675 double angle = ff_effect_direction_to_rad(This->effect.direction + 0xc000);
676 factor[0] = sin(angle);
677 factor[1] = -cos(angle);
678 sources = 1;
680 else if (peff->cbTypeSpecificParams == 2 * sizeof(DICONDITION))
682 /* Direct parameter copy without changes */
683 factor[0] = factor[1] = 1;
684 sources = 2;
686 else
687 return DIERR_INVALIDPARAM;
689 for (i = j = 0; i < 2; ++i)
691 cond[i].center = (int)(factor[i] * (tsp[j].lOffset / 10) * 32);
692 cond[i].right_coeff = (int)(factor[i] * (tsp[j].lPositiveCoefficient / 10) * 32);
693 cond[i].left_coeff = (int)(factor[i] * (tsp[j].lNegativeCoefficient / 10) * 32);
694 cond[i].right_saturation = (int)(factor[i] * (tsp[j].dwPositiveSaturation / 10) * 65);
695 cond[i].left_saturation = (int)(factor[i] * (tsp[j].dwNegativeSaturation / 10) * 65);
696 cond[i].deadband = (int)(factor[i] * (tsp[j].lDeadBand / 10) * 32);
697 if (sources == 2)
698 j++;
701 else
703 FIXME("Custom force types are not supported\n");
704 return DIERR_INVALIDPARAM;
708 if (!(dwFlags & DIEP_NODOWNLOAD))
709 retval = LinuxInputEffectImpl_Download(iface);
710 if (retval != DI_OK)
711 return DI_DOWNLOADSKIPPED;
713 if (dwFlags & DIEP_NORESTART)
714 TRACE("DIEP_NORESTART: not handled (we have no control of that).\n");
716 if (dwFlags & DIEP_START)
717 retval = LinuxInputEffectImpl_Start(iface, 1, 0);
718 if (retval != DI_OK)
719 return retval;
721 return DI_OK;
724 static HRESULT WINAPI LinuxInputEffectImpl_Stop(
725 LPDIRECTINPUTEFFECT iface)
727 struct input_event event;
728 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
730 TRACE("(this=%p)\n", This);
732 event.type = EV_FF;
733 event.code = This->effect.id;
734 event.value = 0;
735 /* we don't care about the success or failure of this call */
736 write(*(This->fd), &event, sizeof(event));
738 return DI_OK;
741 static HRESULT WINAPI LinuxInputEffectImpl_Unload(
742 LPDIRECTINPUTEFFECT iface)
744 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
745 TRACE("(this=%p)\n", This);
747 /* Erase the downloaded effect */
748 if (ioctl(*(This->fd), EVIOCRMFF, This->effect.id) == -1)
749 return DIERR_INVALIDPARAM;
751 /* Mark the effect as deallocated */
752 This->effect.id = -1;
754 return DI_OK;
757 static ULONG WINAPI LinuxInputEffectImpl_Release(LPDIRECTINPUTEFFECT iface)
759 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
760 ULONG ref = InterlockedDecrement(&(This->ref));
762 if (ref == 0)
764 LinuxInputEffectImpl_Stop(iface);
765 LinuxInputEffectImpl_Unload(iface);
766 list_remove(This->entry);
767 HeapFree(GetProcessHeap(), 0, LIST_ENTRY(This->entry, effect_list_item, entry));
768 HeapFree(GetProcessHeap(), 0, This);
770 return ref;
773 /******************************************************************************
774 * LinuxInputEffect
777 DECLSPEC_HIDDEN HRESULT linuxinput_create_effect(
778 int* fd,
779 REFGUID rguid,
780 struct list *parent_list_entry,
781 LPDIRECTINPUTEFFECT* peff)
783 LinuxInputEffectImpl* newEffect = HeapAlloc(GetProcessHeap(),
784 HEAP_ZERO_MEMORY, sizeof(LinuxInputEffectImpl));
785 DWORD type = typeFromGUID(rguid);
787 newEffect->IDirectInputEffect_iface.lpVtbl = &LinuxInputEffectVtbl;
788 newEffect->ref = 1;
789 newEffect->guid = *rguid;
790 newEffect->fd = fd;
791 newEffect->gain = 0xFFFF;
793 /* set the type. this cannot be changed over the effect's life. */
794 switch (type) {
795 case DIEFT_PERIODIC:
796 newEffect->effect.type = FF_PERIODIC;
797 if (IsEqualGUID(rguid, &GUID_Sine)) {
798 newEffect->effect.u.periodic.waveform = FF_SINE;
799 } else if (IsEqualGUID(rguid, &GUID_Triangle)) {
800 newEffect->effect.u.periodic.waveform = FF_TRIANGLE;
801 } else if (IsEqualGUID(rguid, &GUID_Square)) {
802 newEffect->effect.u.periodic.waveform = FF_SQUARE;
803 } else if (IsEqualGUID(rguid, &GUID_SawtoothUp)) {
804 newEffect->effect.u.periodic.waveform = FF_SAW_UP;
805 } else if (IsEqualGUID(rguid, &GUID_SawtoothDown)) {
806 newEffect->effect.u.periodic.waveform = FF_SAW_DOWN;
808 break;
809 case DIEFT_CONSTANTFORCE:
810 newEffect->effect.type = FF_CONSTANT;
811 break;
812 case DIEFT_RAMPFORCE:
813 newEffect->effect.type = FF_RAMP;
814 break;
815 case DIEFT_CONDITION:
816 if (IsEqualGUID(rguid, &GUID_Spring)) {
817 newEffect->effect.type = FF_SPRING;
818 } else if (IsEqualGUID(rguid, &GUID_Friction)) {
819 newEffect->effect.type = FF_FRICTION;
820 } else if (IsEqualGUID(rguid, &GUID_Inertia)) {
821 newEffect->effect.type = FF_INERTIA;
822 } else if (IsEqualGUID(rguid, &GUID_Damper)) {
823 newEffect->effect.type = FF_DAMPER;
825 break;
826 case DIEFT_CUSTOMFORCE:
827 FIXME("Custom forces are not supported.\n");
828 HeapFree(GetProcessHeap(), 0, newEffect);
829 return DIERR_INVALIDPARAM;
830 default:
831 FIXME("Unknown force type 0x%x.\n", type);
832 HeapFree(GetProcessHeap(), 0, newEffect);
833 return DIERR_INVALIDPARAM;
836 /* mark as non-uploaded */
837 newEffect->effect.id = -1;
839 newEffect->entry = parent_list_entry;
841 *peff = &newEffect->IDirectInputEffect_iface;
843 TRACE("Creating linux input system effect (%p) with guid %s\n",
844 *peff, _dump_dinput_GUID(rguid));
846 return DI_OK;
849 DECLSPEC_HIDDEN HRESULT linuxinput_get_info_A(
850 int fd,
851 REFGUID rguid,
852 LPDIEFFECTINFOA info)
854 DWORD type = typeFromGUID(rguid);
856 TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
858 if (!info) return E_POINTER;
860 if (info->dwSize != sizeof(DIEFFECTINFOA)) return DIERR_INVALIDPARAM;
862 info->guid = *rguid;
864 info->dwEffType = type;
865 /* the event device API does not support querying for all these things
866 * therefore we assume that we have support for them
867 * that's not as dangerous as it sounds, since drivers are allowed to
868 * ignore parameters they claim to support anyway */
869 info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE
870 | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
871 | DIEFT_SATURATION | DIEFT_STARTDELAY;
873 /* again, assume we have support for everything */
874 info->dwStaticParams = DIEP_ALLPARAMS;
875 info->dwDynamicParams = info->dwStaticParams;
877 /* yes, this is windows behavior (print the GUID_Name for name) */
878 strcpy(info->tszName, _dump_dinput_GUID(rguid));
880 return DI_OK;
883 DECLSPEC_HIDDEN HRESULT linuxinput_get_info_W(
884 int fd,
885 REFGUID rguid,
886 LPDIEFFECTINFOW info)
888 DWORD type = typeFromGUID(rguid);
890 TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
892 if (!info) return E_POINTER;
894 if (info->dwSize != sizeof(DIEFFECTINFOW)) return DIERR_INVALIDPARAM;
896 info->guid = *rguid;
898 info->dwEffType = type;
899 /* the event device API does not support querying for all these things
900 * therefore we assume that we have support for them
901 * that's not as dangerous as it sounds, since drivers are allowed to
902 * ignore parameters they claim to support anyway */
903 info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE
904 | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
905 | DIEFT_SATURATION | DIEFT_STARTDELAY;
907 /* again, assume we have support for everything */
908 info->dwStaticParams = DIEP_ALLPARAMS;
909 info->dwDynamicParams = info->dwStaticParams;
911 /* yes, this is windows behavior (print the GUID_Name for name) */
912 MultiByteToWideChar(CP_ACP, 0, _dump_dinput_GUID(rguid), -1,
913 info->tszName, MAX_PATH);
915 return DI_OK;
918 static const IDirectInputEffectVtbl LinuxInputEffectVtbl = {
919 LinuxInputEffectImpl_QueryInterface,
920 LinuxInputEffectImpl_AddRef,
921 LinuxInputEffectImpl_Release,
922 LinuxInputEffectImpl_Initialize,
923 LinuxInputEffectImpl_GetEffectGuid,
924 LinuxInputEffectImpl_GetParameters,
925 LinuxInputEffectImpl_SetParameters,
926 LinuxInputEffectImpl_Start,
927 LinuxInputEffectImpl_Stop,
928 LinuxInputEffectImpl_GetEffectStatus,
929 LinuxInputEffectImpl_Download,
930 LinuxInputEffectImpl_Unload,
931 LinuxInputEffectImpl_Escape
934 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */