📄 effect_linuxinput.c
字号:
LPDIRECTINPUTEFFECT iface,
LPDIEFFECT peff,
DWORD dwFlags)
{
HRESULT diErr = DI_OK;
LinuxInputEffectImpl *This = (LinuxInputEffectImpl *)iface;
TRACE("(this=%p,%p,%ld)\n", This, peff, dwFlags);
/* Major conversion factors are:
* times: millisecond (linux) -> microsecond (windows) (x * 1000)
* forces: scale 0x7FFF (linux) -> scale 10000 (windows) approx ((x / 33) * 10)
* angles: scale 0x7FFF (linux) -> scale 35999 (windows) approx ((x / 33) * 36)
* angle bases: 0 -> -y (down) (linux) -> 0 -> +x (right) (windows)
*/
if (dwFlags & DIEP_AXES) {
if (peff->cAxes < 2 /* linuxinput effects always use 2 axes, x and y */)
diErr = DIERR_MOREDATA;
peff->cAxes = 2;
if (diErr)
return diErr;
else {
peff->rgdwAxes[0] = DIJOFS_X;
peff->rgdwAxes[1] = DIJOFS_Y;
}
}
if (dwFlags & DIEP_DIRECTION) {
if (peff->cAxes < 2)
diErr = DIERR_MOREDATA;
peff->cAxes = 2;
if (diErr)
return diErr;
else {
if (peff->dwFlags & DIEFF_CARTESIAN) {
peff->rglDirection[0] = (long)(sin(M_PI * 3 * This->effect.direction / 0x7FFF) * 1000);
peff->rglDirection[1] = (long)(cos(M_PI * 3 * This->effect.direction / 0x7FFF) * 1000);
} else {
/* Polar and spherical coordinates are the same for two or less
* axes.
* Note that we also use this case if NO flags are marked.
* According to MSDN, we should return the direction in the
* format that it was specified in, if no flags are marked.
*/
peff->rglDirection[0] = (This->effect.direction / 33) * 36 + 9000;
if (peff->rglDirection[0] > 35999)
peff->rglDirection[0] -= 35999;
}
}
}
if (dwFlags & DIEP_DURATION) {
peff->dwDuration = (DWORD)This->effect.replay.length * 1000;
}
if (dwFlags & DIEP_ENVELOPE) {
struct ff_envelope* env;
if (This->effect.type == FF_CONSTANT) env = &This->effect.u.constant.envelope;
else if (This->effect.type == FF_PERIODIC) env = &This->effect.u.periodic.envelope;
else if (This->effect.type == FF_RAMP) env = &This->effect.u.ramp.envelope;
else env = NULL;
if (env == NULL) {
peff->lpEnvelope = NULL;
} else if (peff->lpEnvelope == NULL) {
return DIERR_INVALIDPARAM;
} else {
peff->lpEnvelope->dwAttackLevel = (env->attack_level / 33) * 10;
peff->lpEnvelope->dwAttackTime = env->attack_length * 1000;
peff->lpEnvelope->dwFadeLevel = (env->fade_level / 33) * 10;
peff->lpEnvelope->dwFadeTime = env->fade_length * 1000;
}
}
if (dwFlags & DIEP_GAIN) {
/* the linux input ff driver apparently has no support
* for setting the device's gain. */
peff->dwGain = DI_FFNOMINALMAX;
}
if (dwFlags & DIEP_SAMPLEPERIOD) {
/* the linux input ff driver has no support for setting
* the playback sample period. 0 means default. */
peff->dwSamplePeriod = 0;
}
if (dwFlags & DIEP_STARTDELAY) {
peff->dwStartDelay = This->effect.replay.delay * 1000;
}
if (dwFlags & DIEP_TRIGGERBUTTON) {
FIXME("LinuxInput button mapping needs redoing; for now, assuming we're using an actual joystick.\n");
peff->dwTriggerButton = DIJOFS_BUTTON(This->effect.trigger.button - BTN_JOYSTICK);
}
if (dwFlags & DIEP_TRIGGERREPEATINTERVAL) {
peff->dwTriggerRepeatInterval = This->effect.trigger.interval * 1000;
}
if (dwFlags & DIEP_TYPESPECIFICPARAMS) {
int expectedsize = 0;
if (This->effect.type == FF_PERIODIC) {
expectedsize = sizeof(DIPERIODIC);
} else if (This->effect.type == FF_CONSTANT) {
expectedsize = sizeof(DICONSTANTFORCE);
} else if (This->effect.type == FF_SPRING
|| This->effect.type == FF_FRICTION
|| This->effect.type == FF_INERTIA
|| This->effect.type == FF_DAMPER) {
expectedsize = sizeof(DICONDITION) * 2;
} else if (This->effect.type == FF_RAMP) {
expectedsize = sizeof(DIRAMPFORCE);
}
if (expectedsize > peff->cbTypeSpecificParams)
diErr = DIERR_MOREDATA;
peff->cbTypeSpecificParams = expectedsize;
if (diErr)
return diErr;
else {
if (This->effect.type == FF_PERIODIC) {
LPDIPERIODIC tsp = (LPDIPERIODIC)(peff->lpvTypeSpecificParams);
tsp->dwMagnitude = (This->effect.u.periodic.magnitude / 33) * 10;
tsp->lOffset = (This->effect.u.periodic.offset / 33) * 10;
tsp->dwPhase = (This->effect.u.periodic.phase / 33) * 36;
tsp->dwPeriod = (This->effect.u.periodic.period * 1000);
} else if (This->effect.type == FF_CONSTANT) {
LPDICONSTANTFORCE tsp = (LPDICONSTANTFORCE)(peff->lpvTypeSpecificParams);
tsp->lMagnitude = (This->effect.u.constant.level / 33) * 10;
} else if (This->effect.type == FF_SPRING
|| This->effect.type == FF_FRICTION
|| This->effect.type == FF_INERTIA
|| This->effect.type == FF_DAMPER) {
LPDICONDITION tsp = (LPDICONDITION)(peff->lpvTypeSpecificParams);
int i;
for (i = 0; i < 2; ++i) {
tsp[i].lOffset = (This->effect.u.condition[i].center / 33) * 10;
tsp[i].lPositiveCoefficient = (This->effect.u.condition[i].right_coeff / 33) * 10;
tsp[i].lNegativeCoefficient = (This->effect.u.condition[i].left_coeff / 33) * 10;
tsp[i].dwPositiveSaturation = (This->effect.u.condition[i].right_saturation / 33) * 10;
tsp[i].dwNegativeSaturation = (This->effect.u.condition[i].left_saturation / 33) * 10;
tsp[i].lDeadBand = (This->effect.u.condition[i].deadband / 33) * 10;
}
} else if (This->effect.type == FF_RAMP) {
LPDIRAMPFORCE tsp = (LPDIRAMPFORCE)(peff->lpvTypeSpecificParams);
tsp->lStart = (This->effect.u.ramp.start_level / 33) * 10;
tsp->lEnd = (This->effect.u.ramp.end_level / 33) * 10;
}
}
}
return diErr;
}
static HRESULT WINAPI LinuxInputEffectImpl_Initialize(
LPDIRECTINPUTEFFECT iface,
HINSTANCE hinst,
DWORD dwVersion,
REFGUID rguid)
{
FIXME("(this=%p,%p,%ld,%s): stub!\n",
iface, hinst, dwVersion, debugstr_guid(rguid));
return DI_OK;
}
static HRESULT WINAPI LinuxInputEffectImpl_QueryInterface(
LPDIRECTINPUTEFFECT iface,
REFIID riid,
void **ppvObject)
{
LinuxInputEffectImpl* This = (LinuxInputEffectImpl*)iface;
TRACE("(this=%p,%s,%p)\n", This, debugstr_guid(riid), ppvObject);
if (IsEqualGUID(&IID_IUnknown, riid) ||
IsEqualGUID(&IID_IDirectInputEffect, riid)) {
LinuxInputEffectImpl_AddRef(iface);
*ppvObject = This;
return 0;
}
TRACE("Unsupported interface!\n");
return E_FAIL;
}
static HRESULT WINAPI LinuxInputEffectImpl_Start(
LPDIRECTINPUTEFFECT iface,
DWORD dwIterations,
DWORD dwFlags)
{
struct input_event event;
LinuxInputEffectImpl* This = (LinuxInputEffectImpl*)iface;
TRACE("(this=%p,%ld,%ld)\n", This, dwIterations, dwFlags);
if (!(dwFlags & DIES_NODOWNLOAD)) {
/* Download the effect if necessary */
if (This->effect.id == -1) {
HRESULT res = LinuxInputEffectImpl_Download(iface);
if (res != DI_OK)
return res;
}
}
if (dwFlags & DIES_SOLO) {
FIXME("Solo mode requested: should be stopping all effects here!\n");
}
event.type = EV_FF;
event.code = This->effect.id;
event.value = dwIterations;
if (write(*(This->fd), &event, sizeof(event)) == -1) {
FIXME("Unable to write event. Assuming device disconnected.\n");
return DIERR_INPUTLOST;
}
return DI_OK;
}
static HRESULT WINAPI LinuxInputEffectImpl_SetParameters(
LPDIRECTINPUTEFFECT iface,
LPCDIEFFECT peff,
DWORD dwFlags)
{
LinuxInputEffectImpl* This = (LinuxInputEffectImpl*)iface;
DWORD type = _typeFromGUID(&This->guid);
HRESULT retval = DI_OK;
TRACE("(this=%p,%p,%ld)\n", This, peff, dwFlags);
_dump_DIEFFECT(peff, &This->guid);
if ((dwFlags & !DIEP_NORESTART & !DIEP_NODOWNLOAD & !DIEP_START) == 0) {
/* set everything */
dwFlags = DIEP_AXES | DIEP_DIRECTION | DIEP_DURATION | DIEP_ENVELOPE |
DIEP_GAIN | DIEP_SAMPLEPERIOD | DIEP_STARTDELAY | DIEP_TRIGGERBUTTON |
DIEP_TRIGGERREPEATINTERVAL | DIEP_TYPESPECIFICPARAMS;
}
if (dwFlags & DIEP_AXES) {
/* the linux input effect system only supports one or two axes */
if (peff->cAxes > 2)
return DIERR_INVALIDPARAM;
else if (peff->cAxes < 1)
return DIERR_INCOMPLETEEFFECT;
}
/* some of this may look funky, but it's 'cause the linux driver and directx have
* different opinions about which way direction "0" is. directx has 0 along the x
* axis (left), linux has it along the y axis (down). */
if (dwFlags & DIEP_DIRECTION) {
if (peff->cAxes == 1) {
if (peff->dwFlags & DIEFF_CARTESIAN) {
if (dwFlags & DIEP_AXES) {
if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] >= 0)
This->effect.direction = 0x4000;
else if (peff->rgdwAxes[0] == DIJOFS_X && peff->rglDirection[0] < 0)
This->effect.direction = 0xC000;
else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] >= 0)
This->effect.direction = 0;
else if (peff->rgdwAxes[0] == DIJOFS_Y && peff->rglDirection[0] < 0)
This->effect.direction = 0x8000;
}
} else {
/* one-axis effects must use cartesian coords */
return DIERR_INVALIDPARAM;
}
} else { /* two axes */
if (peff->dwFlags & DIEFF_CARTESIAN) {
/* avoid divide-by-zero */
if (peff->rglDirection[1] == 0) {
if (peff->rglDirection[0] >= 0)
This->effect.direction = 0x4000;
else if (peff->rglDirection[0] < 0)
This->effect.direction = 0xC000;
} else {
This->effect.direction = (int)(atan(peff->rglDirection[0] / peff->rglDirection[1]) * 0x7FFF / (3 * M_PI));
}
} else {
/* Polar and spherical are the same for 2 axes */
/* Precision is important here, so we do double math with exact constants */
This->effect.direction = (int)(((double)peff->rglDirection[0] - 90) / 35999) * 0x7FFF;
}
}
}
if (dwFlags & DIEP_DURATION)
This->effect.replay.length = peff->dwDuration / 1000;
if (dwFlags & DIEP_ENVELOPE) {
struct ff_envelope* env;
if (This->effect.type == FF_CONSTANT) env = &This->effect.u.constant.envelope;
else if (This->effect.type == FF_PERIODIC) env = &This->effect.u.periodic.envelope;
else if (This->effect.type == FF_RAMP) env = &This->effect.u.ramp.envelope;
else env = NULL;
if (peff->lpEnvelope == NULL) {
/* if this type had an envelope, reset it
* note that length can never be zero, so we set it to something miniscule */
if (env) {
env->attack_length = 0x10;
env->attack_level = 0x7FFF;
env->fade_length = 0x10;
env->fade_level = 0x7FFF;
}
} else {
/* did we get passed an envelope for a type that doesn't even have one? */
if (!env) return DIERR_INVALIDPARAM;
/* copy the envelope */
env->attack_length = peff->lpEnvelope->dwAttackTime / 1000;
env->attack_level = (peff->lpEnvelope->dwAttackLevel / 10) * 32;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -