GUIslice  0.16.0
Embedded GUI in C
GUIslice_drv_sdl.h
Go to the documentation of this file.
1 #ifndef _GUISLICE_DRV_SDL_H_
2 #define _GUISLICE_DRV_SDL_H_
3 
4 // =======================================================================
5 // GUIslice library (driver layer for SDL 1.2 & 2.0)
6 // - Calvin Hass
7 // - https://www.impulseadventure.com/elec/guislice-gui.html
8 // - https://github.com/ImpulseAdventure/GUIslice
9 // =======================================================================
10 //
11 // The MIT License
12 //
13 // Copyright 2016-2020 Calvin Hass
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining a copy
16 // of this software and associated documentation files (the "Software"), to deal
17 // in the Software without restriction, including without limitation the rights
18 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 // copies of the Software, and to permit persons to whom the Software is
20 // furnished to do so, subject to the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be included in
23 // all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 // THE SOFTWARE.
32 //
33 // =======================================================================
36 
37 
38 // =======================================================================
39 // Driver Layer for SDL
40 // =======================================================================
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif // __cplusplus
45 
46 #include "GUIslice.h"
47 
48 #include <stdio.h>
49 
50 #if defined(DRV_DISP_SDL1)
51  #include <SDL/SDL.h>
52  #include <SDL/SDL_getenv.h>
53  #include <SDL/SDL_ttf.h>
54 #endif
55 #if defined(DRV_DISP_SDL2)
56  #include <SDL2/SDL.h>
57  #include <SDL2/SDL_ttf.h>
58 #endif
59 
60 // Includes for optional tslib touch handling
61 #if defined(DRV_TOUCH_TSLIB)
62  #include "tslib.h"
63 #endif
64 
65 // =======================================================================
66 // API support definitions
67 // - These defines indicate whether the driver includes optimized
68 // support for various APIs. If a define is set to 0, then the
69 // GUIslice core emulation will be used instead.
70 // - At the very minimum, the point draw routine must be available:
71 // gslc_DrvDrawPoint()
72 // =======================================================================
73 
74 #define DRV_HAS_DRAW_POINT 1
75 
76 #if defined(DRV_DISP_SDL1)
77  #define DRV_HAS_DRAW_POINTS 1
78  #define DRV_HAS_DRAW_LINE 0
79  #define DRV_HAS_DRAW_RECT_FRAME 0
80  #define DRV_HAS_DRAW_RECT_FILL 1
81  #define DRV_HAS_DRAW_RECT_ROUND_FRAME 0
82  #define DRV_HAS_DRAW_RECT_ROUND_FILL 0
83  #define DRV_HAS_DRAW_CIRCLE_FRAME 0
84  #define DRV_HAS_DRAW_CIRCLE_FILL 0
85  #define DRV_HAS_DRAW_TRI_FRAME 0
86  #define DRV_HAS_DRAW_TRI_FILL 0
87  #define DRV_HAS_DRAW_TEXT 1
88  #define DRV_HAS_DRAW_BMP_MEM 0
89 #endif
90 
91 #if defined(DRV_DISP_SDL2)
92  #define DRV_HAS_DRAW_POINTS 1
93  #define DRV_HAS_DRAW_LINE 1
94  #define DRV_HAS_DRAW_RECT_FRAME 1
95  #define DRV_HAS_DRAW_RECT_FILL 1
96  #define DRV_HAS_DRAW_RECT_ROUND_FRAME 0
97  #define DRV_HAS_DRAW_RECT_ROUND_FILL 0
98  #define DRV_HAS_DRAW_CIRCLE_FRAME 0
99  #define DRV_HAS_DRAW_CIRCLE_FILL 0
100  #define DRV_HAS_DRAW_TRI_FRAME 0
101  #define DRV_HAS_DRAW_TRI_FILL 0
102  #define DRV_HAS_DRAW_TEXT 1
103  #define DRV_HAS_DRAW_BMP_MEM 0
104 #endif
105 
106 #define DRV_OVERRIDE_TXT_ALIGN 0
107 
108 // =======================================================================
109 // Driver-specific members
110 // =======================================================================
111 typedef struct {
112 
113  #if defined(DRV_DISP_SDL1)
114  SDL_Surface* pSurfScreen;
115  #endif
116 
117  #if defined(DRV_DISP_SDL2)
118  SDL_Window* pWind;
119  SDL_Renderer* pRender;
120  #endif
121 
122  #if defined(DRV_TOUCH_TSLIB)
123  struct tsdev* pTsDev;
124  #endif
125 
126  gslc_tsRect rClipRect;
127 
128 } gslc_tsDriver;
129 
130 
131 
132 // =======================================================================
133 // Public APIs to GUIslice core library
134 // - These functions define the renderer / driver-dependent
135 // implementations for the core drawing operations within
136 // GUIslice.
137 // =======================================================================
138 
139 
140 // -----------------------------------------------------------------------
141 // Configuration Functions
142 // -----------------------------------------------------------------------
143 
159 bool gslc_DrvInit(gslc_tsGui* pGui);
160 
161 
170 void gslc_DrvDestruct(gslc_tsGui* pGui);
171 
172 
180 const char* gslc_DrvGetNameDisp(gslc_tsGui* pGui);
181 
182 
190 const char* gslc_DrvGetNameTouch(gslc_tsGui* pGui);
191 
204 void* gslc_DrvGetDriverDisp(gslc_tsGui* pGui);
205 
219 
220 
221 // -----------------------------------------------------------------------
222 // Image/surface handling Functions
223 // -----------------------------------------------------------------------
224 
225 
236 void* gslc_DrvLoadImage(gslc_tsGui* pGui,gslc_tsImgRef sImgRef);
237 
238 
249 
260 
271 
282 
283 
291 void gslc_DrvImageDestruct(void* pvImg);
292 
293 
302 bool gslc_DrvSetClipRect(gslc_tsGui* pGui,gslc_tsRect* pRect);
303 
304 
305 // -----------------------------------------------------------------------
306 // Font handling Functions
307 // -----------------------------------------------------------------------
308 
309 
319 const void* gslc_DrvFontAdd(gslc_teFontRefType eFontRefType,const void* pvFontRef,uint16_t nFontSz);
320 
329 
330 
345 bool gslc_DrvGetTxtSize(gslc_tsGui* pGui,gslc_tsFont* pFont,const char* pStr,gslc_teTxtFlags eTxtFlags,
346  int16_t* pnTxtX,int16_t* pnTxtY,uint16_t* pnTxtSzW,uint16_t* pnTxtSzH);
347 
348 
363 bool gslc_DrvDrawTxt(gslc_tsGui* pGui,int16_t nTxtX,int16_t nTxtY,gslc_tsFont* pFont,const char* pStr,gslc_teTxtFlags eTxtFlags,gslc_tsColor colTxt,gslc_tsColor colBg);
364 
365 
366 // -----------------------------------------------------------------------
367 // Screen Management Functions
368 // -----------------------------------------------------------------------
369 
378 void gslc_DrvPageFlipNow(gslc_tsGui* pGui);
379 
380 
381 // -----------------------------------------------------------------------
382 // Graphics Primitives Functions
383 // -----------------------------------------------------------------------
384 
395 bool gslc_DrvDrawPoint(gslc_tsGui* pGui,int16_t nX,int16_t nY,gslc_tsColor nCol);
396 
407 bool gslc_DrvDrawPoints(gslc_tsGui* pGui,gslc_tsPt* asPt,uint16_t nNumPt,gslc_tsColor nCol);
408 
419 
420 
431 
432 
445 bool gslc_DrvDrawLine(gslc_tsGui* pGui,int16_t nX0,int16_t nY0,int16_t nX1,int16_t nY1,gslc_tsColor nCol);
446 
447 
448 // TODO: Add DrvDrawFrameCircle()
449 // TODO: Add DrvDrawFillCircle()
450 // TODO: Add DrvDrawFrameTriangle()
451 // TODO: Add DrvDrawFillTriangle()
452 
453 
464 bool gslc_DrvDrawImage(gslc_tsGui* pGui,int16_t nDstX,int16_t nDstY,gslc_tsImgRef sImgRef);
465 
466 
467 // TODO: Add DrvDrawMonoFromMem()
468 // TODO: Add DrvDrawBmp24FromMem()
469 
470 
478 void gslc_DrvDrawBkgnd(gslc_tsGui* pGui);
479 
480 
481 // -----------------------------------------------------------------------
482 // Touch Functions
483 // -----------------------------------------------------------------------
484 
497 bool gslc_DrvGetTouch(gslc_tsGui* pGui,int16_t* pnX,int16_t* pnY,uint16_t* pnPress,gslc_teInputRawEvent* peInputEvent,int16_t* pnInputVal);
498 
499 
500 // -----------------------------------------------------------------------
501 // Dynamic Screen rotation and Touch axes swap/flip functions
502 // -----------------------------------------------------------------------
503 
512 bool gslc_DrvRotate(gslc_tsGui* pGui, uint8_t nRotation);
513 
514 
515 // =======================================================================
516 // Private Functions
517 // - These functions are not included in the scope of APIs used by
518 // the core GUIslice library. Instead, these functions are used
519 // to support the operations within this driver layer.
520 // =======================================================================
521 
522 // -----------------------------------------------------------------------
523 // Private Configuration Functions
524 // -----------------------------------------------------------------------
525 
535 bool gslc_DrvCleanStart(const char* sTTY);
536 
537 
543 void gslc_DrvReportInfoPre();
544 
551 
552 // -----------------------------------------------------------------------
553 // Private Conversion Functions
554 // -----------------------------------------------------------------------
555 
563 SDL_Rect gslc_DrvAdaptRect(gslc_tsRect rRect);
564 
572 SDL_Color gslc_DrvAdaptColor(gslc_tsColor sCol);
573 
574 
575 // -----------------------------------------------------------------------
576 // Private Drawing Functions
577 // -----------------------------------------------------------------------
578 
579 #if defined(DRV_DISP_SDL1)
580 bool gslc_DrvScreenLock(gslc_tsGui* pGui);
593 
594 
607 void gslc_DrvScreenUnlock(gslc_tsGui* pGui);
608 
609 
620 uint32_t gslc_DrvAdaptColorRaw(gslc_tsGui* pGui,gslc_tsColor nCol);
621 
622 
635 uint32_t gslc_DrvDrawGetPixelRaw(gslc_tsGui* pGui,int16_t nX,int16_t nY);
636 
637 
651 void gslc_DrvDrawSetPixelRaw(gslc_tsGui* pGui,int16_t nX,int16_t nY,uint32_t nPixelCol);
652 
665 void gslc_DrvPasteSurface(gslc_tsGui* pGui,int16_t nX, int16_t nY, void* pvSrc, void* pvDest);
666 
667 #endif // DRV_DISP_SDL1
668 
669 
670 
671 // -----------------------------------------------------------------------
672 // Private Touchscreen Functions (if using SDL)
673 // -----------------------------------------------------------------------
674 
684 bool gslc_DrvInitTouch(gslc_tsGui* pGui,const char* acDev);
685 
686 
687 // -----------------------------------------------------------------------
688 // Touchscreen Functions (if using tslib)
689 // -----------------------------------------------------------------------
690 
691 #if defined(DRV_TOUCH_TSLIB)
692 bool gslc_TDrvInitTouch(gslc_tsGui* pGui,const char* acDev);
702 
703 
716 bool gslc_TDrvGetTouch(gslc_tsGui* pGui, int16_t* pnX, int16_t* pnY, uint16_t* pnPress, gslc_teInputRawEvent* peInputEvent, int16_t* pnInputVal);
717 
718 #endif // DRV_TOUCH_TSLIB
719 
720 
721 #ifdef __cplusplus
722 }
723 #endif // __cplusplus
724 #endif // _GUISLICE_DRV_SDL_H_
void gslc_DrvDestruct(gslc_tsGui *pGui)
Free up any members associated with the driver.
Definition: GUIslice_drv_adagfx.cpp:787
void gslc_DrvReportInfoPost()
Report driver debug info (after initialization)
gslc_teFontRefType
Font Reference types.
Definition: GUIslice.h:383
bool gslc_TDrvGetTouch(gslc_tsGui *pGui, int16_t *pnX, int16_t *pnY, uint16_t *pnPress, gslc_teInputRawEvent *peInputEvent, int16_t *pnInputVal)
Get the last touch event from the SDL_Event handler.
Definition: GUIslice_drv_adagfx.cpp:2399
Image reference structure.
Definition: GUIslice.h:569
gslc_teTxtFlags
Text reference flags: Describes the characteristics of a text string (ie.
Definition: GUIslice.h:463
bool gslc_DrvSetElemImageNorm(gslc_tsGui *pGui, gslc_tsElem *pElem, gslc_tsImgRef sImgRef)
Set an element&#39;s normal-state image.
Definition: GUIslice_drv_adagfx.cpp:862
void * gslc_DrvLoadImage(gslc_tsGui *pGui, gslc_tsImgRef sImgRef)
Load a bitmap (*.bmp) and create a new image resource.
Definition: GUIslice_drv_adagfx.cpp:809
bool gslc_DrvDrawPoints(gslc_tsGui *pGui, gslc_tsPt *asPt, uint16_t nNumPt, gslc_tsColor nCol)
Draw a point.
Definition: GUIslice_drv_adagfx.cpp:1541
bool gslc_TDrvInitTouch(gslc_tsGui *pGui, const char *acDev)
Perform any touchscreen-specific initialization.
Definition: GUIslice_drv_adagfx.cpp:2295
void gslc_DrvImageDestruct(void *pvImg)
Release an image surface.
Definition: GUIslice_drv_adagfx.cpp:882
bool gslc_DrvDrawFrameRect(gslc_tsGui *pGui, gslc_tsRect rRect, gslc_tsColor nCol)
Draw a framed rectangle.
Definition: GUIslice_drv_adagfx.cpp:1591
bool gslc_DrvInit(gslc_tsGui *pGui)
Initialize the SDL library.
Definition: GUIslice_drv_adagfx.cpp:571
bool gslc_DrvInitTouch(gslc_tsGui *pGui, const char *acDev)
Perform any touchscreen-specific initialization.
Definition: GUIslice_drv_adagfx.cpp:2135
const void * gslc_DrvFontAdd(gslc_teFontRefType eFontRefType, const void *pvFontRef, uint16_t nFontSz)
Load a font from a resource and return pointer to it.
Definition: GUIslice_drv_adagfx.cpp:910
SDL_Color gslc_DrvAdaptColor(gslc_tsColor sCol)
Translate a gslc_tsColor into an SDL_Color.
Rectangular region. Defines X,Y corner coordinates plus dimensions.
Definition: GUIslice.h:519
Definition: GUIslice_drv_adagfx.h:199
gslc_teInputRawEvent
Raw input event types: touch, key, GPIOs.
Definition: GUIslice.h:260
void * gslc_DrvGetDriverTouch(gslc_tsGui *pGui)
Get the native touch driver instance.
Definition: GUIslice_drv_adagfx.cpp:2146
void gslc_DrvReportInfoPre()
Report driver debug info (before initialization)
bool gslc_DrvDrawImage(gslc_tsGui *pGui, int16_t nDstX, int16_t nDstY, gslc_tsImgRef sImgRef)
Copy all of source image to destination screen at specified coordinate.
Definition: GUIslice_drv_adagfx.cpp:2026
void gslc_DrvPageFlipNow(gslc_tsGui *pGui)
Force a page flip to occur.
Definition: GUIslice_drv_adagfx.cpp:1483
void gslc_DrvFontsDestruct(gslc_tsGui *pGui)
Release all fonts defined in the GUI.
Definition: GUIslice_drv_adagfx.cpp:922
const char * gslc_DrvGetNameTouch(gslc_tsGui *pGui)
Get the touch driver name.
Definition: GUIslice_drv_adagfx.cpp:798
bool gslc_DrvDrawLine(gslc_tsGui *pGui, int16_t nX0, int16_t nY0, int16_t nX1, int16_t nY1, gslc_tsColor nCol)
Draw a line.
Definition: GUIslice_drv_adagfx.cpp:1658
bool gslc_DrvGetTouch(gslc_tsGui *pGui, int16_t *pnX, int16_t *pnY, uint16_t *pnPress, gslc_teInputRawEvent *peInputEvent, int16_t *pnInputVal)
Get the last touch event from the SDL_Event handler.
Definition: GUIslice_drv_adagfx.cpp:2159
bool gslc_DrvDrawPoint(gslc_tsGui *pGui, int16_t nX, int16_t nY, gslc_tsColor nCol)
Draw a point.
Definition: GUIslice_drv_adagfx.cpp:1527
bool gslc_DrvCleanStart(const char *sTTY)
Ensure SDL initializes cleanly to workaround possible issues if previous SDL application failed to cl...
bool gslc_DrvSetElemImageGlow(gslc_tsGui *pGui, gslc_tsElem *pElem, gslc_tsImgRef sImgRef)
Set an element&#39;s glow-state image.
Definition: GUIslice_drv_adagfx.cpp:872
bool gslc_DrvSetClipRect(gslc_tsGui *pGui, gslc_tsRect *pRect)
Set the clipping rectangle for future drawing updates.
Definition: GUIslice_drv_adagfx.cpp:887
SDL_Rect gslc_DrvAdaptRect(gslc_tsRect rRect)
Translate a gslc_tsRect into an SDL_Rect.
void gslc_DrvDrawBkgnd(gslc_tsGui *pGui)
Copy the background image to destination screen.
Definition: GUIslice_drv_adagfx.cpp:2099
Define point coordinates.
Definition: GUIslice.h:528
bool gslc_DrvGetTxtSize(gslc_tsGui *pGui, gslc_tsFont *pFont, const char *pStr, gslc_teTxtFlags eTxtFlags, int16_t *pnTxtX, int16_t *pnTxtY, uint16_t *pnTxtSzW, uint16_t *pnTxtSzH)
Get the extent (width and height) of a text string.
Definition: GUIslice_drv_adagfx.cpp:982
bool gslc_DrvSetBkgndColor(gslc_tsGui *pGui, gslc_tsColor nCol)
Configure the background to use a solid color.
Definition: GUIslice_drv_adagfx.cpp:852
GUI structure.
Definition: GUIslice.h:716
bool gslc_DrvDrawFillRect(gslc_tsGui *pGui, gslc_tsRect rRect, gslc_tsColor nCol)
Draw a filled rectangle.
Definition: GUIslice_drv_adagfx.cpp:1550
Element Struct.
Definition: GUIslice.h:593
void * gslc_DrvGetDriverDisp(gslc_tsGui *pGui)
Get the native display driver instance.
Definition: GUIslice_drv_adagfx.cpp:781
Color structure. Defines RGB triplet.
Definition: GUIslice.h:534
Font reference structure.
Definition: GUIslice.h:559
const char * gslc_DrvGetNameDisp(gslc_tsGui *pGui)
Get the display driver name.
Definition: GUIslice_drv_adagfx.cpp:792
bool gslc_DrvSetBkgndImage(gslc_tsGui *pGui, gslc_tsImgRef sImgRef)
Configure the background to use a bitmap image.
Definition: GUIslice_drv_adagfx.cpp:833
bool gslc_DrvDrawTxt(gslc_tsGui *pGui, int16_t nTxtX, int16_t nTxtY, gslc_tsFont *pFont, const char *pStr, gslc_teTxtFlags eTxtFlags, gslc_tsColor colTxt, gslc_tsColor colBg)
Draw a text string at the given coordinate.
Definition: GUIslice_drv_adagfx.cpp:1198
bool gslc_DrvRotate(gslc_tsGui *pGui, uint8_t nRotation)
Change rotation, automatically adapt touchscreen axes swap/flip.
Definition: GUIslice_drv_adagfx.cpp:3059