comdlg32/tests: PrintDlgExW returns E_NOTIMPL on some versions of winME.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23
24 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
25
26 #include <stdarg.h>
27 #include <string.h>
28 #ifdef HAVE_LINUX_INPUT_H
29 #  include <linux/input.h>
30 #  undef SW_MAX
31 #endif
32 #include <errno.h>
33 #ifdef HAVE_UNISTD_H
34 #  include <unistd.h>
35 #endif
36 #include <math.h>
37 #include "wine/debug.h"
38 #include "wine/unicode.h"
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winerror.h"
42 #include "dinput.h"
43
44 #include "device_private.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
47
48 static const IDirectInputEffectVtbl LinuxInputEffectVtbl;
49 typedef struct LinuxInputEffectImpl LinuxInputEffectImpl;
50 struct LinuxInputEffectImpl
51 {
52     const void *lpVtbl;
53     LONG        ref;
54     GUID        guid;
55
56     struct ff_effect    effect; /* Effect data */
57     int                 gain;   /* Effect gain */
58     int                 first_axis_is_x;
59     int*                fd;     /* Parent device */
60     struct list        *entry;  /* Entry into the parent's list of effects */
61 };
62
63
64 /******************************************************************************
65  *      DirectInputEffect Functional Helper
66  */
67
68 static DWORD _typeFromGUID(REFGUID guid)
69 {
70     if (IsEqualGUID(guid, &GUID_ConstantForce)) {
71         return DIEFT_CONSTANTFORCE;
72     } else if (IsEqualGUID(guid, &GUID_Square)
73             || IsEqualGUID(guid, &GUID_Sine)
74             || IsEqualGUID(guid, &GUID_Triangle)
75             || IsEqualGUID(guid, &GUID_SawtoothUp)
76             || IsEqualGUID(guid, &GUID_SawtoothDown)) {
77         return DIEFT_PERIODIC;
78     } else if (IsEqualGUID(guid, &GUID_RampForce)) {
79         return DIEFT_RAMPFORCE;
80     } else if (IsEqualGUID(guid, &GUID_Spring)
81             || IsEqualGUID(guid, &GUID_Damper)
82             || IsEqualGUID(guid, &GUID_Inertia)
83             || IsEqualGUID(guid, &GUID_Friction)) {
84         return DIEFT_CONDITION;
85     } else if (IsEqualGUID(guid, &GUID_CustomForce)) {
86         return DIEFT_CUSTOMFORCE;
87     } else {
88         WARN("GUID (%s) is not a known force type\n", _dump_dinput_GUID(guid));
89         return 0;
90     }
91 }
92
93
94 /******************************************************************************
95  *      DirectInputEffect debug helpers 
96  */
97
98 static void _dump_DIEFFECT_flags(DWORD dwFlags)
99 {
100     if (TRACE_ON(dinput)) {
101         unsigned int   i;
102         static const struct {
103             DWORD       mask;
104             const char  *name;
105         } flags[] = {
106 #define FE(x) { x, #x}
107             FE(DIEFF_CARTESIAN),
108             FE(DIEFF_OBJECTIDS),
109             FE(DIEFF_OBJECTOFFSETS),
110             FE(DIEFF_POLAR),
111             FE(DIEFF_SPHERICAL)
112 #undef FE
113         };
114         for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
115             if (flags[i].mask & dwFlags)
116                 TRACE("%s ", flags[i].name);
117         TRACE("\n");
118     }       
119 }
120
121 static void _dump_DIENVELOPE(LPCDIENVELOPE env)
122 {
123     if (env->dwSize != sizeof(DIENVELOPE)) {
124         WARN("Non-standard DIENVELOPE structure size %d.\n", env->dwSize);
125     }
126     TRACE("Envelope has attack (level: %d time: %d), fade (level: %d time: %d)\n",
127           env->dwAttackLevel, env->dwAttackTime, env->dwFadeLevel, env->dwFadeTime);
128
129
130 static void _dump_DICONSTANTFORCE(LPCDICONSTANTFORCE frc)
131 {
132     TRACE("Constant force has magnitude %d\n", frc->lMagnitude);
133 }
134
135 static void _dump_DIPERIODIC(LPCDIPERIODIC frc)
136 {
137     TRACE("Periodic force has magnitude %d, offset %d, phase %d, period %d\n",
138           frc->dwMagnitude, frc->lOffset, frc->dwPhase, frc->dwPeriod);
139 }
140
141 static void _dump_DIRAMPFORCE(LPCDIRAMPFORCE frc)
142 {
143     TRACE("Ramp force has start %d, end %d\n",
144           frc->lStart, frc->lEnd);
145 }
146
147 static void _dump_DICONDITION(LPCDICONDITION frc)
148 {
149     TRACE("Condition has offset %d, pos/neg coefficients %d and %d, pos/neg saturations %d and %d, deadband %d\n",
150           frc->lOffset, frc->lPositiveCoefficient, frc->lNegativeCoefficient,
151           frc->dwPositiveSaturation, frc->dwNegativeSaturation, frc->lDeadBand);
152 }
153
154 static void _dump_DICUSTOMFORCE(LPCDICUSTOMFORCE frc)
155 {
156     unsigned int i;
157     TRACE("Custom force uses %d channels, sample period %d.  Has %d 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         TRACE("Custom force data (time aligned, axes in order):\n");
163         for (i = 1; i <= frc->cSamples; ++i) {
164             TRACE("%d ", frc->rglForceData[i]);
165             if (i % frc->cChannels == 0)
166                 TRACE("\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: %d\n", eff->dwSize);
178     if ((eff->dwSize != sizeof(DIEFFECT)) && (eff->dwSize != sizeof(DIEFFECT_DX5))) {
179         WARN("Non-standard DIEFFECT structure size %d\n", eff->dwSize);
180     }
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");
197     }
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);
211         }
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);
217         }
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);
223         }
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);
229         }
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);
235         }
236     }
237 }
238
239
240 /******************************************************************************
241  *      LinuxInputEffectImpl 
242  */
243
244 static ULONG WINAPI LinuxInputEffectImpl_AddRef(
245         LPDIRECTINPUTEFFECT iface)
246 {
247     LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
248     return InterlockedIncrement(&(This->ref));
249 }
250
251 static HRESULT WINAPI LinuxInputEffectImpl_Download(
252         LPDIRECTINPUTEFFECT iface)
253 {
254     LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
255
256     TRACE("(this=%p)\n", This);
257
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;
264         }
265     }
266
267     return DI_OK;
268 }
269
270 static HRESULT WINAPI LinuxInputEffectImpl_Escape(
271         LPDIRECTINPUTEFFECT iface,
272         LPDIEFFESCAPE pesc)
273 {
274     WARN("(this=%p,%p): invalid: no hardware-specific escape codes in this" 
275          " driver!\n", iface, pesc);
276
277     return DI_OK;
278 }
279
280 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectGuid(
281         LPDIRECTINPUTEFFECT iface,
282         LPGUID pguid)
283 {
284     LinuxInputEffectImpl *This = (LinuxInputEffectImpl*)iface;
285
286     TRACE("(this=%p,%p)\n", This, pguid);
287
288     pguid = &This->guid;
289     
290     return DI_OK;
291 }
292
293 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectStatus(
294         LPDIRECTINPUTEFFECT iface,
295         LPDWORD pdwFlags)
296 {
297     TRACE("(this=%p,%p)\n", iface, pdwFlags);
298
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");
303
304     (*pdwFlags) = 0;
305
306     return DI_OK;
307 }
308
309 static HRESULT WINAPI LinuxInputEffectImpl_GetParameters(
310         LPDIRECTINPUTEFFECT iface,
311         LPDIEFFECT peff,
312         DWORD dwFlags)
313 {
314     HRESULT diErr = DI_OK;
315     LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
316     TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
317
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)
323      */
324
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;
334         }
335     }
336  
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.
353                  */
354                 peff->rglDirection[0] = (This->effect.direction / 33) * 36 + 9000;
355                 if (peff->rglDirection[0] > 35999)
356                     peff->rglDirection[0] -= 35999;
357             }
358         }
359     }
360
361     if (dwFlags & DIEP_DURATION) {
362         peff->dwDuration = (DWORD)This->effect.replay.length * 1000;
363     }
364
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;
380         }
381     }
382
383     if (dwFlags & DIEP_GAIN) {
384         peff->dwGain = This->gain * 10000 / 0xFFFF;
385     }
386
387     if (dwFlags & DIEP_SAMPLEPERIOD) {
388         /* the linux input ff driver has no support for setting
389          * the playback sample period.  0 means default. */
390         peff->dwSamplePeriod = 0;
391     }
392
393     if (dwFlags & DIEP_STARTDELAY) {
394         peff->dwStartDelay = This->effect.replay.delay * 1000;
395     }
396
397     if (dwFlags & DIEP_TRIGGERBUTTON) {
398         FIXME("LinuxInput button mapping needs redoing; for now, assuming we're using an actual joystick.\n");
399         peff->dwTriggerButton = DIJOFS_BUTTON(This->effect.trigger.button - BTN_JOYSTICK);
400     }
401
402     if (dwFlags & DIEP_TRIGGERREPEATINTERVAL) {
403         peff->dwTriggerRepeatInterval = This->effect.trigger.interval * 1000;
404     }
405
406     if (dwFlags & DIEP_TYPESPECIFICPARAMS) {
407         DWORD expectedsize = 0;
408         if (This->effect.type == FF_PERIODIC) {
409             expectedsize = sizeof(DIPERIODIC);
410         } else if (This->effect.type == FF_CONSTANT) {
411             expectedsize = sizeof(DICONSTANTFORCE);
412         } else if (This->effect.type == FF_SPRING 
413                 || This->effect.type == FF_FRICTION 
414                 || This->effect.type == FF_INERTIA 
415                 || This->effect.type == FF_DAMPER) {
416             expectedsize = sizeof(DICONDITION) * 2;
417         } else if (This->effect.type == FF_RAMP) {
418             expectedsize = sizeof(DIRAMPFORCE);
419         }
420         if (expectedsize > peff->cbTypeSpecificParams)
421             diErr = DIERR_MOREDATA;
422         peff->cbTypeSpecificParams = expectedsize;
423         if (diErr)
424             return diErr;
425         else {
426             if (This->effect.type == FF_PERIODIC) {
427                 LPDIPERIODIC tsp = peff->lpvTypeSpecificParams;
428                 tsp->dwMagnitude = (This->effect.u.periodic.magnitude / 33) * 10;
429                 tsp->lOffset = (This->effect.u.periodic.offset / 33) * 10;
430                 tsp->dwPhase = (This->effect.u.periodic.phase / 33) * 36;
431                 tsp->dwPeriod = (This->effect.u.periodic.period * 1000);
432             } else if (This->effect.type == FF_CONSTANT) {
433                 LPDICONSTANTFORCE tsp = peff->lpvTypeSpecificParams;
434                 tsp->lMagnitude = (This->effect.u.constant.level / 33) * 10;
435             } else if (This->effect.type == FF_SPRING 
436                     || This->effect.type == FF_FRICTION 
437                     || This->effect.type == FF_INERTIA 
438                     || This->effect.type == FF_DAMPER) {
439                 LPDICONDITION tsp = peff->lpvTypeSpecificParams;
440                 int i;
441                 for (i = 0; i < 2; ++i) {
442                     tsp[i].lOffset = (This->effect.u.condition[i].center / 33) * 10; 
443                     tsp[i].lPositiveCoefficient = (This->effect.u.condition[i].right_coeff / 33) * 10;
444                     tsp[i].lNegativeCoefficient = (This->effect.u.condition[i].left_coeff / 33) * 10; 
445                     tsp[i].dwPositiveSaturation = (This->effect.u.condition[i].right_saturation / 33) * 10;
446                     tsp[i].dwNegativeSaturation = (This->effect.u.condition[i].left_saturation / 33) * 10;
447                     tsp[i].lDeadBand = (This->effect.u.condition[i].deadband / 33) * 10;
448                 }
449             } else if (This->effect.type == FF_RAMP) {
450                 LPDIRAMPFORCE tsp = peff->lpvTypeSpecificParams;
451                 tsp->lStart = (This->effect.u.ramp.start_level / 33) * 10;
452                 tsp->lEnd = (This->effect.u.ramp.end_level / 33) * 10;
453             }
454         }
455     } 
456
457     return diErr;
458 }
459
460 static HRESULT WINAPI LinuxInputEffectImpl_Initialize(
461         LPDIRECTINPUTEFFECT iface,
462         HINSTANCE hinst,
463         DWORD dwVersion,
464         REFGUID rguid)
465 {
466     FIXME("(this=%p,%p,%d,%s): stub!\n",
467          iface, hinst, dwVersion, debugstr_guid(rguid));
468
469     return DI_OK;
470 }
471
472 static HRESULT WINAPI LinuxInputEffectImpl_QueryInterface(
473         LPDIRECTINPUTEFFECT iface,
474         REFIID riid,
475         void **ppvObject)
476 {
477     LinuxInputEffectImpl* This = (LinuxInputEffectImpl*)iface;
478
479     TRACE("(this=%p,%s,%p)\n", This, debugstr_guid(riid), ppvObject);
480
481     if (IsEqualGUID(&IID_IUnknown, riid) ||
482         IsEqualGUID(&IID_IDirectInputEffect, riid)) {
483             LinuxInputEffectImpl_AddRef(iface);
484             *ppvObject = This;
485             return 0;
486     }
487
488     TRACE("Unsupported interface!\n");
489     return E_FAIL;
490 }
491
492 static HRESULT WINAPI LinuxInputEffectImpl_Start(
493         LPDIRECTINPUTEFFECT iface,
494         DWORD dwIterations,
495         DWORD dwFlags)
496 {
497     struct input_event event;
498     LinuxInputEffectImpl* This = (LinuxInputEffectImpl*)iface;
499
500     TRACE("(this=%p,%d,%d)\n", This, dwIterations, dwFlags);
501
502     if (!(dwFlags & DIES_NODOWNLOAD)) {
503         /* Download the effect if necessary */
504         if (This->effect.id == -1) {
505             HRESULT res = LinuxInputEffectImpl_Download(iface);
506             if (res != DI_OK)
507                 return res;
508         }
509     }
510
511     if (dwFlags & DIES_SOLO) {
512         FIXME("Solo mode requested: should be stopping all effects here!\n");
513     }
514
515     event.type = EV_FF;
516     event.code = This->effect.id;
517     event.value = dwIterations;
518     if (write(*(This->fd), &event, sizeof(event)) == -1) {
519         FIXME("Unable to write event.  Assuming device disconnected.\n");
520         return DIERR_INPUTLOST;
521     }
522
523     return DI_OK;
524 }
525
526 static HRESULT WINAPI LinuxInputEffectImpl_SetParameters(
527         LPDIRECTINPUTEFFECT iface,
528         LPCDIEFFECT peff,
529         DWORD dwFlags)
530 {       
531     LinuxInputEffectImpl* This = (LinuxInputEffectImpl*)iface; 
532     DWORD type = _typeFromGUID(&This->guid);
533     HRESULT retval = DI_OK;
534
535     TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
536
537     _dump_DIEFFECT(peff, &This->guid);
538
539     if ((dwFlags & ~DIEP_NORESTART & ~DIEP_NODOWNLOAD & ~DIEP_START) == 0) {
540         /* set everything */
541         dwFlags = DIEP_AXES | DIEP_DIRECTION | DIEP_DURATION | DIEP_ENVELOPE |
542             DIEP_GAIN | DIEP_SAMPLEPERIOD | DIEP_STARTDELAY | DIEP_TRIGGERBUTTON |
543             DIEP_TRIGGERREPEATINTERVAL | DIEP_TYPESPECIFICPARAMS;
544     }
545
546     if (dwFlags & DIEP_AXES) {
547         /* the linux input effect system only supports one or two axes */
548         if (peff->cAxes > 2)
549             return DIERR_INVALIDPARAM;
550         else if (peff->cAxes < 1)
551             return DIERR_INCOMPLETEEFFECT;
552         This->first_axis_is_x = peff->rgdwAxes[0] == DIJOFS_X;
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                 LONG x, y;
578                 if (This->first_axis_is_x) {
579                     x = peff->rglDirection[0];
580                     y = peff->rglDirection[1];
581                 } else {
582                     x = peff->rglDirection[1];
583                     y = peff->rglDirection[0];
584                 }
585                 This->effect.direction = (int)((3 * M_PI / 2 - atan2(y, x)) * -0x7FFF / M_PI);
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 minuscule */
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         This->gain = 0xFFFF * peff->dwGain / 10000;
628         TRACE("Effect gain requested but no effect gain functionality present.\n");
629     }
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 = 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 = peff->lpvTypeSpecificParams;
665             This->effect.u.constant.level = (max(min(tsp->lMagnitude, 10000), -10000) / 10) * 32;
666         } else if (type == DIEFT_RAMPFORCE) {
667             LPCDIRAMPFORCE tsp;
668             if (peff->cbTypeSpecificParams != sizeof(DIRAMPFORCE))
669                 return DIERR_INVALIDPARAM;
670             tsp = 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 = 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 HRESULT WINAPI LinuxInputEffectImpl_Stop(
727         LPDIRECTINPUTEFFECT iface)
728 {
729     struct input_event event;
730     LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
731
732     TRACE("(this=%p)\n", This);
733
734     event.type = EV_FF;
735     event.code = This->effect.id;
736     event.value = 0;
737     /* we don't care about the success or failure of this call */
738     write(*(This->fd), &event, sizeof(event));
739
740     return DI_OK;
741 }
742
743 static HRESULT WINAPI LinuxInputEffectImpl_Unload(
744         LPDIRECTINPUTEFFECT iface)
745 {
746     LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
747     TRACE("(this=%p)\n", This);
748
749     /* Erase the downloaded effect */
750     if (ioctl(*(This->fd), EVIOCRMFF, This->effect.id) == -1)
751         return DIERR_INVALIDPARAM;
752
753     /* Mark the effect as deallocated */
754     This->effect.id = -1;
755
756     return DI_OK;
757 }
758
759 static ULONG WINAPI LinuxInputEffectImpl_Release(LPDIRECTINPUTEFFECT iface)
760 {
761     LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
762     ULONG ref = InterlockedDecrement(&(This->ref));
763
764     if (ref == 0)
765     {
766         LinuxInputEffectImpl_Stop(iface);
767         LinuxInputEffectImpl_Unload(iface);
768         list_remove(This->entry);
769         HeapFree(GetProcessHeap(), 0, LIST_ENTRY(This->entry, effect_list_item, entry));
770         HeapFree(GetProcessHeap(), 0, This);
771     }
772     return ref;
773 }
774
775 /******************************************************************************
776  *      LinuxInputEffect
777  */
778
779 HRESULT linuxinput_create_effect(
780         int* fd,
781         REFGUID rguid,
782         struct list *parent_list_entry,
783         LPDIRECTINPUTEFFECT* peff)
784 {
785     LinuxInputEffectImpl* newEffect = HeapAlloc(GetProcessHeap(), 
786         HEAP_ZERO_MEMORY, sizeof(LinuxInputEffectImpl));
787     DWORD type = _typeFromGUID(rguid);
788
789     newEffect->lpVtbl = &LinuxInputEffectVtbl;
790     newEffect->ref = 1;
791     newEffect->guid = *rguid;
792     newEffect->fd = fd;
793     newEffect->gain = 0xFFFF;
794
795     /* set the type.  this cannot be changed over the effect's life. */
796     switch (type) {
797         case DIEFT_PERIODIC: 
798             newEffect->effect.type = FF_PERIODIC;
799             if (IsEqualGUID(rguid, &GUID_Sine)) {
800                 newEffect->effect.u.periodic.waveform = FF_SINE;
801             } else if (IsEqualGUID(rguid, &GUID_Triangle)) {
802                 newEffect->effect.u.periodic.waveform = FF_TRIANGLE;
803             } else if (IsEqualGUID(rguid, &GUID_Square)) {
804                 newEffect->effect.u.periodic.waveform = FF_SQUARE;
805             } else if (IsEqualGUID(rguid, &GUID_SawtoothUp)) {
806                 newEffect->effect.u.periodic.waveform = FF_SAW_UP;
807             } else if (IsEqualGUID(rguid, &GUID_SawtoothDown)) {
808                 newEffect->effect.u.periodic.waveform = FF_SAW_DOWN;
809             }
810             break;
811         case DIEFT_CONSTANTFORCE: 
812             newEffect->effect.type = FF_CONSTANT;
813             break;
814         case DIEFT_RAMPFORCE: 
815             newEffect->effect.type = FF_RAMP;
816             break;
817         case DIEFT_CONDITION: 
818             if (IsEqualGUID(rguid, &GUID_Spring)) {
819                 newEffect->effect.type = FF_SPRING;
820             } else if (IsEqualGUID(rguid, &GUID_Friction)) {
821                 newEffect->effect.type = FF_FRICTION;
822             } else if (IsEqualGUID(rguid, &GUID_Inertia)) {
823                 newEffect->effect.type = FF_INERTIA;
824             } else if (IsEqualGUID(rguid, &GUID_Damper)) {
825                 newEffect->effect.type = FF_DAMPER;
826             }
827             break;
828         case DIEFT_CUSTOMFORCE:
829             FIXME("Custom forces are not supported.\n");
830             HeapFree(GetProcessHeap(), 0, newEffect);
831             return DIERR_INVALIDPARAM;
832         default:
833             FIXME("Unknown force type 0x%x.\n", type);
834             HeapFree(GetProcessHeap(), 0, newEffect);
835             return DIERR_INVALIDPARAM;
836     }
837
838     /* mark as non-uploaded */
839     newEffect->effect.id = -1;
840
841     newEffect->entry = parent_list_entry;
842
843     *peff = (LPDIRECTINPUTEFFECT)newEffect; 
844
845     TRACE("Creating linux input system effect (%p) with guid %s\n", 
846           *peff, _dump_dinput_GUID(rguid));
847
848     return DI_OK;
849 }
850
851 HRESULT linuxinput_get_info_A(
852         int fd,
853         REFGUID rguid,
854         LPDIEFFECTINFOA info)
855 {
856     DWORD type = _typeFromGUID(rguid);
857
858     TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
859
860     if (!info) return E_POINTER;
861
862     if (info->dwSize != sizeof(DIEFFECTINFOA)) return DIERR_INVALIDPARAM;
863
864     info->guid = *rguid;
865     
866     info->dwEffType = type; 
867     /* the event device API does not support querying for all these things
868      * therefore we assume that we have support for them
869      * that's not as dangerous as it sounds, since drivers are allowed to
870      * ignore parameters they claim to support anyway */
871     info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE 
872                     | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
873                     | DIEFT_SATURATION | DIEFT_STARTDELAY; 
874
875     /* again, assume we have support for everything */
876     info->dwStaticParams = DIEP_ALLPARAMS;
877     info->dwDynamicParams = info->dwStaticParams;
878
879     /* yes, this is windows behavior (print the GUID_Name for name) */
880     strcpy(info->tszName, _dump_dinput_GUID(rguid));
881
882     return DI_OK;
883 }
884
885 HRESULT linuxinput_get_info_W(
886         int fd,
887         REFGUID rguid,
888         LPDIEFFECTINFOW info)
889 {
890     DWORD type = _typeFromGUID(rguid);
891
892     TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
893
894     if (!info) return E_POINTER;
895
896     if (info->dwSize != sizeof(DIEFFECTINFOW)) return DIERR_INVALIDPARAM;
897
898     info->guid = *rguid;
899
900     info->dwEffType = type;
901     /* the event device API does not support querying for all these things
902      * therefore we assume that we have support for them
903      * that's not as dangerous as it sounds, since drivers are allowed to
904      * ignore parameters they claim to support anyway */
905     info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE
906                     | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
907                     | DIEFT_SATURATION | DIEFT_STARTDELAY; 
908
909     /* again, assume we have support for everything */
910     info->dwStaticParams = DIEP_ALLPARAMS;
911     info->dwDynamicParams = info->dwStaticParams;
912
913     /* yes, this is windows behavior (print the GUID_Name for name) */
914     MultiByteToWideChar(CP_ACP, 0, _dump_dinput_GUID(rguid), -1, 
915                         info->tszName, MAX_PATH);
916
917     return DI_OK;
918 }
919
920 static const IDirectInputEffectVtbl LinuxInputEffectVtbl = {
921     LinuxInputEffectImpl_QueryInterface,
922     LinuxInputEffectImpl_AddRef,
923     LinuxInputEffectImpl_Release,
924     LinuxInputEffectImpl_Initialize,
925     LinuxInputEffectImpl_GetEffectGuid,
926     LinuxInputEffectImpl_GetParameters,
927     LinuxInputEffectImpl_SetParameters,
928     LinuxInputEffectImpl_Start,
929     LinuxInputEffectImpl_Stop,
930     LinuxInputEffectImpl_GetEffectStatus,
931     LinuxInputEffectImpl_Download,
932     LinuxInputEffectImpl_Unload,
933     LinuxInputEffectImpl_Escape
934 };
935
936 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */