[AMSTREAM] We don't need to define WIDL_C_INLINE_WRAPPERS here anymore.
[reactos.git] / dll / directx / wine / dinput / effect_linuxinput.c
1 /* DirectInput Linux Event Device Effect
2 *
3 * Copyright 2005 Daniel Remenak
4 *
5 * Thanks to Google's Summer of Code Program (2005)
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "config.h"
23
24 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
25
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"
44
45 #include "device_private.h"
46 #include "joystick_private.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
49
50 static const IDirectInputEffectVtbl LinuxInputEffectVtbl;
51 typedef struct LinuxInputEffectImpl LinuxInputEffectImpl;
52 struct LinuxInputEffectImpl
53 {
54 IDirectInputEffect IDirectInputEffect_iface;
55 LONG ref;
56 GUID guid;
57
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 */
63 };
64
65 static inline LinuxInputEffectImpl *impl_from_IDirectInputEffect(IDirectInputEffect *iface)
66 {
67 return CONTAINING_RECORD(iface, LinuxInputEffectImpl, IDirectInputEffect_iface);
68 }
69
70 static double ff_effect_direction_to_rad(unsigned int dir)
71 {
72 return (dir & 0xffff) * M_PI / 0x8000;
73 }
74
75 static void ff_dump_effect(struct ff_effect *effect)
76 {
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)
82 {
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);
91 }
92 #undef FE
93
94 /* rotate so 0 points right */
95 angle = 360 - ff_effect_direction_to_rad(effect->direction + 0xc000) * 180 / M_PI;
96
97 if (effect->replay.length)
98 length = wine_dbg_sprintf("%u ms", effect->replay.length);
99
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);
105
106 if (effect->type == FF_PERIODIC)
107 {
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)
112 {
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);
119 }
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;
125 }
126 else if (effect->type == FF_CONSTANT)
127 {
128 struct ff_constant_effect *cons = &effect->u.constant;
129 TRACE(" -> level %d\n", cons->level);
130 env = &cons->envelope;
131 }
132 else if (effect->type == FF_RAMP)
133 {
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;
137 }
138 else if (effect->type == FF_RUMBLE)
139 {
140 struct ff_rumble_effect *rumble = &effect->u.rumble;
141 TRACE(" -> strong/weak magnitude %u/%u\n", rumble->strong_magnitude, rumble->weak_magnitude);
142 }
143 else if (effect->type == FF_SPRING || effect->type == FF_FRICTION ||
144 effect->type == FF_DAMPER || effect->type == FF_INERTIA)
145 {
146 struct ff_condition_effect *cond = effect->u.condition;
147 int i;
148 for (i = 0; i < 2; i++)
149 {
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);
154 }
155 }
156
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);
160 }
161
162 /******************************************************************************
163 * LinuxInputEffectImpl
164 */
165
166 static ULONG WINAPI LinuxInputEffectImpl_AddRef(
167 LPDIRECTINPUTEFFECT iface)
168 {
169 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
170 return InterlockedIncrement(&(This->ref));
171 }
172
173 static HRESULT WINAPI LinuxInputEffectImpl_Download(
174 LPDIRECTINPUTEFFECT iface)
175 {
176 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
177 int ret, old_effect_id;
178
179 TRACE("(this=%p)\n", This);
180 ff_dump_effect(&This->effect);
181
182 old_effect_id = This->effect.id;
183 if (ioctl(*(This->fd), EVIOCSFF, &This->effect) != -1)
184 return DI_OK;
185
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;
189
190 switch (errno)
191 {
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;
204 }
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;
208 }
209
210 static HRESULT WINAPI LinuxInputEffectImpl_Escape(
211 LPDIRECTINPUTEFFECT iface,
212 LPDIEFFESCAPE pesc)
213 {
214 WARN("(this=%p,%p): invalid: no hardware-specific escape codes in this"
215 " driver!\n", iface, pesc);
216
217 return DI_OK;
218 }
219
220 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectGuid(
221 LPDIRECTINPUTEFFECT iface,
222 LPGUID pguid)
223 {
224 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
225
226 TRACE("(this=%p,%p)\n", This, pguid);
227
228 *pguid = This->guid;
229
230 return DI_OK;
231 }
232
233 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectStatus(
234 LPDIRECTINPUTEFFECT iface,
235 LPDWORD pdwFlags)
236 {
237 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
238
239 TRACE("(this=%p,%p)\n", This, pdwFlags);
240
241 if (!pdwFlags)
242 return E_POINTER;
243
244 if (This->effect.id == -1)
245 return DIERR_NOTDOWNLOADED;
246
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");
251
252 (*pdwFlags) = 0;
253
254 return DI_OK;
255 }
256
257 static HRESULT WINAPI LinuxInputEffectImpl_GetParameters(
258 LPDIRECTINPUTEFFECT iface,
259 LPDIEFFECT peff,
260 DWORD dwFlags)
261 {
262 HRESULT diErr = DI_OK;
263 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
264 TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
265
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)
271 */
272
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;
282 }
283 }
284
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.
303 */
304 peff->rglDirection[0] = (This->effect.direction / 33) * 36 + 9000;
305 if (peff->rglDirection[0] > 35999)
306 peff->rglDirection[0] -= 35999;
307 }
308 }
309 }
310
311 if (dwFlags & DIEP_DURATION)
312 {
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;
317 }
318
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;
334 }
335 }
336
337 if (dwFlags & DIEP_GAIN) {
338 peff->dwGain = This->gain * 10000 / 0xFFFF;
339 }
340
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;
345 }
346
347 if ((dwFlags & DIEP_STARTDELAY) && peff->dwSize > sizeof(DIEFFECT_DX5))
348 peff->dwStartDelay = This->effect.replay.delay * 1000;
349
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);
353 }
354
355 if (dwFlags & DIEP_TRIGGERREPEATINTERVAL) {
356 peff->dwTriggerRepeatInterval = This->effect.trigger.interval * 1000;
357 }
358
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);
372 }
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;
401 }
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;
406 }
407 }
408 }
409
410 return diErr;
411 }
412
413 static HRESULT WINAPI LinuxInputEffectImpl_Initialize(
414 LPDIRECTINPUTEFFECT iface,
415 HINSTANCE hinst,
416 DWORD dwVersion,
417 REFGUID rguid)
418 {
419 FIXME("(this=%p,%p,%d,%s): stub!\n",
420 iface, hinst, dwVersion, debugstr_guid(rguid));
421
422 return DI_OK;
423 }
424
425 static HRESULT WINAPI LinuxInputEffectImpl_QueryInterface(
426 LPDIRECTINPUTEFFECT iface,
427 REFIID riid,
428 void **ppvObject)
429 {
430 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
431
432 TRACE("(this=%p,%s,%p)\n", This, debugstr_guid(riid), ppvObject);
433
434 if (IsEqualGUID(&IID_IUnknown, riid) ||
435 IsEqualGUID(&IID_IDirectInputEffect, riid)) {
436 LinuxInputEffectImpl_AddRef(iface);
437 *ppvObject = This;
438 return 0;
439 }
440
441 TRACE("Unsupported interface!\n");
442 return E_FAIL;
443 }
444
445 static HRESULT WINAPI LinuxInputEffectImpl_Start(
446 LPDIRECTINPUTEFFECT iface,
447 DWORD dwIterations,
448 DWORD dwFlags)
449 {
450 struct input_event event;
451 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
452
453 TRACE("(this=%p,%d,%d)\n", This, dwIterations, dwFlags);
454
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;
461 }
462 }
463
464 if (dwFlags & DIES_SOLO) {
465 FIXME("Solo mode requested: should be stopping all effects here!\n");
466 }
467
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;
474 }
475
476 return DI_OK;
477 }
478
479 static HRESULT WINAPI LinuxInputEffectImpl_SetParameters(
480 LPDIRECTINPUTEFFECT iface,
481 LPCDIEFFECT peff,
482 DWORD dwFlags)
483 {
484 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
485 DWORD type = typeFromGUID(&This->guid);
486 HRESULT retval = DI_OK;
487
488 TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
489
490 dump_DIEFFECT(peff, &This->guid, dwFlags);
491
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;
497 }
498
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;
506 }
507
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;
523 }
524 } else {
525 /* one-axis effects must use cartesian coords */
526 return DIERR_INVALIDPARAM;
527 }
528 }
529 /* two axes */
530 else
531 {
532 if (peff->dwFlags & DIEFF_CARTESIAN)
533 {
534 LONG x, y;
535 if (This->first_axis_is_x)
536 {
537 x = peff->rglDirection[0];
538 y = peff->rglDirection[1];
539 }
540 else
541 {
542 x = peff->rglDirection[1];
543 y = peff->rglDirection[0];
544 }
545 This->effect.direction = (unsigned int)((M_PI / 2 + atan2(y, x)) * 0x8000 / M_PI);
546 }
547 else
548 {
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);
552 }
553 }
554 }
555
556 if (dwFlags & DIEP_DURATION)
557 {
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;
564 }
565
566 if (dwFlags & DIEP_ENVELOPE)
567 {
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;
577
578 /* copy the envelope if it is present and the linux effect supports it */
579 if (peff->lpEnvelope && env)
580 {
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;
585 }
586 /* if the dinput envelope is NULL we will clear the linux envelope */
587 else if (env)
588 {
589 env->attack_length = 0;
590 env->attack_level = 0;
591 env->fade_length = 0;
592 env->fade_level = 0;
593 }
594 else if(peff->lpEnvelope)
595 {
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");
599 }
600 }
601
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");
607 }
608
609 if (dwFlags & DIEP_SAMPLEPERIOD)
610 TRACE("Sample period requested but no sample period functionality present.\n");
611
612 if (dwFlags & DIEP_STARTDELAY)
613 if ((dwFlags & DIEP_STARTDELAY) && peff->dwSize > sizeof(DIEFFECT_DX5))
614 This->effect.replay.delay = peff->dwStartDelay / 1000;
615
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");
620 }
621 This->effect.trigger.button = 0;
622 }
623
624 if (dwFlags & DIEP_TRIGGERREPEATINTERVAL)
625 This->effect.trigger.interval = peff->dwTriggerRepeatInterval / 1000;
626
627 if (dwFlags & DIEP_TYPESPECIFICPARAMS)
628 {
629 if (!(peff->lpvTypeSpecificParams))
630 return DIERR_INCOMPLETEEFFECT;
631
632 if (type == DIEFT_PERIODIC)
633 {
634 DIPERIODIC *tsp;
635 if (peff->cbTypeSpecificParams != sizeof(DIPERIODIC))
636 return DIERR_INVALIDPARAM;
637 tsp = peff->lpvTypeSpecificParams;
638
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;
648 }
649 else if (type == DIEFT_CONSTANTFORCE)
650 {
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;
663 }
664 else if (type == DIEFT_CONDITION)
665 {
666 DICONDITION *tsp = peff->lpvTypeSpecificParams;
667 struct ff_condition_effect *cond = This->effect.u.condition;
668 int i, j, sources;
669 double factor[2];
670
671 if (peff->cbTypeSpecificParams == sizeof(DICONDITION))
672 {
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;
679 }
680 else if (peff->cbTypeSpecificParams == 2 * sizeof(DICONDITION))
681 {
682 /* Direct parameter copy without changes */
683 factor[0] = factor[1] = 1;
684 sources = 2;
685 }
686 else
687 return DIERR_INVALIDPARAM;
688
689 for (i = j = 0; i < 2; ++i)
690 {
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++;
699 }
700 }
701 else
702 {
703 FIXME("Custom force types are not supported\n");
704 return DIERR_INVALIDPARAM;
705 }
706 }
707
708 if (!(dwFlags & DIEP_NODOWNLOAD))
709 retval = LinuxInputEffectImpl_Download(iface);
710 if (retval != DI_OK)
711 return DI_DOWNLOADSKIPPED;
712
713 if (dwFlags & DIEP_NORESTART)
714 TRACE("DIEP_NORESTART: not handled (we have no control of that).\n");
715
716 if (dwFlags & DIEP_START)
717 retval = LinuxInputEffectImpl_Start(iface, 1, 0);
718 if (retval != DI_OK)
719 return retval;
720
721 return DI_OK;
722 }
723
724 static HRESULT WINAPI LinuxInputEffectImpl_Stop(
725 LPDIRECTINPUTEFFECT iface)
726 {
727 struct input_event event;
728 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
729
730 TRACE("(this=%p)\n", This);
731
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));
737
738 return DI_OK;
739 }
740
741 static HRESULT WINAPI LinuxInputEffectImpl_Unload(
742 LPDIRECTINPUTEFFECT iface)
743 {
744 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
745 TRACE("(this=%p)\n", This);
746
747 /* Erase the downloaded effect */
748 if (ioctl(*(This->fd), EVIOCRMFF, This->effect.id) == -1)
749 return DIERR_INVALIDPARAM;
750
751 /* Mark the effect as deallocated */
752 This->effect.id = -1;
753
754 return DI_OK;
755 }
756
757 static ULONG WINAPI LinuxInputEffectImpl_Release(LPDIRECTINPUTEFFECT iface)
758 {
759 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
760 ULONG ref = InterlockedDecrement(&(This->ref));
761
762 if (ref == 0)
763 {
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);
769 }
770 return ref;
771 }
772
773 /******************************************************************************
774 * LinuxInputEffect
775 */
776
777 DECLSPEC_HIDDEN HRESULT linuxinput_create_effect(
778 int* fd,
779 REFGUID rguid,
780 struct list *parent_list_entry,
781 LPDIRECTINPUTEFFECT* peff)
782 {
783 LinuxInputEffectImpl* newEffect = HeapAlloc(GetProcessHeap(),
784 HEAP_ZERO_MEMORY, sizeof(LinuxInputEffectImpl));
785 DWORD type = typeFromGUID(rguid);
786
787 newEffect->IDirectInputEffect_iface.lpVtbl = &LinuxInputEffectVtbl;
788 newEffect->ref = 1;
789 newEffect->guid = *rguid;
790 newEffect->fd = fd;
791 newEffect->gain = 0xFFFF;
792
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;
807 }
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;
824 }
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;
834 }
835
836 /* mark as non-uploaded */
837 newEffect->effect.id = -1;
838
839 newEffect->entry = parent_list_entry;
840
841 *peff = &newEffect->IDirectInputEffect_iface;
842
843 TRACE("Creating linux input system effect (%p) with guid %s\n",
844 *peff, _dump_dinput_GUID(rguid));
845
846 return DI_OK;
847 }
848
849 DECLSPEC_HIDDEN HRESULT linuxinput_get_info_A(
850 int fd,
851 REFGUID rguid,
852 LPDIEFFECTINFOA info)
853 {
854 DWORD type = typeFromGUID(rguid);
855
856 TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
857
858 if (!info) return E_POINTER;
859
860 if (info->dwSize != sizeof(DIEFFECTINFOA)) return DIERR_INVALIDPARAM;
861
862 info->guid = *rguid;
863
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;
872
873 /* again, assume we have support for everything */
874 info->dwStaticParams = DIEP_ALLPARAMS;
875 info->dwDynamicParams = info->dwStaticParams;
876
877 /* yes, this is windows behavior (print the GUID_Name for name) */
878 strcpy(info->tszName, _dump_dinput_GUID(rguid));
879
880 return DI_OK;
881 }
882
883 DECLSPEC_HIDDEN HRESULT linuxinput_get_info_W(
884 int fd,
885 REFGUID rguid,
886 LPDIEFFECTINFOW info)
887 {
888 DWORD type = typeFromGUID(rguid);
889
890 TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
891
892 if (!info) return E_POINTER;
893
894 if (info->dwSize != sizeof(DIEFFECTINFOW)) return DIERR_INVALIDPARAM;
895
896 info->guid = *rguid;
897
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;
906
907 /* again, assume we have support for everything */
908 info->dwStaticParams = DIEP_ALLPARAMS;
909 info->dwDynamicParams = info->dwStaticParams;
910
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);
914
915 return DI_OK;
916 }
917
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
932 };
933
934 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */