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