File: js.c

package info (click to toggle)
dmx4linux 2.5%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,660 kB
  • ctags: 2,835
  • sloc: ansic: 23,341; yacc: 918; asm: 846; makefile: 614; sh: 439
file content (109 lines) | stat: -rw-r--r-- 2,082 bytes parent folder | download
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
/*
 * Copyright (C) Dirk Jagdmann <doj@cubic.org>
 *
 * 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 <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <linux/joystick.h>

#include "pointer.h"
#include "pointer_internal.h"

static FILE *f=0;

static void cleanup()
{
  if(f)
    fclose(f);
}

static int jsgetfd()
{
  if(f==0)
    return -1;
  return fileno(f);
}

static int jsinit(char *device)
{
  /* open device */
  f=fopen(device, "rb");
  if(f==0)
    {
      printf("could not open %s: %s\n", device, strerror(errno));
      return -1;
    }

  atexit(cleanup);

  return 0;
}

static int jspoll()
{
  int X=pointerrawX(), Y=pointerrawY(), B=pointerB();
  struct js_event e;
  fread(&e, sizeof(struct js_event), 1, f);

  e.type &= ~JS_EVENT_INIT;
  switch(e.type)
    {
    case JS_EVENT_BUTTON:
      {
	int b;
	switch(e.number)
	  {
	  default:
	  case 0: b=BUT2; break;
	  case 1: b=BUT3; break;
	  case 2: b=BUT1; break;
	  case 3: b=BUT4; break;
	  case 4: b=BUT5; break;
	  }
	if(e.value)
	  B|=b;
	else
	  B&=~b;
      }
      break;

    case JS_EVENT_AXIS:
      if(e.number==0)
	X+=e.value;
      else if(e.number==1)
	Y+=e.value;
      break;

    default:
      break;
    }

  pointersetrawX(X);
  pointersetrawY(Y);
  pointersetB(B);

  return 0;
}

void jsregister(struct driver *drv)
{
  drv->init=jsinit;
  drv->poll=jspoll;
  drv->getfd=jsgetfd;
}