tablet.cpp

Go to the documentation of this file.
00001 // $Id: tablet.cpp 751 2006-03-31 15:43:49Z alex $
00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
00003 ================================XARAHEADERSTART===========================
00004  
00005                Xara LX, a vector drawing and manipulation program.
00006                     Copyright (C) 1993-2006 Xara Group Ltd.
00007        Copyright on certain contributions may be held in joint with their
00008               respective authors. See AUTHORS file for details.
00009 
00010 LICENSE TO USE AND MODIFY SOFTWARE
00011 ----------------------------------
00012 
00013 This file is part of Xara LX.
00014 
00015 Xara LX is free software; you can redistribute it and/or modify it
00016 under the terms of the GNU General Public License version 2 as published
00017 by the Free Software Foundation.
00018 
00019 Xara LX and its component source files are distributed in the hope
00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00022 See the GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License along
00025 with Xara LX (see the file GPL in the root directory of the
00026 distribution); if not, write to the Free Software Foundation, Inc., 51
00027 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00028 
00029 
00030 ADDITIONAL RIGHTS
00031 -----------------
00032 
00033 Conditional upon your continuing compliance with the GNU General Public
00034 License described above, Xara Group Ltd grants to you certain additional
00035 rights. 
00036 
00037 The additional rights are to use, modify, and distribute the software
00038 together with the wxWidgets library, the wxXtra library, and the "CDraw"
00039 library and any other such library that any version of Xara LX relased
00040 by Xara Group Ltd requires in order to compile and execute, including
00041 the static linking of that library to XaraLX. In the case of the
00042 "CDraw" library, you may satisfy obligation under the GNU General Public
00043 License to provide source code by providing a binary copy of the library
00044 concerned and a copy of the license accompanying it.
00045 
00046 Nothing in this section restricts any of the rights you have under
00047 the GNU General Public License.
00048 
00049 
00050 SCOPE OF LICENSE
00051 ----------------
00052 
00053 This license applies to this program (XaraLX) and its constituent source
00054 files only, and does not necessarily apply to other Xara products which may
00055 in part share the same code base, and are subject to their own licensing
00056 terms.
00057 
00058 This license does not apply to files in the wxXtra directory, which
00059 are built into a separate library, and are subject to the wxWindows
00060 license contained within that directory in the file "WXXTRA-LICENSE".
00061 
00062 This license does not apply to the binary libraries (if any) within
00063 the "libs" directory, which are subject to a separate license contained
00064 within that directory in the file "LIBS-LICENSE".
00065 
00066 
00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
00068 ----------------------------------------------
00069 
00070 Subject to the terms of the GNU Public License (see above), you are
00071 free to do whatever you like with your modifications. However, you may
00072 (at your option) wish contribute them to Xara's source tree. You can
00073 find details of how to do this at:
00074   http://www.xaraxtreme.org/developers/
00075 
00076 Prior to contributing your modifications, you will need to complete our
00077 contributor agreement. This can be found at:
00078   http://www.xaraxtreme.org/developers/contribute/
00079 
00080 Please note that Xara will not accept modifications which modify any of
00081 the text between the start and end of this header (marked
00082 XARAHEADERSTART and XARAHEADEREND).
00083 
00084 
00085 MARKS
00086 -----
00087 
00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
00089 designs are registered or unregistered trademarks, design-marks, and/or
00090 service marks of Xara Group Ltd. All rights in these marks are reserved.
00091 
00092 
00093       Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
00094                         http://www.xara.com/
00095 
00096 =================================XARAHEADEREND============================
00097  */
00098 // Tablet.cpp : implementation file
00099 //
00100 
00101 #include "camtypes.h"
00102 
00103 #include "tablet.h"
00104 #include "wintab.h"
00105 
00106 // This must be defined before the include of pktdef.h in order to get what we want out of it
00107 #define PACKETDATA  (PK_CURSOR | PK_BUTTONS | PK_NORMAL_PRESSURE)
00108 #define PACKETMODE  0
00109 #include "pktdef.h"
00110 
00111 
00112 CC_IMPLEMENT_DYNAMIC(WinTabPressurePen, CCPen);
00113 
00114 #define new CAM_DEBUG_NEW
00115 
00116 
00117 
00118 //const INT32 NPACKETQSIZE = 32;
00119 
00120 
00121 /********************************************************************************************
00122 
00123 >   WinTabPressurePen::WinTabPressurePen()
00124 
00125     Author:     Martin_Donelly (Xara Group Ltd) <camelotdev@xara.com> & Jason
00126     Created:    24/1/97
00127 
00128     Purpose:    Constructor. Initialises the pen
00129 
00130 ********************************************************************************************/
00131 
00132 WinTabPressurePen::WinTabPressurePen()
00133 {
00134     // Initialise member vars to sensible defaults
00135     hTab            = NULL;
00136     wActiveCsr      = NULL;
00137     wOldCsr         = NULL;
00138     wPrsBtn         = 0;
00139     ClickThreshold  = 0;
00140     PressureMax     = MAXPRESSURE;
00141     PenNotOnTablet  = TRUE;
00142 
00143     // Read the tablet default settings as a basis for our settings
00144     LOGCONTEXT lcMine;      
00145     INT32 Result = WTInfo(WTI_DEFCONTEXT, 0, &lcMine);
00146     if (Result == 0)
00147     {
00148         TRACE( _T("Pressure sensitive tablet not detected\n"));
00149         return;
00150     }
00151 
00152     // Modify some of the flags in the block we read above
00153     wsprintf(lcMine.lcName, "Camelot");
00154     lcMine.lcOptions    |= CXO_SYSTEM;
00155     lcMine.lcPktData    = PACKETDATA;
00156     lcMine.lcPktMode    = PACKETMODE;
00157     lcMine.lcMoveMask   = PACKETDATA;
00158 
00159     hTab = WTOpen(hMainframeWnd, &lcMine, TRUE);
00160     if (hTab == NULL)
00161     {
00162         ERROR3("Failed to initialise pressure sensitive tablet");
00163         return;
00164     }
00165 
00166     PressureAvailable = TRUE;       // We initialised! Woo hoo!
00167 
00168     // Set the packet queue size we desire - Why? The default of 8 should be ample
00169 /*
00170     Result = WTQueueSizeGet(hTab);
00171     TRACE( _T("Q size is %ld\n"), Result);
00172 
00173     INT32 QSize = NPACKETQSIZE;
00174     Result = 0;
00175     while (QSize >= 1 && Result == 0)
00176     {
00177         Result = WTQueueSizeSet(hTab, QSize);
00178         QSize /= 2;
00179     }
00180 */
00181     // And initalise the tablet ready for use
00182     InitPressure();
00183 }
00184 
00185 
00186 
00187 /********************************************************************************************
00188 
00189 >   WinTabPressurePen::~WinTabPressurePen()
00190 
00191     Author:     Jason_Williams (Xara Group Ltd) <camelotdev@xara.com>
00192     Created:    24/1/97
00193 
00194     Purpose:    Destructor. Deinitialises the pen
00195 
00196 ********************************************************************************************/
00197 
00198 WinTabPressurePen::~WinTabPressurePen()
00199 {
00200     // If we've got a context open, let's close it to be tidy
00201     if (hTab != NULL)
00202         WTClose(hTab);
00203 }
00204 
00205 
00206 
00207 /********************************************************************************************
00208 
00209 >   virtual void WinTabPressurePen::ReadTabletPressureData(void)
00210 
00211     Author:     Martin_Donelly (Xara Group Ltd) <camelotdev@xara.com> & Jason
00212     Created:    24/1/97
00213 
00214     Returns:    TRUE if it read pressure successfully
00215                 FALSE if it failed - in this case, the caller (base class) will default
00216                 to calculating a faked pressure value from movement information. (e.g.
00217                 if you stop getting pen pressure packets, revert to using movement
00218                 rather than always returning zero!)
00219 
00220     Purpose:    Records the latest pressure info from the pressure sensitive tablet
00221                 Does nothing if this is not a "real" pen
00222 
00223                 Does nothing in the base class.
00224 
00225 ********************************************************************************************/
00226 
00227 BOOL WinTabPressurePen::ReadTabletPressureData(void)
00228 {
00229     // If we couldn't initialise the tablet, or the pen isn't physically present on
00230     // the tablet at the moment (meaning that mouse movements are being generated
00231     // by a mouse), then revert to using the mouse simulation modes.
00232     if (!PressureAvailable || PenNotOnTablet)
00233         return(FALSE);
00234 
00235     if (hTab == NULL)
00236     {
00237         ERROR3("No tablet in ReadTabletPressureData");
00238         return(FALSE);
00239     }
00240 
00241     // Make sure our tablet context is "on top" of all others
00242     WTOverlap(hTab, TRUE);
00243 
00244     PACKET localPacketBuf;
00245     INT32 nPackets = WTPacketsGet(hTab, 1, &localPacketBuf);
00246 
00247     // If there are no new packets, then return immediately, pretending to the base class
00248     // that we were happy, so that it doesn't add in mouse handling code at random while we draw!
00249     if (nPackets <= 0)
00250         return(TRUE);
00251 
00252     wActiveCsr = localPacketBuf.pkCursor;
00253 
00254     // If this cursor doesn't return pressure data we need to behave like a normal mouse...
00255     WTPKT pktflags = 0;
00256     WTInfo(WTI_CURSORS + wActiveCsr, CSR_PKTDATA, &pktflags);
00257     if ((pktflags & PK_NORMAL_PRESSURE) == 0)
00258         return FALSE;
00259 
00260     // Determine which cursor is active - note that a cursor in this sense is
00261     // an input device, such as the pen end/eraser end of the pen. I think.
00262     if (wActiveCsr != wOldCsr)
00263     {
00264         // re-init on cursor change.
00265         InitPressure();
00266         wOldCsr = wActiveCsr;
00267     }
00268 
00269     if (localPacketBuf.pkButtons & (1 << wPrsBtn))
00270     {
00271         // The user has pressed hard enough to pass the "click" threshold
00272         // so the "button" is effectively down. We subtract the click
00273         // threshold value from the pressure, and return that. It is automatically
00274         // scaled by the pen caller, using our GetPressureMax() value.
00275         // We must be careful not to return negative pressure, though, because
00276         // a second threshold is used to control "release" of the click, so
00277         // that the pen does not "flutter" between clicked/unclicked when it nears
00278         // the threshold.
00279         if (localPacketBuf.pkNormalPressure > ClickThreshold)
00280             PenPressure = localPacketBuf.pkNormalPressure - ClickThreshold;
00281         else
00282             PenPressure = 0;
00283     }
00284     else
00285     {
00286         // The user has not pressed hard enough to pass the "click" threshold
00287         // so the "button" is effectively up - we ignore any pressure in this case.
00288         PenPressure = 0;
00289     }
00290 
00291     // And flush any enqueued packets so the next one we get is vaguely related to
00292     // the mouse position at which it occurred.
00293     WTPacketsGet(hTab, 1, NULL);
00294     return(TRUE);
00295 }
00296 
00297 
00298 
00299 /********************************************************************************************
00300 
00301 >   void WinTabPressurePen::InitPressure(void)
00302 
00303     Author:     Martin_Donelly (Xara Group Ltd) <camelotdev@xara.com> & Jason
00304     Created:    24/1/97
00305 
00306     Purpose:    Sets up the pen ready for reading pressure information.
00307                 Reads back the maximum pressure value we'll be given, etc
00308 
00309 ********************************************************************************************/
00310 
00311 void WinTabPressurePen::InitPressure(void)
00312 {
00313     /* browse WinTab's many info items to discover pressure handling. */
00314     AXIS np;
00315     LOGCONTEXT lc;
00316     BYTE logBtns[32];
00317     UINT32 btnMarks[2];
00318     UINT32 size;
00319 
00320     /* discover the LOGICAL button generated by the pressure channel. */
00321     /* get the PHYSICAL button from the cursor category and run it */
00322     /* through that cursor's button map (usually the identity map). */
00323     wPrsBtn = (BYTE)-1;
00324     WTInfo(WTI_CURSORS + wActiveCsr, CSR_NPBUTTON, &wPrsBtn);
00325     size = WTInfo(WTI_CURSORS + wActiveCsr, CSR_BUTTONMAP, &logBtns);
00326     if ((UINT32)wPrsBtn < size)
00327         wPrsBtn = logBtns[wPrsBtn];
00328 
00329     /* get the current context for its device variable. */
00330     WTGet(hTab, &lc);
00331 
00332     /* get the size of the pressure axis. */
00333     WTInfo(WTI_DEVICES + lc.lcDevice, DVC_NPRESSURE, &np);
00334     UINT32 prsNoBtnOrg = (UINT32)np.axMin;
00335     UINT32 prsNoBtnExt = (UINT32)np.axMax - (UINT32)np.axMin;
00336 
00337     /* get the button marks (up & down generation thresholds) */
00338     /* and calculate what pressure range we get when pressure-button is down. */
00339     btnMarks[1] = 0; /* default if info item not present. */
00340     WTInfo(WTI_CURSORS + wActiveCsr, CSR_NPBTNMARKS, btnMarks);
00341     ClickThreshold = btnMarks[1];
00342 
00343     PressureMax = (UINT32)np.axMax - ClickThreshold;
00344 }
00345 
00346 
00347 
00348 /********************************************************************************************
00349 
00350 >   virtual void WinTabPressurePen::StartStroke(void)
00351 
00352     Author:     Jason_Williams (Xara Group Ltd) <camelotdev@xara.com>
00353     Created:    25/1/97
00354 
00355     Purpose:    Informs the Pen class that you are starting a stroke (a drag
00356                 operation for which you wish to record pressure information)
00357 
00358                 If you do not call this method, then pressure information
00359                 will be "faked" based on mouse speed/direction information.
00360 
00361                 It should be called when you start your drag (on button down)
00362 
00363                 When the stroke finishes, remember to call WinTabPressurePen::EndStroke
00364 
00365 ********************************************************************************************/
00366 
00367 void WinTabPressurePen::StartStroke(void)
00368 {
00369     // Set the base class up to generate pressure in the correct way
00370     // In case we fail, set pressure to maximum, so if we fall through
00371     // to a mouse handler, we don't get "blank" strokes
00372     CurrentPressureMode = DefaultPressureMode;
00373     PenPressure = PressureMax;
00374 
00375     // If we're not currently configured to use the pen pressure, then
00376     // we don't need to do any more - let the base class handle everything.
00377     if (CurrentPressureMode != PressureMode_Pen)
00378         return;
00379     
00380     // DY - we no longer wish to fake pressure using mouse speed, so if we're not
00381     // on the tablet at the start of the stroke then we're not doing it
00382     if (PenNotOnTablet)
00383         CurrentPressureMode = PressureMode_None;
00384 
00385     // If we failed to initialise a tablet, then revert to speed mode
00386     if (hTab == NULL)
00387     {
00388         CurrentPressureMode = PressureMode_Speed;
00389         return;
00390     }
00391 
00392     // ****!!!!TODO
00393     // We should really check tablet proximity here. If the pen's not
00394     // near the tablet, the user must be dragging with a different input
00395     // device. This requires that our Mainframe watches out for WT_PROXIMITY
00396     // messages and tells us about them.
00397 
00398     // OK, turn that baby on!
00399 //  WTEnable(hTab, TRUE);
00400 
00401     PenPressure = 0;
00402 }
00403 
00404 
00405 
00406 /********************************************************************************************
00407 
00408 >   virtual void WinTabPressurePen::EndStroke(void)
00409 
00410     Author:     Jason_Williams (Xara Group Ltd) <camelotdev@xara.com>
00411     Created:    25/1/97
00412 
00413     Purpose:    Informs the Pen class that you are completing a stroke (a drag
00414                 operation for which you recorded pressure information)
00415 
00416                 This function must be called to "cancel" out a call to StartStroke.
00417                 It should be called in your drag completion routine, and should
00418                 be called under all "end of drag" circumstances.
00419 
00420 ********************************************************************************************/
00421 
00422 void WinTabPressurePen::EndStroke(void)
00423 {
00424     // Stop WinTab bothering to collect packets now - we don't want em
00425     if (hTab != NULL)
00426     {
00427 //      WTEnable(hTab, FALSE);          // Disable the pen
00428         WTPacketsGet(hTab, 1, NULL);    // And flush remaining packets
00429     }
00430 }
00431 
00432 
00433 
00434 /********************************************************************************************
00435 
00436 >   void WinTabPressurePen::PenProximityChanged(BOOL LeavingTablet)
00437 
00438     Author:     Jason_Williams (Xara Group Ltd) <camelotdev@xara.com>
00439     Created:    25/1/97
00440 
00441     Inputs:     LeavingTablet - TRUE if the pen is leaving the tablet detection area
00442                                 FALSE if the pen is re-entering the tablet area
00443 
00444     Purpose:    Called by the CMainFrame window when it recieves a WinTab WT_PROXIMITY
00445                 message. This indicates that the pen is moving in or out of the pen
00446                 detection area over the tablet. We keep track of the proximity of the
00447                 pen, so that if the user puts it down and starts drawing with their
00448                 mouse, we realise this and revert to base-class simulated pressure.
00449 
00450 ********************************************************************************************/
00451 
00452 void WinTabPressurePen::PenProximityChanged(BOOL LeavingTablet)
00453 {
00454     PenNotOnTablet = LeavingTablet;
00455 }
00456 

Generated on Sat Nov 10 03:48:57 2007 for Camelot by  doxygen 1.4.4