OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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() : pApp(nullptr) { }
42  ~CStdMessageProc() { }
43 
44 private:
45  C4AbstractApp *pApp;
46 
47 public:
48  void SetApp(C4AbstractApp *pnApp) { pApp = pnApp; }
49 
50  // StdSchedulerProc overrides
51  virtual bool Execute(int iTimeout = -1, pollfd *dummy=0);
52  virtual HANDLE GetEvent() { return STDSCHEDULER_EVENT_MESSAGE; }
53 
54 };
55 #endif
56 
58 {
59 public:
60  C4AbstractApp ();
61  virtual ~C4AbstractApp ();
62 
63  bool Active;
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  virtual bool DoScheduleProcs(int iTimeout);
76  bool FlushMessages();
77 #ifdef WITH_QT_EDITOR
78  void ProcessQtEvents();
79 #endif
81  bool fQuitMsgReceived; // 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;
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; // 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
bool AssertMainThread()
Definition: C4App.h:123
virtual void OnResolutionChanged(unsigned int iXRes, unsigned int iYRes)=0
void MessageDialog(const char *message)
Definition: C4AppMac.mm:63
virtual void OnCommand(const char *szCmd)=0
bool SetVideoMode(int iXRes, int iYRes, unsigned int iRefreshRate, unsigned int iMonitor, bool fFullScreen)
Definition: C4AppSDL.cpp:358
friend class C4GtkWindow
Definition: C4App.h:163
pthread_t MainThread
Definition: C4App.h:128
const char * KeycodeToString(C4KeyCode code)
Definition: C4AppSDL.cpp:252
bool FlushMessages()
Definition: C4AppSDL.cpp:119
virtual ~C4AbstractApp()
Definition: C4AppSDL.cpp:87
bool Active
Definition: C4App.h:63
virtual bool DoScheduleProcs(int iTimeout)
Definition: C4App.cpp:59
virtual bool DoInit(int argc, char *argv[])=0
void Error(const char *m)
Definition: C4App.h:99
virtual bool Execute(int iTimeout=-1, pollfd *readyfds=0)=0
bool fQuitMsgReceived
Definition: C4App.h:81
virtual void Quit()
Definition: C4AppSDL.cpp:114
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:339
bool Copy(const std::string &text, bool fClipboard=true)
Definition: C4AppMac.mm:34
virtual void OnKeyboardLayoutChanged()=0
void RestoreVideoMode()
Definition: C4AppSDL.cpp:447
bool Init(int argc, char *argv[])
Definition: C4AppSDL.cpp:91
virtual void Clear()
Definition: C4AppSDL.cpp:109
void NotifyUserIfInactive()
Definition: C4App.cpp:89
std::string Paste(bool fClipboard=true)
Definition: C4AppMac.mm:47
bool fDspModeSet
Definition: C4App.h:157
const char * GetLastError()
Definition: C4App.h:98
std::string sLastError
Definition: C4App.h:156
uint32_t DWORD
C4Window * pWindow
Definition: C4App.h:80
unsigned long C4KeyCode
bool IsClipboardFull(bool fClipboard=true)
Definition: C4AppMac.mm:58