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