dinput: Spelling and case fixes in comments.
[wine.git] / dlls / dinput / effect_linuxinput.c
blob0fd16ed66da75867a16712b410b5f0f3fd611a46
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 TRACE("(this=%p,%p)\n", iface, pdwFlags);
239 if (!pdwFlags)
240 return E_POINTER;
242 /* linux sends the effect status through an event.
243 * that event is trapped by our parent joystick driver
244 * and there is no clean way to pass it back to us. */
245 FIXME("Not enough information to provide a status.\n");
247 (*pdwFlags) = 0;
249 return DI_OK;
252 static HRESULT WINAPI LinuxInputEffectImpl_GetParameters(
253 LPDIRECTINPUTEFFECT iface,
254 LPDIEFFECT peff,
255 DWORD dwFlags)
257 HRESULT diErr = DI_OK;
258 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
259 TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
261 /* Major conversion factors are:
262 * times: millisecond (linux) -> microsecond (windows) (x * 1000)
263 * forces: scale 0x7FFF (linux) -> scale 10000 (windows) approx ((x / 33) * 10)
264 * angles: scale 0x7FFF (linux) -> scale 35999 (windows) approx ((x / 33) * 36)
265 * angle bases: 0 -> -y (down) (linux) -> 0 -> +x (right) (windows)
268 if (dwFlags & DIEP_AXES) {
269 if (peff->cAxes < 2 /* linuxinput effects always use 2 axes, x and y */)
270 diErr = DIERR_MOREDATA;
271 peff->cAxes = 2;
272 if (diErr)
273 return diErr;
274 else {
275 peff->rgdwAxes[0] = DIJOFS_X;
276 peff->rgdwAxes[1] = DIJOFS_Y;
280 if (dwFlags & DIEP_DIRECTION) {
281 if (peff->cAxes < 2)
282 diErr = DIERR_MOREDATA;
283 peff->cAxes = 2;
284 if (diErr)
285 return diErr;
286 else {
287 if (peff->dwFlags & DIEFF_CARTESIAN) {
288 /* rotate so 0 points right */
289 double angle = ff_effect_direction_to_rad(This->effect.direction + 0xc000);
290 peff->rglDirection[0] = sin(angle) * 1000;
291 peff->rglDirection[1] = -cos(angle) * 1000;
292 } else {
293 /* Polar and spherical coordinates are the same for two or less
294 * axes.
295 * Note that we also use this case if NO flags are marked.
296 * According to MSDN, we should return the direction in the
297 * format that it was specified in, if no flags are marked.
299 peff->rglDirection[0] = (This->effect.direction / 33) * 36 + 9000;
300 if (peff->rglDirection[0] > 35999)
301 peff->rglDirection[0] -= 35999;
306 if (dwFlags & DIEP_DURATION)
308 if (!This->effect.replay.length) /* infinite for the linux driver */
309 peff->dwDuration = INFINITE;
310 else
311 peff->dwDuration = (DWORD)This->effect.replay.length * 1000;
314 if (dwFlags & DIEP_ENVELOPE) {
315 struct ff_envelope* env;
316 if (This->effect.type == FF_CONSTANT) env = &This->effect.u.constant.envelope;
317 else if (This->effect.type == FF_PERIODIC) env = &This->effect.u.periodic.envelope;
318 else if (This->effect.type == FF_RAMP) env = &This->effect.u.ramp.envelope;
319 else env = NULL;
320 if (env == NULL) {
321 peff->lpEnvelope = NULL;
322 } else if (peff->lpEnvelope == NULL) {
323 return DIERR_INVALIDPARAM;
324 } else {
325 peff->lpEnvelope->dwAttackLevel = (env->attack_level / 33) * 10;
326 peff->lpEnvelope->dwAttackTime = env->attack_length * 1000;
327 peff->lpEnvelope->dwFadeLevel = (env->fade_level / 33) * 10;
328 peff->lpEnvelope->dwFadeTime = env->fade_length * 1000;
332 if (dwFlags & DIEP_GAIN) {
333 peff->dwGain = This->gain * 10000 / 0xFFFF;
336 if (dwFlags & DIEP_SAMPLEPERIOD) {
337 /* the linux input ff driver has no support for setting
338 * the playback sample period. 0 means default. */
339 peff->dwSamplePeriod = 0;
342 if ((dwFlags & DIEP_STARTDELAY) && peff->dwSize > sizeof(DIEFFECT_DX5))
343 peff->dwStartDelay = This->effect.replay.delay * 1000;
345 if (dwFlags & DIEP_TRIGGERBUTTON) {
346 FIXME("LinuxInput button mapping needs redoing; for now, assuming we're using an actual joystick.\n");
347 peff->dwTriggerButton = DIJOFS_BUTTON(This->effect.trigger.button - BTN_JOYSTICK);
350 if (dwFlags & DIEP_TRIGGERREPEATINTERVAL) {
351 peff->dwTriggerRepeatInterval = This->effect.trigger.interval * 1000;
354 if (dwFlags & DIEP_TYPESPECIFICPARAMS) {
355 DWORD expectedsize = 0;
356 if (This->effect.type == FF_PERIODIC) {
357 expectedsize = sizeof(DIPERIODIC);
358 } else if (This->effect.type == FF_CONSTANT) {
359 expectedsize = sizeof(DICONSTANTFORCE);
360 } else if (This->effect.type == FF_SPRING
361 || This->effect.type == FF_FRICTION
362 || This->effect.type == FF_INERTIA
363 || This->effect.type == FF_DAMPER) {
364 expectedsize = sizeof(DICONDITION) * 2;
365 } else if (This->effect.type == FF_RAMP) {
366 expectedsize = sizeof(DIRAMPFORCE);
368 if (expectedsize > peff->cbTypeSpecificParams)
369 diErr = DIERR_MOREDATA;
370 peff->cbTypeSpecificParams = expectedsize;
371 if (diErr)
372 return diErr;
373 else {
374 if (This->effect.type == FF_PERIODIC) {
375 LPDIPERIODIC tsp = peff->lpvTypeSpecificParams;
376 tsp->dwMagnitude = (This->effect.u.periodic.magnitude / 33) * 10;
377 tsp->lOffset = (This->effect.u.periodic.offset / 33) * 10;
378 tsp->dwPhase = (This->effect.u.periodic.phase / 33) * 36;
379 tsp->dwPeriod = (This->effect.u.periodic.period * 1000);
380 } else if (This->effect.type == FF_CONSTANT) {
381 LPDICONSTANTFORCE tsp = peff->lpvTypeSpecificParams;
382 tsp->lMagnitude = (This->effect.u.constant.level / 33) * 10;
383 } else if (This->effect.type == FF_SPRING
384 || This->effect.type == FF_FRICTION
385 || This->effect.type == FF_INERTIA
386 || This->effect.type == FF_DAMPER) {
387 LPDICONDITION tsp = peff->lpvTypeSpecificParams;
388 int i;
389 for (i = 0; i < 2; ++i) {
390 tsp[i].lOffset = (This->effect.u.condition[i].center / 33) * 10;
391 tsp[i].lPositiveCoefficient = (This->effect.u.condition[i].right_coeff / 33) * 10;
392 tsp[i].lNegativeCoefficient = (This->effect.u.condition[i].left_coeff / 33) * 10;
393 tsp[i].dwPositiveSaturation = (This->effect.u.condition[i].right_saturation / 33) * 10;
394 tsp[i].dwNegativeSaturation = (This->effect.u.condition[i].left_saturation / 33) * 10;
395 tsp[i].lDeadBand = (This->effect.u.condition[i].deadband / 33) * 10;
397 } else if (This->effect.type == FF_RAMP) {
398 LPDIRAMPFORCE tsp = peff->lpvTypeSpecificParams;
399 tsp->lStart = (This->effect.u.ramp.start_level / 33) * 10;
400 tsp->lEnd = (This->effect.u.ramp.end_level / 33) * 10;
405 return diErr;
408 static HRESULT WINAPI LinuxInputEffectImpl_Initialize(
409 LPDIRECTINPUTEFFECT iface,
410 HINSTANCE hinst,
411 DWORD dwVersion,
412 REFGUID rguid)
414 FIXME("(this=%p,%p,%d,%s): stub!\n",
415 iface, hinst, dwVersion, debugstr_guid(rguid));
417 return DI_OK;
420 static HRESULT WINAPI LinuxInputEffectImpl_QueryInterface(
421 LPDIRECTINPUTEFFECT iface,
422 REFIID riid,
423 void **ppvObject)
425 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
427 TRACE("(this=%p,%s,%p)\n", This, debugstr_guid(riid), ppvObject);
429 if (IsEqualGUID(&IID_IUnknown, riid) ||
430 IsEqualGUID(&IID_IDirectInputEffect, riid)) {
431 LinuxInputEffectImpl_AddRef(iface);
432 *ppvObject = This;
433 return 0;
436 TRACE("Unsupported interface!\n");
437 return E_FAIL;
440 static HRESULT WINAPI LinuxInputEffectImpl_Start(
441 LPDIRECTINPUTEFFECT iface,
442 DWORD dwIterations,
443 DWORD dwFlags)
445 struct input_event event;
446 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
448 TRACE("(this=%p,%d,%d)\n", This, dwIterations, dwFlags);
450 if (!(dwFlags & DIES_NODOWNLOAD)) {
451 /* Download the effect if necessary */
452 if (This->effect.id == -1) {
453 HRESULT res = LinuxInputEffectImpl_Download(iface);
454 if (res != DI_OK)
455 return res;
459 if (dwFlags & DIES_SOLO) {
460 FIXME("Solo mode requested: should be stopping all effects here!\n");
463 event.type = EV_FF;
464 event.code = This->effect.id;
465 event.value = min( dwIterations, INT_MAX );
466 if (write(*(This->fd), &event, sizeof(event)) == -1) {
467 FIXME("Unable to write event. Assuming device disconnected.\n");
468 return DIERR_INPUTLOST;
471 return DI_OK;
474 static HRESULT WINAPI LinuxInputEffectImpl_SetParameters(
475 LPDIRECTINPUTEFFECT iface,
476 LPCDIEFFECT peff,
477 DWORD dwFlags)
479 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
480 DWORD type = typeFromGUID(&This->guid);
481 HRESULT retval = DI_OK;
483 TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
485 dump_DIEFFECT(peff, &This->guid, dwFlags);
487 if ((dwFlags & ~DIEP_NORESTART & ~DIEP_NODOWNLOAD & ~DIEP_START) == 0) {
488 /* set everything */
489 dwFlags = DIEP_AXES | DIEP_DIRECTION | DIEP_DURATION | DIEP_ENVELOPE |
490 DIEP_GAIN | DIEP_SAMPLEPERIOD | DIEP_STARTDELAY | DIEP_TRIGGERBUTTON |
491 DIEP_TRIGGERREPEATINTERVAL | DIEP_TYPESPECIFICPARAMS;
494 if (dwFlags & DIEP_AXES) {
495 /* the linux input effect system only supports one or two axes */
496 if (peff->cAxes > 2)
497 return DIERR_INVALIDPARAM;
498 else if (peff->cAxes < 1)
499 return DIERR_INCOMPLETEEFFECT;
500 This->first_axis_is_x = peff->rgdwAxes[0] == DIJOFS_X;
503 /* some of this may look funky, but it's 'cause the linux driver and directx have
504 * different opinions about which way direction "0" is. directx has 0 along the x
505 * axis (left), linux has it along the y axis (down). */
506 if (dwFlags & DIEP_DIRECTION) {
507 if (peff->cAxes == 1) {
508 if (peff->dwFlags & DIEFF_CARTESIAN) {
509 if (dwFlags & DIEP_AXES) {
510 if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] >= 0)
511 This->effect.direction = 0x4000;
512 else if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] < 0)
513 This->effect.direction = 0xC000;
514 else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] >= 0)
515 This->effect.direction = 0;
516 else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] < 0)
517 This->effect.direction = 0x8000;
519 } else {
520 /* one-axis effects must use cartesian coords */
521 return DIERR_INVALIDPARAM;
524 /* two axes */
525 else
527 if (peff->dwFlags & DIEFF_CARTESIAN)
529 LONG x, y;
530 if (This->first_axis_is_x)
532 x = peff->rglDirection[0];
533 y = peff->rglDirection[1];
535 else
537 x = peff->rglDirection[1];
538 y = peff->rglDirection[0];
540 This->effect.direction = (unsigned int)((M_PI / 2 + atan2(y, x)) * 0x8000 / M_PI);
542 else
544 /* Polar and spherical are the same for 2 axes */
545 /* Precision is important here, so we do double math with exact constants */
546 This->effect.direction = (unsigned int)(((double)peff->rglDirection[0] / 18000) * 0x8000);
551 if (dwFlags & DIEP_DURATION)
553 if (peff->dwDuration == INFINITE)
554 This->effect.replay.length = 0; /* infinite for the linux driver */
555 else if(peff->dwDuration > 1000)
556 This->effect.replay.length = peff->dwDuration / 1000;
557 else
558 This->effect.replay.length = 1;
561 if (dwFlags & DIEP_ENVELOPE)
563 struct ff_envelope* env;
564 if (This->effect.type == FF_CONSTANT)
565 env = &This->effect.u.constant.envelope;
566 else if (This->effect.type == FF_PERIODIC)
567 env = &This->effect.u.periodic.envelope;
568 else if (This->effect.type == FF_RAMP)
569 env = &This->effect.u.ramp.envelope;
570 else
571 env = NULL;
573 /* copy the envelope if it is present and the linux effect supports it */
574 if (peff->lpEnvelope && env)
576 env->attack_length = peff->lpEnvelope->dwAttackTime / 1000;
577 env->attack_level = (peff->lpEnvelope->dwAttackLevel / 10) * 32;
578 env->fade_length = peff->lpEnvelope->dwFadeTime / 1000;
579 env->fade_level = (peff->lpEnvelope->dwFadeLevel / 10) * 32;
581 /* if the dinput envelope is NULL we will clear the linux envelope */
582 else if (env)
584 env->attack_length = 0;
585 env->attack_level = 0;
586 env->fade_length = 0;
587 env->fade_level = 0;
589 else if(peff->lpEnvelope)
591 if(peff->lpEnvelope->dwAttackTime || peff->lpEnvelope->dwAttackLevel ||
592 peff->lpEnvelope->dwFadeTime || peff->lpEnvelope->dwFadeLevel)
593 WARN("Ignoring dinput envelope not supported in the linux effect\n");
597 /* Gain and Sample Period settings are not supported by the linux
598 * event system */
599 if (dwFlags & DIEP_GAIN) {
600 This->gain = 0xFFFF * peff->dwGain / 10000;
601 TRACE("Effect gain requested but no effect gain functionality present.\n");
604 if (dwFlags & DIEP_SAMPLEPERIOD)
605 TRACE("Sample period requested but no sample period functionality present.\n");
607 if (dwFlags & DIEP_STARTDELAY)
608 if ((dwFlags & DIEP_STARTDELAY) && peff->dwSize > sizeof(DIEFFECT_DX5))
609 This->effect.replay.delay = peff->dwStartDelay / 1000;
611 if (dwFlags & DIEP_TRIGGERBUTTON) {
612 if (peff->dwTriggerButton != -1) {
613 FIXME("Linuxinput button mapping needs redoing, assuming we're using a joystick.\n");
614 FIXME("Trigger button translation not yet implemented!\n");
616 This->effect.trigger.button = 0;
619 if (dwFlags & DIEP_TRIGGERREPEATINTERVAL)
620 This->effect.trigger.interval = peff->dwTriggerRepeatInterval / 1000;
622 if (dwFlags & DIEP_TYPESPECIFICPARAMS)
624 if (!(peff->lpvTypeSpecificParams))
625 return DIERR_INCOMPLETEEFFECT;
627 if (type == DIEFT_PERIODIC)
629 DIPERIODIC *tsp;
630 if (peff->cbTypeSpecificParams != sizeof(DIPERIODIC))
631 return DIERR_INVALIDPARAM;
632 tsp = peff->lpvTypeSpecificParams;
634 This->effect.u.periodic.magnitude = (tsp->dwMagnitude / 10) * 32;
635 This->effect.u.periodic.offset = (tsp->lOffset / 10) * 32;
636 /* phase ranges from 0 - 35999 in dinput and 0 - 65535 on Linux */
637 This->effect.u.periodic.phase = (tsp->dwPhase / 36) * 65;
638 /* dinput uses microseconds, Linux uses milliseconds */
639 if (tsp->dwPeriod <= 1000)
640 This->effect.u.periodic.period = 1;
641 else
642 This->effect.u.periodic.period = tsp->dwPeriod / 1000;
644 else if (type == DIEFT_CONSTANTFORCE)
646 LPCDICONSTANTFORCE tsp;
647 if (peff->cbTypeSpecificParams != sizeof(DICONSTANTFORCE))
648 return DIERR_INVALIDPARAM;
649 tsp = peff->lpvTypeSpecificParams;
650 This->effect.u.constant.level = (max(min(tsp->lMagnitude, 10000), -10000) / 10) * 32;
651 } else if (type == DIEFT_RAMPFORCE) {
652 LPCDIRAMPFORCE tsp;
653 if (peff->cbTypeSpecificParams != sizeof(DIRAMPFORCE))
654 return DIERR_INVALIDPARAM;
655 tsp = peff->lpvTypeSpecificParams;
656 This->effect.u.ramp.start_level = (tsp->lStart / 10) * 32;
657 This->effect.u.ramp.end_level = (tsp->lEnd / 10) * 32;
659 else if (type == DIEFT_CONDITION)
661 DICONDITION *tsp = peff->lpvTypeSpecificParams;
662 struct ff_condition_effect *cond = This->effect.u.condition;
663 int i, j, sources;
664 double factor[2];
666 if (peff->cbTypeSpecificParams == sizeof(DICONDITION))
668 /* One condition block. This needs to be rotated to direction,
669 * and expanded to separate x and y conditions. Ensures 0 points right */
670 double angle = ff_effect_direction_to_rad(This->effect.direction + 0xc000);
671 factor[0] = sin(angle);
672 factor[1] = -cos(angle);
673 sources = 1;
675 else if (peff->cbTypeSpecificParams == 2 * sizeof(DICONDITION))
677 /* Direct parameter copy without changes */
678 factor[0] = factor[1] = 1;
679 sources = 2;
681 else
682 return DIERR_INVALIDPARAM;
684 for (i = j = 0; i < 2; ++i)
686 cond[i].center = (int)(factor[i] * (tsp[j].lOffset / 10) * 32);
687 cond[i].right_coeff = (int)(factor[i] * (tsp[j].lPositiveCoefficient / 10) * 32);
688 cond[i].left_coeff = (int)(factor[i] * (tsp[j].lNegativeCoefficient / 10) * 32);
689 cond[i].right_saturation = (int)(factor[i] * (tsp[j].dwPositiveSaturation / 10) * 65);
690 cond[i].left_saturation = (int)(factor[i] * (tsp[j].dwNegativeSaturation / 10) * 65);
691 cond[i].deadband = (int)(factor[i] * (tsp[j].lDeadBand / 10) * 32);
692 if (sources == 2)
693 j++;
696 else
698 FIXME("Custom force types are not supported\n");
699 return DIERR_INVALIDPARAM;
703 if (!(dwFlags & DIEP_NODOWNLOAD))
704 retval = LinuxInputEffectImpl_Download(iface);
705 if (retval != DI_OK)
706 return DI_DOWNLOADSKIPPED;
708 if (dwFlags & DIEP_NORESTART)
709 TRACE("DIEP_NORESTART: not handled (we have no control of that).\n");
711 if (dwFlags & DIEP_START)
712 retval = LinuxInputEffectImpl_Start(iface, 1, 0);
713 if (retval != DI_OK)
714 return retval;
716 return DI_OK;
719 static HRESULT WINAPI LinuxInputEffectImpl_Stop(
720 LPDIRECTINPUTEFFECT iface)
722 struct input_event event;
723 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
725 TRACE("(this=%p)\n", This);
727 event.type = EV_FF;
728 event.code = This->effect.id;
729 event.value = 0;
730 /* we don't care about the success or failure of this call */
731 write(*(This->fd), &event, sizeof(event));
733 return DI_OK;
736 static HRESULT WINAPI LinuxInputEffectImpl_Unload(
737 LPDIRECTINPUTEFFECT iface)
739 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
740 TRACE("(this=%p)\n", This);
742 /* Erase the downloaded effect */
743 if (ioctl(*(This->fd), EVIOCRMFF, This->effect.id) == -1)
744 return DIERR_INVALIDPARAM;
746 /* Mark the effect as deallocated */
747 This->effect.id = -1;
749 return DI_OK;
752 static ULONG WINAPI LinuxInputEffectImpl_Release(LPDIRECTINPUTEFFECT iface)
754 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
755 ULONG ref = InterlockedDecrement(&(This->ref));
757 if (ref == 0)
759 LinuxInputEffectImpl_Stop(iface);
760 LinuxInputEffectImpl_Unload(iface);
761 list_remove(This->entry);
762 HeapFree(GetProcessHeap(), 0, LIST_ENTRY(This->entry, effect_list_item, entry));
763 HeapFree(GetProcessHeap(), 0, This);
765 return ref;
768 /******************************************************************************
769 * LinuxInputEffect
772 DECLSPEC_HIDDEN HRESULT linuxinput_create_effect(
773 int* fd,
774 REFGUID rguid,
775 struct list *parent_list_entry,
776 LPDIRECTINPUTEFFECT* peff)
778 LinuxInputEffectImpl* newEffect = HeapAlloc(GetProcessHeap(),
779 HEAP_ZERO_MEMORY, sizeof(LinuxInputEffectImpl));
780 DWORD type = typeFromGUID(rguid);
782 newEffect->IDirectInputEffect_iface.lpVtbl = &LinuxInputEffectVtbl;
783 newEffect->ref = 1;
784 newEffect->guid = *rguid;
785 newEffect->fd = fd;
786 newEffect->gain = 0xFFFF;
788 /* set the type. this cannot be changed over the effect's life. */
789 switch (type) {
790 case DIEFT_PERIODIC:
791 newEffect->effect.type = FF_PERIODIC;
792 if (IsEqualGUID(rguid, &GUID_Sine)) {
793 newEffect->effect.u.periodic.waveform = FF_SINE;
794 } else if (IsEqualGUID(rguid, &GUID_Triangle)) {
795 newEffect->effect.u.periodic.waveform = FF_TRIANGLE;
796 } else if (IsEqualGUID(rguid, &GUID_Square)) {
797 newEffect->effect.u.periodic.waveform = FF_SQUARE;
798 } else if (IsEqualGUID(rguid, &GUID_SawtoothUp)) {
799 newEffect->effect.u.periodic.waveform = FF_SAW_UP;
800 } else if (IsEqualGUID(rguid, &GUID_SawtoothDown)) {
801 newEffect->effect.u.periodic.waveform = FF_SAW_DOWN;
803 break;
804 case DIEFT_CONSTANTFORCE:
805 newEffect->effect.type = FF_CONSTANT;
806 break;
807 case DIEFT_RAMPFORCE:
808 newEffect->effect.type = FF_RAMP;
809 break;
810 case DIEFT_CONDITION:
811 if (IsEqualGUID(rguid, &GUID_Spring)) {
812 newEffect->effect.type = FF_SPRING;
813 } else if (IsEqualGUID(rguid, &GUID_Friction)) {
814 newEffect->effect.type = FF_FRICTION;
815 } else if (IsEqualGUID(rguid, &GUID_Inertia)) {
816 newEffect->effect.type = FF_INERTIA;
817 } else if (IsEqualGUID(rguid, &GUID_Damper)) {
818 newEffect->effect.type = FF_DAMPER;
820 break;
821 case DIEFT_CUSTOMFORCE:
822 FIXME("Custom forces are not supported.\n");
823 HeapFree(GetProcessHeap(), 0, newEffect);
824 return DIERR_INVALIDPARAM;
825 default:
826 FIXME("Unknown force type 0x%x.\n", type);
827 HeapFree(GetProcessHeap(), 0, newEffect);
828 return DIERR_INVALIDPARAM;
831 /* mark as non-uploaded */
832 newEffect->effect.id = -1;
834 newEffect->entry = parent_list_entry;
836 *peff = &newEffect->IDirectInputEffect_iface;
838 TRACE("Creating linux input system effect (%p) with guid %s\n",
839 *peff, _dump_dinput_GUID(rguid));
841 return DI_OK;
844 DECLSPEC_HIDDEN HRESULT linuxinput_get_info_A(
845 int fd,
846 REFGUID rguid,
847 LPDIEFFECTINFOA info)
849 DWORD type = typeFromGUID(rguid);
851 TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
853 if (!info) return E_POINTER;
855 if (info->dwSize != sizeof(DIEFFECTINFOA)) return DIERR_INVALIDPARAM;
857 info->guid = *rguid;
859 info->dwEffType = type;
860 /* the event device API does not support querying for all these things
861 * therefore we assume that we have support for them
862 * that's not as dangerous as it sounds, since drivers are allowed to
863 * ignore parameters they claim to support anyway */
864 info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE
865 | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
866 | DIEFT_SATURATION | DIEFT_STARTDELAY;
868 /* again, assume we have support for everything */
869 info->dwStaticParams = DIEP_ALLPARAMS;
870 info->dwDynamicParams = info->dwStaticParams;
872 /* yes, this is windows behavior (print the GUID_Name for name) */
873 strcpy(info->tszName, _dump_dinput_GUID(rguid));
875 return DI_OK;
878 DECLSPEC_HIDDEN HRESULT linuxinput_get_info_W(
879 int fd,
880 REFGUID rguid,
881 LPDIEFFECTINFOW info)
883 DWORD type = typeFromGUID(rguid);
885 TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
887 if (!info) return E_POINTER;
889 if (info->dwSize != sizeof(DIEFFECTINFOW)) return DIERR_INVALIDPARAM;
891 info->guid = *rguid;
893 info->dwEffType = type;
894 /* the event device API does not support querying for all these things
895 * therefore we assume that we have support for them
896 * that's not as dangerous as it sounds, since drivers are allowed to
897 * ignore parameters they claim to support anyway */
898 info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE
899 | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
900 | DIEFT_SATURATION | DIEFT_STARTDELAY;
902 /* again, assume we have support for everything */
903 info->dwStaticParams = DIEP_ALLPARAMS;
904 info->dwDynamicParams = info->dwStaticParams;
906 /* yes, this is windows behavior (print the GUID_Name for name) */
907 MultiByteToWideChar(CP_ACP, 0, _dump_dinput_GUID(rguid), -1,
908 info->tszName, MAX_PATH);
910 return DI_OK;
913 static const IDirectInputEffectVtbl LinuxInputEffectVtbl = {
914 LinuxInputEffectImpl_QueryInterface,
915 LinuxInputEffectImpl_AddRef,
916 LinuxInputEffectImpl_Release,
917 LinuxInputEffectImpl_Initialize,
918 LinuxInputEffectImpl_GetEffectGuid,
919 LinuxInputEffectImpl_GetParameters,
920 LinuxInputEffectImpl_SetParameters,
921 LinuxInputEffectImpl_Start,
922 LinuxInputEffectImpl_Stop,
923 LinuxInputEffectImpl_GetEffectStatus,
924 LinuxInputEffectImpl_Download,
925 LinuxInputEffectImpl_Unload,
926 LinuxInputEffectImpl_Escape
929 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */