155d800d97a05ee8fd8dcfb0263a22ea4f4f4d6f
[reactos.git] / reactos / dll / directx / 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 <errno.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include <math.h>
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winerror.h"
42 #include "dinput.h"
43
44 #include "device_private.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
47
48 static const IDirectInputEffectVtbl LinuxInputEffectVtbl;
49 typedef struct LinuxInputEffectImpl LinuxInputEffectImpl;
50 struct LinuxInputEffectImpl
51 {
52 const void *lpVtbl;
53 LONG ref;
54 GUID guid;
55
56 struct ff_effect effect; /* Effect data */
57 int gain; /* Effect gain */
58 int* fd; /* Parent device */
59 struct list *entry; /* Entry into the parent's list of effects */
60 };
61
62
63 /******************************************************************************
64 * DirectInputEffect Functional Helper
65 */
66
67 static DWORD _typeFromGUID(REFGUID guid)
68 {
69 if (IsEqualGUID(guid, &GUID_ConstantForce)) {
70 return DIEFT_CONSTANTFORCE;
71 } else if (IsEqualGUID(guid, &GUID_Square)
72 || IsEqualGUID(guid, &GUID_Sine)
73 || IsEqualGUID(guid, &GUID_Triangle)
74 || IsEqualGUID(guid, &GUID_SawtoothUp)
75 || IsEqualGUID(guid, &GUID_SawtoothDown)) {
76 return DIEFT_PERIODIC;
77 } else if (IsEqualGUID(guid, &GUID_RampForce)) {
78 return DIEFT_RAMPFORCE;
79 } else if (IsEqualGUID(guid, &GUID_Spring)
80 || IsEqualGUID(guid, &GUID_Damper)
81 || IsEqualGUID(guid, &GUID_Inertia)
82 || IsEqualGUID(guid, &GUID_Friction)) {
83 return DIEFT_CONDITION;
84 } else if (IsEqualGUID(guid, &GUID_CustomForce)) {
85 return DIEFT_CUSTOMFORCE;
86 } else {
87 WARN("GUID (%s) is not a known force type\n", _dump_dinput_GUID(guid));
88 return 0;
89 }
90 }
91
92
93 /******************************************************************************
94 * DirectInputEffect debug helpers
95 */
96
97 static void _dump_DIEFFECT_flags(DWORD dwFlags)
98 {
99 if (TRACE_ON(dinput)) {
100 unsigned int i;
101 static const struct {
102 DWORD mask;
103 const char *name;
104 } flags[] = {
105 #define FE(x) { x, #x}
106 FE(DIEFF_CARTESIAN),
107 FE(DIEFF_OBJECTIDS),
108 FE(DIEFF_OBJECTOFFSETS),
109 FE(DIEFF_POLAR),
110 FE(DIEFF_SPHERICAL)
111 #undef FE
112 };
113 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
114 if (flags[i].mask & dwFlags)
115 TRACE("%s ", flags[i].name);
116 TRACE("\n");
117 }
118 }
119
120 static void _dump_DIENVELOPE(LPCDIENVELOPE env)
121 {
122 if (env->dwSize != sizeof(DIENVELOPE)) {
123 WARN("Non-standard DIENVELOPE structure size %d.\n", env->dwSize);
124 }
125 TRACE("Envelope has attack (level: %d time: %d), fade (level: %d time: %d)\n",
126 env->dwAttackLevel, env->dwAttackTime, env->dwFadeLevel, env->dwFadeTime);
127 }
128
129 static void _dump_DICONSTANTFORCE(LPCDICONSTANTFORCE frc)
130 {
131 TRACE("Constant force has magnitude %d\n", frc->lMagnitude);
132 }
133
134 static void _dump_DIPERIODIC(LPCDIPERIODIC frc)
135 {
136 TRACE("Periodic force has magnitude %d, offset %d, phase %d, period %d\n",
137 frc->dwMagnitude, frc->lOffset, frc->dwPhase, frc->dwPeriod);
138 }
139
140 static void _dump_DIRAMPFORCE(LPCDIRAMPFORCE frc)
141 {
142 TRACE("Ramp force has start %d, end %d\n",
143 frc->lStart, frc->lEnd);
144 }
145
146 static void _dump_DICONDITION(LPCDICONDITION frc)
147 {
148 TRACE("Condition has offset %d, pos/neg coefficients %d and %d, pos/neg saturations %d and %d, deadband %d\n",
149 frc->lOffset, frc->lPositiveCoefficient, frc->lNegativeCoefficient,
150 frc->dwPositiveSaturation, frc->dwNegativeSaturation, frc->lDeadBand);
151 }
152
153 static void _dump_DICUSTOMFORCE(LPCDICUSTOMFORCE frc)
154 {
155 unsigned int i;
156 TRACE("Custom force uses %d channels, sample period %d. Has %d samples at %p.\n",
157 frc->cChannels, frc->dwSamplePeriod, frc->cSamples, frc->rglForceData);
158 if (frc->cSamples % frc->cChannels != 0)
159 WARN("Custom force has a non-integral samples-per-channel count!\n");
160 if (TRACE_ON(dinput)) {
161 TRACE("Custom force data (time aligned, axes in order):\n");
162 for (i = 1; i <= frc->cSamples; ++i) {
163 TRACE("%d ", frc->rglForceData[i]);
164 if (i % frc->cChannels == 0)
165 TRACE("\n");
166 }
167 }
168 }
169
170 static void _dump_DIEFFECT(LPCDIEFFECT eff, REFGUID guid)
171 {
172 unsigned int i;
173 DWORD type = _typeFromGUID(guid);
174
175 TRACE("Dumping DIEFFECT structure:\n");
176 TRACE(" - dwSize: %d\n", eff->dwSize);
177 if ((eff->dwSize != sizeof(DIEFFECT)) && (eff->dwSize != sizeof(DIEFFECT_DX5))) {
178 WARN("Non-standard DIEFFECT structure size %d\n", eff->dwSize);
179 }
180 TRACE(" - dwFlags: %d\n", eff->dwFlags);
181 TRACE(" ");
182 _dump_DIEFFECT_flags(eff->dwFlags);
183 TRACE(" - dwDuration: %d\n", eff->dwDuration);
184 TRACE(" - dwGain: %d\n", eff->dwGain);
185 if (eff->dwGain > 10000)
186 WARN("dwGain is out of range (>10,000)\n");
187 TRACE(" - dwTriggerButton: %d\n", eff->dwTriggerButton);
188 TRACE(" - dwTriggerRepeatInterval: %d\n", eff->dwTriggerRepeatInterval);
189 TRACE(" - cAxes: %d\n", eff->cAxes);
190 TRACE(" - rgdwAxes: %p\n", eff->rgdwAxes);
191 if (TRACE_ON(dinput) && eff->rgdwAxes) {
192 TRACE(" ");
193 for (i = 0; i < eff->cAxes; ++i)
194 TRACE("%d ", eff->rgdwAxes[i]);
195 TRACE("\n");
196 }
197 TRACE(" - rglDirection: %p\n", eff->rglDirection);
198 TRACE(" - lpEnvelope: %p\n", eff->lpEnvelope);
199 TRACE(" - cbTypeSpecificParams: %d\n", eff->cbTypeSpecificParams);
200 TRACE(" - lpvTypeSpecificParams: %p\n", eff->lpvTypeSpecificParams);
201 if (eff->dwSize > sizeof(DIEFFECT_DX5))
202 TRACE(" - dwStartDelay: %d\n", eff->dwStartDelay);
203 if (eff->lpEnvelope != NULL)
204 _dump_DIENVELOPE(eff->lpEnvelope);
205 if (type == DIEFT_CONSTANTFORCE) {
206 if (eff->cbTypeSpecificParams != sizeof(DICONSTANTFORCE)) {
207 WARN("Effect claims to be a constant force but the type-specific params are the wrong size!\n");
208 } else {
209 _dump_DICONSTANTFORCE(eff->lpvTypeSpecificParams);
210 }
211 } else if (type == DIEFT_PERIODIC) {
212 if (eff->cbTypeSpecificParams != sizeof(DIPERIODIC)) {
213 WARN("Effect claims to be a periodic force but the type-specific params are the wrong size!\n");
214 } else {
215 _dump_DIPERIODIC(eff->lpvTypeSpecificParams);
216 }
217 } else if (type == DIEFT_RAMPFORCE) {
218 if (eff->cbTypeSpecificParams != sizeof(DIRAMPFORCE)) {
219 WARN("Effect claims to be a ramp force but the type-specific params are the wrong size!\n");
220 } else {
221 _dump_DIRAMPFORCE(eff->lpvTypeSpecificParams);
222 }
223 } else if (type == DIEFT_CONDITION) {
224 if (eff->cbTypeSpecificParams != sizeof(DICONDITION)) {
225 WARN("Effect claims to be a condition but the type-specific params are the wrong size!\n");
226 } else {
227 _dump_DICONDITION(eff->lpvTypeSpecificParams);
228 }
229 } else if (type == DIEFT_CUSTOMFORCE) {
230 if (eff->cbTypeSpecificParams != sizeof(DICUSTOMFORCE)) {
231 WARN("Effect claims to be a custom force but the type-specific params are the wrong size!\n");
232 } else {
233 _dump_DICUSTOMFORCE(eff->lpvTypeSpecificParams);
234 }
235 }
236 }
237
238
239 /******************************************************************************
240 * LinuxInputEffectImpl
241 */
242
243 static ULONG WINAPI LinuxInputEffectImpl_AddRef(
244 LPDIRECTINPUTEFFECT iface)
245 {
246 LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
247 return InterlockedIncrement(&(This->ref));
248 }
249
250 static HRESULT WINAPI LinuxInputEffectImpl_Download(
251 LPDIRECTINPUTEFFECT iface)
252 {
253 LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
254
255 TRACE("(this=%p)\n", This);
256
257 if (ioctl(*(This->fd), EVIOCSFF, &This->effect) == -1) {
258 if (errno == ENOMEM) {
259 return DIERR_DEVICEFULL;
260 } else {
261 FIXME("Could not upload effect. Assuming a disconnected device %d \"%s\".\n", *This->fd, strerror(errno));
262 return DIERR_INPUTLOST;
263 }
264 }
265
266 return DI_OK;
267 }
268
269 static HRESULT WINAPI LinuxInputEffectImpl_Escape(
270 LPDIRECTINPUTEFFECT iface,
271 LPDIEFFESCAPE pesc)
272 {
273 WARN("(this=%p,%p): invalid: no hardware-specific escape codes in this"
274 " driver!\n", iface, pesc);
275
276 return DI_OK;
277 }
278
279 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectGuid(
280 LPDIRECTINPUTEFFECT iface,
281 LPGUID pguid)
282 {
283 LinuxInputEffectImpl *This = (LinuxInputEffectImpl*)iface;
284
285 TRACE("(this=%p,%p)\n", This, pguid);
286
287 pguid = &This->guid;
288
289 return DI_OK;
290 }
291
292 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectStatus(
293 LPDIRECTINPUTEFFECT iface,
294 LPDWORD pdwFlags)
295 {
296 TRACE("(this=%p,%p)\n", iface, pdwFlags);
297
298 /* linux sends the effect status through an event.
299 * that event is trapped by our parent joystick driver
300 * and there is no clean way to pass it back to us. */
301 FIXME("Not enough information to provide a status.\n");
302
303 (*pdwFlags) = 0;
304
305 return DI_OK;
306 }
307
308 static HRESULT WINAPI LinuxInputEffectImpl_GetParameters(
309 LPDIRECTINPUTEFFECT iface,
310 LPDIEFFECT peff,
311 DWORD dwFlags)
312 {
313 HRESULT diErr = DI_OK;
314 LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
315 TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
316
317 /* Major conversion factors are:
318 * times: millisecond (linux) -> microsecond (windows) (x * 1000)
319 * forces: scale 0x7FFF (linux) -> scale 10000 (windows) approx ((x / 33) * 10)
320 * angles: scale 0x7FFF (linux) -> scale 35999 (windows) approx ((x / 33) * 36)
321 * angle bases: 0 -> -y (down) (linux) -> 0 -> +x (right) (windows)
322 */
323
324 if (dwFlags & DIEP_AXES) {
325 if (peff->cAxes < 2 /* linuxinput effects always use 2 axes, x and y */)
326 diErr = DIERR_MOREDATA;
327 peff->cAxes = 2;
328 if (diErr)
329 return diErr;
330 else {
331 peff->rgdwAxes[0] = DIJOFS_X;
332 peff->rgdwAxes[1] = DIJOFS_Y;
333 }
334 }
335
336 if (dwFlags & DIEP_DIRECTION) {
337 if (peff->cAxes < 2)
338 diErr = DIERR_MOREDATA;
339 peff->cAxes = 2;
340 if (diErr)
341 return diErr;
342 else {
343 if (peff->dwFlags & DIEFF_CARTESIAN) {
344 peff->rglDirection[0] = (long)(sin(M_PI * 3 * This->effect.direction / 0x7FFF) * 1000);
345 peff->rglDirection[1] = (long)(cos(M_PI * 3 * This->effect.direction / 0x7FFF) * 1000);
346 } else {
347 /* Polar and spherical coordinates are the same for two or less
348 * axes.
349 * Note that we also use this case if NO flags are marked.
350 * According to MSDN, we should return the direction in the
351 * format that it was specified in, if no flags are marked.
352 */
353 peff->rglDirection[0] = (This->effect.direction / 33) * 36 + 9000;
354 if (peff->rglDirection[0] > 35999)
355 peff->rglDirection[0] -= 35999;
356 }
357 }
358 }
359
360 if (dwFlags & DIEP_DURATION) {
361 peff->dwDuration = (DWORD)This->effect.replay.length * 1000;
362 }
363
364 if (dwFlags & DIEP_ENVELOPE) {
365 struct ff_envelope* env;
366 if (This->effect.type == FF_CONSTANT) env = &This->effect.u.constant.envelope;
367 else if (This->effect.type == FF_PERIODIC) env = &This->effect.u.periodic.envelope;
368 else if (This->effect.type == FF_RAMP) env = &This->effect.u.ramp.envelope;
369 else env = NULL;
370 if (env == NULL) {
371 peff->lpEnvelope = NULL;
372 } else if (peff->lpEnvelope == NULL) {
373 return DIERR_INVALIDPARAM;
374 } else {
375 peff->lpEnvelope->dwAttackLevel = (env->attack_level / 33) * 10;
376 peff->lpEnvelope->dwAttackTime = env->attack_length * 1000;
377 peff->lpEnvelope->dwFadeLevel = (env->fade_level / 33) * 10;
378 peff->lpEnvelope->dwFadeTime = env->fade_length * 1000;
379 }
380 }
381
382 if (dwFlags & DIEP_GAIN) {
383 peff->dwGain = This->gain * 10000 / 0xFFFF;
384 }
385
386 if (dwFlags & DIEP_SAMPLEPERIOD) {
387 /* the linux input ff driver has no support for setting
388 * the playback sample period. 0 means default. */
389 peff->dwSamplePeriod = 0;
390 }
391
392 if (dwFlags & DIEP_STARTDELAY) {
393 peff->dwStartDelay = This->effect.replay.delay * 1000;
394 }
395
396 if (dwFlags & DIEP_TRIGGERBUTTON) {
397 FIXME("LinuxInput button mapping needs redoing; for now, assuming we're using an actual joystick.\n");
398 peff->dwTriggerButton = DIJOFS_BUTTON(This->effect.trigger.button - BTN_JOYSTICK);
399 }
400
401 if (dwFlags & DIEP_TRIGGERREPEATINTERVAL) {
402 peff->dwTriggerRepeatInterval = This->effect.trigger.interval * 1000;
403 }
404
405 if (dwFlags & DIEP_TYPESPECIFICPARAMS) {
406 DWORD expectedsize = 0;
407 if (This->effect.type == FF_PERIODIC) {
408 expectedsize = sizeof(DIPERIODIC);
409 } else if (This->effect.type == FF_CONSTANT) {
410 expectedsize = sizeof(DICONSTANTFORCE);
411 } else if (This->effect.type == FF_SPRING
412 || This->effect.type == FF_FRICTION
413 || This->effect.type == FF_INERTIA
414 || This->effect.type == FF_DAMPER) {
415 expectedsize = sizeof(DICONDITION) * 2;
416 } else if (This->effect.type == FF_RAMP) {
417 expectedsize = sizeof(DIRAMPFORCE);
418 }
419 if (expectedsize > peff->cbTypeSpecificParams)
420 diErr = DIERR_MOREDATA;
421 peff->cbTypeSpecificParams = expectedsize;
422 if (diErr)
423 return diErr;
424 else {
425 if (This->effect.type == FF_PERIODIC) {
426 LPDIPERIODIC tsp = peff->lpvTypeSpecificParams;
427 tsp->dwMagnitude = (This->effect.u.periodic.magnitude / 33) * 10;
428 tsp->lOffset = (This->effect.u.periodic.offset / 33) * 10;
429 tsp->dwPhase = (This->effect.u.periodic.phase / 33) * 36;
430 tsp->dwPeriod = (This->effect.u.periodic.period * 1000);
431 } else if (This->effect.type == FF_CONSTANT) {
432 LPDICONSTANTFORCE tsp = peff->lpvTypeSpecificParams;
433 tsp->lMagnitude = (This->effect.u.constant.level / 33) * 10;
434 } else if (This->effect.type == FF_SPRING
435 || This->effect.type == FF_FRICTION
436 || This->effect.type == FF_INERTIA
437 || This->effect.type == FF_DAMPER) {
438 LPDICONDITION tsp = peff->lpvTypeSpecificParams;
439 int i;
440 for (i = 0; i < 2; ++i) {
441 tsp[i].lOffset = (This->effect.u.condition[i].center / 33) * 10;
442 tsp[i].lPositiveCoefficient = (This->effect.u.condition[i].right_coeff / 33) * 10;
443 tsp[i].lNegativeCoefficient = (This->effect.u.condition[i].left_coeff / 33) * 10;
444 tsp[i].dwPositiveSaturation = (This->effect.u.condition[i].right_saturation / 33) * 10;
445 tsp[i].dwNegativeSaturation = (This->effect.u.condition[i].left_saturation / 33) * 10;
446 tsp[i].lDeadBand = (This->effect.u.condition[i].deadband / 33) * 10;
447 }
448 } else if (This->effect.type == FF_RAMP) {
449 LPDIRAMPFORCE tsp = peff->lpvTypeSpecificParams;
450 tsp->lStart = (This->effect.u.ramp.start_level / 33) * 10;
451 tsp->lEnd = (This->effect.u.ramp.end_level / 33) * 10;
452 }
453 }
454 }
455
456 return diErr;
457 }
458
459 static HRESULT WINAPI LinuxInputEffectImpl_Initialize(
460 LPDIRECTINPUTEFFECT iface,
461 HINSTANCE hinst,
462 DWORD dwVersion,
463 REFGUID rguid)
464 {
465 FIXME("(this=%p,%p,%d,%s): stub!\n",
466 iface, hinst, dwVersion, debugstr_guid(rguid));
467
468 return DI_OK;
469 }
470
471 static HRESULT WINAPI LinuxInputEffectImpl_QueryInterface(
472 LPDIRECTINPUTEFFECT iface,
473 REFIID riid,
474 void **ppvObject)
475 {
476 LinuxInputEffectImpl* This = (LinuxInputEffectImpl*)iface;
477
478 TRACE("(this=%p,%s,%p)\n", This, debugstr_guid(riid), ppvObject);
479
480 if (IsEqualGUID(&IID_IUnknown, riid) ||
481 IsEqualGUID(&IID_IDirectInputEffect, riid)) {
482 LinuxInputEffectImpl_AddRef(iface);
483 *ppvObject = This;
484 return 0;
485 }
486
487 TRACE("Unsupported interface!\n");
488 return E_FAIL;
489 }
490
491 static HRESULT WINAPI LinuxInputEffectImpl_Start(
492 LPDIRECTINPUTEFFECT iface,
493 DWORD dwIterations,
494 DWORD dwFlags)
495 {
496 struct input_event event;
497 LinuxInputEffectImpl* This = (LinuxInputEffectImpl*)iface;
498
499 TRACE("(this=%p,%d,%d)\n", This, dwIterations, dwFlags);
500
501 if (!(dwFlags & DIES_NODOWNLOAD)) {
502 /* Download the effect if necessary */
503 if (This->effect.id == -1) {
504 HRESULT res = LinuxInputEffectImpl_Download(iface);
505 if (res != DI_OK)
506 return res;
507 }
508 }
509
510 if (dwFlags & DIES_SOLO) {
511 FIXME("Solo mode requested: should be stopping all effects here!\n");
512 }
513
514 event.type = EV_FF;
515
516 event.code = FF_GAIN;
517 event.value = This->gain;
518 if (write(*(This->fd), &event, sizeof(event)) == -1)
519 FIXME("Failed setting gain. Error: %d \"%s\".\n", errno, strerror(errno));
520
521 event.code = This->effect.id;
522 event.value = dwIterations;
523 if (write(*(This->fd), &event, sizeof(event)) == -1) {
524 FIXME("Unable to write event. Assuming device disconnected.\n");
525 return DIERR_INPUTLOST;
526 }
527
528 return DI_OK;
529 }
530
531 static HRESULT WINAPI LinuxInputEffectImpl_SetParameters(
532 LPDIRECTINPUTEFFECT iface,
533 LPCDIEFFECT peff,
534 DWORD dwFlags)
535 {
536 LinuxInputEffectImpl* This = (LinuxInputEffectImpl*)iface;
537 DWORD type = _typeFromGUID(&This->guid);
538 HRESULT retval = DI_OK;
539
540 TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
541
542 _dump_DIEFFECT(peff, &This->guid);
543
544 if ((dwFlags & ~DIEP_NORESTART & ~DIEP_NODOWNLOAD & ~DIEP_START) == 0) {
545 /* set everything */
546 dwFlags = DIEP_AXES | DIEP_DIRECTION | DIEP_DURATION | DIEP_ENVELOPE |
547 DIEP_GAIN | DIEP_SAMPLEPERIOD | DIEP_STARTDELAY | DIEP_TRIGGERBUTTON |
548 DIEP_TRIGGERREPEATINTERVAL | DIEP_TYPESPECIFICPARAMS;
549 }
550
551 if (dwFlags & DIEP_AXES) {
552 /* the linux input effect system only supports one or two axes */
553 if (peff->cAxes > 2)
554 return DIERR_INVALIDPARAM;
555 else if (peff->cAxes < 1)
556 return DIERR_INCOMPLETEEFFECT;
557 }
558
559 /* some of this may look funky, but it's 'cause the linux driver and directx have
560 * different opinions about which way direction "0" is. directx has 0 along the x
561 * axis (left), linux has it along the y axis (down). */
562 if (dwFlags & DIEP_DIRECTION) {
563 if (peff->cAxes == 1) {
564 if (peff->dwFlags & DIEFF_CARTESIAN) {
565 if (dwFlags & DIEP_AXES) {
566 if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] >= 0)
567 This->effect.direction = 0x4000;
568 else if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] < 0)
569 This->effect.direction = 0xC000;
570 else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] >= 0)
571 This->effect.direction = 0;
572 else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] < 0)
573 This->effect.direction = 0x8000;
574 }
575 } else {
576 /* one-axis effects must use cartesian coords */
577 return DIERR_INVALIDPARAM;
578 }
579 } else { /* two axes */
580 if (peff->dwFlags & DIEFF_CARTESIAN) {
581 /* avoid divide-by-zero */
582 if (peff->rglDirection[1] == 0) {
583 if (peff->rglDirection[0] >= 0)
584 This->effect.direction = 0x4000;
585 else if (peff->rglDirection[0] < 0)
586 This->effect.direction = 0xC000;
587 } else {
588 This->effect.direction = (int)(atan(peff->rglDirection[0] / peff->rglDirection[1]) * 0x7FFF / (3 * M_PI));
589 }
590 } else {
591 /* Polar and spherical are the same for 2 axes */
592 /* Precision is important here, so we do double math with exact constants */
593 This->effect.direction = (int)(((double)peff->rglDirection[0] - 90) / 35999) * 0x7FFF;
594 }
595 }
596 }
597
598 if (dwFlags & DIEP_DURATION)
599 This->effect.replay.length = peff->dwDuration / 1000;
600
601 if (dwFlags & DIEP_ENVELOPE) {
602 struct ff_envelope* env;
603 if (This->effect.type == FF_CONSTANT) env = &This->effect.u.constant.envelope;
604 else if (This->effect.type == FF_PERIODIC) env = &This->effect.u.periodic.envelope;
605 else if (This->effect.type == FF_RAMP) env = &This->effect.u.ramp.envelope;
606 else env = NULL;
607
608 if (peff->lpEnvelope == NULL) {
609 /* if this type had an envelope, reset it
610 * note that length can never be zero, so we set it to something minuscule */
611 if (env) {
612 env->attack_length = 0x10;
613 env->attack_level = 0x7FFF;
614 env->fade_length = 0x10;
615 env->fade_level = 0x7FFF;
616 }
617 } else {
618 /* did we get passed an envelope for a type that doesn't even have one? */
619 if (!env) return DIERR_INVALIDPARAM;
620 /* copy the envelope */
621 env->attack_length = peff->lpEnvelope->dwAttackTime / 1000;
622 env->attack_level = (peff->lpEnvelope->dwAttackLevel / 10) * 32;
623 env->fade_length = peff->lpEnvelope->dwFadeTime / 1000;
624 env->fade_level = (peff->lpEnvelope->dwFadeLevel / 10) * 32;
625 }
626 }
627
628 /* Gain and Sample Period settings are not supported by the linux
629 * event system */
630 if (dwFlags & DIEP_GAIN)
631 This->gain = 0xFFFF * peff->dwGain / 10000;
632
633 if (dwFlags & DIEP_SAMPLEPERIOD)
634 TRACE("Sample period requested but no sample period functionality present.\n");
635
636 if (dwFlags & DIEP_STARTDELAY)
637 This->effect.replay.delay = peff->dwStartDelay / 1000;
638
639 if (dwFlags & DIEP_TRIGGERBUTTON) {
640 if (peff->dwTriggerButton != -1) {
641 FIXME("Linuxinput button mapping needs redoing, assuming we're using a joystick.\n");
642 FIXME("Trigger button translation not yet implemented!\n");
643 }
644 This->effect.trigger.button = 0;
645 }
646
647 if (dwFlags & DIEP_TRIGGERREPEATINTERVAL)
648 This->effect.trigger.interval = peff->dwTriggerRepeatInterval / 1000;
649
650 if (dwFlags & DIEP_TYPESPECIFICPARAMS) {
651 if (!(peff->lpvTypeSpecificParams))
652 return DIERR_INCOMPLETEEFFECT;
653 if (type == DIEFT_PERIODIC) {
654 LPCDIPERIODIC tsp;
655 if (peff->cbTypeSpecificParams != sizeof(DIPERIODIC))
656 return DIERR_INVALIDPARAM;
657 tsp = peff->lpvTypeSpecificParams;
658 This->effect.u.periodic.magnitude = (tsp->dwMagnitude / 10) * 32;
659 This->effect.u.periodic.offset = (tsp->lOffset / 10) * 32;
660 This->effect.u.periodic.phase = (tsp->dwPhase / 9) * 8; /* == (/ 36 * 32) */
661 This->effect.u.periodic.period = tsp->dwPeriod / 1000;
662 } else if (type == DIEFT_CONSTANTFORCE) {
663 LPCDICONSTANTFORCE tsp;
664 if (peff->cbTypeSpecificParams != sizeof(DICONSTANTFORCE))
665 return DIERR_INVALIDPARAM;
666 tsp = peff->lpvTypeSpecificParams;
667 This->effect.u.constant.level = (max(min(tsp->lMagnitude, 10000), -10000) / 10) * 32;
668 } else if (type == DIEFT_RAMPFORCE) {
669 LPCDIRAMPFORCE tsp;
670 if (peff->cbTypeSpecificParams != sizeof(DIRAMPFORCE))
671 return DIERR_INVALIDPARAM;
672 tsp = peff->lpvTypeSpecificParams;
673 This->effect.u.ramp.start_level = (tsp->lStart / 10) * 32;
674 This->effect.u.ramp.end_level = (tsp->lStart / 10) * 32;
675 } else if (type == DIEFT_CONDITION) {
676 LPCDICONDITION tsp = peff->lpvTypeSpecificParams;
677 if (peff->cbTypeSpecificParams == sizeof(DICONDITION)) {
678 /* One condition block. This needs to be rotated to direction,
679 * and expanded to separate x and y conditions. */
680 int i;
681 double factor[2];
682 factor[0] = asin((This->effect.direction * 3.0 * M_PI) / 0x7FFF);
683 factor[1] = acos((This->effect.direction * 3.0 * M_PI) / 0x7FFF);
684 for (i = 0; i < 2; ++i) {
685 This->effect.u.condition[i].center = (int)(factor[i] * (tsp->lOffset / 10) * 32);
686 This->effect.u.condition[i].right_coeff = (int)(factor[i] * (tsp->lPositiveCoefficient / 10) * 32);
687 This->effect.u.condition[i].left_coeff = (int)(factor[i] * (tsp->lNegativeCoefficient / 10) * 32);
688 This->effect.u.condition[i].right_saturation = (int)(factor[i] * (tsp->dwPositiveSaturation / 10) * 32);
689 This->effect.u.condition[i].left_saturation = (int)(factor[i] * (tsp->dwNegativeSaturation / 10) * 32);
690 This->effect.u.condition[i].deadband = (int)(factor[i] * (tsp->lDeadBand / 10) * 32);
691 }
692 } else if (peff->cbTypeSpecificParams == 2 * sizeof(DICONDITION)) {
693 /* Two condition blocks. Direct parameter copy. */
694 int i;
695 for (i = 0; i < 2; ++i) {
696 This->effect.u.condition[i].center = (tsp[i].lOffset / 10) * 32;
697 This->effect.u.condition[i].right_coeff = (tsp[i].lPositiveCoefficient / 10) * 32;
698 This->effect.u.condition[i].left_coeff = (tsp[i].lNegativeCoefficient / 10) * 32;
699 This->effect.u.condition[i].right_saturation = (tsp[i].dwPositiveSaturation / 10) * 32;
700 This->effect.u.condition[i].left_saturation = (tsp[i].dwNegativeSaturation / 10) * 32;
701 This->effect.u.condition[i].deadband = (tsp[i].lDeadBand / 10) * 32;
702 }
703 } else {
704 return DIERR_INVALIDPARAM;
705 }
706 } else {
707 FIXME("Custom force types are not supported\n");
708 return DIERR_INVALIDPARAM;
709 }
710 }
711
712 if (!(dwFlags & DIEP_NODOWNLOAD))
713 retval = LinuxInputEffectImpl_Download(iface);
714 if (retval != DI_OK)
715 return DI_DOWNLOADSKIPPED;
716
717 if (dwFlags & DIEP_NORESTART)
718 TRACE("DIEP_NORESTART: not handled (we have no control of that).\n");
719
720 if (dwFlags & DIEP_START)
721 retval = LinuxInputEffectImpl_Start(iface, 1, 0);
722 if (retval != DI_OK)
723 return retval;
724
725 return DI_OK;
726 }
727
728 static HRESULT WINAPI LinuxInputEffectImpl_Stop(
729 LPDIRECTINPUTEFFECT iface)
730 {
731 struct input_event event;
732 LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
733
734 TRACE("(this=%p)\n", This);
735
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));
741
742 return DI_OK;
743 }
744
745 static HRESULT WINAPI LinuxInputEffectImpl_Unload(
746 LPDIRECTINPUTEFFECT iface)
747 {
748 LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
749 TRACE("(this=%p)\n", This);
750
751 /* Erase the downloaded effect */
752 if (ioctl(*(This->fd), EVIOCRMFF, This->effect.id) == -1)
753 return DIERR_INVALIDPARAM;
754
755 /* Mark the effect as deallocated */
756 This->effect.id = -1;
757
758 return DI_OK;
759 }
760
761 static ULONG WINAPI LinuxInputEffectImpl_Release(LPDIRECTINPUTEFFECT iface)
762 {
763 LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
764 ULONG ref = InterlockedDecrement(&(This->ref));
765
766 if (ref == 0)
767 {
768 LinuxInputEffectImpl_Stop(iface);
769 LinuxInputEffectImpl_Unload(iface);
770 list_remove(This->entry);
771 HeapFree(GetProcessHeap(), 0, LIST_ENTRY(This->entry, effect_list_item, entry));
772 HeapFree(GetProcessHeap(), 0, This);
773 }
774 return ref;
775 }
776
777 /******************************************************************************
778 * LinuxInputEffect
779 */
780
781 HRESULT linuxinput_create_effect(
782 int* fd,
783 REFGUID rguid,
784 struct list *parent_list_entry,
785 LPDIRECTINPUTEFFECT* peff)
786 {
787 LinuxInputEffectImpl* newEffect = HeapAlloc(GetProcessHeap(),
788 HEAP_ZERO_MEMORY, sizeof(LinuxInputEffectImpl));
789 DWORD type = _typeFromGUID(rguid);
790
791 newEffect->lpVtbl = &LinuxInputEffectVtbl;
792 newEffect->ref = 1;
793 newEffect->guid = *rguid;
794 newEffect->fd = fd;
795 newEffect->gain = 0xFFFF;
796
797 /* set the type. this cannot be changed over the effect's life. */
798 switch (type) {
799 case DIEFT_PERIODIC:
800 newEffect->effect.type = FF_PERIODIC;
801 if (IsEqualGUID(rguid, &GUID_Sine)) {
802 newEffect->effect.u.periodic.waveform = FF_SINE;
803 } else if (IsEqualGUID(rguid, &GUID_Triangle)) {
804 newEffect->effect.u.periodic.waveform = FF_TRIANGLE;
805 } else if (IsEqualGUID(rguid, &GUID_Square)) {
806 newEffect->effect.u.periodic.waveform = FF_SQUARE;
807 } else if (IsEqualGUID(rguid, &GUID_SawtoothUp)) {
808 newEffect->effect.u.periodic.waveform = FF_SAW_UP;
809 } else if (IsEqualGUID(rguid, &GUID_SawtoothDown)) {
810 newEffect->effect.u.periodic.waveform = FF_SAW_DOWN;
811 }
812 break;
813 case DIEFT_CONSTANTFORCE:
814 newEffect->effect.type = FF_CONSTANT;
815 break;
816 case DIEFT_RAMPFORCE:
817 newEffect->effect.type = FF_RAMP;
818 break;
819 case DIEFT_CONDITION:
820 if (IsEqualGUID(rguid, &GUID_Spring)) {
821 newEffect->effect.type = FF_SPRING;
822 } else if (IsEqualGUID(rguid, &GUID_Friction)) {
823 newEffect->effect.type = FF_FRICTION;
824 } else if (IsEqualGUID(rguid, &GUID_Inertia)) {
825 newEffect->effect.type = FF_INERTIA;
826 } else if (IsEqualGUID(rguid, &GUID_Damper)) {
827 newEffect->effect.type = FF_DAMPER;
828 }
829 break;
830 case DIEFT_CUSTOMFORCE:
831 FIXME("Custom forces are not supported.\n");
832 HeapFree(GetProcessHeap(), 0, newEffect);
833 return DIERR_INVALIDPARAM;
834 default:
835 FIXME("Unknown force type 0x%x.\n", type);
836 HeapFree(GetProcessHeap(), 0, newEffect);
837 return DIERR_INVALIDPARAM;
838 }
839
840 /* mark as non-uploaded */
841 newEffect->effect.id = -1;
842
843 newEffect->entry = parent_list_entry;
844
845 *peff = (LPDIRECTINPUTEFFECT)newEffect;
846
847 TRACE("Creating linux input system effect (%p) with guid %s\n",
848 *peff, _dump_dinput_GUID(rguid));
849
850 return DI_OK;
851 }
852
853 HRESULT linuxinput_get_info_A(
854 int fd,
855 REFGUID rguid,
856 LPDIEFFECTINFOA info)
857 {
858 DWORD type = _typeFromGUID(rguid);
859
860 TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
861
862 if (!info) return E_POINTER;
863
864 if (info->dwSize != sizeof(DIEFFECTINFOA)) return DIERR_INVALIDPARAM;
865
866 info->guid = *rguid;
867
868 info->dwEffType = type;
869 /* the event device API does not support querying for all these things
870 * therefore we assume that we have support for them
871 * that's not as dangerous as it sounds, since drivers are allowed to
872 * ignore parameters they claim to support anyway */
873 info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE
874 | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
875 | DIEFT_SATURATION | DIEFT_STARTDELAY;
876
877 /* again, assume we have support for everything */
878 info->dwStaticParams = DIEP_ALLPARAMS;
879 info->dwDynamicParams = info->dwStaticParams;
880
881 /* yes, this is windows behavior (print the GUID_Name for name) */
882 strcpy(info->tszName, _dump_dinput_GUID(rguid));
883
884 return DI_OK;
885 }
886
887 HRESULT linuxinput_get_info_W(
888 int fd,
889 REFGUID rguid,
890 LPDIEFFECTINFOW info)
891 {
892 DWORD type = _typeFromGUID(rguid);
893
894 TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
895
896 if (!info) return E_POINTER;
897
898 if (info->dwSize != sizeof(DIEFFECTINFOW)) return DIERR_INVALIDPARAM;
899
900 info->guid = *rguid;
901
902 info->dwEffType = type;
903 /* the event device API does not support querying for all these things
904 * therefore we assume that we have support for them
905 * that's not as dangerous as it sounds, since drivers are allowed to
906 * ignore parameters they claim to support anyway */
907 info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE
908 | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
909 | DIEFT_SATURATION | DIEFT_STARTDELAY;
910
911 /* again, assume we have support for everything */
912 info->dwStaticParams = DIEP_ALLPARAMS;
913 info->dwDynamicParams = info->dwStaticParams;
914
915 /* yes, this is windows behavior (print the GUID_Name for name) */
916 MultiByteToWideChar(CP_ACP, 0, _dump_dinput_GUID(rguid), -1,
917 info->tszName, MAX_PATH);
918
919 return DI_OK;
920 }
921
922 static const IDirectInputEffectVtbl LinuxInputEffectVtbl = {
923 LinuxInputEffectImpl_QueryInterface,
924 LinuxInputEffectImpl_AddRef,
925 LinuxInputEffectImpl_Release,
926 LinuxInputEffectImpl_Initialize,
927 LinuxInputEffectImpl_GetEffectGuid,
928 LinuxInputEffectImpl_GetParameters,
929 LinuxInputEffectImpl_SetParameters,
930 LinuxInputEffectImpl_Start,
931 LinuxInputEffectImpl_Stop,
932 LinuxInputEffectImpl_GetEffectStatus,
933 LinuxInputEffectImpl_Download,
934 LinuxInputEffectImpl_Unload,
935 LinuxInputEffectImpl_Escape
936 };
937
938 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */