os_windows.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*************************************************************************/
  2. /* os_windows.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef OS_WINDOWS_H
  30. #define OS_WINDOWS_H
  31. #define WINVER 0x0500
  32. #include "os/input.h"
  33. #include "os/os.h"
  34. #include "context_gl_win.h"
  35. #include "servers/visual_server.h"
  36. #include "servers/visual/rasterizer.h"
  37. #include "servers/physics/physics_server_sw.h"
  38. #include "servers/audio/audio_server_sw.h"
  39. #include "servers/audio/sample_manager_sw.h"
  40. #include "drivers/rtaudio/audio_driver_rtaudio.h"
  41. #include "servers/spatial_sound/spatial_sound_server_sw.h"
  42. #include "servers/spatial_sound_2d/spatial_sound_2d_server_sw.h"
  43. #include "drivers/unix/ip_unix.h"
  44. #include "servers/physics_2d/physics_2d_server_sw.h"
  45. #include <windows.h>
  46. #include "key_mapping_win.h"
  47. #include <windowsx.h>
  48. #include <io.h>
  49. #include <fcntl.h>
  50. #include <stdio.h>
  51. /**
  52. @author Juan Linietsky <reduzio@gmail.com>
  53. */
  54. class OS_Windows : public OS {
  55. enum {
  56. JOYSTICKS_MAX = 8,
  57. JOY_AXIS_COUNT = 6,
  58. MAX_JOY_AXIS = 32768, // I've no idea
  59. KEY_EVENT_BUFFER_SIZE=512
  60. };
  61. FILE *stdo;
  62. struct KeyEvent {
  63. InputModifierState mod_state;
  64. UINT uMsg;
  65. WPARAM wParam;
  66. LPARAM lParam;
  67. };
  68. KeyEvent key_event_buffer[KEY_EVENT_BUFFER_SIZE];
  69. int key_event_pos;
  70. uint64_t ticks_start;
  71. uint64_t ticks_per_second;
  72. bool old_invalid;
  73. bool outside;
  74. int old_x,old_y;
  75. Point2i center;
  76. unsigned int last_id;
  77. #if defined(OPENGL_ENABLED) || defined(LEGACYGL_ENABLED) || defined(GLES2_ENABLED)
  78. ContextGL_Win *gl_context;
  79. #endif
  80. VisualServer *visual_server;
  81. Rasterizer *rasterizer;
  82. PhysicsServer *physics_server;
  83. Physics2DServer *physics_2d_server;
  84. int pressrc;
  85. HDC hDC; // Private GDI Device Context
  86. HINSTANCE hInstance; // Holds The Instance Of The Application
  87. HWND hWnd;
  88. struct Joystick {
  89. int id;
  90. bool attached;
  91. DWORD last_axis[JOY_AXIS_COUNT];
  92. DWORD last_buttons;
  93. DWORD last_pov;
  94. String name;
  95. Joystick() {
  96. id = -1;
  97. attached = false;
  98. for (int i=0; i<JOY_AXIS_COUNT; i++) {
  99. last_axis[i] = 0;
  100. };
  101. last_buttons = 0;
  102. last_pov = 0;
  103. };
  104. };
  105. List<Joystick> joystick_change_queue;
  106. int joystick_count;
  107. Joystick joysticks[JOYSTICKS_MAX];
  108. Size2 window_rect;
  109. VideoMode video_mode;
  110. MainLoop *main_loop;
  111. WNDPROC user_proc;
  112. AudioServerSW *audio_server;
  113. SampleManagerMallocSW *sample_manager;
  114. SpatialSoundServerSW *spatial_sound_server;
  115. SpatialSound2DServerSW *spatial_sound_2d_server;
  116. MouseMode mouse_mode;
  117. bool alt_mem;
  118. bool gr_mem;
  119. bool shift_mem;
  120. bool control_mem;
  121. bool meta_mem;
  122. bool force_quit;
  123. uint32_t last_button_state;
  124. CursorShape cursor_shape;
  125. InputDefault *input;
  126. #ifdef RTAUDIO_ENABLED
  127. AudioDriverRtAudio driver_rtaudio;
  128. #endif
  129. void _post_dpad(DWORD p_dpad, int p_device, bool p_pressed);
  130. void _drag_event(int p_x, int p_y, int idx);
  131. void _touch_event(bool p_pressed, int p_x, int p_y, int idx);
  132. // functions used by main to initialize/deintialize the OS
  133. protected:
  134. virtual int get_video_driver_count() const;
  135. virtual const char * get_video_driver_name(int p_driver) const;
  136. virtual VideoMode get_default_video_mode() const;
  137. virtual int get_audio_driver_count() const;
  138. virtual const char * get_audio_driver_name(int p_driver) const;
  139. virtual void initialize_core();
  140. virtual void initialize(const VideoMode& p_desired,int p_video_driver,int p_audio_driver);
  141. virtual void set_main_loop( MainLoop * p_main_loop );
  142. virtual void delete_main_loop();
  143. virtual void finalize();
  144. virtual void finalize_core();
  145. void process_events();
  146. void probe_joysticks();
  147. void process_joysticks();
  148. void process_key_events();
  149. String get_joystick_name( int id, JOYCAPS jcaps);
  150. struct ProcessInfo {
  151. STARTUPINFO si;
  152. PROCESS_INFORMATION pi;
  153. };
  154. Map<ProcessID, ProcessInfo>* process_map;
  155. struct MonitorInfo {
  156. HMONITOR hMonitor;
  157. HDC hdcMonitor;
  158. Rect2 rect;
  159. };
  160. bool pre_fs_valid;
  161. RECT pre_fs_rect;
  162. Vector<MonitorInfo> monitor_info;
  163. bool maximized;
  164. bool minimized;
  165. static BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData);
  166. public:
  167. LRESULT WndProc(HWND hWnd,UINT uMsg, WPARAM wParam, LPARAM lParam);
  168. void print_error(const char* p_function,const char* p_file,int p_line,const char *p_code,const char*p_rationale,ErrorType p_type);
  169. virtual void vprint(const char *p_format, va_list p_list, bool p_stderr=false);
  170. virtual void alert(const String& p_alert,const String& p_title="ALERT!");
  171. String get_stdin_string(bool p_block);
  172. void set_mouse_mode(MouseMode p_mode);
  173. MouseMode get_mouse_mode() const;
  174. virtual void warp_mouse_pos(const Point2& p_to);
  175. virtual Point2 get_mouse_pos() const;
  176. virtual int get_mouse_button_state() const;
  177. virtual void set_window_title(const String& p_title);
  178. virtual void set_video_mode(const VideoMode& p_video_mode,int p_screen=0);
  179. virtual VideoMode get_video_mode(int p_screen=0) const;
  180. virtual void get_fullscreen_mode_list(List<VideoMode> *p_list,int p_screen=0) const;
  181. virtual int get_screen_count() const;
  182. virtual int get_current_screen() const;
  183. virtual void set_current_screen(int p_screen);
  184. virtual Point2 get_screen_position(int p_screen=0) const;
  185. virtual Size2 get_screen_size(int p_screen=0) const;
  186. virtual Point2 get_window_position() const;
  187. virtual void set_window_position(const Point2& p_position);
  188. virtual Size2 get_window_size() const;
  189. virtual void set_window_size(const Size2 p_size);
  190. virtual void set_window_fullscreen(bool p_enabled);
  191. virtual bool is_window_fullscreen() const;
  192. virtual void set_window_resizable(bool p_enabled);
  193. virtual bool is_window_resizable() const;
  194. virtual void set_window_minimized(bool p_enabled);
  195. virtual bool is_window_minimized() const;
  196. virtual void set_window_maximized(bool p_enabled);
  197. virtual bool is_window_maximized() const;
  198. virtual MainLoop *get_main_loop() const;
  199. virtual String get_name();
  200. virtual Date get_date() const;
  201. virtual Time get_time() const;
  202. virtual uint64_t get_unix_time() const;
  203. virtual bool can_draw() const;
  204. virtual Error set_cwd(const String& p_cwd);
  205. virtual void delay_usec(uint32_t p_usec) const;
  206. virtual uint64_t get_ticks_usec() const;
  207. virtual Error execute(const String& p_path, const List<String>& p_arguments,bool p_blocking,ProcessID *r_child_id=NULL,String* r_pipe=NULL,int *r_exitcode=NULL);
  208. virtual Error kill(const ProcessID& p_pid);
  209. virtual bool has_environment(const String& p_var) const;
  210. virtual String get_environment(const String& p_var) const;
  211. virtual void set_clipboard(const String& p_text);
  212. virtual String get_clipboard() const;
  213. void set_cursor_shape(CursorShape p_shape);
  214. void set_icon(const Image& p_icon);
  215. virtual String get_executable_path() const;
  216. virtual String get_locale() const;
  217. virtual void move_window_to_foreground();
  218. virtual String get_data_dir() const;
  219. virtual String get_system_dir(SystemDir p_dir) const;
  220. virtual void release_rendering_thread();
  221. virtual void make_rendering_thread();
  222. virtual void swap_buffers();
  223. virtual Error shell_open(String p_uri);
  224. void run();
  225. virtual bool get_swap_ok_cancel() { return true; }
  226. OS_Windows(HINSTANCE _hInstance);
  227. ~OS_Windows();
  228. };
  229. #endif