1 /*
2 	This file is part of Warzone 2100.
3 	Copyright (C) 1999-2004  Eidos Interactive
4 	Copyright (C) 2005-2020  Warzone 2100 Project
5 
6 	Warzone 2100 is free software; you can redistribute it and/or modify
7 	it under the terms of the GNU General Public License as published by
8 	the Free Software Foundation; either version 2 of the License, or
9 	(at your option) any later version.
10 
11 	Warzone 2100 is distributed in the hope that it will be useful,
12 	but WITHOUT ANY WARRANTY; without even the implied warranty of
13 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 	GNU General Public License for more details.
15 
16 	You should have received a copy of the GNU General Public License
17 	along with Warzone 2100; if not, write to the Free Software
18 	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 #ifndef __INCLUDED_WZAPP_C_H__
22 #define __INCLUDED_WZAPP_C_H__
23 
24 #include "frame.h"
25 #include "wzstring.h"
26 #include <vector>
27 #include <functional>
28 #include <optional-lite/optional.hpp>
29 using nonstd::optional;
30 using nonstd::nullopt;
31 
32 struct WZ_THREAD;
33 struct WZ_MUTEX;
34 struct WZ_SEMAPHORE;
35 
36 class WZ_MAINTHREADEXEC
37 {
38 public:
WZ_MAINTHREADEXEC()39 	WZ_MAINTHREADEXEC() { }
~WZ_MAINTHREADEXEC()40 	virtual ~WZ_MAINTHREADEXEC() { };
41 
42 	// subclass should override this
doExecOnMainThread()43 	virtual void doExecOnMainThread() { };
44 };
45 
46 class WZ_MAINTHREADEXECFUNC: public WZ_MAINTHREADEXEC
47 {
48 public:
49 	typedef std::function<void ()> execFuncType;
50 public:
WZ_MAINTHREADEXECFUNC(const execFuncType & execFunc)51 	WZ_MAINTHREADEXECFUNC(const execFuncType &execFunc)
52 	: execFunc(execFunc)
53 	{ }
~WZ_MAINTHREADEXECFUNC()54 	virtual ~WZ_MAINTHREADEXECFUNC() { };
55 
doExecOnMainThread()56 	void doExecOnMainThread()
57 	{
58 		execFunc();
59 	};
60 private:
61 	execFuncType execFunc;
62 };
63 
64 struct screeninfo
65 {
66 	int width;
67 	int height;
68 	int refresh_rate;
69 	int screen;
70 };
71 
72 void wzMain(int &argc, char **argv);
73 bool wzMainScreenSetup(optional<video_backend> backend, int antialiasing = 0, WINDOW_MODE fullscreen = WINDOW_MODE::windowed, int vsync = 1, bool highDPI = true);
74 video_backend wzGetDefaultGfxBackendForCurrentSystem();
75 void wzGetGameToRendererScaleFactor(float *horizScaleFactor, float *vertScaleFactor);
76 void wzMainEventLoop();
77 void wzPumpEventsWhileLoading();
78 void wzQuit();              ///< Quit game
79 void wzShutdown();
80 std::vector<WINDOW_MODE> wzSupportedWindowModes();
81 WINDOW_MODE wzGetNextWindowMode(WINDOW_MODE currentMode);
82 WINDOW_MODE wzAltEnterToggleFullscreen();
83 bool wzChangeWindowMode(WINDOW_MODE mode);
84 WINDOW_MODE wzGetCurrentWindowMode();
85 bool wzIsFullscreen();
86 void wzSetWindowIsResizable(bool resizable);
87 bool wzIsWindowResizable();
88 bool wzChangeDisplayScale(unsigned int displayScale);
89 bool wzChangeWindowResolution(int screen, unsigned int width, unsigned int height);
90 unsigned int wzGetMaximumDisplayScaleForWindowSize(unsigned int windowWidth, unsigned int windowHeight);
91 unsigned int wzGetMaximumDisplayScaleForCurrentWindowSize();
92 unsigned int wzGetSuggestedDisplayScaleForCurrentWindowSize(unsigned int desiredMaxScreenDimension);
93 unsigned int wzGetCurrentDisplayScale();
94 void wzGetWindowResolution(int *screen, unsigned int *width, unsigned int *height);
95 bool wzSetClipboardText(const char *text);
96 void wzSetCursor(CURSOR index);
97 void wzApplyCursor();
98 void wzShowMouse(bool visible); ///< Show the Mouse?
99 void wzGrabMouse();		///< Trap mouse cursor in application window
100 void wzReleaseMouse();	///< Undo the wzGrabMouse operation
101 int wzGetTicks();		///< Milliseconds since start of game
102 enum DialogType {
103 	Dialog_Error,
104 	Dialog_Warning,
105 	Dialog_Information
106 };
107 WZ_DECL_NONNULL(2, 3) void wzDisplayDialog(DialogType type, const char *title, const char *message);	///< Throw up a modal warning dialog - title & message are UTF-8 text
108 
109 WzString wzGetPlatform();
110 std::vector<screeninfo> wzAvailableResolutions();
111 std::vector<unsigned int> wzAvailableDisplayScales();
112 std::vector<video_backend> wzAvailableGfxBackends();
113 WzString wzGetSelection();
114 unsigned int wzGetCurrentKey();
115 void wzDelay(unsigned int delay);	//delay in ms
116 // unicode text support
117 void StartTextInput(void* pTextInputRequester);
118 void StopTextInput(void* pTextInputResigner);
119 bool isInTextInputMode();
120 // Thread related
121 WZ_THREAD *wzThreadCreate(int (*threadFunc)(void *), void *data);
122 unsigned long wzThreadID(WZ_THREAD *thread);
123 WZ_DECL_NONNULL(1) int wzThreadJoin(WZ_THREAD *thread);
124 WZ_DECL_NONNULL(1) void wzThreadDetach(WZ_THREAD *thread);
125 WZ_DECL_NONNULL(1) void wzThreadStart(WZ_THREAD *thread);
126 void wzYieldCurrentThread();
127 WZ_MUTEX *wzMutexCreate();
128 WZ_DECL_NONNULL(1) void wzMutexDestroy(WZ_MUTEX *mutex);
129 WZ_DECL_NONNULL(1) void wzMutexLock(WZ_MUTEX *mutex);
130 WZ_DECL_NONNULL(1) void wzMutexUnlock(WZ_MUTEX *mutex);
131 WZ_SEMAPHORE *wzSemaphoreCreate(int startValue);
132 WZ_DECL_NONNULL(1) void wzSemaphoreDestroy(WZ_SEMAPHORE *semaphore);
133 WZ_DECL_NONNULL(1) void wzSemaphoreWait(WZ_SEMAPHORE *semaphore);
134 WZ_DECL_NONNULL(1) void wzSemaphorePost(WZ_SEMAPHORE *semaphore);
135 WZ_DECL_NONNULL(1) void wzAsyncExecOnMainThread(WZ_MAINTHREADEXEC *exec);
136 
137 // Asynchronously executes execFunc() on the main thread.
138 // This function must be thread-safe, and may be safely called from any thread.
139 //
140 // No guarantees are made about when execFunc() will be called relative to the
141 // calling of this function - this function may return before, during, or after
142 // execFunc()'s execution on the main thread.
wzAsyncExecOnMainThread(const std::function<void ()> & execFunc)143 inline void wzAsyncExecOnMainThread(const std::function<void ()> &execFunc)
144 {
145 	wzAsyncExecOnMainThread(new WZ_MAINTHREADEXECFUNC(execFunc));
146 	// receiver handles deleting the parameter on the main thread after doExecOnMainThread() has been called
147 }
148 
149 #if !defined(WZ_CC_MINGW)
150 
151 #include <mutex>
152 #include <future>
153 
154 namespace wz
155 {
156 	using mutex = std::mutex;
157 	template <typename R>
158 	using future = std::future<R>;
159 	template <typename RA>
160 	using packaged_task = std::packaged_task<RA>;
161 	using thread = std::thread;
162 }
163 
164 #else  // Workaround for cross-compiler without std::mutex.
165 
166 #include <memory>
167 #include <functional>
168 
169 namespace wz
170 {
171 	class mutex
172 	{
173 	public:
mutex()174 		mutex() : handle(wzMutexCreate()) {}
~mutex()175 		~mutex() { wzMutexDestroy(handle); }
176 
177 		mutex(mutex const &) = delete;
178 		mutex &operator =(mutex const &) = delete;
179 
lock()180 		void lock() { wzMutexLock(handle); }
181 		//bool try_lock();
unlock()182 		void unlock() { wzMutexUnlock(handle); }
183 
184 	private:
185 		WZ_MUTEX *handle;
186 	};
187 
188 	template <typename R>
189 	class future
190 	{
191 	public:
192 		future() = default;
future(future && other)193 		future(future &&other) : internal(std::move(other.internal)) {}
194 		future(future const &) = delete;
195 		future &operator =(future &&other) = default;
196 		future &operator =(future const &) = delete;
197 		//std::shared_future<T> share();
get()198 		R get() { auto &data = *internal; wzSemaphoreWait(data.sem); return std::move(data.ret); }
199 		//valid(), wait*();
200 
201 		struct Internal  // Should really be a promise.
202 		{
InternalInternal203 			Internal() : sem(wzSemaphoreCreate(0)) {}
~InternalInternal204 			~Internal() { wzSemaphoreDestroy(sem); }
205 			R ret;
206 			WZ_SEMAPHORE *sem;
207 		};
208 
209 		std::shared_ptr<Internal> internal;
210 	};
211 
212 	template <typename RA>
213 	class packaged_task;
214 
215 	template <typename R, typename... A>
216 	class packaged_task<R (A...)>
217 	{
218 	public:
219 		packaged_task() = default;
220 		template <typename F>
packaged_task(F && f)221 		explicit packaged_task(F &&f) { function = std::move(f); internal = std::make_shared<typename future<R>::Internal>(); }
222 		packaged_task(packaged_task &&) = default;
223 		packaged_task(packaged_task const &) = delete;
224 
get_future()225 		future<R> get_future() { future<R> future; future.internal = internal; return std::move(future); }
operator()226 		void operator ()(A &&... args) { auto &data = *internal; data.ret = function(std::forward<A>(args)...); wzSemaphorePost(data.sem); }
227 
228 	private:
229 		std::function<R (A...)> function;
230 		std::shared_ptr<typename future<R>::Internal> internal;
231 	};
232 
233 	class thread
234 	{
235 	public:
thread()236 		thread() : internal(nullptr) {}
thread(thread && other)237 		thread(thread &&other) : internal(other.internal) { other.internal = nullptr; }
238 		template <typename Function, typename... Args>
thread(Function && f,Args &&...args)239 		explicit thread(Function &&f, Args &&...args) : internal(wzThreadCreate([](void *vf) { auto F = (std::function<void ()> *)vf; (*F)(); delete F; return 0; }, new std::function<void ()>(std::bind(std::forward<Function>(f), std::forward<Args>(args)...)))) { wzThreadStart(internal); }
240 		thread(thread const &) = delete;
~thread()241 		~thread() { if (internal) { std::terminate(); } }
242 		thread &operator =(thread &&other) { std::swap(internal, other.internal); return *this; }
join()243 		void join() { if (!internal) { std::terminate(); } wzThreadJoin(internal); internal = nullptr; }
detach()244 		void detach() { if (!internal) { std::terminate(); } wzThreadDetach(internal); internal = nullptr; }
245 
246 	private:
247 		WZ_THREAD *internal;
248 	};
249 }
250 
251 #endif
252 
253 #endif
254