summaryrefslogtreecommitdiff
path: root/src/in_evdev.c
blob: 27715a9c867ff6e734730255ebb18c428ce2b2c5 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include "common.h"
#include "key_public.h"
#include "in_public.h"
#include "cl_public.h"
#include "io_sleep.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>

#include <SDL.h>

static cvar_t   *in_device;

static struct {
    qboolean    initialized;
    grab_t      grabbed;
    int         fd;
    int         dx, dy;
    ioentry_t   *io;
} evdev;

#define MAX_EVENTS    64
#define EVENT_SIZE    sizeof( struct input_event )

static void ShutdownMouse( void );

static void GetMouseEvents( void ) {
    struct input_event ev[MAX_EVENTS];
    ssize_t bytes;
    size_t i, count;
    unsigned button, time;

    if( !evdev.initialized || !evdev.grabbed /*|| !evdev.io->canread*/ ) {
        return;
    }

    bytes = read( evdev.fd, ev, EVENT_SIZE * MAX_EVENTS );
    if( bytes == -1 ) {
        if( errno == EAGAIN || errno == EINTR ) {
            return;
        }
        Com_EPrintf( "Couldn't read event: %s\n", strerror( errno ) );
        ShutdownMouse();
        return;
    }

    if( bytes < EVENT_SIZE ) {
        return; // should not happen
    }

    count = bytes / EVENT_SIZE;
    for( i = 0 ; i < count; i++ ) {
        time = ev[i].time.tv_sec * 1000 + ev[i].time.tv_usec / 1000;
        switch( ev[i].type ) {
        case EV_KEY:
            if( ev[i].code >= BTN_MOUSE && ev[i].code < BTN_MOUSE + 8 ) {
                button = K_MOUSE1 + ev[i].code - BTN_MOUSE;
                Key_Event( button, !!ev[i].value, time );
            }
            break;
        case EV_REL: 
            switch( ev[i].code ) { 
            case REL_X: 
                evdev.dx += ( int )ev[i].value; 
                break; 
            case REL_Y: 
                evdev.dy += ( int )ev[i].value; 
                break; 
            case REL_WHEEL: 
                if( ( int )ev[i].value == 1 ) {
                    Key_Event( K_MWHEELUP, qtrue, time );
                    Key_Event( K_MWHEELUP, qfalse, time );
                } else if( ( int )ev[i].value == -1 ) {
                    Key_Event( K_MWHEELDOWN, qtrue, time );
                    Key_Event( K_MWHEELDOWN, qfalse, time );
                }
                break;
            case REL_HWHEEL:
                if( ( int )ev[i].value == 1 ) {
                    Key_Event( K_MWHEELRIGHT, qtrue, time );
                    Key_Event( K_MWHEELRIGHT, qfalse, time );
                } else if( ( int )ev[i].value == -1 ) {
                    Key_Event( K_MWHEELLEFT, qtrue, time );
                    Key_Event( K_MWHEELLEFT, qfalse, time );
                }
                break;
            default:
                break;
            }
            break;
        default:
            break;
        }
    }
}

static qboolean GetMouseMotion( int *dx, int *dy ) {
    if( !evdev.initialized || !evdev.grabbed ) {
        return qfalse;
    }
    *dx = evdev.dx;
    *dy = evdev.dy;
    evdev.dx = 0;
    evdev.dy = 0;
    return qtrue;
}

static void ShutdownMouse( void ) {
    if( !evdev.initialized ) {
        return;
    }
    IO_Remove( evdev.fd );
    close( evdev.fd );
    SDL_ShowCursor( SDL_ENABLE );
    SDL_WM_GrabInput( SDL_GRAB_OFF );
    SDL_WM_SetCaption( PRODUCT, APPLICATION );
    memset( &evdev, 0, sizeof( evdev ) );
}

static qboolean InitMouse( void ) {
    in_device = Cvar_Get( "in_device", "", 0 );
    if( !in_device->string[0] ) {
        Com_WPrintf( "No evdev input device specified.\n" );
        return qfalse;
    }
 
    evdev.fd = open( in_device->string, O_RDONLY );
    if( evdev.fd == -1 ) {
        Com_EPrintf( "Couldn't open %s: %s\n", in_device->string,
            strerror( errno ) );
        return qfalse;
    }
 
    fcntl( evdev.fd, F_SETFL, fcntl( evdev.fd, F_GETFL, 0 ) | FNDELAY );
    evdev.io = IO_Add( evdev.fd );

    Com_Printf( "Evdev interface initialized.\n" );
    evdev.initialized = qtrue;

    return qtrue;
}

static void GrabMouse( grab_t grab ) {
    if( !evdev.initialized ) {
        return;
    }

    if( evdev.grabbed == grab ) {
        evdev.dx = 0;
        evdev.dy = 0;
        return;
    }

    if( grab == IN_GRAB ) {
        SDL_WM_GrabInput( SDL_GRAB_ON );
        SDL_WM_SetCaption( "[" PRODUCT "]", APPLICATION );
        SDL_ShowCursor( SDL_DISABLE );
        evdev.io->wantread = qtrue;
    } else {
        if( evdev.grabbed == IN_GRAB ) {
            SDL_WM_GrabInput( SDL_GRAB_OFF );
            SDL_WM_SetCaption( PRODUCT, APPLICATION );
        }
        if( grab == IN_HIDE ) {
            SDL_ShowCursor( SDL_DISABLE );
        } else {
            SDL_ShowCursor( SDL_ENABLE );
        }
        evdev.io->wantread = !!grab;
    }

    // pump pending events
    if( grab ) {
        struct input_event ev;

        while( read( evdev.fd, &ev, EVENT_SIZE ) == EVENT_SIZE )
            ;
    }

    evdev.dx = 0;
    evdev.dy = 0;
    evdev.grabbed = grab;
}

static void WarpMouse( int x, int y ) {
    SDL_WarpMouse( x, y );
}

/*
@@@@@@@@@@@@@@@@@@@
DI_FillAPI
@@@@@@@@@@@@@@@@@@@
*/
void DI_FillAPI( inputAPI_t *api ) {
    api->Init = InitMouse;
    api->Shutdown = ShutdownMouse;
    api->Grab = GrabMouse;
    api->Warp = WarpMouse;
    api->GetEvents = GetMouseEvents;
    api->GetMotion = GetMouseMotion;
}