OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [MW_0_8_9PRE7/] [mw/] [src/] [drivers/] [mou_x11.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 673 markom
/*
2
 * Author: Tony Rogvall <tony@bluetail.com>
3
 *
4
 */
5
#include <stdio.h>
6
#include <errno.h>
7
#include <unistd.h>
8
#include <X11/Xlib.h>
9
#include "device.h"
10
 
11
#define SCALE           3       /* default scaling factor for acceleration */
12
#define THRESH          5       /* default threshhold for acceleration */
13
 
14
static int      X11_Open(MOUSEDEVICE *pmd);
15
static void     X11_Close(void);
16
static int      X11_GetButtonInfo(void);
17
static void     X11_GetDefaultAccel(int *pscale,int *pthresh);
18
static int      X11_Read(MWCOORD *dx, MWCOORD *dy, MWCOORD *dz, int *bp);
19
 
20
extern Display*     x11_dpy;
21
extern int          x11_scr;
22
extern Visual*      x11_vis;
23
extern Window       x11_win;
24
extern GC           x11_gc;
25
extern unsigned int x11_event_mask;
26
extern int          x11_setup_display();
27
extern void         x11_handle_event(XEvent*);
28
 
29
MOUSEDEVICE mousedev = {
30
    X11_Open,
31
    X11_Close,
32
    X11_GetButtonInfo,
33
    X11_GetDefaultAccel,
34
    X11_Read,
35
    NULL
36
};
37
 
38
/*
39
 * Open up the mouse device.
40
 * Returns the fd if successful, or negative if unsuccessful.
41
 */
42
static int X11_Open(MOUSEDEVICE *pmd)
43
{
44
    if (x11_setup_display() < 0)
45
        return -1;
46
    /* return the x11 file descriptor for select */
47
    return ConnectionNumber(x11_dpy);
48
}
49
 
50
/*
51
 * Close the mouse device.
52
 */
53
static void
54
X11_Close(void)
55
{
56
    /* nop */
57
}
58
 
59
/*
60
 * Get mouse buttons supported
61
 */
62
static int
63
X11_GetButtonInfo(void)
64
{
65
        return MWBUTTON_L | MWBUTTON_M | MWBUTTON_R;
66
}
67
 
68
/*
69
 * Get default mouse acceleration settings
70
 */
71
static void
72
X11_GetDefaultAccel(int *pscale,int *pthresh)
73
{
74
    *pscale = SCALE;
75
    *pthresh = THRESH;
76
}
77
 
78
/*
79
 * Attempt to read bytes from the mouse and interpret them.
80
 * Returns -1 on error, 0 if either no bytes were read or not enough
81
 * was read for a complete state, or 1 if the new state was read.
82
 * When a new state is read, the current buttons and x and y deltas
83
 * are returned.  This routine does not block.
84
 */
85
static int
86
X11_Read(MWCOORD *dx, MWCOORD *dy, MWCOORD *dz, int *bp)
87
{
88
    static int noevent_count = 0;
89
    XEvent ev;
90
    int events = 0;
91
    long mask = /* x11_event_mask | */
92
#ifdef USE_EXPOSURE
93
      ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask;
94
#else
95
      ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
96
#endif
97
 
98
    while (XCheckMaskEvent(x11_dpy, mask, &ev)) {
99
        if (ev.type == MotionNotify) {
100
            if (ev.xmotion.window == x11_win) {
101
                int button = 0;
102
                *dx = ev.xmotion.x;
103
                *dy = ev.xmotion.y;
104
                *dz = 0;
105
                if (ev.xmotion.state & Button1Mask)
106
                    button |= MWBUTTON_L;
107
                if (ev.xmotion.state & Button2Mask)
108
                    button |= MWBUTTON_M;
109
                if (ev.xmotion.state & Button3Mask)
110
                    button |= MWBUTTON_R;
111
                *bp = button;
112
                events++;
113
            }
114
        }
115
        else if (ev.type == ButtonPress) {
116
            if (ev.xbutton.window == x11_win) {
117
                int button = 0;
118
 
119
                /* Get pressed button */
120
                if(ev.xbutton.button == 1)
121
                        button = MWBUTTON_L;
122
                else if(ev.xbutton.button == 2)
123
                        button = MWBUTTON_M;
124
                else if(ev.xbutton.button == 3)
125
                        button = MWBUTTON_R;
126
 
127
                /* Get any other buttons that might be already held */
128
                if (ev.xbutton.state & Button1Mask)
129
                    button |= MWBUTTON_L;
130
                if (ev.xbutton.state & Button2Mask)
131
                    button |= MWBUTTON_M;
132
                if (ev.xbutton.state & Button3Mask)
133
                    button |= MWBUTTON_R;
134
 
135
/*              printf("!Pressing button: 0x%x, state: 0x%x, button: 0x%x\n",
136
                        button,ev.xbutton.state, ev.xbutton.button);*/
137
                *bp = button;
138
                *dx = ev.xbutton.x;
139
                *dy = ev.xbutton.y;
140
                *dz = 0;
141
                events++;
142
            }
143
        }
144
        else if (ev.type == ButtonRelease) {
145
            if (ev.xbutton.window == x11_win) {
146
                int button = 0;
147
                int released = 0;
148
 
149
                /* Get released button */
150
                if(ev.xbutton.button == 1)
151
                        released = MWBUTTON_L;
152
                else if(ev.xbutton.button == 2)
153
                        released = MWBUTTON_M;
154
                else if(ev.xbutton.button == 3)
155
                        released = MWBUTTON_R;
156
 
157
                /* Get any other buttons that might be already held */
158
                if (ev.xbutton.state & Button1Mask)
159
                    button |= MWBUTTON_L;
160
                if (ev.xbutton.state & Button2Mask)
161
                    button |= MWBUTTON_M;
162
                if (ev.xbutton.state & Button3Mask)
163
                    button |= MWBUTTON_R;
164
 
165
                /* We need to remove the released button from the button mask*/
166
                button &= ~released;
167
 
168
                /*printf("!Releasing button: 0x%x, state: 0x%x, button: 0x%x\n",
169
                        button,ev.xbutton.state, ev.xbutton.button);*/
170
 
171
                *bp = button;
172
                *dx = ev.xbutton.x;
173
                *dy = ev.xbutton.y;
174
                *dz = 0;
175
                events++;
176
            }
177
        }
178
        else {
179
            x11_handle_event(&ev);
180
        }
181
    }
182
    if (events == 0) {
183
        /* after a bunch of consecutive noevent calls here
184
           (meaning select() says there's something to read but nothing
185
            is returned......), force an event read (which will
186
            most likely terminate the connection) */
187
        if (++noevent_count >= 50) {
188
            while(XNextEvent(x11_dpy, &ev)) {
189
                /* if we return, then we got an event...put it back
190
                   so we can properly process it next time through */
191
                XPutBackEvent(x11_dpy, &ev);
192
            }
193
            noevent_count = 0;
194
        }
195
        return 0;
196
    }
197
    noevent_count = 0;
198
    return 2;           /* absolute position returned*/
199
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.