~ubuntu-branches/ubuntu/trusty/powerpc-utils/trusty

« back to all changes in this revision

Viewing changes to mousemode.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Jacobowitz
  • Date: 2001-07-15 22:15:13 UTC
  • Revision ID: james.westby@ubuntu.com-20010715221513-ptyrz3o919viteco
Tags: 1.1.3-5
* Be more aggressive about removing the diversion.
* Be more aggressive about actually documenting the problem.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* mousemode.c
2
2
 *
3
3
 * A program for linux-pmac by jonh Tue Feb 18 00:46:10 EST 1997
 
4
 * hacked mercilessly by warner@lothar.com: don't blame jonh for my bugs!
4
5
 *
5
6
 * which feeds the right things to /dev/adb to reconfigure
6
7
 * Apple Desktop Bus mice. It sets a mouse's ADB register 3 to the
14
15
#include <unistd.h>
15
16
#include <errno.h>
16
17
#include <fcntl.h>
 
18
#include <linux/adb.h>
 
19
#include <linux/cuda.h>
17
20
 
18
21
int fd; /* put this here where everybody can see it. Hey, it's not so   */
19
22
        /* much a global as it is an "object variable," where this      */
20
23
        /* program is the object. Yeah, that's it! Ahem.                */
21
24
 
22
 
static void setmouse( int );
23
 
static int showmouse( void );
24
 
static void send( char *, int );
25
 
static void listen( char * );
26
 
 
27
 
int main(int argc, char **argv)
28
 
{
29
 
        int mode;
30
 
 
31
 
        fd = open("/dev/adb", O_RDWR);
32
 
        if (fd <= 0) {
33
 
                perror("opening /dev/adb");
34
 
                exit(EXIT_FAILURE);
35
 
        }
36
 
 
37
 
        if (argc >= 2) {
38
 
           mode = atoi(argv[1]);
39
 
           if ((argc == 2 && (mode <= 0 || mode >= 0x0fd)) || argc >= 3) {
40
 
              (void) printf("usage: mousemode [n]\n");
41
 
              (void) printf("  Configures mouse at ADB address 3 to use handler ID n.\n");
42
 
              (void) printf("  If n is omitted, prints value of current handler ID.\n");
43
 
              exit(EXIT_FAILURE);
44
 
           }
45
 
 
46
 
           if (argc == 2) {
47
 
              setmouse(mode);
48
 
           }
49
 
        }
50
 
 
51
 
        (void) printf("%d\n", showmouse());
52
 
 
53
 
        (void) close(fd);
54
 
        exit(EXIT_SUCCESS);
55
 
}
56
 
 
57
 
static void setmouse(int mode)
58
 
{
59
 
        char y[15];
60
 
 
61
 
        /* curious parties should read Inside Mac/Devices/ADB Manager,  */
62
 
        /* looking at page 5-11. Inside Mac is available as pdf files   */
63
 
        /* from Apple's site.                                           */
64
 
 
65
 
        /* CUDA device 0? (the clock seems to be at 1) */
66
 
        y[0]=(char) 0x000;
67
 
        /* mouse (0x30) listen (0x08) reg 3 (0x03) */
68
 
        y[1]=(char) 0x03b;
69
 
        /* service request enable (0x20), device addr 3 (0x03) */
70
 
        y[2]=(char) 0x023;
71
 
        /* device handler ID == mode */
72
 
        y[3]=(char) mode;
73
 
 
74
 
        send(y, 4);
75
 
        listen(y);
76
 
}
77
 
 
78
 
static int showmouse()
79
 
{
80
 
        char y[15];
81
 
 
82
 
        y[0]=(char) 0x000;      /* Cuda device 0 */
83
 
        y[1]=(char) 0x03f;      /* mouse talk reg 3 */
84
 
 
85
 
        send(y, 2);
86
 
        listen(y);
87
 
 
88
 
                                        /* make sure reply is from: */
89
 
        if (y[0] == (char) 0            /* cuda device 0 */
90
 
                && y[1] == (char) 0     /* no status/error bits (I'm guessing) */
91
 
                && y[2] == (char) 0x03f) {      /* mouse talk reg 3 */
92
 
          /* skip 1st byte of reg 3, and return handler ID */
93
 
                return( (int) y[4] );
94
 
        } else {
95
 
                return -1;
96
 
        }
97
 
}
98
 
 
99
 
static void send(char *y, int len)
100
 
{
101
 
        int n;
102
 
 
103
 
        n = (int) write(fd, y, (size_t) len);
104
 
        if (n < len) {
105
 
                perror("writing /dev/adb");
106
 
                (void) close(fd);
107
 
                exit (EXIT_FAILURE);
108
 
        }
109
 
}
110
 
 
111
 
static void listen(char *y)
112
 
{
113
 
        int n;
114
 
 
115
 
        do {
116
 
                n = (int) read(fd, y, (size_t) 80);
117
 
                if (n > 0) {
118
 
                        y += (char) n;
119
 
                } else if (n<0) {
120
 
                        perror("reading /dev/adb");
121
 
                        (void) close(fd);
122
 
                        exit(EXIT_FAILURE);
123
 
                }
124
 
        } while (n > 0);
 
25
 
 
26
void
 
27
send(unsigned char *y, int len)
 
28
{
 
29
    int n;
 
30
 
 
31
#if 0
 
32
    printf("send: ");
 
33
    for (n=0; n < len; n++)
 
34
        printf("0x%02x ",y[n]);
 
35
    printf("\n");
 
36
#endif
 
37
 
 
38
    n = write(fd, y, (size_t) len);
 
39
    if (n < len) {
 
40
        perror("writing /dev/adb");
 
41
        close(fd);
 
42
        exit (EXIT_FAILURE);
 
43
    }
 
44
}
 
45
 
 
46
void
 
47
listen(unsigned char *y)
 
48
{
 
49
    int n;
 
50
    
 
51
    n = read(fd, y, 80);
 
52
#if 0
 
53
    printf("%d: ",n);
 
54
    if (n > 0) {
 
55
        int i;
 
56
        for (i=0; i < n; i++)
 
57
            printf("0x%02x ",y[i]);
 
58
    }
 
59
    printf("\n");
 
60
#endif
 
61
    if (n < 0) {
 
62
        perror("reading /dev/adb");
 
63
        close(fd);
 
64
        exit(EXIT_FAILURE);
 
65
    }
 
66
}
 
67
 
 
68
void
 
69
setmouse(int addr, int mode)
 
70
{
 
71
    unsigned char y[15];
 
72
 
 
73
    /* curious parties should read Inside Mac/Devices/ADB Manager,      */
 
74
    /* looking at page 5-11. Inside Mac is available as pdf files       */
 
75
    /* from Apple's site.                                               */
 
76
 
 
77
    /* CUDA device 0? (the clock seems to be at 1) */
 
78
    y[0] = ADB_PACKET;
 
79
    /* mouse (0x30) listen (0x08) reg 3 (0x03) */
 
80
    y[1] = ADB_WRITEREG(addr, 3);
 
81
    /* service request enable (0x20), device addr 3 (0x03) */
 
82
    //y[2]=(char) 0x023;
 
83
    y[2] = 0x20 + addr;
 
84
    /* device handler ID == mode */
 
85
    y[3]= mode;
 
86
 
 
87
    send(y, 4);
 
88
    listen(y);
 
89
}
 
90
 
 
91
int 
 
92
showmouse(int addr)
 
93
{
 
94
    unsigned char y[15];
 
95
    
 
96
    y[0] = ADB_PACKET;
 
97
    y[1] = ADB_READREG(addr, 3);
 
98
 
 
99
    send(y, 2);
 
100
    listen(y);
 
101
 
 
102
    /* make sure reply is from: */
 
103
    if (y[0] == ADB_READREG(addr, 3)) {
 
104
        return (y[2]);
 
105
    } else {
 
106
        return -1;
 
107
    }
 
108
}
 
109
 
 
110
int
 
111
main(int argc, char **argv)
 
112
{
 
113
    int addr, mode;
 
114
    
 
115
    // argc==2: 'mousemode <addr>'
 
116
    // argc==3: 'mousemode <addr> <handler>'
 
117
    if (argc < 2 || argc > 3) {
 
118
        printf("usage: mousemode <addr> [<handler>]\n");
 
119
        printf(" Configures mouse at ADB address <addr> to use <handler>\n");
 
120
        printf(" without <handler>, print value of current handler\n");
 
121
        exit(EXIT_FAILURE);
 
122
    }
 
123
    
 
124
    fd = open("/dev/adb", O_RDWR);
 
125
    if (fd <= 0) {
 
126
        perror("opening /dev/adb");
 
127
        exit(EXIT_FAILURE);
 
128
    }
 
129
    
 
130
    addr = atoi(argv[1]);
 
131
    if (argc == 3) {
 
132
        mode = atoi(argv[2]);
 
133
        printf("handler for addr %d was %d\n", addr, showmouse(addr));
 
134
        printf("trying to set handler to %d...\n", mode);
 
135
        setmouse(addr, mode);
 
136
        printf("handler is now %d\n", showmouse(addr));
 
137
    } else {
 
138
        printf("handler for addr %d is %d\n", addr, showmouse(addr));
 
139
    }
 
140
    
 
141
    close(fd);
 
142
    return 0;
125
143
}