#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <evdev.h>
#include <evdev-properties.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <exevents.h>
#include <xf86.h>
#include <xf86Xinput.h>
#include <X11/Xatom.h>
#define FNMODE_PATH "/sys/module/hid_apple/parameters/fnmode"
#define USB_VENDOR_ID_APPLE 0x05ac
#define USB_DEVICE_ID_APPLE_ALU_MINI_ANSI 0x021d
#define USB_DEVICE_ID_APPLE_ALU_MINI_ISO 0x021e
#define USB_DEVICE_ID_APPLE_ALU_MINI_JIS 0x021f
#define USB_DEVICE_ID_APPLE_ALU_ANSI 0x0220
#define USB_DEVICE_ID_APPLE_ALU_ISO 0x0221
#define USB_DEVICE_ID_APPLE_ALU_JIS 0x0222
#define USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI 0x022c
#define USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO 0x022d
#define USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS 0x022e
#define USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI 0x0239
#define USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO 0x023a
#define USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS 0x023b
static int EvdevAppleGetProperty (DeviceIntPtr dev, Atom property);
static int EvdevAppleSetProperty(DeviceIntPtr dev, Atom atom,
XIPropertyValuePtr val, BOOL checkonly);
static Atom prop_fkeymode;
static Bool fnmode_readonly;
struct product_table
{
unsigned int vendor;
unsigned int product;
} apple_keyboard_table[] = {
{ USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ANSI},
{ USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_ISO},
{ USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_MINI_JIS},
{ USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ANSI},
{ USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_ISO},
{ USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_JIS},
{ USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI},
{ USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO},
{ USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS},
{ USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI},
{ USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO},
{ USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS},
{ 0, 0}
};
static Bool product_check(const struct product_table *t, int vendor, int product)
{
while (t->vendor)
{
if (vendor == t->vendor && product == t->product)
return TRUE;
t++;
}
return FALSE;
}
static int
set_fnmode(enum fkeymode fkeymode)
{
int fd;
char mode;
int bytes_written;
if (fkeymode == FKEYMODE_UNKNOWN)
{
errno = EINVAL;
return -1;
}
fd = open(FNMODE_PATH, O_WRONLY);
if (fd < 0)
return -1;
mode = (fkeymode == FKEYMODE_FKEYS) ? '2' : '1';
bytes_written = write(fd, &mode, 1);
close(fd);
return (bytes_written == 1) ? 0 : -1;
}
static enum fkeymode
get_fnmode(void)
{
int fd;
char retvalue;
fd = open(FNMODE_PATH, O_RDWR);
if (fd < 0 && errno == EACCES)
{
fnmode_readonly = TRUE;
fd = open(FNMODE_PATH, O_RDONLY);
}
if (fd < 0)
goto err;
if (read(fd, &retvalue, 1) != 1)
goto err;
if (retvalue != '0' && retvalue != '1' && retvalue != '2')
{
xf86Msg(X_ERROR, "Invalid fnmode value: %c\n", retvalue);
errno = EINVAL;
goto err;
}
close(fd);
if (retvalue == '0')
{
if (fnmode_readonly)
xf86Msg(X_WARNING, "fnmode is disabled and read-only. Fn key will"
"not toggle to multimedia keys.\n");
else
set_fnmode(FKEYMODE_FKEYS);
}
return retvalue == '1' ? FKEYMODE_MMKEYS : FKEYMODE_FKEYS;
err:
if (fd >= 0)
close(fd);
return FKEYMODE_UNKNOWN;
}
static void set_fkeymode_property(InputInfoPtr pInfo, enum fkeymode fkeymode)
{
DeviceIntPtr dev = pInfo->dev;
BOOL init = FALSE;
char data;
switch(fkeymode)
{
case FKEYMODE_FKEYS: data = 0; break;
case FKEYMODE_MMKEYS: data = 1; break;
case FKEYMODE_UNKNOWN:
xf86IDrvMsg(pInfo, X_ERROR, "Failed to get fnmode (%s)\n", strerror(errno));
return;
}
if (!prop_fkeymode) {
init = TRUE;
prop_fkeymode = MakeAtom(EVDEV_PROP_FUNCTION_KEYS, strlen(EVDEV_PROP_FUNCTION_KEYS), TRUE);
}
XIChangeDeviceProperty(dev, prop_fkeymode, XA_INTEGER, 8,
PropModeReplace, 1, &data, !init);
if (init)
{
XISetDevicePropertyDeletable(dev, prop_fkeymode, FALSE);
XIRegisterPropertyHandler(dev, EvdevAppleSetProperty, EvdevAppleGetProperty, NULL);
}
}
static int
EvdevAppleGetProperty (DeviceIntPtr dev, Atom property)
{
if (property == prop_fkeymode)
{
InputInfoPtr pInfo = dev->public.devicePrivate;
EvdevPtr pEvdev = pInfo->private;
enum fkeymode fkeymode;
fkeymode = get_fnmode();
if (fkeymode != pEvdev->fkeymode) {
pEvdev->fkeymode = fkeymode;
set_fkeymode_property(pInfo, fkeymode);
}
}
return Success;
}
static int
EvdevAppleSetProperty(DeviceIntPtr dev, Atom atom,
XIPropertyValuePtr val, BOOL checkonly)
{
InputInfoPtr pInfo = dev->public.devicePrivate;
EvdevPtr pEvdev = pInfo->private;
if (atom == prop_fkeymode)
{
CARD8 v = *(CARD8*)val->data;
if (val->format != 8 || val->type != XA_INTEGER)
return BadMatch;
if (fnmode_readonly)
return BadAccess;
if (v > 1)
return BadValue;
if (!checkonly)
{
if ((!v && pEvdev->fkeymode != FKEYMODE_FKEYS) ||
(v && pEvdev->fkeymode != FKEYMODE_MMKEYS))
{
pEvdev->fkeymode = v ? FKEYMODE_MMKEYS : FKEYMODE_FKEYS;
set_fnmode(pEvdev->fkeymode);
}
}
}
return Success;
}
void
EvdevAppleInitProperty(DeviceIntPtr dev)
{
InputInfoPtr pInfo = dev->public.devicePrivate;
EvdevPtr pEvdev = pInfo->private;
enum fkeymode fkeymode;
if (!product_check(apple_keyboard_table,
libevdev_get_id_vendor(pEvdev->dev),
libevdev_get_id_product(pEvdev->dev)))
return;
fkeymode = get_fnmode();
pEvdev->fkeymode = fkeymode;
set_fkeymode_property(pInfo, fkeymode);
}