winmouse.c revision 4642e01f
105b261ecSmrg/* 205b261ecSmrg *Copyright (C) 1994-2000 The XFree86 Project, Inc. All Rights Reserved. 305b261ecSmrg * 405b261ecSmrg *Permission is hereby granted, free of charge, to any person obtaining 505b261ecSmrg * a copy of this software and associated documentation files (the 605b261ecSmrg *"Software"), to deal in the Software without restriction, including 705b261ecSmrg *without limitation the rights to use, copy, modify, merge, publish, 805b261ecSmrg *distribute, sublicense, and/or sell copies of the Software, and to 905b261ecSmrg *permit persons to whom the Software is furnished to do so, subject to 1005b261ecSmrg *the following conditions: 1105b261ecSmrg * 1205b261ecSmrg *The above copyright notice and this permission notice shall be 1305b261ecSmrg *included in all copies or substantial portions of the Software. 1405b261ecSmrg * 1505b261ecSmrg *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 1605b261ecSmrg *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 1705b261ecSmrg *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 1805b261ecSmrg *NONINFRINGEMENT. IN NO EVENT SHALL THE XFREE86 PROJECT BE LIABLE FOR 1905b261ecSmrg *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 2005b261ecSmrg *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 2105b261ecSmrg *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2205b261ecSmrg * 2305b261ecSmrg *Except as contained in this notice, the name of the XFree86 Project 2405b261ecSmrg *shall not be used in advertising or otherwise to promote the sale, use 2505b261ecSmrg *or other dealings in this Software without prior written authorization 2605b261ecSmrg *from the XFree86 Project. 2705b261ecSmrg * 2805b261ecSmrg * Authors: Dakshinamurthy Karra 2905b261ecSmrg * Suhaib M Siddiqi 3005b261ecSmrg * Peter Busch 3105b261ecSmrg * Harold L Hunt II 3205b261ecSmrg */ 3305b261ecSmrg 3405b261ecSmrg#ifdef HAVE_XWIN_CONFIG_H 3505b261ecSmrg#include <xwin-config.h> 3605b261ecSmrg#endif 3705b261ecSmrg#include "win.h" 3805b261ecSmrg 394642e01fSmrg#if defined(XFree86Server) 4005b261ecSmrg#include "inputstr.h" 4105b261ecSmrg 4205b261ecSmrg/* Peek the internal button mapping */ 4305b261ecSmrgstatic CARD8 const *g_winMouseButtonMap = NULL; 4405b261ecSmrg#endif 4505b261ecSmrg 4605b261ecSmrg 4705b261ecSmrg/* 4805b261ecSmrg * Local prototypes 4905b261ecSmrg */ 5005b261ecSmrg 5105b261ecSmrgstatic void 5205b261ecSmrgwinMouseCtrl (DeviceIntPtr pDevice, PtrCtrl *pCtrl); 5305b261ecSmrg 5405b261ecSmrg 5505b261ecSmrgstatic void 5605b261ecSmrgwinMouseCtrl (DeviceIntPtr pDevice, PtrCtrl *pCtrl) 5705b261ecSmrg{ 5805b261ecSmrg} 5905b261ecSmrg 6005b261ecSmrg 6105b261ecSmrg/* 6205b261ecSmrg * See Porting Layer Definition - p. 18 6305b261ecSmrg * This is known as a DeviceProc 6405b261ecSmrg */ 6505b261ecSmrg 6605b261ecSmrgint 6705b261ecSmrgwinMouseProc (DeviceIntPtr pDeviceInt, int iState) 6805b261ecSmrg{ 6905b261ecSmrg int lngMouseButtons, i; 7005b261ecSmrg int lngWheelEvents = 2; 7105b261ecSmrg CARD8 *map; 7205b261ecSmrg DevicePtr pDevice = (DevicePtr) pDeviceInt; 7305b261ecSmrg 7405b261ecSmrg switch (iState) 7505b261ecSmrg { 7605b261ecSmrg case DEVICE_INIT: 7705b261ecSmrg /* Get number of mouse buttons */ 7805b261ecSmrg lngMouseButtons = GetSystemMetrics(SM_CMOUSEBUTTONS); 7905b261ecSmrg 8005b261ecSmrg /* Mapping of windows events to X events: 8105b261ecSmrg * LEFT:1 MIDDLE:2 RIGHT:3 8205b261ecSmrg * SCROLL_UP:4 SCROLL_DOWN:5 8305b261ecSmrg * XBUTTON 1:6 XBUTTON 2:7 ... 8405b261ecSmrg * 8505b261ecSmrg * To map scroll wheel correctly we need at least the 3 normal buttons 8605b261ecSmrg */ 8705b261ecSmrg if (lngMouseButtons < 3) 8805b261ecSmrg lngMouseButtons = 3; 8905b261ecSmrg winMsg(X_PROBED, "%d mouse buttons found\n", lngMouseButtons); 9005b261ecSmrg 9105b261ecSmrg /* allocate memory: 9205b261ecSmrg * number of buttons + 2x mouse wheel event + 1 extra (offset for map) 9305b261ecSmrg */ 9405b261ecSmrg map = malloc(sizeof(CARD8) * (lngMouseButtons + lngWheelEvents + 1)); 9505b261ecSmrg 9605b261ecSmrg /* initalize button map */ 9705b261ecSmrg map[0] = 0; 9805b261ecSmrg for (i=1; i <= lngMouseButtons + lngWheelEvents; i++) 9905b261ecSmrg map[i] = i; 10005b261ecSmrg InitPointerDeviceStruct (pDevice, 10105b261ecSmrg map, 10205b261ecSmrg lngMouseButtons + lngWheelEvents, 10305b261ecSmrg GetMotionHistory, 10405b261ecSmrg winMouseCtrl, 10505b261ecSmrg GetMotionHistorySize(), 10605b261ecSmrg 2); 10705b261ecSmrg free(map); 10805b261ecSmrg 1094642e01fSmrg#if defined(XFree86Server) 11005b261ecSmrg g_winMouseButtonMap = pDeviceInt->button->map; 11105b261ecSmrg#endif 11205b261ecSmrg break; 11305b261ecSmrg 11405b261ecSmrg case DEVICE_ON: 11505b261ecSmrg pDevice->on = TRUE; 11605b261ecSmrg break; 11705b261ecSmrg 11805b261ecSmrg case DEVICE_CLOSE: 1194642e01fSmrg#if defined(XFree86Server) 12005b261ecSmrg g_winMouseButtonMap = NULL; 12105b261ecSmrg#endif 12205b261ecSmrg case DEVICE_OFF: 12305b261ecSmrg pDevice->on = FALSE; 12405b261ecSmrg break; 12505b261ecSmrg } 12605b261ecSmrg return Success; 12705b261ecSmrg} 12805b261ecSmrg 12905b261ecSmrg 13005b261ecSmrg/* Handle the mouse wheel */ 13105b261ecSmrgint 13205b261ecSmrgwinMouseWheel (ScreenPtr pScreen, int iDeltaZ) 13305b261ecSmrg{ 13405b261ecSmrg winScreenPriv(pScreen); 13505b261ecSmrg int button; /* Button4 or Button5 */ 13605b261ecSmrg 13705b261ecSmrg /* Button4 = WheelUp */ 13805b261ecSmrg /* Button5 = WheelDown */ 13905b261ecSmrg 14005b261ecSmrg /* Do we have any previous delta stored? */ 14105b261ecSmrg if ((pScreenPriv->iDeltaZ > 0 14205b261ecSmrg && iDeltaZ > 0) 14305b261ecSmrg || (pScreenPriv->iDeltaZ < 0 14405b261ecSmrg && iDeltaZ < 0)) 14505b261ecSmrg { 14605b261ecSmrg /* Previous delta and of same sign as current delta */ 14705b261ecSmrg iDeltaZ += pScreenPriv->iDeltaZ; 14805b261ecSmrg pScreenPriv->iDeltaZ = 0; 14905b261ecSmrg } 15005b261ecSmrg else 15105b261ecSmrg { 15205b261ecSmrg /* 15305b261ecSmrg * Previous delta of different sign, or zero. 15405b261ecSmrg * We will set it to zero for either case, 15505b261ecSmrg * as blindly setting takes just as much time 15605b261ecSmrg * as checking, then setting if necessary :) 15705b261ecSmrg */ 15805b261ecSmrg pScreenPriv->iDeltaZ = 0; 15905b261ecSmrg } 16005b261ecSmrg 16105b261ecSmrg /* 16205b261ecSmrg * Only process this message if the wheel has moved further than 16305b261ecSmrg * WHEEL_DELTA 16405b261ecSmrg */ 16505b261ecSmrg if (iDeltaZ >= WHEEL_DELTA || (-1 * iDeltaZ) >= WHEEL_DELTA) 16605b261ecSmrg { 16705b261ecSmrg pScreenPriv->iDeltaZ = 0; 16805b261ecSmrg 16905b261ecSmrg /* Figure out how many whole deltas of the wheel we have */ 17005b261ecSmrg iDeltaZ /= WHEEL_DELTA; 17105b261ecSmrg } 17205b261ecSmrg else 17305b261ecSmrg { 17405b261ecSmrg /* 17505b261ecSmrg * Wheel has not moved past WHEEL_DELTA threshold; 17605b261ecSmrg * we will store the wheel delta until the threshold 17705b261ecSmrg * has been reached. 17805b261ecSmrg */ 17905b261ecSmrg pScreenPriv->iDeltaZ = iDeltaZ; 18005b261ecSmrg return 0; 18105b261ecSmrg } 18205b261ecSmrg 18305b261ecSmrg /* Set the button to indicate up or down wheel delta */ 18405b261ecSmrg if (iDeltaZ > 0) 18505b261ecSmrg { 18605b261ecSmrg button = Button4; 18705b261ecSmrg } 18805b261ecSmrg else 18905b261ecSmrg { 19005b261ecSmrg button = Button5; 19105b261ecSmrg } 19205b261ecSmrg 19305b261ecSmrg /* 19405b261ecSmrg * Flip iDeltaZ to positive, if negative, 19505b261ecSmrg * because always need to generate a *positive* number of 19605b261ecSmrg * button clicks for the Z axis. 19705b261ecSmrg */ 19805b261ecSmrg if (iDeltaZ < 0) 19905b261ecSmrg { 20005b261ecSmrg iDeltaZ *= -1; 20105b261ecSmrg } 20205b261ecSmrg 20305b261ecSmrg /* Generate X input messages for each wheel delta we have seen */ 20405b261ecSmrg while (iDeltaZ--) 20505b261ecSmrg { 20605b261ecSmrg /* Push the wheel button */ 20705b261ecSmrg winMouseButtonsSendEvent (ButtonPress, button); 20805b261ecSmrg 20905b261ecSmrg /* Release the wheel button */ 21005b261ecSmrg winMouseButtonsSendEvent (ButtonRelease, button); 21105b261ecSmrg } 21205b261ecSmrg 21305b261ecSmrg return 0; 21405b261ecSmrg} 21505b261ecSmrg 21605b261ecSmrg 21705b261ecSmrg/* 21805b261ecSmrg * Enqueue a mouse button event 21905b261ecSmrg */ 22005b261ecSmrg 22105b261ecSmrgvoid 22205b261ecSmrgwinMouseButtonsSendEvent (int iEventType, int iButton) 22305b261ecSmrg{ 22405b261ecSmrg xEvent xCurrentEvent; 22505b261ecSmrg 22605b261ecSmrg /* Load an xEvent and enqueue the event */ 22705b261ecSmrg xCurrentEvent.u.u.type = iEventType; 2284642e01fSmrg#if defined(XFree86Server) 22905b261ecSmrg if (g_winMouseButtonMap) 23005b261ecSmrg xCurrentEvent.u.u.detail = g_winMouseButtonMap[iButton]; 23105b261ecSmrg else 23205b261ecSmrg#endif 23305b261ecSmrg xCurrentEvent.u.u.detail = iButton; 23405b261ecSmrg xCurrentEvent.u.keyButtonPointer.time 23505b261ecSmrg = g_c32LastInputEventTime = GetTickCount (); 23605b261ecSmrg mieqEnqueue (&xCurrentEvent); 23705b261ecSmrg} 23805b261ecSmrg 23905b261ecSmrg 24005b261ecSmrg/* 24105b261ecSmrg * Decide what to do with a Windows mouse message 24205b261ecSmrg */ 24305b261ecSmrg 24405b261ecSmrgint 24505b261ecSmrgwinMouseButtonsHandle (ScreenPtr pScreen, 24605b261ecSmrg int iEventType, int iButton, 24705b261ecSmrg WPARAM wParam) 24805b261ecSmrg{ 24905b261ecSmrg winScreenPriv(pScreen); 25005b261ecSmrg winScreenInfo *pScreenInfo = pScreenPriv->pScreenInfo; 25105b261ecSmrg 25205b261ecSmrg /* Send button events right away if emulate 3 buttons is off */ 25305b261ecSmrg if (pScreenInfo->iE3BTimeout == WIN_E3B_OFF) 25405b261ecSmrg { 25505b261ecSmrg /* Emulate 3 buttons is off, send the button event */ 25605b261ecSmrg winMouseButtonsSendEvent (iEventType, iButton); 25705b261ecSmrg return 0; 25805b261ecSmrg } 25905b261ecSmrg 26005b261ecSmrg /* Emulate 3 buttons is on, let the fun begin */ 26105b261ecSmrg if (iEventType == ButtonPress 26205b261ecSmrg && pScreenPriv->iE3BCachedPress == 0 26305b261ecSmrg && !pScreenPriv->fE3BFakeButton2Sent) 26405b261ecSmrg { 26505b261ecSmrg /* 26605b261ecSmrg * Button was pressed, no press is cached, 26705b261ecSmrg * and there is no fake button 2 release pending. 26805b261ecSmrg */ 26905b261ecSmrg 27005b261ecSmrg /* Store button press type */ 27105b261ecSmrg pScreenPriv->iE3BCachedPress = iButton; 27205b261ecSmrg 27305b261ecSmrg /* 27405b261ecSmrg * Set a timer to send this button press if the other button 27505b261ecSmrg * is not pressed within the timeout time. 27605b261ecSmrg */ 27705b261ecSmrg SetTimer (pScreenPriv->hwndScreen, 27805b261ecSmrg WIN_E3B_TIMER_ID, 27905b261ecSmrg pScreenInfo->iE3BTimeout, 28005b261ecSmrg NULL); 28105b261ecSmrg } 28205b261ecSmrg else if (iEventType == ButtonPress 28305b261ecSmrg && pScreenPriv->iE3BCachedPress != 0 28405b261ecSmrg && pScreenPriv->iE3BCachedPress != iButton 28505b261ecSmrg && !pScreenPriv->fE3BFakeButton2Sent) 28605b261ecSmrg { 28705b261ecSmrg /* 28805b261ecSmrg * Button press is cached, other button was pressed, 28905b261ecSmrg * and there is no fake button 2 release pending. 29005b261ecSmrg */ 29105b261ecSmrg 29205b261ecSmrg /* Mouse button was cached and other button was pressed */ 29305b261ecSmrg KillTimer (pScreenPriv->hwndScreen, WIN_E3B_TIMER_ID); 29405b261ecSmrg pScreenPriv->iE3BCachedPress = 0; 29505b261ecSmrg 29605b261ecSmrg /* Send fake middle button */ 29705b261ecSmrg winMouseButtonsSendEvent (ButtonPress, Button2); 29805b261ecSmrg 29905b261ecSmrg /* Indicate that a fake middle button event was sent */ 30005b261ecSmrg pScreenPriv->fE3BFakeButton2Sent = TRUE; 30105b261ecSmrg } 30205b261ecSmrg else if (iEventType == ButtonRelease 30305b261ecSmrg && pScreenPriv->iE3BCachedPress == iButton) 30405b261ecSmrg { 30505b261ecSmrg /* 30605b261ecSmrg * Cached button was released before timer ran out, 30705b261ecSmrg * and before the other mouse button was pressed. 30805b261ecSmrg */ 30905b261ecSmrg KillTimer (pScreenPriv->hwndScreen, WIN_E3B_TIMER_ID); 31005b261ecSmrg pScreenPriv->iE3BCachedPress = 0; 31105b261ecSmrg 31205b261ecSmrg /* Send cached press, then send release */ 31305b261ecSmrg winMouseButtonsSendEvent (ButtonPress, iButton); 31405b261ecSmrg winMouseButtonsSendEvent (ButtonRelease, iButton); 31505b261ecSmrg } 31605b261ecSmrg else if (iEventType == ButtonRelease 31705b261ecSmrg && pScreenPriv->fE3BFakeButton2Sent 31805b261ecSmrg && !(wParam & MK_LBUTTON) 31905b261ecSmrg && !(wParam & MK_RBUTTON)) 32005b261ecSmrg { 32105b261ecSmrg /* 32205b261ecSmrg * Fake button 2 was sent and both mouse buttons have now been released 32305b261ecSmrg */ 32405b261ecSmrg pScreenPriv->fE3BFakeButton2Sent = FALSE; 32505b261ecSmrg 32605b261ecSmrg /* Send middle mouse button release */ 32705b261ecSmrg winMouseButtonsSendEvent (ButtonRelease, Button2); 32805b261ecSmrg } 32905b261ecSmrg else if (iEventType == ButtonRelease 33005b261ecSmrg && pScreenPriv->iE3BCachedPress == 0 33105b261ecSmrg && !pScreenPriv->fE3BFakeButton2Sent) 33205b261ecSmrg { 33305b261ecSmrg /* 33405b261ecSmrg * Button was release, no button is cached, 33505b261ecSmrg * and there is no fake button 2 release is pending. 33605b261ecSmrg */ 33705b261ecSmrg winMouseButtonsSendEvent (ButtonRelease, iButton); 33805b261ecSmrg } 33905b261ecSmrg 34005b261ecSmrg return 0; 34105b261ecSmrg} 342