OpenClonk
C4App.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2005, Sven Eberhardt
5  * Copyright (c) 2005-2006, Günther Brammer
6  * Copyright (c) 2006, Armin Burgmeier
7  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
8  *
9  * Distributed under the terms of the ISC license; see accompanying file
10  * "COPYING" for details.
11  *
12  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
13  * See accompanying file "TRADEMARK" for details.
14  *
15  * To redistribute this file separately, substitute the full license texts
16  * for the above references.
17  */
18 
19 #ifndef INC_STDAPP
20 #define INC_STDAPP
21 
23 #include "platform/StdScheduler.h"
24 #include "platform/StdSync.h"
25 #include "platform/C4StdInProc.h"
26 
27 #ifdef HAVE_PTHREAD
28 #include <pthread.h>
29 #endif
30 
31 #ifdef USE_SDL_MAINLOOP
32 #include <SDL.h>
33 
34 const char* KeycodeToString(C4KeyCode code);
35 #endif
36 
37 #ifdef USE_WIN32_WINDOWS
38 class CStdMessageProc : public StdSchedulerProc
39 {
40 public:
41  CStdMessageProc() = default;
42  ~CStdMessageProc() override = default;
43 
44 private:
45  C4AbstractApp *pApp{nullptr};
46 
47 public:
48  void SetApp(C4AbstractApp *pnApp) { pApp = pnApp; }
49 
50  // StdSchedulerProc overrides
51  bool Execute(int iTimeout = -1, pollfd *dummy=nullptr) override;
52  HANDLE GetEvent() override { return STDSCHEDULER_EVENT_MESSAGE; }
53 
54 };
55 #endif
56 
58 {
59 public:
60  C4AbstractApp ();
61  ~C4AbstractApp () override;
62 
63  bool Active{false};
64 
65  virtual void Clear();
66 
67  bool Init(int argc, char * argv[]);
68  void Run();
69  virtual void Quit();
70 
71  bool GetIndexedDisplayMode(int32_t iIndex, int32_t *piXRes, int32_t *piYRes, int32_t *piBitDepth, int32_t *piRefreshRate, uint32_t iMonitor);
72  bool SetVideoMode(int iXRes, int iYRes, unsigned int iRefreshRate, unsigned int iMonitor, bool fFullScreen);
73  void RestoreVideoMode();
74 
75  bool DoScheduleProcs(int iTimeout) override;
76  bool FlushMessages();
77 #ifdef WITH_QT_EDITOR
78  void ProcessQtEvents();
79 #endif
80  C4Window * pWindow{nullptr};
81  bool fQuitMsgReceived{false}; // if true, a quit message has been received and the application should terminate
82 
83  // Copy the text to the clipboard or the primary selection
84  bool Copy(const std::string &text, bool fClipboard = true);
85  // Paste the text from the clipboard or the primary selection
86  std::string Paste(bool fClipboard = true);
87  // Is there something in the clipboard?
88  bool IsClipboardFull(bool fClipboard = true);
89  // a command from stdin
90  virtual void OnCommand(const char *szCmd) = 0; // callback
91  // Callback from SetVideoMode
92  virtual void OnResolutionChanged(unsigned int iXRes, unsigned int iYRes) = 0;
93  // Keyboard layout changed
94  virtual void OnKeyboardLayoutChanged() = 0;
95  // notify user to get back to the program
96  void NotifyUserIfInactive();
97  void MessageDialog(const char * message);
98  const char *GetLastError() { return sLastError.c_str(); }
99  void Error(const char * m) { sLastError = m; }
100 
101 #ifdef _WIN32
102 private:
103  HINSTANCE hInstance{nullptr};
104  DWORD idMainThread; // ID of main thread that initialized the app
105 
106  void SetLastErrorFromOS();
107 
108 public:
109  void SetInstance(HINSTANCE hInst) { hInstance = hInst; }
110  HINSTANCE GetInstance() const { return hInstance; }
111  bool AssertMainThread()
112  {
113 # ifdef _DEBUG
114  if (idMainThread && idMainThread != ::GetCurrentThreadId())
115  {
116  assert(false);
117  return false;
118  }
119 # endif
120  return true;
121  }
122 #else
124  {
125  assert(MainThread == pthread_self());
126  return MainThread == pthread_self();
127  }
128  pthread_t MainThread;
129 #endif
130 
131 #if defined(USE_SDL_MAINLOOP)
132 public:
133  void HandleSDLEvent(SDL_Event& event);
134 
135 #elif defined(USE_CONSOLE)
136 protected:
137  C4StdInProc InProc;
138 #endif
139 
140 #ifdef __APPLE__
141 public:
142  std::string GetGameDataPath();
143 #endif
144 
145 #ifdef USE_WIN32_WINDOWS
146 private:
147  CStdMessageProc MessageProc;
148 public:
149  HMONITOR hMon; // monitor handle of used monitor
150  RECT MonitorRect; // output window rect
151 protected:
152  DEVMODEW dspMode, OldDspMode;// display mode for fullscreen
153 #endif
154 
155 protected:
156  std::string sLastError;
157  bool fDspModeSet{false}; // true if display mode was changed
158  virtual bool DoInit(int argc, char * argv[]) = 0;
159 
160  friend class CStdGL;
161  friend class CStdGLCtx;
162  friend class C4Window;
163  friend class C4GtkWindow;
164 };
165 
166 #endif // INC_STDAPP
const char * KeycodeToString(C4KeyCode code)
Definition: C4AppSDL.cpp:248
unsigned long C4KeyCode
uint32_t DWORD
void MessageDialog(const char *message)
Definition: C4AppMac.mm:63
std::string sLastError
Definition: C4App.h:156
pthread_t MainThread
Definition: C4App.h:128
bool SetVideoMode(int iXRes, int iYRes, unsigned int iRefreshRate, unsigned int iMonitor, bool fFullScreen)
Definition: C4AppSDL.cpp:354
friend class C4GtkWindow
Definition: C4App.h:163
bool IsClipboardFull(bool fClipboard=true)
Definition: C4AppMac.mm:58
bool fQuitMsgReceived
Definition: C4App.h:81
virtual void Quit()
Definition: C4AppSDL.cpp:110
virtual void OnResolutionChanged(unsigned int iXRes, unsigned int iYRes)=0
bool AssertMainThread()
Definition: C4App.h:123
void Run()
Definition: C4App.cpp:26
bool GetIndexedDisplayMode(int32_t iIndex, int32_t *piXRes, int32_t *piYRes, int32_t *piBitDepth, int32_t *piRefreshRate, uint32_t iMonitor)
Definition: C4AppSDL.cpp:335
~C4AbstractApp() override
Definition: C4AppT.cpp:40
bool Active
Definition: C4App.h:63
bool fDspModeSet
Definition: C4App.h:157
virtual void OnCommand(const char *szCmd)=0
bool FlushMessages()
Definition: C4AppSDL.cpp:115
void NotifyUserIfInactive()
Definition: C4App.cpp:89
void RestoreVideoMode()
Definition: C4AppSDL.cpp:443
bool Copy(const std::string &text, bool fClipboard=true)
Definition: C4AppMac.mm:34
virtual void OnKeyboardLayoutChanged()=0
std::string Paste(bool fClipboard=true)
Definition: C4AppMac.mm:47
bool DoScheduleProcs(int iTimeout) override
Definition: C4App.cpp:59
virtual bool DoInit(int argc, char *argv[])=0
C4Window * pWindow
Definition: C4App.h:80
void Error(const char *m)
Definition: C4App.h:99
bool Init(int argc, char *argv[])
Definition: C4AppSDL.cpp:87
virtual void Clear()
Definition: C4AppSDL.cpp:105
const char * GetLastError()
Definition: C4App.h:98
virtual bool Execute(int iTimeout=-1, pollfd *readyfds=nullptr)=0