osdep/mplayer.rc: change to use "mplayer2" name
[mplayer.git] / input / appleir.c
blobc64bc9648d4a9c9b260b59e6570a024c74e1817c
1 /*
2 * Linux Apple IR Remote input interface
4 * Copyright (C) 2008 Benjamin Zores <ben at geexbox dot org>
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "config.h"
25 #include "ar.h"
26 #include "input.h"
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <errno.h>
37 #include <linux/types.h>
38 #include <linux/input.h>
40 #include "mp_msg.h"
42 // keycodes.h defines would conflict with linux/input.h ones
43 #define AR_DEFINES_ONLY
44 #include "keycodes.h"
46 #define EVDEV_MAX_EVENTS 32
48 /* ripped from AppleIR driver */
49 #define USB_VENDOR_APPLE 0x05ac
50 #define USB_DEV_APPLE_IR 0x8240
51 #define USB_DEV_APPLE_IR_2 0x8242
53 /* Apple IR Remote evdev mapping */
54 #define APPLE_IR_MINUS KEY_VOLUMEDOWN
55 #define APPLE_IR_PLUS KEY_VOLUMEUP
56 #define APPLE_IR_MENU KEY_MENU
57 #define APPLE_IR_FORWARD KEY_NEXTSONG
58 #define APPLE_IR_PLAY KEY_PLAYPAUSE
59 #define APPLE_IR_BACKWARD KEY_PREVIOUSSONG
61 static const struct {
62 int linux_keycode;
63 int value;
64 int mp_keycode;
65 } apple_ir_mapping[] = {
66 { APPLE_IR_PLAY, 1, AR_PLAY },
67 { APPLE_IR_PLAY, 2, AR_PLAY_HOLD },
68 { APPLE_IR_FORWARD, 1, AR_NEXT },
69 { APPLE_IR_FORWARD, 2, AR_NEXT_HOLD },
70 { APPLE_IR_BACKWARD, 1, AR_PREV },
71 { APPLE_IR_BACKWARD, 2, AR_PREV_HOLD },
72 { APPLE_IR_MENU, 1, AR_MENU },
73 { APPLE_IR_MENU, 2, AR_MENU_HOLD },
74 { APPLE_IR_PLUS, 1, AR_VUP },
75 { APPLE_IR_MINUS, 1, AR_VDOWN },
76 { -1, -1, -1 }
79 int mp_input_appleir_init (char *dev)
81 int i, fd;
83 if (dev)
85 mp_tmsg (MSGT_INPUT, MSGL_V, "Initializing Apple IR on %s\n", dev);
86 fd = open (dev, O_RDONLY | O_NONBLOCK);
87 if (fd < 0)
89 mp_tmsg (MSGT_INPUT, MSGL_ERR,
90 "Can't open Apple IR device: %s\n", strerror (errno));
91 return -1;
94 return fd;
96 else
98 /* look for a valid AppleIR device on system */
99 for (i = 0; i < EVDEV_MAX_EVENTS; i++)
101 struct input_id id;
102 char file[64];
104 sprintf (file, "/dev/input/event%d", i);
105 fd = open (file, O_RDONLY | O_NONBLOCK);
106 if (fd < 0)
107 continue;
109 ioctl (fd, EVIOCGID, &id);
110 if (id.bustype == BUS_USB &&
111 id.vendor == USB_VENDOR_APPLE &&
112 (id.product == USB_DEV_APPLE_IR ||id.product == USB_DEV_APPLE_IR_2))
114 mp_tmsg (MSGT_INPUT, MSGL_V, "Detected Apple IR on %s\n", file);
115 return fd;
117 close (fd);
120 mp_tmsg (MSGT_INPUT, MSGL_ERR,
121 "Can't open Apple IR device: %s\n", strerror (errno));
124 return -1;
127 int mp_input_appleir_read(void *ctx, int fd)
129 struct input_event ev;
130 int i, r;
132 r = read (fd, &ev, sizeof (struct input_event));
133 if (r <= 0 || r < sizeof (struct input_event))
134 return MP_INPUT_NOTHING;
136 /* check for key press only */
137 if (ev.type != EV_KEY)
138 return MP_INPUT_NOTHING;
140 /* EvDev Key values:
141 * 0: key release
142 * 1: key press
143 * 2: key auto-repeat
145 if (ev.value == 0)
146 return MP_INPUT_NOTHING;
148 /* find Linux evdev -> MPlayer keycode mapping */
149 for (i = 0; apple_ir_mapping[i].linux_keycode != -1; i++)
150 if (apple_ir_mapping[i].linux_keycode == ev.code &&
151 apple_ir_mapping[i].value == ev.value)
152 return apple_ir_mapping[i].mp_keycode;
154 return MP_INPUT_NOTHING;