ad7d3ad9f3a17733fcbc67e804b698685c2d84ac
[reactos.git] / reactos / 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 /******************************************************************************
76 * LinuxInputEffectImpl
77 */
78
79 static ULONG WINAPI LinuxInputEffectImpl_AddRef(
80 LPDIRECTINPUTEFFECT iface)
81 {
82 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
83 return InterlockedIncrement(&(This->ref));
84 }
85
86 static HRESULT WINAPI LinuxInputEffectImpl_Download(
87 LPDIRECTINPUTEFFECT iface)
88 {
89 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
90
91 TRACE("(this=%p)\n", This);
92
93 if (ioctl(*(This->fd), EVIOCSFF, &This->effect) == -1) {
94 if (errno == ENOMEM) {
95 return DIERR_DEVICEFULL;
96 } else {
97 FIXME("Could not upload effect. Assuming a disconnected device %d \"%s\".\n", *This->fd, strerror(errno));
98 return DIERR_INPUTLOST;
99 }
100 }
101
102 return DI_OK;
103 }
104
105 static HRESULT WINAPI LinuxInputEffectImpl_Escape(
106 LPDIRECTINPUTEFFECT iface,
107 LPDIEFFESCAPE pesc)
108 {
109 WARN("(this=%p,%p): invalid: no hardware-specific escape codes in this"
110 " driver!\n", iface, pesc);
111
112 return DI_OK;
113 }
114
115 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectGuid(
116 LPDIRECTINPUTEFFECT iface,
117 LPGUID pguid)
118 {
119 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
120
121 TRACE("(this=%p,%p)\n", This, pguid);
122
123 *pguid = This->guid;
124
125 return DI_OK;
126 }
127
128 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectStatus(
129 LPDIRECTINPUTEFFECT iface,
130 LPDWORD pdwFlags)
131 {
132 TRACE("(this=%p,%p)\n", iface, pdwFlags);
133
134 /* linux sends the effect status through an event.
135 * that event is trapped by our parent joystick driver
136 * and there is no clean way to pass it back to us. */
137 FIXME("Not enough information to provide a status.\n");
138
139 (*pdwFlags) = 0;
140
141 return DI_OK;
142 }
143
144 static HRESULT WINAPI LinuxInputEffectImpl_GetParameters(
145 LPDIRECTINPUTEFFECT iface,
146 LPDIEFFECT peff,
147 DWORD dwFlags)
148 {
149 HRESULT diErr = DI_OK;
150 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
151 TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
152
153 /* Major conversion factors are:
154 * times: millisecond (linux) -> microsecond (windows) (x * 1000)
155 * forces: scale 0x7FFF (linux) -> scale 10000 (windows) approx ((x / 33) * 10)
156 * angles: scale 0x7FFF (linux) -> scale 35999 (windows) approx ((x / 33) * 36)
157 * angle bases: 0 -> -y (down) (linux) -> 0 -> +x (right) (windows)
158 */
159
160 if (dwFlags & DIEP_AXES) {
161 if (peff->cAxes < 2 /* linuxinput effects always use 2 axes, x and y */)
162 diErr = DIERR_MOREDATA;
163 peff->cAxes = 2;
164 if (diErr)
165 return diErr;
166 else {
167 peff->rgdwAxes[0] = DIJOFS_X;
168 peff->rgdwAxes[1] = DIJOFS_Y;
169 }
170 }
171
172 if (dwFlags & DIEP_DIRECTION) {
173 if (peff->cAxes < 2)
174 diErr = DIERR_MOREDATA;
175 peff->cAxes = 2;
176 if (diErr)
177 return diErr;
178 else {
179 if (peff->dwFlags & DIEFF_CARTESIAN) {
180 /* rotate so 0 points right */
181 double angle = ff_effect_direction_to_rad(This->effect.direction + 0xc000);
182 peff->rglDirection[0] = sin(angle) * 1000;
183 peff->rglDirection[1] = -cos(angle) * 1000;
184 } else {
185 /* Polar and spherical coordinates are the same for two or less
186 * axes.
187 * Note that we also use this case if NO flags are marked.
188 * According to MSDN, we should return the direction in the
189 * format that it was specified in, if no flags are marked.
190 */
191 peff->rglDirection[0] = (This->effect.direction / 33) * 36 + 9000;
192 if (peff->rglDirection[0] > 35999)
193 peff->rglDirection[0] -= 35999;
194 }
195 }
196 }
197
198 if (dwFlags & DIEP_DURATION) {
199 peff->dwDuration = (DWORD)This->effect.replay.length * 1000;
200 }
201
202 if (dwFlags & DIEP_ENVELOPE) {
203 struct ff_envelope* env;
204 if (This->effect.type == FF_CONSTANT) env = &This->effect.u.constant.envelope;
205 else if (This->effect.type == FF_PERIODIC) env = &This->effect.u.periodic.envelope;
206 else if (This->effect.type == FF_RAMP) env = &This->effect.u.ramp.envelope;
207 else env = NULL;
208 if (env == NULL) {
209 peff->lpEnvelope = NULL;
210 } else if (peff->lpEnvelope == NULL) {
211 return DIERR_INVALIDPARAM;
212 } else {
213 peff->lpEnvelope->dwAttackLevel = (env->attack_level / 33) * 10;
214 peff->lpEnvelope->dwAttackTime = env->attack_length * 1000;
215 peff->lpEnvelope->dwFadeLevel = (env->fade_level / 33) * 10;
216 peff->lpEnvelope->dwFadeTime = env->fade_length * 1000;
217 }
218 }
219
220 if (dwFlags & DIEP_GAIN) {
221 peff->dwGain = This->gain * 10000 / 0xFFFF;
222 }
223
224 if (dwFlags & DIEP_SAMPLEPERIOD) {
225 /* the linux input ff driver has no support for setting
226 * the playback sample period. 0 means default. */
227 peff->dwSamplePeriod = 0;
228 }
229
230 if (dwFlags & DIEP_STARTDELAY) {
231 peff->dwStartDelay = This->effect.replay.delay * 1000;
232 }
233
234 if (dwFlags & DIEP_TRIGGERBUTTON) {
235 FIXME("LinuxInput button mapping needs redoing; for now, assuming we're using an actual joystick.\n");
236 peff->dwTriggerButton = DIJOFS_BUTTON(This->effect.trigger.button - BTN_JOYSTICK);
237 }
238
239 if (dwFlags & DIEP_TRIGGERREPEATINTERVAL) {
240 peff->dwTriggerRepeatInterval = This->effect.trigger.interval * 1000;
241 }
242
243 if (dwFlags & DIEP_TYPESPECIFICPARAMS) {
244 DWORD expectedsize = 0;
245 if (This->effect.type == FF_PERIODIC) {
246 expectedsize = sizeof(DIPERIODIC);
247 } else if (This->effect.type == FF_CONSTANT) {
248 expectedsize = sizeof(DICONSTANTFORCE);
249 } else if (This->effect.type == FF_SPRING
250 || This->effect.type == FF_FRICTION
251 || This->effect.type == FF_INERTIA
252 || This->effect.type == FF_DAMPER) {
253 expectedsize = sizeof(DICONDITION) * 2;
254 } else if (This->effect.type == FF_RAMP) {
255 expectedsize = sizeof(DIRAMPFORCE);
256 }
257 if (expectedsize > peff->cbTypeSpecificParams)
258 diErr = DIERR_MOREDATA;
259 peff->cbTypeSpecificParams = expectedsize;
260 if (diErr)
261 return diErr;
262 else {
263 if (This->effect.type == FF_PERIODIC) {
264 LPDIPERIODIC tsp = peff->lpvTypeSpecificParams;
265 tsp->dwMagnitude = (This->effect.u.periodic.magnitude / 33) * 10;
266 tsp->lOffset = (This->effect.u.periodic.offset / 33) * 10;
267 tsp->dwPhase = (This->effect.u.periodic.phase / 33) * 36;
268 tsp->dwPeriod = (This->effect.u.periodic.period * 1000);
269 } else if (This->effect.type == FF_CONSTANT) {
270 LPDICONSTANTFORCE tsp = peff->lpvTypeSpecificParams;
271 tsp->lMagnitude = (This->effect.u.constant.level / 33) * 10;
272 } else if (This->effect.type == FF_SPRING
273 || This->effect.type == FF_FRICTION
274 || This->effect.type == FF_INERTIA
275 || This->effect.type == FF_DAMPER) {
276 LPDICONDITION tsp = peff->lpvTypeSpecificParams;
277 int i;
278 for (i = 0; i < 2; ++i) {
279 tsp[i].lOffset = (This->effect.u.condition[i].center / 33) * 10;
280 tsp[i].lPositiveCoefficient = (This->effect.u.condition[i].right_coeff / 33) * 10;
281 tsp[i].lNegativeCoefficient = (This->effect.u.condition[i].left_coeff / 33) * 10;
282 tsp[i].dwPositiveSaturation = (This->effect.u.condition[i].right_saturation / 33) * 10;
283 tsp[i].dwNegativeSaturation = (This->effect.u.condition[i].left_saturation / 33) * 10;
284 tsp[i].lDeadBand = (This->effect.u.condition[i].deadband / 33) * 10;
285 }
286 } else if (This->effect.type == FF_RAMP) {
287 LPDIRAMPFORCE tsp = peff->lpvTypeSpecificParams;
288 tsp->lStart = (This->effect.u.ramp.start_level / 33) * 10;
289 tsp->lEnd = (This->effect.u.ramp.end_level / 33) * 10;
290 }
291 }
292 }
293
294 return diErr;
295 }
296
297 static HRESULT WINAPI LinuxInputEffectImpl_Initialize(
298 LPDIRECTINPUTEFFECT iface,
299 HINSTANCE hinst,
300 DWORD dwVersion,
301 REFGUID rguid)
302 {
303 FIXME("(this=%p,%p,%d,%s): stub!\n",
304 iface, hinst, dwVersion, debugstr_guid(rguid));
305
306 return DI_OK;
307 }
308
309 static HRESULT WINAPI LinuxInputEffectImpl_QueryInterface(
310 LPDIRECTINPUTEFFECT iface,
311 REFIID riid,
312 void **ppvObject)
313 {
314 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
315
316 TRACE("(this=%p,%s,%p)\n", This, debugstr_guid(riid), ppvObject);
317
318 if (IsEqualGUID(&IID_IUnknown, riid) ||
319 IsEqualGUID(&IID_IDirectInputEffect, riid)) {
320 LinuxInputEffectImpl_AddRef(iface);
321 *ppvObject = This;
322 return 0;
323 }
324
325 TRACE("Unsupported interface!\n");
326 return E_FAIL;
327 }
328
329 static HRESULT WINAPI LinuxInputEffectImpl_Start(
330 LPDIRECTINPUTEFFECT iface,
331 DWORD dwIterations,
332 DWORD dwFlags)
333 {
334 struct input_event event;
335 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
336
337 TRACE("(this=%p,%d,%d)\n", This, dwIterations, dwFlags);
338
339 if (!(dwFlags & DIES_NODOWNLOAD)) {
340 /* Download the effect if necessary */
341 if (This->effect.id == -1) {
342 HRESULT res = LinuxInputEffectImpl_Download(iface);
343 if (res != DI_OK)
344 return res;
345 }
346 }
347
348 if (dwFlags & DIES_SOLO) {
349 FIXME("Solo mode requested: should be stopping all effects here!\n");
350 }
351
352 event.type = EV_FF;
353 event.code = This->effect.id;
354 event.value = min( dwIterations, INT_MAX );
355 if (write(*(This->fd), &event, sizeof(event)) == -1) {
356 FIXME("Unable to write event. Assuming device disconnected.\n");
357 return DIERR_INPUTLOST;
358 }
359
360 return DI_OK;
361 }
362
363 static HRESULT WINAPI LinuxInputEffectImpl_SetParameters(
364 LPDIRECTINPUTEFFECT iface,
365 LPCDIEFFECT peff,
366 DWORD dwFlags)
367 {
368 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
369 DWORD type = typeFromGUID(&This->guid);
370 HRESULT retval = DI_OK;
371
372 TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
373
374 dump_DIEFFECT(peff, &This->guid, dwFlags);
375
376 if ((dwFlags & ~DIEP_NORESTART & ~DIEP_NODOWNLOAD & ~DIEP_START) == 0) {
377 /* set everything */
378 dwFlags = DIEP_AXES | DIEP_DIRECTION | DIEP_DURATION | DIEP_ENVELOPE |
379 DIEP_GAIN | DIEP_SAMPLEPERIOD | DIEP_STARTDELAY | DIEP_TRIGGERBUTTON |
380 DIEP_TRIGGERREPEATINTERVAL | DIEP_TYPESPECIFICPARAMS;
381 }
382
383 if (dwFlags & DIEP_AXES) {
384 /* the linux input effect system only supports one or two axes */
385 if (peff->cAxes > 2)
386 return DIERR_INVALIDPARAM;
387 else if (peff->cAxes < 1)
388 return DIERR_INCOMPLETEEFFECT;
389 This->first_axis_is_x = peff->rgdwAxes[0] == DIJOFS_X;
390 }
391
392 /* some of this may look funky, but it's 'cause the linux driver and directx have
393 * different opinions about which way direction "0" is. directx has 0 along the x
394 * axis (left), linux has it along the y axis (down). */
395 if (dwFlags & DIEP_DIRECTION) {
396 if (peff->cAxes == 1) {
397 if (peff->dwFlags & DIEFF_CARTESIAN) {
398 if (dwFlags & DIEP_AXES) {
399 if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] >= 0)
400 This->effect.direction = 0x4000;
401 else if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] < 0)
402 This->effect.direction = 0xC000;
403 else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] >= 0)
404 This->effect.direction = 0;
405 else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] < 0)
406 This->effect.direction = 0x8000;
407 }
408 } else {
409 /* one-axis effects must use cartesian coords */
410 return DIERR_INVALIDPARAM;
411 }
412 } else { /* two axes */
413 if (peff->dwFlags & DIEFF_CARTESIAN) {
414 LONG x, y;
415 if (This->first_axis_is_x) {
416 x = peff->rglDirection[0];
417 y = peff->rglDirection[1];
418 } else {
419 x = peff->rglDirection[1];
420 y = peff->rglDirection[0];
421 }
422 This->effect.direction = (int)((3 * M_PI / 2 - atan2(y, x)) * -0x7FFF / M_PI);
423 } else {
424 /* Polar and spherical are the same for 2 axes */
425 /* Precision is important here, so we do double math with exact constants */
426 This->effect.direction = (int)(((double)peff->rglDirection[0] - 90) / 35999) * 0x7FFF;
427 }
428 }
429 }
430
431 if (dwFlags & DIEP_DURATION)
432 This->effect.replay.length = peff->dwDuration / 1000;
433
434 if (dwFlags & DIEP_ENVELOPE) {
435 struct ff_envelope* env;
436 if (This->effect.type == FF_CONSTANT) env = &This->effect.u.constant.envelope;
437 else if (This->effect.type == FF_PERIODIC) env = &This->effect.u.periodic.envelope;
438 else if (This->effect.type == FF_RAMP) env = &This->effect.u.ramp.envelope;
439 else env = NULL;
440
441 if (peff->lpEnvelope == NULL) {
442 /* if this type had an envelope, reset it */
443 if (env) {
444 env->attack_length = 0;
445 env->attack_level = 0;
446 env->fade_length = 0;
447 env->fade_level = 0;
448 }
449 } else {
450 /* did we get passed an envelope for a type that doesn't even have one? */
451 if (!env) return DIERR_INVALIDPARAM;
452 /* copy the envelope */
453 env->attack_length = peff->lpEnvelope->dwAttackTime / 1000;
454 env->attack_level = (peff->lpEnvelope->dwAttackLevel / 10) * 32;
455 env->fade_length = peff->lpEnvelope->dwFadeTime / 1000;
456 env->fade_level = (peff->lpEnvelope->dwFadeLevel / 10) * 32;
457 }
458 }
459
460 /* Gain and Sample Period settings are not supported by the linux
461 * event system */
462 if (dwFlags & DIEP_GAIN) {
463 This->gain = 0xFFFF * peff->dwGain / 10000;
464 TRACE("Effect gain requested but no effect gain functionality present.\n");
465 }
466
467 if (dwFlags & DIEP_SAMPLEPERIOD)
468 TRACE("Sample period requested but no sample period functionality present.\n");
469
470 if (dwFlags & DIEP_STARTDELAY)
471 This->effect.replay.delay = peff->dwStartDelay / 1000;
472
473 if (dwFlags & DIEP_TRIGGERBUTTON) {
474 if (peff->dwTriggerButton != -1) {
475 FIXME("Linuxinput button mapping needs redoing, assuming we're using a joystick.\n");
476 FIXME("Trigger button translation not yet implemented!\n");
477 }
478 This->effect.trigger.button = 0;
479 }
480
481 if (dwFlags & DIEP_TRIGGERREPEATINTERVAL)
482 This->effect.trigger.interval = peff->dwTriggerRepeatInterval / 1000;
483
484 if (dwFlags & DIEP_TYPESPECIFICPARAMS) {
485 if (!(peff->lpvTypeSpecificParams))
486 return DIERR_INCOMPLETEEFFECT;
487 if (type == DIEFT_PERIODIC) {
488 LPCDIPERIODIC tsp;
489 if (peff->cbTypeSpecificParams != sizeof(DIPERIODIC))
490 return DIERR_INVALIDPARAM;
491 tsp = peff->lpvTypeSpecificParams;
492 This->effect.u.periodic.magnitude = (tsp->dwMagnitude / 10) * 32;
493 This->effect.u.periodic.offset = (tsp->lOffset / 10) * 32;
494 This->effect.u.periodic.phase = (tsp->dwPhase / 9) * 8; /* == (/ 36 * 32) */
495 This->effect.u.periodic.period = tsp->dwPeriod / 1000;
496 } else if (type == DIEFT_CONSTANTFORCE) {
497 LPCDICONSTANTFORCE tsp;
498 if (peff->cbTypeSpecificParams != sizeof(DICONSTANTFORCE))
499 return DIERR_INVALIDPARAM;
500 tsp = peff->lpvTypeSpecificParams;
501 This->effect.u.constant.level = (max(min(tsp->lMagnitude, 10000), -10000) / 10) * 32;
502 } else if (type == DIEFT_RAMPFORCE) {
503 LPCDIRAMPFORCE tsp;
504 if (peff->cbTypeSpecificParams != sizeof(DIRAMPFORCE))
505 return DIERR_INVALIDPARAM;
506 tsp = peff->lpvTypeSpecificParams;
507 This->effect.u.ramp.start_level = (tsp->lStart / 10) * 32;
508 This->effect.u.ramp.end_level = (tsp->lEnd / 10) * 32;
509 } else if (type == DIEFT_CONDITION) {
510 LPCDICONDITION tsp = peff->lpvTypeSpecificParams;
511 if (peff->cbTypeSpecificParams == sizeof(DICONDITION)) {
512 /* One condition block. This needs to be rotated to direction,
513 * and expanded to separate x and y conditions. */
514 int i;
515 double factor[2], angle;
516 /* rotate so 0 points right */
517 angle = ff_effect_direction_to_rad(This->effect.direction + 0xc000);
518 factor[0] = sin(angle);
519 factor[1] = -cos(angle);
520 for (i = 0; i < 2; ++i) {
521 This->effect.u.condition[i].center = (int)(factor[i] * (tsp->lOffset / 10) * 32);
522 This->effect.u.condition[i].right_coeff = (int)(factor[i] * (tsp->lPositiveCoefficient / 10) * 32);
523 This->effect.u.condition[i].left_coeff = (int)(factor[i] * (tsp->lNegativeCoefficient / 10) * 32);
524 This->effect.u.condition[i].right_saturation = (int)(factor[i] * (tsp->dwPositiveSaturation / 10) * 32);
525 This->effect.u.condition[i].left_saturation = (int)(factor[i] * (tsp->dwNegativeSaturation / 10) * 32);
526 This->effect.u.condition[i].deadband = (int)(factor[i] * (tsp->lDeadBand / 10) * 32);
527 }
528 } else if (peff->cbTypeSpecificParams == 2 * sizeof(DICONDITION)) {
529 /* Two condition blocks. Direct parameter copy. */
530 int i;
531 for (i = 0; i < 2; ++i) {
532 This->effect.u.condition[i].center = (tsp[i].lOffset / 10) * 32;
533 This->effect.u.condition[i].right_coeff = (tsp[i].lPositiveCoefficient / 10) * 32;
534 This->effect.u.condition[i].left_coeff = (tsp[i].lNegativeCoefficient / 10) * 32;
535 This->effect.u.condition[i].right_saturation = (tsp[i].dwPositiveSaturation / 10) * 32;
536 This->effect.u.condition[i].left_saturation = (tsp[i].dwNegativeSaturation / 10) * 32;
537 This->effect.u.condition[i].deadband = (tsp[i].lDeadBand / 10) * 32;
538 }
539 } else {
540 return DIERR_INVALIDPARAM;
541 }
542 } else {
543 FIXME("Custom force types are not supported\n");
544 return DIERR_INVALIDPARAM;
545 }
546 }
547
548 if (!(dwFlags & DIEP_NODOWNLOAD))
549 retval = LinuxInputEffectImpl_Download(iface);
550 if (retval != DI_OK)
551 return DI_DOWNLOADSKIPPED;
552
553 if (dwFlags & DIEP_NORESTART)
554 TRACE("DIEP_NORESTART: not handled (we have no control of that).\n");
555
556 if (dwFlags & DIEP_START)
557 retval = LinuxInputEffectImpl_Start(iface, 1, 0);
558 if (retval != DI_OK)
559 return retval;
560
561 return DI_OK;
562 }
563
564 static HRESULT WINAPI LinuxInputEffectImpl_Stop(
565 LPDIRECTINPUTEFFECT iface)
566 {
567 struct input_event event;
568 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
569
570 TRACE("(this=%p)\n", This);
571
572 event.type = EV_FF;
573 event.code = This->effect.id;
574 event.value = 0;
575 /* we don't care about the success or failure of this call */
576 write(*(This->fd), &event, sizeof(event));
577
578 return DI_OK;
579 }
580
581 static HRESULT WINAPI LinuxInputEffectImpl_Unload(
582 LPDIRECTINPUTEFFECT iface)
583 {
584 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
585 TRACE("(this=%p)\n", This);
586
587 /* Erase the downloaded effect */
588 if (ioctl(*(This->fd), EVIOCRMFF, This->effect.id) == -1)
589 return DIERR_INVALIDPARAM;
590
591 /* Mark the effect as deallocated */
592 This->effect.id = -1;
593
594 return DI_OK;
595 }
596
597 static ULONG WINAPI LinuxInputEffectImpl_Release(LPDIRECTINPUTEFFECT iface)
598 {
599 LinuxInputEffectImpl *This = impl_from_IDirectInputEffect(iface);
600 ULONG ref = InterlockedDecrement(&(This->ref));
601
602 if (ref == 0)
603 {
604 LinuxInputEffectImpl_Stop(iface);
605 LinuxInputEffectImpl_Unload(iface);
606 list_remove(This->entry);
607 HeapFree(GetProcessHeap(), 0, LIST_ENTRY(This->entry, effect_list_item, entry));
608 HeapFree(GetProcessHeap(), 0, This);
609 }
610 return ref;
611 }
612
613 /******************************************************************************
614 * LinuxInputEffect
615 */
616
617 DECLSPEC_HIDDEN HRESULT linuxinput_create_effect(
618 int* fd,
619 REFGUID rguid,
620 struct list *parent_list_entry,
621 LPDIRECTINPUTEFFECT* peff)
622 {
623 LinuxInputEffectImpl* newEffect = HeapAlloc(GetProcessHeap(),
624 HEAP_ZERO_MEMORY, sizeof(LinuxInputEffectImpl));
625 DWORD type = typeFromGUID(rguid);
626
627 newEffect->IDirectInputEffect_iface.lpVtbl = &LinuxInputEffectVtbl;
628 newEffect->ref = 1;
629 newEffect->guid = *rguid;
630 newEffect->fd = fd;
631 newEffect->gain = 0xFFFF;
632
633 /* set the type. this cannot be changed over the effect's life. */
634 switch (type) {
635 case DIEFT_PERIODIC:
636 newEffect->effect.type = FF_PERIODIC;
637 if (IsEqualGUID(rguid, &GUID_Sine)) {
638 newEffect->effect.u.periodic.waveform = FF_SINE;
639 } else if (IsEqualGUID(rguid, &GUID_Triangle)) {
640 newEffect->effect.u.periodic.waveform = FF_TRIANGLE;
641 } else if (IsEqualGUID(rguid, &GUID_Square)) {
642 newEffect->effect.u.periodic.waveform = FF_SQUARE;
643 } else if (IsEqualGUID(rguid, &GUID_SawtoothUp)) {
644 newEffect->effect.u.periodic.waveform = FF_SAW_UP;
645 } else if (IsEqualGUID(rguid, &GUID_SawtoothDown)) {
646 newEffect->effect.u.periodic.waveform = FF_SAW_DOWN;
647 }
648 break;
649 case DIEFT_CONSTANTFORCE:
650 newEffect->effect.type = FF_CONSTANT;
651 break;
652 case DIEFT_RAMPFORCE:
653 newEffect->effect.type = FF_RAMP;
654 break;
655 case DIEFT_CONDITION:
656 if (IsEqualGUID(rguid, &GUID_Spring)) {
657 newEffect->effect.type = FF_SPRING;
658 } else if (IsEqualGUID(rguid, &GUID_Friction)) {
659 newEffect->effect.type = FF_FRICTION;
660 } else if (IsEqualGUID(rguid, &GUID_Inertia)) {
661 newEffect->effect.type = FF_INERTIA;
662 } else if (IsEqualGUID(rguid, &GUID_Damper)) {
663 newEffect->effect.type = FF_DAMPER;
664 }
665 break;
666 case DIEFT_CUSTOMFORCE:
667 FIXME("Custom forces are not supported.\n");
668 HeapFree(GetProcessHeap(), 0, newEffect);
669 return DIERR_INVALIDPARAM;
670 default:
671 FIXME("Unknown force type 0x%x.\n", type);
672 HeapFree(GetProcessHeap(), 0, newEffect);
673 return DIERR_INVALIDPARAM;
674 }
675
676 /* mark as non-uploaded */
677 newEffect->effect.id = -1;
678
679 newEffect->entry = parent_list_entry;
680
681 *peff = &newEffect->IDirectInputEffect_iface;
682
683 TRACE("Creating linux input system effect (%p) with guid %s\n",
684 *peff, _dump_dinput_GUID(rguid));
685
686 return DI_OK;
687 }
688
689 DECLSPEC_HIDDEN HRESULT linuxinput_get_info_A(
690 int fd,
691 REFGUID rguid,
692 LPDIEFFECTINFOA info)
693 {
694 DWORD type = typeFromGUID(rguid);
695
696 TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
697
698 if (!info) return E_POINTER;
699
700 if (info->dwSize != sizeof(DIEFFECTINFOA)) return DIERR_INVALIDPARAM;
701
702 info->guid = *rguid;
703
704 info->dwEffType = type;
705 /* the event device API does not support querying for all these things
706 * therefore we assume that we have support for them
707 * that's not as dangerous as it sounds, since drivers are allowed to
708 * ignore parameters they claim to support anyway */
709 info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE
710 | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
711 | DIEFT_SATURATION | DIEFT_STARTDELAY;
712
713 /* again, assume we have support for everything */
714 info->dwStaticParams = DIEP_ALLPARAMS;
715 info->dwDynamicParams = info->dwStaticParams;
716
717 /* yes, this is windows behavior (print the GUID_Name for name) */
718 strcpy(info->tszName, _dump_dinput_GUID(rguid));
719
720 return DI_OK;
721 }
722
723 DECLSPEC_HIDDEN HRESULT linuxinput_get_info_W(
724 int fd,
725 REFGUID rguid,
726 LPDIEFFECTINFOW info)
727 {
728 DWORD type = typeFromGUID(rguid);
729
730 TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
731
732 if (!info) return E_POINTER;
733
734 if (info->dwSize != sizeof(DIEFFECTINFOW)) return DIERR_INVALIDPARAM;
735
736 info->guid = *rguid;
737
738 info->dwEffType = type;
739 /* the event device API does not support querying for all these things
740 * therefore we assume that we have support for them
741 * that's not as dangerous as it sounds, since drivers are allowed to
742 * ignore parameters they claim to support anyway */
743 info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE
744 | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
745 | DIEFT_SATURATION | DIEFT_STARTDELAY;
746
747 /* again, assume we have support for everything */
748 info->dwStaticParams = DIEP_ALLPARAMS;
749 info->dwDynamicParams = info->dwStaticParams;
750
751 /* yes, this is windows behavior (print the GUID_Name for name) */
752 MultiByteToWideChar(CP_ACP, 0, _dump_dinput_GUID(rguid), -1,
753 info->tszName, MAX_PATH);
754
755 return DI_OK;
756 }
757
758 static const IDirectInputEffectVtbl LinuxInputEffectVtbl = {
759 LinuxInputEffectImpl_QueryInterface,
760 LinuxInputEffectImpl_AddRef,
761 LinuxInputEffectImpl_Release,
762 LinuxInputEffectImpl_Initialize,
763 LinuxInputEffectImpl_GetEffectGuid,
764 LinuxInputEffectImpl_GetParameters,
765 LinuxInputEffectImpl_SetParameters,
766 LinuxInputEffectImpl_Start,
767 LinuxInputEffectImpl_Stop,
768 LinuxInputEffectImpl_GetEffectStatus,
769 LinuxInputEffectImpl_Download,
770 LinuxInputEffectImpl_Unload,
771 LinuxInputEffectImpl_Escape
772 };
773
774 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */