cryptui: Allow toggling a usage in the certificate manager dialog's advanced dialog.
[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*                fd;     /* Parent device */
58     struct list        *entry;  /* Entry into the parent's list of effects */
59 };
60
61
62 /******************************************************************************
63  *      DirectInputEffect Functional Helper
64  */
65
66 static DWORD _typeFromGUID(REFGUID guid)
67 {
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;
88     }
89 }
90
91
92 /******************************************************************************
93  *      DirectInputEffect debug helpers 
94  */
95
96 static void _dump_DIEFFECT_flags(DWORD dwFlags)
97 {
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
111         };
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");
116     }       
117 }
118
119 static void _dump_DIENVELOPE(LPCDIENVELOPE env)
120 {
121     if (env->dwSize != sizeof(DIENVELOPE)) {
122         WARN("Non-standard DIENVELOPE structure size %d.\n", env->dwSize);
123     }
124     TRACE("Envelope has attack (level: %d time: %d), fade (level: %d time: %d)\n",
125           env->dwAttackLevel, env->dwAttackTime, env->dwFadeLevel, env->dwFadeTime);
126
127
128 static void _dump_DICONSTANTFORCE(LPCDICONSTANTFORCE frc)
129 {
130     TRACE("Constant force has magnitude %d\n", frc->lMagnitude);
131 }
132
133 static void _dump_DIPERIODIC(LPCDIPERIODIC frc)
134 {
135     TRACE("Periodic force has magnitude %d, offset %d, phase %d, period %d\n",
136           frc->dwMagnitude, frc->lOffset, frc->dwPhase, frc->dwPeriod);
137 }
138
139 static void _dump_DIRAMPFORCE(LPCDIRAMPFORCE frc)
140 {
141     TRACE("Ramp force has start %d, end %d\n",
142           frc->lStart, frc->lEnd);
143 }
144
145 static void _dump_DICONDITION(LPCDICONDITION frc)
146 {
147     TRACE("Condition has offset %d, pos/neg coefficients %d and %d, pos/neg saturations %d and %d, deadband %d\n",
148           frc->lOffset, frc->lPositiveCoefficient, frc->lNegativeCoefficient,
149           frc->dwPositiveSaturation, frc->dwNegativeSaturation, frc->lDeadBand);
150 }
151
152 static void _dump_DICUSTOMFORCE(LPCDICUSTOMFORCE frc)
153 {
154     unsigned int i;
155     TRACE("Custom force uses %d channels, sample period %d.  Has %d 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         TRACE("Custom force data (time aligned, axes in order):\n");
161         for (i = 1; i <= frc->cSamples; ++i) {
162             TRACE("%d ", frc->rglForceData[i]);
163             if (i % frc->cChannels == 0)
164                 TRACE("\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: %d\n", eff->dwSize);
176     if ((eff->dwSize != sizeof(DIEFFECT)) && (eff->dwSize != sizeof(DIEFFECT_DX5))) {
177         WARN("Non-standard DIEFFECT structure size %d\n", eff->dwSize);
178     }
179     TRACE("  - dwFlags: %d\n", eff->dwFlags);
180     TRACE("    ");
181     _dump_DIEFFECT_flags(eff->dwFlags); 
182     TRACE("  - dwDuration: %d\n", eff->dwDuration);
183     TRACE("  - dwGain: %d\n", eff->dwGain);
184     if (eff->dwGain > 10000)
185         WARN("dwGain is out of range (>10,000)\n");
186     TRACE("  - dwTriggerButton: %d\n", eff->dwTriggerButton);
187     TRACE("  - dwTriggerRepeatInterval: %d\n", eff->dwTriggerRepeatInterval);
188     TRACE("  - cAxes: %d\n", eff->cAxes);
189     TRACE("  - rgdwAxes: %p\n", eff->rgdwAxes);
190     if (TRACE_ON(dinput) && eff->rgdwAxes) {
191         TRACE("    ");  
192         for (i = 0; i < eff->cAxes; ++i)
193             TRACE("%d ", eff->rgdwAxes[i]);
194         TRACE("\n");
195     }
196     TRACE("  - rglDirection: %p\n", eff->rglDirection);
197     TRACE("  - lpEnvelope: %p\n", eff->lpEnvelope);
198     TRACE("  - cbTypeSpecificParams: %d\n", eff->cbTypeSpecificParams);
199     TRACE("  - lpvTypeSpecificParams: %p\n", eff->lpvTypeSpecificParams);
200     if (eff->dwSize > sizeof(DIEFFECT_DX5))
201         TRACE("  - dwStartDelay: %d\n", eff->dwStartDelay);
202     if (eff->lpEnvelope != NULL)
203         _dump_DIENVELOPE(eff->lpEnvelope);
204     if (type == DIEFT_CONSTANTFORCE) {
205         if (eff->cbTypeSpecificParams != sizeof(DICONSTANTFORCE)) {
206             WARN("Effect claims to be a constant force but the type-specific params are the wrong size!\n"); 
207         } else {
208             _dump_DICONSTANTFORCE(eff->lpvTypeSpecificParams);
209         }
210     } else if (type == DIEFT_PERIODIC) { 
211         if (eff->cbTypeSpecificParams != sizeof(DIPERIODIC)) {
212             WARN("Effect claims to be a periodic force but the type-specific params are the wrong size!\n");
213         } else {
214             _dump_DIPERIODIC(eff->lpvTypeSpecificParams);
215         }
216     } else if (type == DIEFT_RAMPFORCE) {
217         if (eff->cbTypeSpecificParams != sizeof(DIRAMPFORCE)) {
218             WARN("Effect claims to be a ramp force but the type-specific params are the wrong size!\n");
219         } else {
220             _dump_DIRAMPFORCE(eff->lpvTypeSpecificParams);
221         }
222     } else if (type == DIEFT_CONDITION) { 
223         if (eff->cbTypeSpecificParams != sizeof(DICONDITION)) {
224             WARN("Effect claims to be a condition but the type-specific params are the wrong size!\n");
225         } else {
226             _dump_DICONDITION(eff->lpvTypeSpecificParams);
227         }
228     } else if (type == DIEFT_CUSTOMFORCE) {
229         if (eff->cbTypeSpecificParams != sizeof(DICUSTOMFORCE)) {
230             WARN("Effect claims to be a custom force but the type-specific params are the wrong size!\n");
231         } else {
232             _dump_DICUSTOMFORCE(eff->lpvTypeSpecificParams);
233         }
234     }
235 }
236
237
238 /******************************************************************************
239  *      LinuxInputEffectImpl 
240  */
241
242 static ULONG WINAPI LinuxInputEffectImpl_AddRef(
243         LPDIRECTINPUTEFFECT iface)
244 {
245     LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
246     return InterlockedIncrement(&(This->ref));
247 }
248
249 static HRESULT WINAPI LinuxInputEffectImpl_Download(
250         LPDIRECTINPUTEFFECT iface)
251 {
252     LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
253
254     TRACE("(this=%p)\n", This);
255
256     if (ioctl(*(This->fd), EVIOCSFF, &This->effect) == -1) {
257         if (errno == ENOMEM) {
258             return DIERR_DEVICEFULL;
259         } else {
260             FIXME("Could not upload effect. Assuming a disconnected device %d \"%s\".\n", *This->fd, strerror(errno));
261             return DIERR_INPUTLOST;
262         }
263     }
264
265     return DI_OK;
266 }
267
268 static HRESULT WINAPI LinuxInputEffectImpl_Escape(
269         LPDIRECTINPUTEFFECT iface,
270         LPDIEFFESCAPE pesc)
271 {
272     WARN("(this=%p,%p): invalid: no hardware-specific escape codes in this" 
273          " driver!\n", iface, pesc);
274
275     return DI_OK;
276 }
277
278 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectGuid(
279         LPDIRECTINPUTEFFECT iface,
280         LPGUID pguid)
281 {
282     LinuxInputEffectImpl *This = (LinuxInputEffectImpl*)iface;
283
284     TRACE("(this=%p,%p)\n", This, pguid);
285
286     pguid = &This->guid;
287     
288     return DI_OK;
289 }
290
291 static HRESULT WINAPI LinuxInputEffectImpl_GetEffectStatus(
292         LPDIRECTINPUTEFFECT iface,
293         LPDWORD pdwFlags)
294 {
295     TRACE("(this=%p,%p)\n", iface, pdwFlags);
296
297     /* linux sends the effect status through an event.
298      * that event is trapped by our parent joystick driver
299      * and there is no clean way to pass it back to us. */
300     FIXME("Not enough information to provide a status.\n");
301
302     (*pdwFlags) = 0;
303
304     return DI_OK;
305 }
306
307 static HRESULT WINAPI LinuxInputEffectImpl_GetParameters(
308         LPDIRECTINPUTEFFECT iface,
309         LPDIEFFECT peff,
310         DWORD dwFlags)
311 {
312     HRESULT diErr = DI_OK;
313     LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
314     TRACE("(this=%p,%p,%d)\n", This, peff, dwFlags);
315
316     /* Major conversion factors are:
317      * times: millisecond (linux) -> microsecond (windows) (x * 1000)
318      * forces: scale 0x7FFF (linux) -> scale 10000 (windows) approx ((x / 33) * 10)
319      * angles: scale 0x7FFF (linux) -> scale 35999 (windows) approx ((x / 33) * 36)
320      * angle bases: 0 -> -y (down) (linux) -> 0 -> +x (right) (windows)
321      */
322
323     if (dwFlags & DIEP_AXES) {
324         if (peff->cAxes < 2 /* linuxinput effects always use 2 axes, x and y */)
325             diErr = DIERR_MOREDATA;
326         peff->cAxes = 2; 
327         if (diErr)
328             return diErr;
329         else {
330             peff->rgdwAxes[0] = DIJOFS_X;
331             peff->rgdwAxes[1] = DIJOFS_Y;
332         }
333     }
334  
335     if (dwFlags & DIEP_DIRECTION) {
336         if (peff->cAxes < 2)
337             diErr = DIERR_MOREDATA;
338         peff->cAxes = 2; 
339         if (diErr)
340             return diErr;
341         else {
342             if (peff->dwFlags & DIEFF_CARTESIAN) {
343                 peff->rglDirection[0] = (long)(sin(M_PI * 3 * This->effect.direction / 0x7FFF) * 1000);
344                 peff->rglDirection[1] = (long)(cos(M_PI * 3 * This->effect.direction / 0x7FFF) * 1000);
345             } else {
346                 /* Polar and spherical coordinates are the same for two or less
347                  * axes.
348                  * Note that we also use this case if NO flags are marked.
349                  * According to MSDN, we should return the direction in the
350                  * format that it was specified in, if no flags are marked.
351                  */
352                 peff->rglDirection[0] = (This->effect.direction / 33) * 36 + 9000;
353                 if (peff->rglDirection[0] > 35999)
354                     peff->rglDirection[0] -= 35999;
355             }
356         }
357     }
358
359     if (dwFlags & DIEP_DURATION) {
360         peff->dwDuration = (DWORD)This->effect.replay.length * 1000;
361     }
362
363     if (dwFlags & DIEP_ENVELOPE) {
364         struct ff_envelope* env;
365         if (This->effect.type == FF_CONSTANT) env = &This->effect.u.constant.envelope;
366         else if (This->effect.type == FF_PERIODIC) env = &This->effect.u.periodic.envelope;
367         else if (This->effect.type == FF_RAMP) env = &This->effect.u.ramp.envelope;
368         else env = NULL;
369         if (env == NULL) {
370             peff->lpEnvelope = NULL;
371         } else if (peff->lpEnvelope == NULL) {
372             return DIERR_INVALIDPARAM;
373         } else { 
374             peff->lpEnvelope->dwAttackLevel = (env->attack_level / 33) * 10;
375             peff->lpEnvelope->dwAttackTime = env->attack_length * 1000;
376             peff->lpEnvelope->dwFadeLevel = (env->fade_level / 33) * 10;
377             peff->lpEnvelope->dwFadeTime = env->fade_length * 1000;
378         }
379     }
380
381     if (dwFlags & DIEP_GAIN) {
382         /* the linux input ff driver apparently has no support
383          * for setting the device's gain. */
384         peff->dwGain = DI_FFNOMINALMAX;
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 = (LPDIPERIODIC)(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 = (LPDICONSTANTFORCE)(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 = (LPDICONDITION)(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 = (LPDIRAMPFORCE)(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     }
553
554     /* some of this may look funky, but it's 'cause the linux driver and directx have
555      * different opinions about which way direction "0" is.  directx has 0 along the x
556      * axis (left), linux has it along the y axis (down). */ 
557     if (dwFlags & DIEP_DIRECTION) {
558         if (peff->cAxes == 1) {
559             if (peff->dwFlags & DIEFF_CARTESIAN) {
560                 if (dwFlags & DIEP_AXES) {
561                     if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] >= 0)
562                         This->effect.direction = 0x4000;
563                     else if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] < 0)
564                         This->effect.direction = 0xC000;
565                     else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] >= 0)
566                         This->effect.direction = 0;
567                     else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] < 0)
568                         This->effect.direction = 0x8000;
569                 }
570             } else {
571                 /* one-axis effects must use cartesian coords */
572                 return DIERR_INVALIDPARAM;
573             }
574         } else { /* two axes */
575             if (peff->dwFlags & DIEFF_CARTESIAN) {
576                 /* avoid divide-by-zero */
577                 if (peff->rglDirection[1] == 0) {
578                     if (peff->rglDirection[0] >= 0)
579                         This->effect.direction = 0x4000;
580                     else if (peff->rglDirection[0] < 0)
581                         This->effect.direction = 0xC000;
582                 } else {
583                     This->effect.direction = (int)(atan(peff->rglDirection[0] / peff->rglDirection[1]) * 0x7FFF / (3 * M_PI));
584                 }
585             } else {
586                 /* Polar and spherical are the same for 2 axes */
587                 /* Precision is important here, so we do double math with exact constants */
588                 This->effect.direction = (int)(((double)peff->rglDirection[0] - 90) / 35999) * 0x7FFF;
589             }
590         }
591     }
592
593     if (dwFlags & DIEP_DURATION)
594         This->effect.replay.length = peff->dwDuration / 1000;
595
596     if (dwFlags & DIEP_ENVELOPE) {
597         struct ff_envelope* env;
598         if (This->effect.type == FF_CONSTANT) env = &This->effect.u.constant.envelope;
599         else if (This->effect.type == FF_PERIODIC) env = &This->effect.u.periodic.envelope;
600         else if (This->effect.type == FF_RAMP) env = &This->effect.u.ramp.envelope;
601         else env = NULL; 
602
603         if (peff->lpEnvelope == NULL) {
604             /* if this type had an envelope, reset it
605              * note that length can never be zero, so we set it to something minuscule */
606             if (env) {
607                 env->attack_length = 0x10;
608                 env->attack_level = 0x7FFF;
609                 env->fade_length = 0x10;
610                 env->fade_level = 0x7FFF;
611             }
612         } else {
613             /* did we get passed an envelope for a type that doesn't even have one? */
614             if (!env) return DIERR_INVALIDPARAM;
615             /* copy the envelope */
616             env->attack_length = peff->lpEnvelope->dwAttackTime / 1000;
617             env->attack_level = (peff->lpEnvelope->dwAttackLevel / 10) * 32;
618             env->fade_length = peff->lpEnvelope->dwFadeTime / 1000;
619             env->fade_level = (peff->lpEnvelope->dwFadeLevel / 10) * 32;
620         }
621     }
622
623     /* Gain and Sample Period settings are not supported by the linux
624      * event system */
625     if (dwFlags & DIEP_GAIN)
626         TRACE("Gain requested but no gain functionality present.\n");
627
628     if (dwFlags & DIEP_SAMPLEPERIOD)
629         TRACE("Sample period requested but no sample period functionality present.\n");
630
631     if (dwFlags & DIEP_STARTDELAY)
632         This->effect.replay.delay = peff->dwStartDelay / 1000;
633
634     if (dwFlags & DIEP_TRIGGERBUTTON) {
635         if (peff->dwTriggerButton != -1) {
636             FIXME("Linuxinput button mapping needs redoing, assuming we're using a joystick.\n");
637             FIXME("Trigger button translation not yet implemented!\n");
638         }
639         This->effect.trigger.button = 0;
640     }
641
642     if (dwFlags & DIEP_TRIGGERREPEATINTERVAL)
643         This->effect.trigger.interval = peff->dwTriggerRepeatInterval / 1000;
644
645     if (dwFlags & DIEP_TYPESPECIFICPARAMS) {
646         if (!(peff->lpvTypeSpecificParams))
647             return DIERR_INCOMPLETEEFFECT;
648         if (type == DIEFT_PERIODIC) {
649             LPCDIPERIODIC tsp;
650             if (peff->cbTypeSpecificParams != sizeof(DIPERIODIC))
651                 return DIERR_INVALIDPARAM;
652             tsp = (LPCDIPERIODIC)(peff->lpvTypeSpecificParams);
653             This->effect.u.periodic.magnitude = (tsp->dwMagnitude / 10) * 32;
654             This->effect.u.periodic.offset = (tsp->lOffset / 10) * 32;
655             This->effect.u.periodic.phase = (tsp->dwPhase / 9) * 8; /* == (/ 36 * 32) */
656             This->effect.u.periodic.period = tsp->dwPeriod / 1000;
657         } else if (type == DIEFT_CONSTANTFORCE) {
658             LPCDICONSTANTFORCE tsp;
659             if (peff->cbTypeSpecificParams != sizeof(DICONSTANTFORCE))
660                 return DIERR_INVALIDPARAM;
661             tsp = (LPCDICONSTANTFORCE)(peff->lpvTypeSpecificParams);
662             This->effect.u.constant.level = (tsp->lMagnitude / 10) * 32;
663         } else if (type == DIEFT_RAMPFORCE) {
664             LPCDIRAMPFORCE tsp;
665             if (peff->cbTypeSpecificParams != sizeof(DIRAMPFORCE))
666                 return DIERR_INVALIDPARAM;
667             tsp = (LPCDIRAMPFORCE)(peff->lpvTypeSpecificParams);
668             This->effect.u.ramp.start_level = (tsp->lStart / 10) * 32;
669             This->effect.u.ramp.end_level = (tsp->lStart / 10) * 32;
670         } else if (type == DIEFT_CONDITION) {
671             LPCDICONDITION tsp = (LPCDICONDITION)(peff->lpvTypeSpecificParams);
672             if (peff->cbTypeSpecificParams == sizeof(DICONDITION)) {
673                 /* One condition block.  This needs to be rotated to direction,
674                  * and expanded to separate x and y conditions. */
675                 int i;
676                 double factor[2];
677                 factor[0] = asin((This->effect.direction * 3.0 * M_PI) / 0x7FFF);
678                 factor[1] = acos((This->effect.direction * 3.0 * M_PI) / 0x7FFF);
679                 for (i = 0; i < 2; ++i) {
680                     This->effect.u.condition[i].center = (int)(factor[i] * (tsp->lOffset / 10) * 32);
681                     This->effect.u.condition[i].right_coeff = (int)(factor[i] * (tsp->lPositiveCoefficient / 10) * 32);
682                     This->effect.u.condition[i].left_coeff = (int)(factor[i] * (tsp->lNegativeCoefficient / 10) * 32); 
683                     This->effect.u.condition[i].right_saturation = (int)(factor[i] * (tsp->dwPositiveSaturation / 10) * 32);
684                     This->effect.u.condition[i].left_saturation = (int)(factor[i] * (tsp->dwNegativeSaturation / 10) * 32);
685                     This->effect.u.condition[i].deadband = (int)(factor[i] * (tsp->lDeadBand / 10) * 32);
686                 }
687             } else if (peff->cbTypeSpecificParams == 2 * sizeof(DICONDITION)) {
688                 /* Two condition blocks.  Direct parameter copy. */
689                 int i;
690                 for (i = 0; i < 2; ++i) {
691                     This->effect.u.condition[i].center = (tsp[i].lOffset / 10) * 32;
692                     This->effect.u.condition[i].right_coeff = (tsp[i].lPositiveCoefficient / 10) * 32;
693                     This->effect.u.condition[i].left_coeff = (tsp[i].lNegativeCoefficient / 10) * 32;
694                     This->effect.u.condition[i].right_saturation = (tsp[i].dwPositiveSaturation / 10) * 32;
695                     This->effect.u.condition[i].left_saturation = (tsp[i].dwNegativeSaturation / 10) * 32;
696                     This->effect.u.condition[i].deadband = (tsp[i].lDeadBand / 10) * 32;
697                 }
698             } else {
699                 return DIERR_INVALIDPARAM;
700             }
701         } else {
702             FIXME("Custom force types are not supported\n");    
703             return DIERR_INVALIDPARAM;
704         }
705     }
706
707     if (!(dwFlags & DIEP_NODOWNLOAD))
708         retval = LinuxInputEffectImpl_Download(iface);
709     if (retval != DI_OK)
710         return DI_DOWNLOADSKIPPED;
711
712     if (dwFlags & DIEP_NORESTART)
713         TRACE("DIEP_NORESTART: not handled (we have no control of that).\n");
714
715     if (dwFlags & DIEP_START)
716         retval = LinuxInputEffectImpl_Start(iface, 1, 0);
717     if (retval != DI_OK)
718         return retval;
719  
720     return DI_OK;
721 }   
722
723 static HRESULT WINAPI LinuxInputEffectImpl_Stop(
724         LPDIRECTINPUTEFFECT iface)
725 {
726     struct input_event event;
727     LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
728
729     TRACE("(this=%p)\n", This);
730
731     event.type = EV_FF;
732     event.code = This->effect.id;
733     event.value = 0;
734     /* we don't care about the success or failure of this call */
735     write(*(This->fd), &event, sizeof(event));
736
737     return DI_OK;
738 }
739
740 static HRESULT WINAPI LinuxInputEffectImpl_Unload(
741         LPDIRECTINPUTEFFECT iface)
742 {
743     LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
744     TRACE("(this=%p)\n", This);
745
746     /* Erase the downloaded effect */
747     if (ioctl(*(This->fd), EVIOCRMFF, This->effect.id) == -1)
748         return DIERR_INVALIDPARAM;
749
750     /* Mark the effect as deallocated */
751     This->effect.id = -1;
752
753     return DI_OK;
754 }
755
756 static ULONG WINAPI LinuxInputEffectImpl_Release(LPDIRECTINPUTEFFECT iface)
757 {
758     LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
759     ULONG ref = InterlockedDecrement(&(This->ref));
760
761     if (ref == 0)
762     {
763         LinuxInputEffectImpl_Stop(iface);
764         LinuxInputEffectImpl_Unload(iface);
765         list_remove(This->entry);
766         HeapFree(GetProcessHeap(), 0, LIST_ENTRY(This->entry, effect_list_item, entry));
767         HeapFree(GetProcessHeap(), 0, This);
768     }
769     return ref;
770 }
771
772 /******************************************************************************
773  *      LinuxInputEffect
774  */
775
776 HRESULT linuxinput_create_effect(
777         int* fd,
778         REFGUID rguid,
779         struct list *parent_list_entry,
780         LPDIRECTINPUTEFFECT* peff)
781 {
782     LinuxInputEffectImpl* newEffect = HeapAlloc(GetProcessHeap(), 
783         HEAP_ZERO_MEMORY, sizeof(LinuxInputEffectImpl));
784     DWORD type = _typeFromGUID(rguid);
785
786     newEffect->lpVtbl = &LinuxInputEffectVtbl;
787     newEffect->ref = 1;
788     newEffect->guid = *rguid;
789     newEffect->fd = fd;
790
791     /* set the type.  this cannot be changed over the effect's life. */
792     switch (type) {
793         case DIEFT_PERIODIC: 
794             newEffect->effect.type = FF_PERIODIC;
795             if (IsEqualGUID(rguid, &GUID_Sine)) {
796                 newEffect->effect.u.periodic.waveform = FF_SINE;
797             } else if (IsEqualGUID(rguid, &GUID_Triangle)) {
798                 newEffect->effect.u.periodic.waveform = FF_TRIANGLE;
799             } else if (IsEqualGUID(rguid, &GUID_Square)) {
800                 newEffect->effect.u.periodic.waveform = FF_SQUARE;
801             } else if (IsEqualGUID(rguid, &GUID_SawtoothUp)) {
802                 newEffect->effect.u.periodic.waveform = FF_SAW_UP;
803             } else if (IsEqualGUID(rguid, &GUID_SawtoothDown)) {
804                 newEffect->effect.u.periodic.waveform = FF_SAW_DOWN;
805             }
806             break;
807         case DIEFT_CONSTANTFORCE: 
808             newEffect->effect.type = FF_CONSTANT;
809             break;
810         case DIEFT_RAMPFORCE: 
811             newEffect->effect.type = FF_RAMP;
812             break;
813         case DIEFT_CONDITION: 
814             if (IsEqualGUID(rguid, &GUID_Spring)) {
815                 newEffect->effect.type = FF_SPRING;
816             } else if (IsEqualGUID(rguid, &GUID_Friction)) {
817                 newEffect->effect.type = FF_FRICTION;
818             } else if (IsEqualGUID(rguid, &GUID_Inertia)) {
819                 newEffect->effect.type = FF_INERTIA;
820             } else if (IsEqualGUID(rguid, &GUID_Damper)) {
821                 newEffect->effect.type = FF_DAMPER;
822             }
823             break;
824         case DIEFT_CUSTOMFORCE:
825             FIXME("Custom forces are not supported.\n");
826             HeapFree(GetProcessHeap(), 0, newEffect);
827             return DIERR_INVALIDPARAM;
828         default:
829             FIXME("Unknown force type 0x%x.\n", type);
830             HeapFree(GetProcessHeap(), 0, newEffect);
831             return DIERR_INVALIDPARAM;
832     }
833
834     /* mark as non-uploaded */
835     newEffect->effect.id = -1;
836
837     newEffect->entry = parent_list_entry;
838
839     *peff = (LPDIRECTINPUTEFFECT)newEffect; 
840
841     TRACE("Creating linux input system effect (%p) with guid %s\n", 
842           *peff, _dump_dinput_GUID(rguid));
843
844     return DI_OK;
845 }
846
847 HRESULT linuxinput_get_info_A(
848         int fd,
849         REFGUID rguid,
850         LPDIEFFECTINFOA info)
851 {
852     DWORD type = _typeFromGUID(rguid);
853
854     TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
855
856     if (!info) return E_POINTER;
857
858     if (info->dwSize != sizeof(DIEFFECTINFOA)) return DIERR_INVALIDPARAM;
859
860     info->guid = *rguid;
861     
862     info->dwEffType = type; 
863     /* the event device API does not support querying for all these things
864      * therefore we assume that we have support for them
865      * that's not as dangerous as it sounds, since drivers are allowed to
866      * ignore parameters they claim to support anyway */
867     info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE 
868                     | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
869                     | DIEFT_SATURATION | DIEFT_STARTDELAY; 
870
871     /* again, assume we have support for everything */
872     info->dwStaticParams = DIEP_ALLPARAMS;
873     info->dwDynamicParams = info->dwStaticParams;
874
875     /* yes, this is windows behavior (print the GUID_Name for name) */
876     strcpy((char*)info->tszName, _dump_dinput_GUID(rguid));
877
878     return DI_OK;
879 }
880
881 HRESULT linuxinput_get_info_W(
882         int fd,
883         REFGUID rguid,
884         LPDIEFFECTINFOW info)
885 {
886     DWORD type = _typeFromGUID(rguid);
887
888     TRACE("(%d, %s, %p) type=%d\n", fd, _dump_dinput_GUID(rguid), info, type);
889
890     if (!info) return E_POINTER;
891
892     if (info->dwSize != sizeof(DIEFFECTINFOW)) return DIERR_INVALIDPARAM;
893
894     info->guid = *rguid;
895
896     info->dwEffType = type;
897     /* the event device API does not support querying for all these things
898      * therefore we assume that we have support for them
899      * that's not as dangerous as it sounds, since drivers are allowed to
900      * ignore parameters they claim to support anyway */
901     info->dwEffType |= DIEFT_DEADBAND | DIEFT_FFATTACK | DIEFT_FFFADE
902                     | DIEFT_POSNEGCOEFFICIENTS | DIEFT_POSNEGSATURATION
903                     | DIEFT_SATURATION | DIEFT_STARTDELAY; 
904
905     /* again, assume we have support for everything */
906     info->dwStaticParams = DIEP_ALLPARAMS;
907     info->dwDynamicParams = info->dwStaticParams;
908
909     /* yes, this is windows behavior (print the GUID_Name for name) */
910     MultiByteToWideChar(CP_ACP, 0, _dump_dinput_GUID(rguid), -1, 
911                         (WCHAR*)info->tszName, MAX_PATH);
912
913     return DI_OK;
914 }
915
916 static const IDirectInputEffectVtbl LinuxInputEffectVtbl = {
917     LinuxInputEffectImpl_QueryInterface,
918     LinuxInputEffectImpl_AddRef,
919     LinuxInputEffectImpl_Release,
920     LinuxInputEffectImpl_Initialize,
921     LinuxInputEffectImpl_GetEffectGuid,
922     LinuxInputEffectImpl_GetParameters,
923     LinuxInputEffectImpl_SetParameters,
924     LinuxInputEffectImpl_Start,
925     LinuxInputEffectImpl_Stop,
926     LinuxInputEffectImpl_GetEffectStatus,
927     LinuxInputEffectImpl_Download,
928     LinuxInputEffectImpl_Unload,
929     LinuxInputEffectImpl_Escape
930 };
931
932 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */