From 1a99b3c321f6456c79aeb402fb34f421d2349d04 Mon Sep 17 00:00:00 2001 From: inmarket Date: Thu, 24 Oct 2013 16:56:09 +1000 Subject: [PATCH] New: ginputSetMouseDisplay() to allow the mouse to work with a non-default display. Fix: Mouse orientation fixed to match the now corrected GDISP orientation. Comment updates --- include/ginput/mouse.h | 19 ++++++++-- src/ginput/mouse.c | 83 ++++++++++++++++++++++++------------------ 2 files changed, 64 insertions(+), 38 deletions(-) diff --git a/include/ginput/mouse.h b/include/ginput/mouse.h index 93537329..f13379c3 100644 --- a/include/ginput/mouse.h +++ b/include/ginput/mouse.h @@ -84,7 +84,8 @@ extern "C" { /** * @brief Creates an instance of a mouse and returns the Source handler - * @note hack: if the instance is 9999, no calibration will be performed! + * @note HACK: if the instance is 9999, it is treated as instance 0 except + * that no calibration will be performed! * * @param[in] instance The ID of the mouse input instance (from 0 to 9999) * @@ -92,6 +93,19 @@ extern "C" { */ GSourceHandle ginputGetMouse(uint16_t instance); + /** + * @brief Assign the display associated with the mouse + * @note This only needs to be called if the mouse is associated with a display + * other than the current default display. It must be called before + * @p ginputGetMouse() if the new display is to be used during the calibration + * process. Other than calibration the display is used for range checking, + * and may also be used to display a mouse pointer. + * + * @param[in] instance The ID of the mouse input instance + * @param[in] g The GDisplay to which this mouse belongs + */ + void ginputSetMouseDisplay(uint16_t instance, GDisplay *g); + /** * @brief Get the current mouse position and button status * @note Unlinke a listener event, this status cannot record meta events such as @@ -129,12 +143,11 @@ extern "C" { * as the gdispGetMouse() routine may attempt to fetch calibration data and perform a startup calibration if there is no way to get it. * If this is called after gdispGetMouse() has been called and the driver requires calibration storage, it will immediately save the * data is has already obtained. - * The 'requireFree' parameter indicates if the fetch buffer must be free()'d to deallocate the buffer provided by the Fetch routine. * * @param[in] instance The ID of the mouse input instance * @param[in] fnsave The routine to save the data * @param[in] fnload The routine to restore the data - * @param[in] requireFree ToDo + * @param[in] requireFree TRUE if the buffer returned by the load function must be freed by the mouse code. */ void ginputSetMouseCalibrationRoutines(uint16_t instance, GMouseCalibrationSaveRoutine fnsave, GMouseCalibrationLoadRoutine fnload, bool_t requireFree); diff --git a/src/ginput/mouse.c b/src/ginput/mouse.c index 74b5a990..b662008e 100644 --- a/src/ginput/mouse.c +++ b/src/ginput/mouse.c @@ -19,6 +19,8 @@ #include "ginput/lld/mouse.h" +static GDisplay *mouseDisplay = 0; + #if GINPUT_MOUSE_NEED_CALIBRATION #if !defined(GFX_USE_GDISP) || !GFX_USE_GDISP #error "GINPUT: GFX_USE_GDISP must be defined when mouse or touch calibration is required" @@ -74,26 +76,26 @@ static struct MouseConfig_t { #if GINPUT_MOUSE_NEED_CALIBRATION static inline void _tsDrawCross(const MousePoint *pp) { - gdispDrawLine(pp->x-15, pp->y, pp->x-2, pp->y, White); - gdispDrawLine(pp->x+2, pp->y, pp->x+15, pp->y, White); - gdispDrawLine(pp->x, pp->y-15, pp->x, pp->y-2, White); - gdispDrawLine(pp->x, pp->y+2, pp->x, pp->y+15, White); + gdispGDrawLine(mouseDisplay, pp->x-15, pp->y, pp->x-2, pp->y, White); + gdispGDrawLine(mouseDisplay, pp->x+2, pp->y, pp->x+15, pp->y, White); + gdispGDrawLine(mouseDisplay, pp->x, pp->y-15, pp->x, pp->y-2, White); + gdispGDrawLine(mouseDisplay, pp->x, pp->y+2, pp->x, pp->y+15, White); - gdispDrawLine(pp->x-15, pp->y+15, pp->x-7, pp->y+15, RGB2COLOR(184,158,131)); - gdispDrawLine(pp->x-15, pp->y+7, pp->x-15, pp->y+15, RGB2COLOR(184,158,131)); + gdispGDrawLine(mouseDisplay, pp->x-15, pp->y+15, pp->x-7, pp->y+15, RGB2COLOR(184,158,131)); + gdispGDrawLine(mouseDisplay, pp->x-15, pp->y+7, pp->x-15, pp->y+15, RGB2COLOR(184,158,131)); - gdispDrawLine(pp->x-15, pp->y-15, pp->x-7, pp->y-15, RGB2COLOR(184,158,131)); - gdispDrawLine(pp->x-15, pp->y-7, pp->x-15, pp->y-15, RGB2COLOR(184,158,131)); + gdispGDrawLine(mouseDisplay, pp->x-15, pp->y-15, pp->x-7, pp->y-15, RGB2COLOR(184,158,131)); + gdispGDrawLine(mouseDisplay, pp->x-15, pp->y-7, pp->x-15, pp->y-15, RGB2COLOR(184,158,131)); - gdispDrawLine(pp->x+7, pp->y+15, pp->x+15, pp->y+15, RGB2COLOR(184,158,131)); - gdispDrawLine(pp->x+15, pp->y+7, pp->x+15, pp->y+15, RGB2COLOR(184,158,131)); + gdispGDrawLine(mouseDisplay, pp->x+7, pp->y+15, pp->x+15, pp->y+15, RGB2COLOR(184,158,131)); + gdispGDrawLine(mouseDisplay, pp->x+15, pp->y+7, pp->x+15, pp->y+15, RGB2COLOR(184,158,131)); - gdispDrawLine(pp->x+7, pp->y-15, pp->x+15, pp->y-15, RGB2COLOR(184,158,131)); - gdispDrawLine(pp->x+15, pp->y-15, pp->x+15, pp->y-7, RGB2COLOR(184,158,131)); + gdispGDrawLine(mouseDisplay, pp->x+7, pp->y-15, pp->x+15, pp->y-15, RGB2COLOR(184,158,131)); + gdispGDrawLine(mouseDisplay, pp->x+15, pp->y-15, pp->x+15, pp->y-7, RGB2COLOR(184,158,131)); } static inline void _tsClearCross(const MousePoint *pp) { - gdispFillArea(pp->x - 15, pp->y - 15, 42, 42, Blue); + gdispGFillArea(mouseDisplay, pp->x - 15, pp->y - 15, 42, 42, Blue); } static inline void _tsTransform(MouseReading *pt, const Calibration *c) { @@ -169,8 +171,8 @@ static void get_calibrated_reading(MouseReading *pt) { get_raw_reading(pt); #if GINPUT_MOUSE_NEED_CALIBRATION || GDISP_NEED_CONTROL - w = gdispGetWidth(); - h = gdispGetHeight(); + w = gdispGGetWidth(mouseDisplay); + h = gdispGGetHeight(mouseDisplay); #endif #if GINPUT_MOUSE_NEED_CALIBRATION @@ -178,14 +180,14 @@ static void get_calibrated_reading(MouseReading *pt) { #endif #if GDISP_NEED_CONTROL - switch(gdispGetOrientation()) { + switch(gdispGGetOrientation(mouseDisplay)) { case GDISP_ROTATE_0: break; case GDISP_ROTATE_90: { - coord_t t = pt->y; - pt->y = h - 1 - pt->x; - pt->x = t; + coord_t t = pt->x; + pt->x = w - 1 - pt->y; + pt->y = t; } break; case GDISP_ROTATE_180: @@ -194,9 +196,9 @@ static void get_calibrated_reading(MouseReading *pt) { break; case GDISP_ROTATE_270: { - coord_t t = pt->x; - pt->x = w - 1 - pt->y; - pt->y = t; + coord_t t = pt->y; + pt->y = h - 1 - pt->x; + pt->x = t; } break; } @@ -319,6 +321,10 @@ GSourceHandle ginputGetMouse(uint16_t instance) { if (instance && instance != 9999) return 0; + // Make sure we have a valid mouse display + if (!mouseDisplay) + mouseDisplay = GDISP; + // Do we need to initialise the mouse subsystem? if (!(MouseConfig.flags & FLG_INIT_DONE)) { ginput_lld_mouse_init(); @@ -362,6 +368,13 @@ GSourceHandle ginputGetMouse(uint16_t instance) { return (GSourceHandle)&MouseConfig; } +void ginputSetMouseDisplay(uint16_t instance, GDisplay *g) { + if (instance) + return; + + mouseDisplay = g ? g : GDISP; +} + bool_t ginputGetMouseStatus(uint16_t instance, GEventMouse *pe) { // Win32 threads don't seem to recognise priority and/or pre-emption // so we add a sleep here to prevent 100% polled applications from locking up. @@ -393,8 +406,8 @@ bool_t ginputCalibrateMouse(uint16_t instance) { return FALSE; #else - const coord_t height = gdispGetHeight(); - const coord_t width = gdispGetWidth(); + const coord_t height = gdispGGetHeight(mouseDisplay); + const coord_t width = gdispGGetWidth(mouseDisplay); const MousePoint cross[] = {{(width / 4), (height / 4)}, {(width - (width / 4)) , (height / 4)}, {(width - (width / 4)) , (height - (height / 4))}, @@ -420,19 +433,19 @@ bool_t ginputCalibrateMouse(uint16_t instance) { MouseConfig.flags &= ~(FLG_CAL_OK|FLG_CAL_SAVED); #if GDISP_NEED_CONTROL - gdispSetOrientation(GDISP_ROTATE_0); + gdispGSetOrientation(mouseDisplay, GDISP_ROTATE_0); #endif #if GDISP_NEED_CLIP - gdispSetClip(0, 0, width, height); + gdispGSetClip(mouseDisplay, 0, 0, width, height); #endif #if GINPUT_MOUSE_MAX_CALIBRATION_ERROR >= 0 while(1) { #endif - gdispClear(Blue); + gdispGClear(mouseDisplay, Blue); - gdispFillStringBox(0, 5, width, 30, GINPUT_MOUSE_CALIBRATION_TEXT, font1, White, Blue, justifyCenter); + gdispGFillStringBox(mouseDisplay, 0, 5, width, 30, GINPUT_MOUSE_CALIBRATION_TEXT, font1, White, Blue, justifyCenter); for(i = 0, pt = points, pc = cross; i < GINPUT_MOUSE_CALIBRATION_POINTS; i++, pt++, pc++) { _tsDrawCross(pc); @@ -461,9 +474,9 @@ bool_t ginputCalibrateMouse(uint16_t instance) { _tsClearCross(pc); if (i >= 1 && pt->x == (pt-1)->x && pt->y == (pt-1)->y) { - gdispFillStringBox(0, 35, width, 40, GINPUT_MOUSE_CALIBRATION_SAME_TEXT, font2, Red, Yellow, justifyCenter); + gdispGFillStringBox(mouseDisplay, 0, 35, width, 40, GINPUT_MOUSE_CALIBRATION_SAME_TEXT, font2, Red, Yellow, justifyCenter); gfxSleepMilliseconds(5000); - gdispFillArea(0, 35, width, 40, Blue); + gdispGFillArea(mouseDisplay, 0, 35, width, 40, Blue); } } @@ -489,7 +502,7 @@ bool_t ginputCalibrateMouse(uint16_t instance) { if (err <= GINPUT_MOUSE_MAX_CALIBRATION_ERROR * GINPUT_MOUSE_MAX_CALIBRATION_ERROR) break; - gdispFillStringBox(0, 35, width, 40, GINPUT_MOUSE_CALIBRATION_ERROR_TEXT, font2, Red, Yellow, justifyCenter); + gdispGFillStringBox(mouseDisplay, 0, 35, width, 40, GINPUT_MOUSE_CALIBRATION_ERROR_TEXT, font2, Red, Yellow, justifyCenter); gfxSleepMilliseconds(5000); } #endif @@ -512,9 +525,9 @@ bool_t ginputCalibrateMouse(uint16_t instance) { // Clear the screen using the GWIN default background color #if GFX_USE_GWIN - gdispClear(gwinGetDefaultBgColor()); + gdispGClear(mouseDisplay, gwinGetDefaultBgColor()); #else - gdispClear(Black); + gdispGClear(mouseDisplay, Black); #endif return TRUE; @@ -523,8 +536,8 @@ bool_t ginputCalibrateMouse(uint16_t instance) { /* Set the routines to save and fetch calibration data. * This function should be called before first calling ginputGetMouse() for a particular instance - * as the gdispGetMouse() routine may attempt to fetch calibration data and perform a startup calibration if there is no way to get it. - * If this is called after gdispGetMouse() has been called and the driver requires calibration storage, it will immediately save the data is has already obtained. + * as the ginputGetMouse() routine may attempt to fetch calibration data and perform a startup calibration if there is no way to get it. + * If this is called after ginputGetMouse() has been called and the driver requires calibration storage, it will immediately save the data is has already obtained. * The 'requireFree' parameter indicates if the fetch buffer must be free()'d to deallocate the buffer provided by the Fetch routine. */ void ginputSetMouseCalibrationRoutines(uint16_t instance, GMouseCalibrationSaveRoutine fnsave, GMouseCalibrationLoadRoutine fnload, bool_t requireFree) {