aboutsummaryrefslogtreecommitdiffstats
path: root/common/joystick.c
blob: 3498bf0319e89817c00c18a93cfcf98084239dd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "config.h"

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>

#ifdef HAVE_LINUX_JOYSTICK_H
# include <linux/joystick.h>
#endif

#include "grab-ng.h"
#include "commands.h"
#include "joystick.h"
#include "event.h"

/*-----------------------------------------------------------------------*/

extern int debug;

#ifdef HAVE_LINUX_JOYSTICK_H
struct JOYTAB {
    int   class;
    int   number;
    int   value;
    char  *event;
};

static struct JOYTAB joytab[] = {
    { JS_EVENT_BUTTON, 0, 1,    "joy-button-0"   },
    { JS_EVENT_BUTTON, 1, 1,    "joy-button-1"   },
    { JS_EVENT_AXIS, 1, -32767, "joy-axis-up"    },
    { JS_EVENT_AXIS, 1,  32767, "joy-axis-down"  },
    { JS_EVENT_AXIS, 0,  32767, "joy-axis-left"  },
    { JS_EVENT_AXIS, 0, -32767, "joy-axis-right" },
};
#define NJOYTAB (sizeof(joytab)/sizeof(struct JOYTAB))

static struct event_entry joy_events[] = {
    {
	event:  "joy-button-0",
	action: "quit",
    },{
	event:  "joy-button-1",
	action: "fullscreen",
    },{
	event:  "joy-axis-up",
	action: "volume inc",
    },{
	event:  "joy-axis-down",
	action: "volume dec",
    },{
	event:  "joy-axis-left",
	action: "setchannel prev",
    },{
	event:  "joy-axis-right",
	action: "setchannel next",
    },{
	/* end of list */
    }
};

#endif

int joystick_tv_init(char *dev)
{
#ifdef HAVE_LINUX_JOYSTICK_H
    int fd;

    if (NULL == dev)
	return -1;
    if (-1 == (fd = open(dev, O_NONBLOCK))) {
	fprintf(stderr, "joystick: open %s: %s\n",dev,strerror(errno));
	return -1;
    }
    fcntl(fd,F_SETFD,FD_CLOEXEC);
    event_register_list(joy_events);
    return fd;
#else
    if (debug)
	fprintf(stderr,"joystick: not enabled at compile time\n");
    return -1;
#endif
}

void joystick_tv_havedata(int js)
{
#ifdef HAVE_LINUX_JOYSTICK_H
    int i;
    struct js_event event;
    if (debug)
	fprintf(stderr, "joystick: received input\n");
    if (read(js, &event, sizeof(struct js_event))) {
	for (i = 0; i < NJOYTAB; i++)
	    if (joytab[i].class == (event.type)
		&& joytab[i].number == event.number
		&& joytab[i].value == event.value)
		break;
	if (i != NJOYTAB)
	    event_dispatch(joytab[i].event);
    }
#endif
}

Privacy Policy