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