TouchCPLib  1.0.0
A touch-enabled GUI interface based on SDL and libTS. It provides a simple desktop-free UI for your embedded Raspberry projects.
Application.h
Go to the documentation of this file.
1 #pragma once
2 
11 #include <SDL2/SDL.h>
12 #include <SDL2/SDL_ttf.h>
13 #include <SDL2/SDL_image.h>
14 #include <string>
15 #include <unistd.h>
16 #include <linux/vt.h>
17 #include <linux/kd.h>
18 #include <fcntl.h>
19 #include <sys/ioctl.h>
20 #include <memory>
21 #include <functional>
22 
23 #include "Window.h"
24 #include "SceneManager.h"
25 #include "TouchInput.h"
26 #include "TouchEventDispatcher.h"
27 #include "IMGException.h"
28 #include "TTFFontException.h"
29 #include "TouchCPException.h"
30 #include "WorkerThread.h"
31 #include "MainThreadTaskRunner.h"
32 
38 {
39 public:
49  Application(int width, int height, SDL_Color bgColor, std::string touchInputDevice, std::string ttyDevice, int FPS_LIMIT);
50  ~Application();
51 
52  Application(const Application &copy) = delete;
53  Application operator=(const Application &copy) = delete;
54 
58  void run();
59  bool isRunning();
64  SDL_Window *getWindow();
69  SDL_Renderer *getRenderer();
79  void runOnWorkerThread(Task *task);
84  void runOnMainThread(Task *task);
89  static SDL_Window *getCurrentWindow();
94  static SDL_Renderer *getCurrentRenderer();
104  static void runOnCurrentWorkerThread(Task *task);
109  static void runOnCurrentMainThread(Task *task);
113  void exit();
114 
119  static Application *getCurrent();
120 
121 private:
122  static Application *application;
123  void MainLoop();
124  void ProcessEvents(TouchEventDispatcher &touchEventDispatcher, const ts_sample_mt touch_event);
125  void DisableTTYCursor();
126  void EnableTTYCursor();
127 
128  SceneManager sceneManager;
129  bool running = true;
130  int width, height;
131  SDL_Color bgColor;
132  std::string touchInputDevice, ttyDevice;
133  SDL_Window *window;
134  SDL_Renderer *renderer;
135  uint32_t lastTouchEvent;
136  std::unique_ptr<TouchEventDispatcher> touchEventDispatcher;
137  std::unique_ptr<Window> mainWindow;
138  std::unique_ptr<TouchInput> touchInput;
139  // This order is important
140  MainThreadTaskRunner taskRunner;
141  WorkerThread workerThread;
143 
144  static constexpr int TOUCH_DEBOUNCE_MS = 250;
145  static constexpr int SAMPLES = 5;
146  static constexpr int SLOTS = 1;
147  int FPS = 30;
148  unsigned int FRAMETIME = 1000 / FPS;
149 };
void exit()
Exit the application.
Definition: Application.cpp:216
void run()
Start the application.
Definition: Application.cpp:113
static void runOnCurrentMainThread(Task *task)
Runs the given task on the main thread. It will be deleted when done (static shortcut) ...
Definition: Application.cpp:210
bool isRunning()
Definition: Application.cpp:159
void runOnMainThread(Task *task)
Runs the given task on the main thread. It will be deleted when done.
Definition: Application.cpp:184
static void runOnCurrentWorkerThread(Task *task)
Runs the given task on the worker thread. It will be deleted when done (static shortcut) ...
Definition: Application.cpp:204
Definition: MainThreadTaskRunner.h:17
A class that takes care of dispatching the touch events to the right objects of the displayed scene...
Definition: TouchEventDispatcher.h:24
SDL_Window * getWindow()
Get the SDL window object.
Definition: Application.cpp:164
static SDL_Renderer * getCurrentRenderer()
Get the SDL renderer object (static shortcut)
Definition: Application.cpp:194
Application operator=(const Application &copy)=delete
The main application class. You can only initialize one instance. Typically, you instanciate it in ma...
Definition: Application.h:37
The scene manager controls which scene is currently displayed, in addition to enumerating the scenes ...
Definition: SceneManager.h:22
static SceneManager * getCurrentSceneManager()
Get the scene manager, needed to change the currently displayed scene (static shortcut) ...
Definition: Application.cpp:199
void runOnWorkerThread(Task *task)
Runs the given task on the worker thread. It will be deleted when done.
Definition: Application.cpp:179
SceneManager * getSceneManager()
Get the scene manager, needed to change the currently displayed scene.
Definition: Application.cpp:174
Application(int width, int height, SDL_Color bgColor, std::string touchInputDevice, std::string ttyDevice, int FPS_LIMIT)
Create an application.
Definition: Application.cpp:5
static Application * getCurrent()
Get the currently running application, or null if none is available.
Definition: Application.cpp:221
SDL_Renderer * getRenderer()
Get the SDL renderer object.
Definition: Application.cpp:169
A class that spawns an Executor Thread (or worker thread). The thread is used for handling intensive ...
Definition: WorkerThread.h:25
A task is an object that wraps the code to run on an thread.
Definition: Task.h:18
~Application()
Definition: Application.cpp:46
static SDL_Window * getCurrentWindow()
Get the SDL window object (static shortcut)
Definition: Application.cpp:189