msctf: Maintain context reference in ranges.
[wine.git] / dlls / dinput / effect_linuxinput.c
blob486163aeffa0fec5458f1082afa6d53f2c8aa29f
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 ULONG ref = InterlockedIncrement(&This->ref);
171 TRACE( "(%p) ref %d\n", This, ref );
172 return ref;
175 static HRESULT WINAPI LinuxInputEffectImpl_Download(
176 LPDIRECTINPUTEFFECT iface)
178 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
179 int ret, old_effect_id;
181 TRACE("(this=%p)\n", This);
182 ff_dump_effect(&This->effect);
184 old_effect_id = This->effect.id;
185 if (ioctl(*(This->fd), EVIOCSFF, &This->effect) != -1)
186 return DI_OK;
188 /* Linux kernel < 3.14 has a bug that incorrectly assigns an effect ID even
189 * on error, restore it here if that is the case. */
190 This->effect.id = old_effect_id;
192 switch (errno)
194 case EINVAL:
195 ret = DIERR_INVALIDPARAM;
196 break;
197 case ENOSPC:
198 ret = DIERR_DEVICEFULL;
199 break;
200 case ENOMEM:
201 ret = DIERR_OUTOFMEMORY;
202 break;
203 default:
204 ret = DIERR_INPUTLOST;
205 break;
207 TRACE("Could not upload effect to fd %d, errno %d \"%s\", returning 0x%x.\n",
208 *This->fd, errno, strerror(errno), ret);
209 return ret;
212 static HRESULT WINAPI LinuxInputEffectImpl_Escape(
213 LPDIRECTINPUTEFFECT iface,
214 LPDIEFFESCAPE pesc)
216 WARN("(this=%p,%p): invalid: no hardware-specific escape codes in this"
217 " driver!\n", iface, pesc);
219 return DI_OK;
222 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectGuid(
223 LPDIRECTINPUTEFFECT iface,
224 LPGUID pguid)
226 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
228 TRACE("(this=%p,%p)\n", This, pguid);
230 *pguid = This->guid;
232 return DI_OK;
235 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectStatus(
236 LPDIRECTINPUTEFFECT iface,
237 LPDWORD pdwFlags)
239 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
241 TRACE("(this=%p,%p)\n", This, pdwFlags);
243 if (!pdwFlags)
244 return E_POINTER;
246 if (This->effect.id == -1)
247 return DIERR_NOTDOWNLOADED;
249 /* linux sends the effect status through an event.
250 * that event is trapped by our parent joystick driver
251 * and there is no clean way to pass it back to us. */
252 FIXME("Not enough information to provide a status.\n");
254 (*pdwFlags) = 0;
256 return DI_OK;
259 static HRESULT WINAPI LinuxInputEffectImpl_GetParameters(
260 LPDIRECTINPUTEFFECT iface,
261 LPDIEFFECT peff,
262 DWORD dwFlags)
264 HRESULT diErr = DI_OK;
265 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
266 TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
268 /* Major conversion factors are:
269 * times: millisecond (linux) -> microsecond (windows) (x * 1000)
270 * forces: scale 0x7FFF (linux) -> scale 10000 (windows) approx ((x / 33) * 10)
271 * angles: scale 0x7FFF (linux) -> scale 35999 (windows) approx ((x / 33) * 36)
272 * angle bases: 0 -> -y (down) (linux) -> 0 -> +x (right) (windows)
275 if (dwFlags & DIEP_AXES) {
276 if (peff->cAxes < 2 /* linuxinput effects always use 2 axes, x and y */)
277 diErr = DIERR_MOREDATA;
278 peff->cAxes = 2;
279 if (diErr)
280 return diErr;
281 else {
282 peff->rgdwAxes[0] = DIJOFS_X;
283 peff->rgdwAxes[1] = DIJOFS_Y;
287 if (dwFlags & DIEP_DIRECTION) {
288 if (peff->cAxes < 2)
289 diErr = DIERR_MOREDATA;
290 peff->cAxes = 2;
291 if (diErr)
292 return diErr;
293 else {
294 if (peff->dwFlags & DIEFF_CARTESIAN) {
295 /* rotate so 0 points right */
296 double angle = ff_effect_direction_to_rad(This->effect.direction + 0xc000);
297 peff->rglDirection[0] = sin(angle) * 1000;
298 peff->rglDirection[1] = -cos(angle) * 1000;
299 } else {
300 /* Polar and spherical coordinates are the same for two or less
301 * axes.
302 * Note that we also use this case if NO flags are marked.
303 * According to MSDN, we should return the direction in the
304 * format that it was specified in, if no flags are marked.
306 peff->rglDirection[0] = (This->effect.direction / 33) * 36 + 9000;
307 if (peff->rglDirection[0] > 35999)
308 peff->rglDirection[0] -= 35999;
313 if (dwFlags & DIEP_DURATION)
315 if (!This->effect.replay.length) /* infinite for the linux driver */
316 peff->dwDuration = INFINITE;
317 else
318 peff->dwDuration = (DWORD)This->effect.replay.length * 1000;
321 if (dwFlags & DIEP_ENVELOPE) {
322 struct ff_envelope* env;
323 if (This->effect.type == FF_CONSTANT) env = &This->effect.u.constant.envelope;
324 else if (This->effect.type == FF_PERIODIC) env = &This->effect.u.periodic.envelope;
325 else if (This->effect.type == FF_RAMP) env = &This->effect.u.ramp.envelope;
326 else env = NULL;
327 if (env == NULL) {
328 peff->lpEnvelope = NULL;
329 } else if (peff->lpEnvelope == NULL) {
330 return DIERR_INVALIDPARAM;
331 } else {
332 peff->lpEnvelope->dwAttackLevel = (env->attack_level / 33) * 10;
333 peff->lpEnvelope->dwAttackTime = env->attack_length * 1000;
334 peff->lpEnvelope->dwFadeLevel = (env->fade_level / 33) * 10;
335 peff->lpEnvelope->dwFadeTime = env->fade_length * 1000;
339 if (dwFlags & DIEP_GAIN) {
340 peff->dwGain = This->gain * 10000 / 0xFFFF;
343 if (dwFlags & DIEP_SAMPLEPERIOD) {
344 /* the linux input ff driver has no support for setting
345 * the playback sample period. 0 means default. */
346 peff->dwSamplePeriod = 0;
349 if ((dwFlags & DIEP_STARTDELAY) && peff->dwSize > sizeof(DIEFFECT_DX5))
350 peff->dwStartDelay = This->effect.replay.delay * 1000;
352 if (dwFlags & DIEP_TRIGGERBUTTON) {
353 FIXME("LinuxInput button mapping needs redoing; for now, assuming we're using an actual joystick.\n");
354 peff->dwTriggerButton = DIJOFS_BUTTON(This->effect.trigger.button - BTN_JOYSTICK);
357 if (dwFlags & DIEP_TRIGGERREPEATINTERVAL) {
358 peff->dwTriggerRepeatInterval = This->effect.trigger.interval * 1000;
361 if (dwFlags & DIEP_TYPESPECIFICPARAMS) {
362 DWORD expectedsize = 0;
363 if (This->effect.type == FF_PERIODIC) {
364 expectedsize = sizeof(DIPERIODIC);
365 } else if (This->effect.type == FF_CONSTANT) {
366 expectedsize = sizeof(DICONSTANTFORCE);
367 } else if (This->effect.type == FF_SPRING
368 || This->effect.type == FF_FRICTION
369 || This->effect.type == FF_INERTIA
370 || This->effect.type == FF_DAMPER) {
371 expectedsize = sizeof(DICONDITION) * 2;
372 } else if (This->effect.type == FF_RAMP) {
373 expectedsize = sizeof(DIRAMPFORCE);
375 if (expectedsize > peff->cbTypeSpecificParams)
376 diErr = DIERR_MOREDATA;
377 peff->cbTypeSpecificParams = expectedsize;
378 if (diErr)
379 return diErr;
380 else {
381 if (This->effect.type == FF_PERIODIC) {
382 LPDIPERIODIC tsp = peff->lpvTypeSpecificParams;
383 tsp->dwMagnitude = (This->effect.u.periodic.magnitude / 33) * 10;
384 tsp->lOffset = (This->effect.u.periodic.offset / 33) * 10;
385 tsp->dwPhase = (This->effect.u.periodic.phase / 33) * 36;
386 tsp->dwPeriod = (This->effect.u.periodic.period * 1000);
387 } else if (This->effect.type == FF_CONSTANT) {
388 LPDICONSTANTFORCE tsp = peff->lpvTypeSpecificParams;
389 tsp->lMagnitude = (This->effect.u.constant.level / 33) * 10;
390 } else if (This->effect.type == FF_SPRING
391 || This->effect.type == FF_FRICTION
392 || This->effect.type == FF_INERTIA
393 || This->effect.type == FF_DAMPER) {
394 LPDICONDITION tsp = peff->lpvTypeSpecificParams;
395 int i;
396 for (i = 0; i < 2; ++i) {
397 tsp[i].lOffset = (This->effect.u.condition[i].center / 33) * 10;
398 tsp[i].lPositiveCoefficient = (This->effect.u.condition[i].right_coeff / 33) * 10;
399 tsp[i].lNegativeCoefficient = (This->effect.u.condition[i].left_coeff / 33) * 10;
400 tsp[i].dwPositiveSaturation = (This->effect.u.condition[i].right_saturation / 33) * 10;
401 tsp[i].dwNegativeSaturation = (This->effect.u.condition[i].left_saturation / 33) * 10;
402 tsp[i].lDeadBand = (This->effect.u.condition[i].deadband / 33) * 10;
404 } else if (This->effect.type == FF_RAMP) {
405 LPDIRAMPFORCE tsp = peff->lpvTypeSpecificParams;
406 tsp->lStart = (This->effect.u.ramp.start_level / 33) * 10;
407 tsp->lEnd = (This->effect.u.ramp.end_level / 33) * 10;
412 return diErr;
415 static HRESULT WINAPI LinuxInputEffectImpl_Initialize(
416 LPDIRECTINPUTEFFECT iface,
417 HINSTANCE hinst,
418 DWORD dwVersion,
419 REFGUID rguid)
421 FIXME("(this=%p,%p,%d,%s): stub!\n",
422 iface, hinst, dwVersion, debugstr_guid(rguid));
424 return DI_OK;
427 static HRESULT WINAPI LinuxInputEffectImpl_QueryInterface(
428 LPDIRECTINPUTEFFECT iface,
429 REFIID riid,
430 void **ppvObject)
432 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
434 TRACE("(this=%p,%s,%p)\n", This, debugstr_guid(riid), ppvObject);
436 if (IsEqualGUID(&IID_IUnknown, riid) ||
437 IsEqualGUID(&IID_IDirectInputEffect, riid)) {
438 LinuxInputEffectImpl_AddRef(iface);
439 *ppvObject = This;
440 return 0;
443 TRACE("Unsupported interface!\n");
444 return E_FAIL;
447 static HRESULT WINAPI LinuxInputEffectImpl_Start(
448 LPDIRECTINPUTEFFECT iface,
449 DWORD dwIterations,
450 DWORD dwFlags)
452 struct input_event event;
453 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
455 TRACE("(this=%p,%d,%d)\n", This, dwIterations, dwFlags);
457 if (!(dwFlags & DIES_NODOWNLOAD)) {
458 /* Download the effect if necessary */
459 if (This->effect.id == -1) {
460 HRESULT res = LinuxInputEffectImpl_Download(iface);
461 if (res != DI_OK)
462 return res;
466 if (dwFlags & DIES_SOLO) {
467 FIXME("Solo mode requested: should be stopping all effects here!\n");
470 event.type = EV_FF;
471 event.code = This->effect.id;
472 event.value = min( dwIterations, INT_MAX );
473 if (write(*(This->fd), &event, sizeof(event)) == -1) {
474 FIXME("Unable to write event. Assuming device disconnected.\n");
475 return DIERR_INPUTLOST;
478 return DI_OK;
481 static HRESULT WINAPI LinuxInputEffectImpl_SetParameters(
482 LPDIRECTINPUTEFFECT iface,
483 LPCDIEFFECT peff,
484 DWORD dwFlags)
486 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
487 DWORD type = typeFromGUID(&This->guid);
488 HRESULT retval = DI_OK;
490 TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
492 dump_DIEFFECT(peff, &This->guid, dwFlags);
494 if (!dwFlags)
495 return DI_NOEFFECT;
497 if (dwFlags & DIEP_AXES) {
498 if (!(peff->rgdwAxes))
499 return DIERR_INVALIDPARAM;
501 /* the linux input effect system only supports one or two axes */
502 if (peff->cAxes > 2)
503 return DIERR_INVALIDPARAM;
504 else if (peff->cAxes < 1)
505 return DIERR_INCOMPLETEEFFECT;
506 This->first_axis_is_x = peff->rgdwAxes[0] == DIJOFS_X;
509 /* some of this may look funky, but it's 'cause the linux driver and directx have
510 * different opinions about which way direction "0" is. directx has 0 along the x
511 * axis (left), linux has it along the y axis (down). */
512 if (dwFlags & DIEP_DIRECTION) {
513 if (!(peff->rglDirection))
514 return DIERR_INVALIDPARAM;
516 if (peff->cAxes == 1) {
517 if (peff->dwFlags & DIEFF_CARTESIAN) {
518 if (dwFlags & DIEP_AXES) {
519 if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] >= 0)
520 This->effect.direction = 0x4000;
521 else if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] < 0)
522 This->effect.direction = 0xC000;
523 else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] >= 0)
524 This->effect.direction = 0;
525 else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] < 0)
526 This->effect.direction = 0x8000;
528 } else {
529 /* one-axis effects must use cartesian coords */
530 return DIERR_INVALIDPARAM;
533 /* two axes */
534 else
536 if (peff->dwFlags & DIEFF_CARTESIAN)
538 LONG x, y;
539 if (This->first_axis_is_x)
541 x = peff->rglDirection[0];
542 y = peff->rglDirection[1];
544 else
546 x = peff->rglDirection[1];
547 y = peff->rglDirection[0];
549 This->effect.direction = (unsigned int)((M_PI / 2 + atan2(y, x)) * 0x8000 / M_PI);
551 else
553 /* Polar and spherical are the same for 2 axes */
554 /* Precision is important here, so we do double math with exact constants */
555 This->effect.direction = (unsigned int)(((double)peff->rglDirection[0] / 18000) * 0x8000);
560 if (dwFlags & DIEP_DURATION)
562 if (peff->dwDuration == INFINITE)
563 This->effect.replay.length = 0; /* infinite for the linux driver */
564 else if(peff->dwDuration > 1000)
565 This->effect.replay.length = peff->dwDuration / 1000;
566 else
567 This->effect.replay.length = 1;
570 if (dwFlags & DIEP_ENVELOPE)
572 struct ff_envelope* env;
573 if (This->effect.type == FF_CONSTANT)
574 env = &This->effect.u.constant.envelope;
575 else if (This->effect.type == FF_PERIODIC)
576 env = &This->effect.u.periodic.envelope;
577 else if (This->effect.type == FF_RAMP)
578 env = &This->effect.u.ramp.envelope;
579 else
580 env = NULL;
582 /* copy the envelope if it is present and the linux effect supports it */
583 if (peff->lpEnvelope && env)
585 env->attack_length = peff->lpEnvelope->dwAttackTime / 1000;
586 env->attack_level = (peff->lpEnvelope->dwAttackLevel / 10) * 32;
587 env->fade_length = peff->lpEnvelope->dwFadeTime / 1000;
588 env->fade_level = (peff->lpEnvelope->dwFadeLevel / 10) * 32;
590 /* if the dinput envelope is NULL we will clear the linux envelope */
591 else if (env)
593 env->attack_length = 0;
594 env->attack_level = 0;
595 env->fade_length = 0;
596 env->fade_level = 0;
598 else if(peff->lpEnvelope)
600 if(peff->lpEnvelope->dwAttackTime || peff->lpEnvelope->dwAttackLevel ||
601 peff->lpEnvelope->dwFadeTime || peff->lpEnvelope->dwFadeLevel)
602 WARN("Ignoring dinput envelope not supported in the linux effect\n");
606 /* Gain and Sample Period settings are not supported by the linux
607 * event system */
608 if (dwFlags & DIEP_GAIN) {
609 This->gain = 0xFFFF * peff->dwGain / 10000;
610 TRACE("Effect gain requested but no effect gain functionality present.\n");
613 if (dwFlags & DIEP_SAMPLEPERIOD)
614 TRACE("Sample period requested but no sample period functionality present.\n");
616 if (dwFlags & DIEP_STARTDELAY)
617 if ((dwFlags & DIEP_STARTDELAY) && peff->dwSize > sizeof(DIEFFECT_DX5))
618 This->effect.replay.delay = peff->dwStartDelay / 1000;
620 if (dwFlags & DIEP_TRIGGERBUTTON) {
621 if (peff->dwTriggerButton != -1) {
622 FIXME("Linuxinput button mapping needs redoing, assuming we're using a joystick.\n");
623 FIXME("Trigger button translation not yet implemented!\n");
625 This->effect.trigger.button = 0;
628 if (dwFlags & DIEP_TRIGGERREPEATINTERVAL)
629 This->effect.trigger.interval = peff->dwTriggerRepeatInterval / 1000;
631 if (dwFlags & DIEP_TYPESPECIFICPARAMS)
633 if (!(peff->lpvTypeSpecificParams))
634 return DIERR_INVALIDPARAM;
636 if (type == DIEFT_PERIODIC)
638 DIPERIODIC *tsp;
639 if (peff->cbTypeSpecificParams != sizeof(DIPERIODIC))
640 return DIERR_INVALIDPARAM;
641 tsp = peff->lpvTypeSpecificParams;
643 This->effect.u.periodic.magnitude = (tsp->dwMagnitude / 10) * 32;
644 This->effect.u.periodic.offset = (tsp->lOffset / 10) * 32;
645 /* phase ranges from 0 - 35999 in dinput and 0 - 65535 on Linux */
646 This->effect.u.periodic.phase = (tsp->dwPhase / 36) * 65;
647 /* dinput uses microseconds, Linux uses milliseconds */
648 if (tsp->dwPeriod <= 1000)
649 This->effect.u.periodic.period = 1;
650 else
651 This->effect.u.periodic.period = tsp->dwPeriod / 1000;
653 else if (type == DIEFT_CONSTANTFORCE)
655 LPCDICONSTANTFORCE tsp;
656 if (peff->cbTypeSpecificParams != sizeof(DICONSTANTFORCE))
657 return DIERR_INVALIDPARAM;
658 tsp = peff->lpvTypeSpecificParams;
659 This->effect.u.constant.level = (max(min(tsp->lMagnitude, 10000), -10000) / 10) * 32;
660 } else if (type == DIEFT_RAMPFORCE) {
661 LPCDIRAMPFORCE tsp;
662 if (peff->cbTypeSpecificParams != sizeof(DIRAMPFORCE))
663 return DIERR_INVALIDPARAM;
664 tsp = peff->lpvTypeSpecificParams;
665 This->effect.u.ramp.start_level = (tsp->lStart / 10) * 32;
666 This->effect.u.ramp.end_level = (tsp->lEnd / 10) * 32;
668 else if (type == DIEFT_CONDITION)
670 DICONDITION *tsp = peff->lpvTypeSpecificParams;
671 struct ff_condition_effect *cond = This->effect.u.condition;
672 int i, j, sources;
673 double factor[2];
675 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. Ensures 0 points right */
679 double angle = ff_effect_direction_to_rad(This->effect.direction + 0xc000);
680 factor[0] = sin(angle);
681 factor[1] = -cos(angle);
682 sources = 1;
684 else if (peff->cbTypeSpecificParams == 2 * sizeof(DICONDITION))
686 /* Direct parameter copy without changes */
687 factor[0] = factor[1] = 1;
688 sources = 2;
690 else
691 return DIERR_INVALIDPARAM;
693 for (i = j = 0; i < 2; ++i)
695 cond[i].center = (int)(factor[i] * (tsp[j].lOffset / 10) * 32);
696 cond[i].right_coeff = (int)(factor[i] * (tsp[j].lPositiveCoefficient / 10) * 32);
697 cond[i].left_coeff = (int)(factor[i] * (tsp[j].lNegativeCoefficient / 10) * 32);
698 cond[i].right_saturation = (int)(factor[i] * (tsp[j].dwPositiveSaturation / 10) * 65);
699 cond[i].left_saturation = (int)(factor[i] * (tsp[j].dwNegativeSaturation / 10) * 65);
700 cond[i].deadband = (int)(factor[i] * (tsp[j].lDeadBand / 10) * 32);
701 if (sources == 2)
702 j++;
705 else
707 FIXME("Custom force types are not supported\n");
708 return DIERR_INVALIDPARAM;
712 if (!(dwFlags & DIEP_NODOWNLOAD))
713 retval = LinuxInputEffectImpl_Download(iface);
714 if (retval != DI_OK)
715 return DI_DOWNLOADSKIPPED;
717 if (dwFlags & DIEP_NORESTART)
718 TRACE("DIEP_NORESTART: not handled (we have no control of that).\n");
720 if (dwFlags & DIEP_START)
721 retval = LinuxInputEffectImpl_Start(iface, 1, 0);
722 if (retval != DI_OK)
723 return retval;
725 return DI_OK;
728 static HRESULT WINAPI LinuxInputEffectImpl_Stop(
729 LPDIRECTINPUTEFFECT iface)
731 struct input_event event;
732 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
734 TRACE("(this=%p)\n", This);
736 event.type = EV_FF;
737 event.code = This->effect.id;
738 event.value = 0;
739 /* we don't care about the success or failure of this call */
740 write(*(This->fd), &event, sizeof(event));
742 return DI_OK;
745 static HRESULT WINAPI LinuxInputEffectImpl_Unload(
746 LPDIRECTINPUTEFFECT iface)
748 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
749 TRACE("(this=%p)\n", This);
751 /* Erase the downloaded effect */
752 if (ioctl(*(This->fd), EVIOCRMFF, This->effect.id) == -1)
753 return DIERR_INVALIDPARAM;
755 /* Mark the effect as deallocated */
756 This->effect.id = -1;
758 return DI_OK;
761 static ULONG WINAPI LinuxInputEffectImpl_Release(LPDIRECTINPUTEFFECT iface)
763 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
764 ULONG ref = InterlockedDecrement(&(This->ref));
766 TRACE( "(%p) ref %d\n", This, ref );
768 if (ref == 0)
770 LinuxInputEffectImpl_Stop(iface);
771 LinuxInputEffectImpl_Unload(iface);
772 list_remove(This->entry);
773 HeapFree(GetProcessHeap(), 0, LIST_ENTRY(This->entry, effect_list_item, entry));
774 HeapFree(GetProcessHeap(), 0, This);
776 return ref;
779 /******************************************************************************
780 * LinuxInputEffect
783 DECLSPEC_HIDDEN HRESULT linuxinput_create_effect(
784 int* fd,
785 REFGUID rguid,
786 struct list *parent_list_entry,
787 LPDIRECTINPUTEFFECT* peff)
789 LinuxInputEffectImpl* newEffect = HeapAlloc(GetProcessHeap(),
790 HEAP_ZERO_MEMORY, sizeof(LinuxInputEffectImpl));
791 DWORD type = typeFromGUID(rguid);
793 newEffect->IDirectInputEffect_iface.lpVtbl = &LinuxInputEffectVtbl;
794 newEffect->ref = 1;
795 newEffect->guid = *rguid;
796 newEffect->fd = fd;
797 newEffect->gain = 0xFFFF;
799 /* set the type. this cannot be changed over the effect's life. */
800 switch (type) {
801 case DIEFT_PERIODIC:
802 newEffect->effect.type = FF_PERIODIC;
803 if (IsEqualGUID(rguid, &GUID_Sine)) {
804 newEffect->effect.u.periodic.waveform = FF_SINE;
805 } else if (IsEqualGUID(rguid, &GUID_Triangle)) {
806 newEffect->effect.u.periodic.waveform = FF_TRIANGLE;
807 } else if (IsEqualGUID(rguid, &GUID_Square)) {
808 newEffect->effect.u.periodic.waveform = FF_SQUARE;
809 } else if (IsEqualGUID(rguid, &GUID_SawtoothUp)) {
810 newEffect->effect.u.periodic.waveform = FF_SAW_UP;
811 } else if (IsEqualGUID(rguid, &GUID_SawtoothDown)) {
812 newEffect->effect.u.periodic.waveform = FF_SAW_DOWN;
814 break;
815 case DIEFT_CONSTANTFORCE:
816 newEffect->effect.type = FF_CONSTANT;
817 break;
818 case DIEFT_RAMPFORCE:
819 newEffect->effect.type = FF_RAMP;
820 break;
821 case DIEFT_CONDITION:
822 if (IsEqualGUID(rguid, &GUID_Spring)) {
823 newEffect->effect.type = FF_SPRING;
824 } else if (IsEqualGUID(rguid, &GUID_Friction)) {
825 newEffect->effect.type = FF_FRICTION;
826 } else if (IsEqualGUID(rguid, &GUID_Inertia)) {
827 newEffect->effect.type = FF_INERTIA;
828 } else if (IsEqualGUID(rguid, &GUID_Damper)) {
829 newEffect->effect.type = FF_DAMPER;
831 break;
832 case DIEFT_CUSTOMFORCE:
833 FIXME("Custom forces are not supported.\n");
834 HeapFree(GetProcessHeap(), 0, newEffect);
835 return DIERR_INVALIDPARAM;
836 default:
837 FIXME("Unknown force type 0x%x.\n", type);
838 HeapFree(GetProcessHeap(), 0, newEffect);
839 return DIERR_INVALIDPARAM;
842 /* mark as non-uploaded */
843 newEffect->effect.id = -1;
845 newEffect->entry = parent_list_entry;
847 *peff = &newEffect->IDirectInputEffect_iface;
849 TRACE("Creating linux input system effect (%p) with guid %s\n",
850 *peff, _dump_dinput_GUID(rguid));
852 return DI_OK;
855 DECLSPEC_HIDDEN HRESULT linuxinput_get_info_W(
856 int fd,
857 REFGUID rguid,
858 LPDIEFFECTINFOW info)
860 DWORD type = typeFromGUID(rguid);
862 TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
864 if (!info) return E_POINTER;
866 if (info->dwSize != sizeof(DIEFFECTINFOW)) return DIERR_INVALIDPARAM;
868 info->guid = *rguid;
870 info->dwEffType = type;
871 /* the event device API does not support querying for all these things
872 * therefore we assume that we have support for them
873 * that's not as dangerous as it sounds, since drivers are allowed to
874 * ignore parameters they claim to support anyway */
875 info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE
876 | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
877 | DIEFT_SATURATION | DIEFT_STARTDELAY;
879 /* again, assume we have support for everything */
880 info->dwStaticParams = DIEP_ALLPARAMS;
881 info->dwDynamicParams = info->dwStaticParams;
883 /* yes, this is windows behavior (print the GUID_Name for name) */
884 MultiByteToWideChar(CP_ACP, 0, _dump_dinput_GUID(rguid), -1,
885 info->tszName, MAX_PATH);
887 return DI_OK;
890 static const IDirectInputEffectVtbl LinuxInputEffectVtbl = {
891 LinuxInputEffectImpl_QueryInterface,
892 LinuxInputEffectImpl_AddRef,
893 LinuxInputEffectImpl_Release,
894 LinuxInputEffectImpl_Initialize,
895 LinuxInputEffectImpl_GetEffectGuid,
896 LinuxInputEffectImpl_GetParameters,
897 LinuxInputEffectImpl_SetParameters,
898 LinuxInputEffectImpl_Start,
899 LinuxInputEffectImpl_Stop,
900 LinuxInputEffectImpl_GetEffectStatus,
901 LinuxInputEffectImpl_Download,
902 LinuxInputEffectImpl_Unload,
903 LinuxInputEffectImpl_Escape
906 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */