mshtml: Added IHTMLElement::put_innerText implementation.
[wine/multimedia.git] / dlls / dinput / effect_linuxinput.c
blob3395a37471cf3263198cdacaca1c2bac3ecf9cb6
1 /* DirectInput Linux Event Device Effect
3 * Copyright 2005 Daniel Remenak
5 * Thanks to Google's Summer of Code Program (2005)
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.
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.
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
22 #include "config.h"
24 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
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"
44 #include "device_private.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
48 static const IDirectInputEffectVtbl LinuxInputEffectVtbl;
49 typedef struct LinuxInputEffectImpl LinuxInputEffectImpl;
50 struct LinuxInputEffectImpl
52 const void *lpVtbl;
53 LONG ref;
54 GUID guid;
56 struct ff_effect effect; /* Effect data */
57 int* fd; /* Parent device */
58 struct list *entry; /* Entry into the parent's list of effects */
62 /******************************************************************************
63 * DirectInputEffect Functional Helper
66 static DWORD _typeFromGUID(REFGUID guid)
68 if (IsEqualGUID(guid, &GUID_ConstantForce)) {
69 return DIEFT_CONSTANTFORCE;
70 } else if (IsEqualGUID(guid, &GUID_Square)
71 || IsEqualGUID(guid, &GUID_Sine)
72 || IsEqualGUID(guid, &GUID_Triangle)
73 || IsEqualGUID(guid, &GUID_SawtoothUp)
74 || IsEqualGUID(guid, &GUID_SawtoothDown)) {
75 return DIEFT_PERIODIC;
76 } else if (IsEqualGUID(guid, &GUID_RampForce)) {
77 return DIEFT_RAMPFORCE;
78 } else if (IsEqualGUID(guid, &GUID_Spring)
79 || IsEqualGUID(guid, &GUID_Damper)
80 || IsEqualGUID(guid, &GUID_Inertia)
81 || IsEqualGUID(guid, &GUID_Friction)) {
82 return DIEFT_CONDITION;
83 } else if (IsEqualGUID(guid, &GUID_CustomForce)) {
84 return DIEFT_CUSTOMFORCE;
85 } else {
86 WARN("GUID (%s) is not a known force type\n", _dump_dinput_GUID(guid));
87 return 0;
92 /******************************************************************************
93 * DirectInputEffect debug helpers
96 static void _dump_DIEFFECT_flags(DWORD dwFlags)
98 if (TRACE_ON(dinput)) {
99 unsigned int i;
100 static const struct {
101 DWORD mask;
102 const char *name;
103 } flags[] = {
104 #define FE(x) { x, #x}
105 FE(DIEFF_CARTESIAN),
106 FE(DIEFF_OBJECTIDS),
107 FE(DIEFF_OBJECTOFFSETS),
108 FE(DIEFF_POLAR),
109 FE(DIEFF_SPHERICAL)
110 #undef FE
112 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
113 if (flags[i].mask & dwFlags)
114 TRACE("%s ", flags[i].name);
115 TRACE("\n");
119 static void _dump_DIENVELOPE(LPCDIENVELOPE env)
121 if (env->dwSize != sizeof(DIENVELOPE)) {
122 WARN("Non-standard DIENVELOPE structure size (%d instead of %d).\n",
123 env->dwSize, sizeof(DIENVELOPE));
125 TRACE("Envelope has attack (level: %d time: %d), fade (level: %d time: %d)\n",
126 env->dwAttackLevel, env->dwAttackTime, env->dwFadeLevel, env->dwFadeTime);
129 static void _dump_DICONSTANTFORCE(LPCDICONSTANTFORCE frc)
131 TRACE("Constant force has magnitude %d\n", frc->lMagnitude);
134 static void _dump_DIPERIODIC(LPCDIPERIODIC frc)
136 TRACE("Periodic force has magnitude %d, offset %d, phase %d, period %d\n",
137 frc->dwMagnitude, frc->lOffset, frc->dwPhase, frc->dwPeriod);
140 static void _dump_DIRAMPFORCE(LPCDIRAMPFORCE frc)
142 TRACE("Ramp force has start %d, end %d\n",
143 frc->lStart, frc->lEnd);
146 static void _dump_DICONDITION(LPCDICONDITION frc)
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);
153 static void _dump_DICUSTOMFORCE(LPCDICUSTOMFORCE frc)
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");
170 static void _dump_DIEFFECT(LPCDIEFFECT eff, REFGUID guid)
172 unsigned int i;
173 DWORD type = _typeFromGUID(guid);
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 instead of %d or %d).\n",
179 eff->dwSize, sizeof(DIEFFECT), sizeof(DIEFFECT_DX5));
181 TRACE(" - dwFlags: %d\n", eff->dwFlags);
182 TRACE(" ");
183 _dump_DIEFFECT_flags(eff->dwFlags);
184 TRACE(" - dwDuration: %d\n", eff->dwDuration);
185 TRACE(" - dwGain: %d\n", eff->dwGain);
186 if (eff->dwGain > 10000)
187 WARN("dwGain is out of range (>10,000)\n");
188 TRACE(" - dwTriggerButton: %d\n", eff->dwTriggerButton);
189 TRACE(" - dwTriggerRepeatInterval: %d\n", eff->dwTriggerRepeatInterval);
190 TRACE(" - cAxes: %d\n", eff->cAxes);
191 TRACE(" - rgdwAxes: %p\n", eff->rgdwAxes);
192 if (TRACE_ON(dinput) && eff->rgdwAxes) {
193 TRACE(" ");
194 for (i = 0; i < eff->cAxes; ++i)
195 TRACE("%d ", eff->rgdwAxes[i]);
196 TRACE("\n");
198 TRACE(" - rglDirection: %p\n", eff->rglDirection);
199 TRACE(" - lpEnvelope: %p\n", eff->lpEnvelope);
200 TRACE(" - cbTypeSpecificParams: %d\n", eff->cbTypeSpecificParams);
201 TRACE(" - lpvTypeSpecificParams: %p\n", eff->lpvTypeSpecificParams);
202 if (eff->dwSize > sizeof(DIEFFECT_DX5))
203 TRACE(" - dwStartDelay: %d\n", eff->dwStartDelay);
204 if (eff->lpEnvelope != NULL)
205 _dump_DIENVELOPE(eff->lpEnvelope);
206 if (type == DIEFT_CONSTANTFORCE) {
207 if (eff->cbTypeSpecificParams != sizeof(DICONSTANTFORCE)) {
208 WARN("Effect claims to be a constant force but the type-specific params are the wrong size!\n");
209 } else {
210 _dump_DICONSTANTFORCE(eff->lpvTypeSpecificParams);
212 } else if (type == DIEFT_PERIODIC) {
213 if (eff->cbTypeSpecificParams != sizeof(DIPERIODIC)) {
214 WARN("Effect claims to be a periodic force but the type-specific params are the wrong size!\n");
215 } else {
216 _dump_DIPERIODIC(eff->lpvTypeSpecificParams);
218 } else if (type == DIEFT_RAMPFORCE) {
219 if (eff->cbTypeSpecificParams != sizeof(DIRAMPFORCE)) {
220 WARN("Effect claims to be a ramp force but the type-specific params are the wrong size!\n");
221 } else {
222 _dump_DIRAMPFORCE(eff->lpvTypeSpecificParams);
224 } else if (type == DIEFT_CONDITION) {
225 if (eff->cbTypeSpecificParams != sizeof(DICONDITION)) {
226 WARN("Effect claims to be a condition but the type-specific params are the wrong size!\n");
227 } else {
228 _dump_DICONDITION(eff->lpvTypeSpecificParams);
230 } else if (type == DIEFT_CUSTOMFORCE) {
231 if (eff->cbTypeSpecificParams != sizeof(DICUSTOMFORCE)) {
232 WARN("Effect claims to be a custom force but the type-specific params are the wrong size!\n");
233 } else {
234 _dump_DICUSTOMFORCE(eff->lpvTypeSpecificParams);
240 /******************************************************************************
241 * LinuxInputEffectImpl
244 static ULONG WINAPI LinuxInputEffectImpl_AddRef(
245 LPDIRECTINPUTEFFECT iface)
247 LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
248 return InterlockedIncrement(&(This->ref));
251 static HRESULT WINAPI LinuxInputEffectImpl_Download(
252 LPDIRECTINPUTEFFECT iface)
254 LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
256 TRACE("(this=%p)\n", This);
258 if (ioctl(*(This->fd), EVIOCSFF, &This->effect) == -1) {
259 if (errno == ENOMEM) {
260 return DIERR_DEVICEFULL;
261 } else {
262 FIXME("Could not upload effect. Assuming a disconnected device %d \"%s\".\n", *This->fd, strerror(errno));
263 return DIERR_INPUTLOST;
267 return DI_OK;
270 static HRESULT WINAPI LinuxInputEffectImpl_Escape(
271 LPDIRECTINPUTEFFECT iface,
272 LPDIEFFESCAPE pesc)
274 WARN("(this=%p,%p): invalid: no hardware-specific escape codes in this"
275 " driver!\n", iface, pesc);
277 return DI_OK;
280 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectGuid(
281 LPDIRECTINPUTEFFECT iface,
282 LPGUID pguid)
284 LinuxInputEffectImpl *This = (LinuxInputEffectImpl*)iface;
286 TRACE("(this=%p,%p)\n", This, pguid);
288 pguid = &This->guid;
290 return DI_OK;
293 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectStatus(
294 LPDIRECTINPUTEFFECT iface,
295 LPDWORD pdwFlags)
297 TRACE("(this=%p,%p)\n", iface, pdwFlags);
299 /* linux sends the effect status through an event.
300 * that event is trapped by our parent joystick driver
301 * and there is no clean way to pass it back to us. */
302 FIXME("Not enough information to provide a status.\n");
304 (*pdwFlags) = 0;
306 return DI_OK;
309 static HRESULT WINAPI LinuxInputEffectImpl_GetParameters(
310 LPDIRECTINPUTEFFECT iface,
311 LPDIEFFECT peff,
312 DWORD dwFlags)
314 HRESULT diErr = DI_OK;
315 LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
316 TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
318 /* Major conversion factors are:
319 * times: millisecond (linux) -> microsecond (windows) (x * 1000)
320 * forces: scale 0x7FFF (linux) -> scale 10000 (windows) approx ((x / 33) * 10)
321 * angles: scale 0x7FFF (linux) -> scale 35999 (windows) approx ((x / 33) * 36)
322 * angle bases: 0 -> -y (down) (linux) -> 0 -> +x (right) (windows)
325 if (dwFlags & DIEP_AXES) {
326 if (peff->cAxes < 2 /* linuxinput effects always use 2 axes, x and y */)
327 diErr = DIERR_MOREDATA;
328 peff->cAxes = 2;
329 if (diErr)
330 return diErr;
331 else {
332 peff->rgdwAxes[0] = DIJOFS_X;
333 peff->rgdwAxes[1] = DIJOFS_Y;
337 if (dwFlags & DIEP_DIRECTION) {
338 if (peff->cAxes < 2)
339 diErr = DIERR_MOREDATA;
340 peff->cAxes = 2;
341 if (diErr)
342 return diErr;
343 else {
344 if (peff->dwFlags & DIEFF_CARTESIAN) {
345 peff->rglDirection[0] = (long)(sin(M_PI * 3 * This->effect.direction / 0x7FFF) * 1000);
346 peff->rglDirection[1] = (long)(cos(M_PI * 3 * This->effect.direction / 0x7FFF) * 1000);
347 } else {
348 /* Polar and spherical coordinates are the same for two or less
349 * axes.
350 * Note that we also use this case if NO flags are marked.
351 * According to MSDN, we should return the direction in the
352 * format that it was specified in, if no flags are marked.
354 peff->rglDirection[0] = (This->effect.direction / 33) * 36 + 9000;
355 if (peff->rglDirection[0] > 35999)
356 peff->rglDirection[0] -= 35999;
361 if (dwFlags & DIEP_DURATION) {
362 peff->dwDuration = (DWORD)This->effect.replay.length * 1000;
365 if (dwFlags & DIEP_ENVELOPE) {
366 struct ff_envelope* env;
367 if (This->effect.type == FF_CONSTANT) env = &This->effect.u.constant.envelope;
368 else if (This->effect.type == FF_PERIODIC) env = &This->effect.u.periodic.envelope;
369 else if (This->effect.type == FF_RAMP) env = &This->effect.u.ramp.envelope;
370 else env = NULL;
371 if (env == NULL) {
372 peff->lpEnvelope = NULL;
373 } else if (peff->lpEnvelope == NULL) {
374 return DIERR_INVALIDPARAM;
375 } else {
376 peff->lpEnvelope->dwAttackLevel = (env->attack_level / 33) * 10;
377 peff->lpEnvelope->dwAttackTime = env->attack_length * 1000;
378 peff->lpEnvelope->dwFadeLevel = (env->fade_level / 33) * 10;
379 peff->lpEnvelope->dwFadeTime = env->fade_length * 1000;
383 if (dwFlags & DIEP_GAIN) {
384 /* the linux input ff driver apparently has no support
385 * for setting the device's gain. */
386 peff->dwGain = DI_FFNOMINALMAX;
389 if (dwFlags & DIEP_SAMPLEPERIOD) {
390 /* the linux input ff driver has no support for setting
391 * the playback sample period. 0 means default. */
392 peff->dwSamplePeriod = 0;
395 if (dwFlags & DIEP_STARTDELAY) {
396 peff->dwStartDelay = This->effect.replay.delay * 1000;
399 if (dwFlags & DIEP_TRIGGERBUTTON) {
400 FIXME("LinuxInput button mapping needs redoing; for now, assuming we're using an actual joystick.\n");
401 peff->dwTriggerButton = DIJOFS_BUTTON(This->effect.trigger.button - BTN_JOYSTICK);
404 if (dwFlags & DIEP_TRIGGERREPEATINTERVAL) {
405 peff->dwTriggerRepeatInterval = This->effect.trigger.interval * 1000;
408 if (dwFlags & DIEP_TYPESPECIFICPARAMS) {
409 DWORD expectedsize = 0;
410 if (This->effect.type == FF_PERIODIC) {
411 expectedsize = sizeof(DIPERIODIC);
412 } else if (This->effect.type == FF_CONSTANT) {
413 expectedsize = sizeof(DICONSTANTFORCE);
414 } else if (This->effect.type == FF_SPRING
415 || This->effect.type == FF_FRICTION
416 || This->effect.type == FF_INERTIA
417 || This->effect.type == FF_DAMPER) {
418 expectedsize = sizeof(DICONDITION) * 2;
419 } else if (This->effect.type == FF_RAMP) {
420 expectedsize = sizeof(DIRAMPFORCE);
422 if (expectedsize > peff->cbTypeSpecificParams)
423 diErr = DIERR_MOREDATA;
424 peff->cbTypeSpecificParams = expectedsize;
425 if (diErr)
426 return diErr;
427 else {
428 if (This->effect.type == FF_PERIODIC) {
429 LPDIPERIODIC tsp = (LPDIPERIODIC)(peff->lpvTypeSpecificParams);
430 tsp->dwMagnitude = (This->effect.u.periodic.magnitude / 33) * 10;
431 tsp->lOffset = (This->effect.u.periodic.offset / 33) * 10;
432 tsp->dwPhase = (This->effect.u.periodic.phase / 33) * 36;
433 tsp->dwPeriod = (This->effect.u.periodic.period * 1000);
434 } else if (This->effect.type == FF_CONSTANT) {
435 LPDICONSTANTFORCE tsp = (LPDICONSTANTFORCE)(peff->lpvTypeSpecificParams);
436 tsp->lMagnitude = (This->effect.u.constant.level / 33) * 10;
437 } else if (This->effect.type == FF_SPRING
438 || This->effect.type == FF_FRICTION
439 || This->effect.type == FF_INERTIA
440 || This->effect.type == FF_DAMPER) {
441 LPDICONDITION tsp = (LPDICONDITION)(peff->lpvTypeSpecificParams);
442 int i;
443 for (i = 0; i < 2; ++i) {
444 tsp[i].lOffset = (This->effect.u.condition[i].center / 33) * 10;
445 tsp[i].lPositiveCoefficient = (This->effect.u.condition[i].right_coeff / 33) * 10;
446 tsp[i].lNegativeCoefficient = (This->effect.u.condition[i].left_coeff / 33) * 10;
447 tsp[i].dwPositiveSaturation = (This->effect.u.condition[i].right_saturation / 33) * 10;
448 tsp[i].dwNegativeSaturation = (This->effect.u.condition[i].left_saturation / 33) * 10;
449 tsp[i].lDeadBand = (This->effect.u.condition[i].deadband / 33) * 10;
451 } else if (This->effect.type == FF_RAMP) {
452 LPDIRAMPFORCE tsp = (LPDIRAMPFORCE)(peff->lpvTypeSpecificParams);
453 tsp->lStart = (This->effect.u.ramp.start_level / 33) * 10;
454 tsp->lEnd = (This->effect.u.ramp.end_level / 33) * 10;
459 return diErr;
462 static HRESULT WINAPI LinuxInputEffectImpl_Initialize(
463 LPDIRECTINPUTEFFECT iface,
464 HINSTANCE hinst,
465 DWORD dwVersion,
466 REFGUID rguid)
468 FIXME("(this=%p,%p,%d,%s): stub!\n",
469 iface, hinst, dwVersion, debugstr_guid(rguid));
471 return DI_OK;
474 static HRESULT WINAPI LinuxInputEffectImpl_QueryInterface(
475 LPDIRECTINPUTEFFECT iface,
476 REFIID riid,
477 void **ppvObject)
479 LinuxInputEffectImpl* This = (LinuxInputEffectImpl*)iface;
481 TRACE("(this=%p,%s,%p)\n", This, debugstr_guid(riid), ppvObject);
483 if (IsEqualGUID(&IID_IUnknown, riid) ||
484 IsEqualGUID(&IID_IDirectInputEffect, riid)) {
485 LinuxInputEffectImpl_AddRef(iface);
486 *ppvObject = This;
487 return 0;
490 TRACE("Unsupported interface!\n");
491 return E_FAIL;
494 static HRESULT WINAPI LinuxInputEffectImpl_Start(
495 LPDIRECTINPUTEFFECT iface,
496 DWORD dwIterations,
497 DWORD dwFlags)
499 struct input_event event;
500 LinuxInputEffectImpl* This = (LinuxInputEffectImpl*)iface;
502 TRACE("(this=%p,%d,%d)\n", This, dwIterations, dwFlags);
504 if (!(dwFlags & DIES_NODOWNLOAD)) {
505 /* Download the effect if necessary */
506 if (This->effect.id == -1) {
507 HRESULT res = LinuxInputEffectImpl_Download(iface);
508 if (res != DI_OK)
509 return res;
513 if (dwFlags & DIES_SOLO) {
514 FIXME("Solo mode requested: should be stopping all effects here!\n");
517 event.type = EV_FF;
518 event.code = This->effect.id;
519 event.value = dwIterations;
520 if (write(*(This->fd), &event, sizeof(event)) == -1) {
521 FIXME("Unable to write event. Assuming device disconnected.\n");
522 return DIERR_INPUTLOST;
525 return DI_OK;
528 static HRESULT WINAPI LinuxInputEffectImpl_SetParameters(
529 LPDIRECTINPUTEFFECT iface,
530 LPCDIEFFECT peff,
531 DWORD dwFlags)
533 LinuxInputEffectImpl* This = (LinuxInputEffectImpl*)iface;
534 DWORD type = _typeFromGUID(&This->guid);
535 HRESULT retval = DI_OK;
537 TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
539 _dump_DIEFFECT(peff, &This->guid);
541 if ((dwFlags & ~DIEP_NORESTART & ~DIEP_NODOWNLOAD & ~DIEP_START) == 0) {
542 /* set everything */
543 dwFlags = DIEP_AXES | DIEP_DIRECTION | DIEP_DURATION | DIEP_ENVELOPE |
544 DIEP_GAIN | DIEP_SAMPLEPERIOD | DIEP_STARTDELAY | DIEP_TRIGGERBUTTON |
545 DIEP_TRIGGERREPEATINTERVAL | DIEP_TYPESPECIFICPARAMS;
548 if (dwFlags & DIEP_AXES) {
549 /* the linux input effect system only supports one or two axes */
550 if (peff->cAxes > 2)
551 return DIERR_INVALIDPARAM;
552 else if (peff->cAxes < 1)
553 return DIERR_INCOMPLETEEFFECT;
556 /* some of this may look funky, but it's 'cause the linux driver and directx have
557 * different opinions about which way direction "0" is. directx has 0 along the x
558 * axis (left), linux has it along the y axis (down). */
559 if (dwFlags & DIEP_DIRECTION) {
560 if (peff->cAxes == 1) {
561 if (peff->dwFlags & DIEFF_CARTESIAN) {
562 if (dwFlags & DIEP_AXES) {
563 if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] >= 0)
564 This->effect.direction = 0x4000;
565 else if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] < 0)
566 This->effect.direction = 0xC000;
567 else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] >= 0)
568 This->effect.direction = 0;
569 else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] < 0)
570 This->effect.direction = 0x8000;
572 } else {
573 /* one-axis effects must use cartesian coords */
574 return DIERR_INVALIDPARAM;
576 } else { /* two axes */
577 if (peff->dwFlags & DIEFF_CARTESIAN) {
578 /* avoid divide-by-zero */
579 if (peff->rglDirection[1] == 0) {
580 if (peff->rglDirection[0] >= 0)
581 This->effect.direction = 0x4000;
582 else if (peff->rglDirection[0] < 0)
583 This->effect.direction = 0xC000;
584 } else {
585 This->effect.direction = (int)(atan(peff->rglDirection[0] / peff->rglDirection[1]) * 0x7FFF / (3 * M_PI));
587 } else {
588 /* Polar and spherical are the same for 2 axes */
589 /* Precision is important here, so we do double math with exact constants */
590 This->effect.direction = (int)(((double)peff->rglDirection[0] - 90) / 35999) * 0x7FFF;
595 if (dwFlags & DIEP_DURATION)
596 This->effect.replay.length = peff->dwDuration / 1000;
598 if (dwFlags & DIEP_ENVELOPE) {
599 struct ff_envelope* env;
600 if (This->effect.type == FF_CONSTANT) env = &This->effect.u.constant.envelope;
601 else if (This->effect.type == FF_PERIODIC) env = &This->effect.u.periodic.envelope;
602 else if (This->effect.type == FF_RAMP) env = &This->effect.u.ramp.envelope;
603 else env = NULL;
605 if (peff->lpEnvelope == NULL) {
606 /* if this type had an envelope, reset it
607 * note that length can never be zero, so we set it to something minuscule */
608 if (env) {
609 env->attack_length = 0x10;
610 env->attack_level = 0x7FFF;
611 env->fade_length = 0x10;
612 env->fade_level = 0x7FFF;
614 } else {
615 /* did we get passed an envelope for a type that doesn't even have one? */
616 if (!env) return DIERR_INVALIDPARAM;
617 /* copy the envelope */
618 env->attack_length = peff->lpEnvelope->dwAttackTime / 1000;
619 env->attack_level = (peff->lpEnvelope->dwAttackLevel / 10) * 32;
620 env->fade_length = peff->lpEnvelope->dwFadeTime / 1000;
621 env->fade_level = (peff->lpEnvelope->dwFadeLevel / 10) * 32;
625 /* Gain and Sample Period settings are not supported by the linux
626 * event system */
627 if (dwFlags & DIEP_GAIN)
628 TRACE("Gain requested but no gain functionality present.\n");
630 if (dwFlags & DIEP_SAMPLEPERIOD)
631 TRACE("Sample period requested but no sample period functionality present.\n");
633 if (dwFlags & DIEP_STARTDELAY)
634 This->effect.replay.delay = peff->dwStartDelay / 1000;
636 if (dwFlags & DIEP_TRIGGERBUTTON) {
637 if (peff->dwTriggerButton != -1) {
638 FIXME("Linuxinput button mapping needs redoing, assuming we're using a joystick.\n");
639 FIXME("Trigger button translation not yet implemented!\n");
641 This->effect.trigger.button = 0;
644 if (dwFlags & DIEP_TRIGGERREPEATINTERVAL)
645 This->effect.trigger.interval = peff->dwTriggerRepeatInterval / 1000;
647 if (dwFlags & DIEP_TYPESPECIFICPARAMS) {
648 if (!(peff->lpvTypeSpecificParams))
649 return DIERR_INCOMPLETEEFFECT;
650 if (type == DIEFT_PERIODIC) {
651 LPCDIPERIODIC tsp;
652 if (peff->cbTypeSpecificParams != sizeof(DIPERIODIC))
653 return DIERR_INVALIDPARAM;
654 tsp = (LPCDIPERIODIC)(peff->lpvTypeSpecificParams);
655 This->effect.u.periodic.magnitude = (tsp->dwMagnitude / 10) * 32;
656 This->effect.u.periodic.offset = (tsp->lOffset / 10) * 32;
657 This->effect.u.periodic.phase = (tsp->dwPhase / 9) * 8; /* == (/ 36 * 32) */
658 This->effect.u.periodic.period = tsp->dwPeriod / 1000;
659 } else if (type == DIEFT_CONSTANTFORCE) {
660 LPCDICONSTANTFORCE tsp;
661 if (peff->cbTypeSpecificParams != sizeof(DICONSTANTFORCE))
662 return DIERR_INVALIDPARAM;
663 tsp = (LPCDICONSTANTFORCE)(peff->lpvTypeSpecificParams);
664 This->effect.u.constant.level = (tsp->lMagnitude / 10) * 32;
665 } else if (type == DIEFT_RAMPFORCE) {
666 LPCDIRAMPFORCE tsp;
667 if (peff->cbTypeSpecificParams != sizeof(DIRAMPFORCE))
668 return DIERR_INVALIDPARAM;
669 tsp = (LPCDIRAMPFORCE)(peff->lpvTypeSpecificParams);
670 This->effect.u.ramp.start_level = (tsp->lStart / 10) * 32;
671 This->effect.u.ramp.end_level = (tsp->lStart / 10) * 32;
672 } else if (type == DIEFT_CONDITION) {
673 LPCDICONDITION tsp = (LPCDICONDITION)(peff->lpvTypeSpecificParams);
674 if (peff->cbTypeSpecificParams == sizeof(DICONDITION)) {
675 /* One condition block. This needs to be rotated to direction,
676 * and expanded to separate x and y conditions. */
677 int i;
678 double factor[2];
679 factor[0] = asin((This->effect.direction * 3.0 * M_PI) / 0x7FFF);
680 factor[1] = acos((This->effect.direction * 3.0 * M_PI) / 0x7FFF);
681 for (i = 0; i < 2; ++i) {
682 This->effect.u.condition[i].center = (int)(factor[i] * (tsp->lOffset / 10) * 32);
683 This->effect.u.condition[i].right_coeff = (int)(factor[i] * (tsp->lPositiveCoefficient / 10) * 32);
684 This->effect.u.condition[i].left_coeff = (int)(factor[i] * (tsp->lNegativeCoefficient / 10) * 32);
685 This->effect.u.condition[i].right_saturation = (int)(factor[i] * (tsp->dwPositiveSaturation / 10) * 32);
686 This->effect.u.condition[i].left_saturation = (int)(factor[i] * (tsp->dwNegativeSaturation / 10) * 32);
687 This->effect.u.condition[i].deadband = (int)(factor[i] * (tsp->lDeadBand / 10) * 32);
689 } else if (peff->cbTypeSpecificParams == 2 * sizeof(DICONDITION)) {
690 /* Two condition blocks. Direct parameter copy. */
691 int i;
692 for (i = 0; i < 2; ++i) {
693 This->effect.u.condition[i].center = (tsp[i].lOffset / 10) * 32;
694 This->effect.u.condition[i].right_coeff = (tsp[i].lPositiveCoefficient / 10) * 32;
695 This->effect.u.condition[i].left_coeff = (tsp[i].lNegativeCoefficient / 10) * 32;
696 This->effect.u.condition[i].right_saturation = (tsp[i].dwPositiveSaturation / 10) * 32;
697 This->effect.u.condition[i].left_saturation = (tsp[i].dwNegativeSaturation / 10) * 32;
698 This->effect.u.condition[i].deadband = (tsp[i].lDeadBand / 10) * 32;
700 } else {
701 return DIERR_INVALIDPARAM;
703 } else {
704 FIXME("Custom force types are not supported\n");
705 return DIERR_INVALIDPARAM;
709 if (!(dwFlags & DIEP_NODOWNLOAD))
710 retval = LinuxInputEffectImpl_Download(iface);
711 if (retval != DI_OK)
712 return DI_DOWNLOADSKIPPED;
714 if (dwFlags & DIEP_NORESTART)
715 TRACE("DIEP_NORESTART: not handled (we have no control of that).\n");
717 if (dwFlags & DIEP_START)
718 retval = LinuxInputEffectImpl_Start(iface, 1, 0);
719 if (retval != DI_OK)
720 return retval;
722 return DI_OK;
725 static HRESULT WINAPI LinuxInputEffectImpl_Stop(
726 LPDIRECTINPUTEFFECT iface)
728 struct input_event event;
729 LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
731 TRACE("(this=%p)\n", This);
733 event.type = EV_FF;
734 event.code = This->effect.id;
735 event.value = 0;
736 /* we don't care about the success or failure of this call */
737 write(*(This->fd), &event, sizeof(event));
739 return DI_OK;
742 static HRESULT WINAPI LinuxInputEffectImpl_Unload(
743 LPDIRECTINPUTEFFECT iface)
745 LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
746 TRACE("(this=%p)\n", This);
748 /* Erase the downloaded effect */
749 if (ioctl(*(This->fd), EVIOCRMFF, This->effect.id) == -1)
750 return DIERR_INVALIDPARAM;
752 /* Mark the effect as deallocated */
753 This->effect.id = -1;
755 return DI_OK;
758 static ULONG WINAPI LinuxInputEffectImpl_Release(LPDIRECTINPUTEFFECT iface)
760 LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
761 ULONG ref = InterlockedDecrement(&(This->ref));
763 if (ref == 0)
765 LinuxInputEffectImpl_Stop(iface);
766 LinuxInputEffectImpl_Unload(iface);
767 list_remove(This->entry);
768 HeapFree(GetProcessHeap(), 0, LIST_ENTRY(This->entry, effect_list_item, entry));
769 HeapFree(GetProcessHeap(), 0, This);
771 return ref;
774 /******************************************************************************
775 * LinuxInputEffect
778 HRESULT linuxinput_create_effect(
779 int* fd,
780 REFGUID rguid,
781 struct list *parent_list_entry,
782 LPDIRECTINPUTEFFECT* peff)
784 LinuxInputEffectImpl* newEffect = HeapAlloc(GetProcessHeap(),
785 HEAP_ZERO_MEMORY, sizeof(LinuxInputEffectImpl));
786 DWORD type = _typeFromGUID(rguid);
788 newEffect->lpVtbl = &LinuxInputEffectVtbl;
789 newEffect->ref = 1;
790 newEffect->guid = *rguid;
791 newEffect->fd = fd;
793 /* set the type. this cannot be changed over the effect's life. */
794 switch (type) {
795 case DIEFT_PERIODIC:
796 newEffect->effect.type = FF_PERIODIC;
797 if (IsEqualGUID(rguid, &GUID_Sine)) {
798 newEffect->effect.u.periodic.waveform = FF_SINE;
799 } else if (IsEqualGUID(rguid, &GUID_Triangle)) {
800 newEffect->effect.u.periodic.waveform = FF_TRIANGLE;
801 } else if (IsEqualGUID(rguid, &GUID_Square)) {
802 newEffect->effect.u.periodic.waveform = FF_SQUARE;
803 } else if (IsEqualGUID(rguid, &GUID_SawtoothUp)) {
804 newEffect->effect.u.periodic.waveform = FF_SAW_UP;
805 } else if (IsEqualGUID(rguid, &GUID_SawtoothDown)) {
806 newEffect->effect.u.periodic.waveform = FF_SAW_DOWN;
808 break;
809 case DIEFT_CONSTANTFORCE:
810 newEffect->effect.type = FF_CONSTANT;
811 break;
812 case DIEFT_RAMPFORCE:
813 newEffect->effect.type = FF_RAMP;
814 break;
815 case DIEFT_CONDITION:
816 if (IsEqualGUID(rguid, &GUID_Spring)) {
817 newEffect->effect.type = FF_SPRING;
818 } else if (IsEqualGUID(rguid, &GUID_Friction)) {
819 newEffect->effect.type = FF_FRICTION;
820 } else if (IsEqualGUID(rguid, &GUID_Inertia)) {
821 newEffect->effect.type = FF_INERTIA;
822 } else if (IsEqualGUID(rguid, &GUID_Damper)) {
823 newEffect->effect.type = FF_DAMPER;
825 break;
826 case DIEFT_CUSTOMFORCE:
827 FIXME("Custom forces are not supported.\n");
828 HeapFree(GetProcessHeap(), 0, newEffect);
829 return DIERR_INVALIDPARAM;
830 default:
831 FIXME("Unknown force type 0x%x.\n", type);
832 HeapFree(GetProcessHeap(), 0, newEffect);
833 return DIERR_INVALIDPARAM;
836 /* mark as non-uploaded */
837 newEffect->effect.id = -1;
839 newEffect->entry = parent_list_entry;
841 *peff = (LPDIRECTINPUTEFFECT)newEffect;
843 TRACE("Creating linux input system effect (%p) with guid %s\n",
844 *peff, _dump_dinput_GUID(rguid));
846 return DI_OK;
849 HRESULT linuxinput_get_info_A(
850 int fd,
851 REFGUID rguid,
852 LPDIEFFECTINFOA info)
854 DWORD type = _typeFromGUID(rguid);
856 TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
858 if (!info) return E_POINTER;
860 if (info->dwSize != sizeof(DIEFFECTINFOA)) return DIERR_INVALIDPARAM;
862 info->guid = *rguid;
864 info->dwEffType = type;
865 /* the event device API does not support querying for all these things
866 * therefore we assume that we have support for them
867 * that's not as dangerous as it sounds, since drivers are allowed to
868 * ignore parameters they claim to support anyway */
869 info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE
870 | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
871 | DIEFT_SATURATION | DIEFT_STARTDELAY;
873 /* again, assume we have support for everything */
874 info->dwStaticParams = DIEP_ALLPARAMS;
875 info->dwDynamicParams = info->dwStaticParams;
877 /* yes, this is windows behavior (print the GUID_Name for name) */
878 strcpy((char*)info->tszName, _dump_dinput_GUID(rguid));
880 return DI_OK;
883 HRESULT linuxinput_get_info_W(
884 int fd,
885 REFGUID rguid,
886 LPDIEFFECTINFOW info)
888 DWORD type = _typeFromGUID(rguid);
890 TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
892 if (!info) return E_POINTER;
894 if (info->dwSize != sizeof(DIEFFECTINFOW)) return DIERR_INVALIDPARAM;
896 info->guid = *rguid;
898 info->dwEffType = type;
899 /* the event device API does not support querying for all these things
900 * therefore we assume that we have support for them
901 * that's not as dangerous as it sounds, since drivers are allowed to
902 * ignore parameters they claim to support anyway */
903 info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE
904 | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
905 | DIEFT_SATURATION | DIEFT_STARTDELAY;
907 /* again, assume we have support for everything */
908 info->dwStaticParams = DIEP_ALLPARAMS;
909 info->dwDynamicParams = info->dwStaticParams;
911 /* yes, this is windows behavior (print the GUID_Name for name) */
912 MultiByteToWideChar(CP_ACP, 0, _dump_dinput_GUID(rguid), -1,
913 (WCHAR*)info->tszName, MAX_PATH);
915 return DI_OK;
918 static const IDirectInputEffectVtbl LinuxInputEffectVtbl = {
919 LinuxInputEffectImpl_QueryInterface,
920 LinuxInputEffectImpl_AddRef,
921 LinuxInputEffectImpl_Release,
922 LinuxInputEffectImpl_Initialize,
923 LinuxInputEffectImpl_GetEffectGuid,
924 LinuxInputEffectImpl_GetParameters,
925 LinuxInputEffectImpl_SetParameters,
926 LinuxInputEffectImpl_Start,
927 LinuxInputEffectImpl_Stop,
928 LinuxInputEffectImpl_GetEffectStatus,
929 LinuxInputEffectImpl_Download,
930 LinuxInputEffectImpl_Unload,
931 LinuxInputEffectImpl_Escape
934 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */