2 * Author: Tony Rogvall <tony@bluetail.com>
11 #define SCALE 3 /* default scaling factor for acceleration */
12 #define THRESH 5 /* default threshhold for acceleration */
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);
20 extern Display* x11_dpy;
22 extern Visual* x11_vis;
23 extern Window x11_win;
25 extern unsigned int x11_event_mask;
26 extern int x11_setup_display();
27 extern void x11_handle_event(XEvent*);
29 MOUSEDEVICE mousedev = {
39 * Open up the mouse device.
40 * Returns the fd if successful, or negative if unsuccessful.
42 static int X11_Open(MOUSEDEVICE *pmd)
44 if (x11_setup_display() < 0)
46 /* return the x11 file descriptor for select */
47 return ConnectionNumber(x11_dpy);
51 * Close the mouse device.
60 * Get mouse buttons supported
63 X11_GetButtonInfo(void)
65 return MWBUTTON_L | MWBUTTON_M | MWBUTTON_R;
69 * Get default mouse acceleration settings
72 X11_GetDefaultAccel(int *pscale,int *pthresh)
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.
86 X11_Read(MWCOORD *dx, MWCOORD *dy, MWCOORD *dz, int *bp)
88 static int noevent_count = 0;
91 long mask = /* x11_event_mask | */
93 ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask;
95 ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
98 while (XCheckMaskEvent(x11_dpy, mask, &ev)) {
99 if (ev.type == MotionNotify) {
100 if (ev.xmotion.window == x11_win) {
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;
115 else if (ev.type == ButtonPress) {
116 if (ev.xbutton.window == x11_win) {
119 /* Get pressed button */
120 if(ev.xbutton.button == 1)
122 else if(ev.xbutton.button == 2)
124 else if(ev.xbutton.button == 3)
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;
135 /* printf("!Pressing button: 0x%x, state: 0x%x, button: 0x%x\n",
136 button,ev.xbutton.state, ev.xbutton.button);*/
144 else if (ev.type == ButtonRelease) {
145 if (ev.xbutton.window == x11_win) {
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;
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;
165 /* We need to remove the released button from the button mask*/
168 /*printf("!Releasing button: 0x%x, state: 0x%x, button: 0x%x\n",
169 button,ev.xbutton.state, ev.xbutton.button);*/
179 x11_handle_event(&ev);
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);
198 return 2; /* absolute position returned*/