GHOST_ISystem.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software Foundation,
  14. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. *
  16. * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  17. * All rights reserved.
  18. */
  19. /** \file
  20. * \ingroup GHOST
  21. * %Main interface file for C++ Api with declaration of GHOST_ISystem interface
  22. * class.
  23. * Contains the doxygen documentation main page.
  24. */
  25. #ifndef __GHOST_ISYSTEM_H__
  26. #define __GHOST_ISYSTEM_H__
  27. #include "GHOST_Types.h"
  28. #include "GHOST_IContext.h"
  29. #include "GHOST_ITimerTask.h"
  30. #include "GHOST_IWindow.h"
  31. class GHOST_IEventConsumer;
  32. /**
  33. * \page GHOSTPage GHOST
  34. *
  35. * \section intro Introduction
  36. *
  37. * GHOST is yet another acronym. It stands for "Generic Handy Operating System
  38. * Toolkit". It has been created to replace the OpenGL utility tool kit
  39. * <a href="http://www.opengl.org/resources/libraries/glut/">GLUT</a>.
  40. * GLUT was used in <a href="http://www.blender3d.com">Blender</a> until the
  41. * point that Blender needed to be ported to Apple's Mac OSX. Blender needed a
  42. * number of modifications in GLUT to work but the GLUT sources for OSX were
  43. * unavailable at the time. The decision was made to build our own replacement
  44. * for GLUT. In those days, NaN Technologies BV was the company that developed
  45. * Blender.
  46. * <br><br>
  47. * Enough history. What does GHOST have to offer?<br>
  48. * In short: everything that Blender needed from GLUT to run on all it's supported
  49. * operating systems and some extra's.
  50. * This includes :
  51. *
  52. * - Time(r) management.
  53. * - Display/window management (windows are only created on the main display).
  54. * - Event management.
  55. * - Cursor shape management (no custom cursors for now).
  56. * - Access to the state of the mouse buttons and the keyboard.
  57. * - Menus for windows with events generated when they are accessed (this is
  58. * work in progress).
  59. * - Video mode switching.
  60. * - Copy/Paste buffers.
  61. * - System paths.
  62. *
  63. * Font management has been moved to a separate library.
  64. *
  65. * \section platforms Platforms
  66. *
  67. * GHOST supports the following platforms:
  68. *
  69. * - OSX Cocoa.
  70. * - Windows.
  71. * - X11.
  72. * - SDL2 (experimental).
  73. * - NULL (headless mode).
  74. *
  75. * \section Building GHOST
  76. *
  77. * GHOST is not build standalone however there are tests in intern/ghost/test
  78. *
  79. * \section interface Interface
  80. * GHOST has two programming interfaces:
  81. *
  82. * - The C-API. For programs written in C.
  83. * - The C++-API. For programs written in C++.
  84. *
  85. * GHOST itself is written in C++ and the C-API is a wrapper around the C++
  86. * API.
  87. *
  88. * \subsection cplusplus_api The C++ API consists of the following files:
  89. *
  90. * - GHOST_IEvent.h
  91. * - GHOST_IEventConsumer.h
  92. * - GHOST_ISystem.h
  93. * - GHOST_ITimerTask.h
  94. * - GHOST_IWindow.h
  95. * - GHOST_Rect.h
  96. * - GHOST_Types.h
  97. *
  98. * For an example of using the C++-API, have a look at the GHOST_C-Test.cpp
  99. * program in the ?/ghost/test/gears/ directory.
  100. *
  101. * \subsection c_api The C-API
  102. * To use GHOST in programs written in C, include the file GHOST_C-API.h in
  103. * your program. This file includes the GHOST_Types.h file for all GHOST types
  104. * and defines functions that give you access to the same functionality present
  105. * in the C++ API.<br>
  106. * For an example of using the C-API, have a look at the GHOST_C-Test.c program
  107. * in the ?/ghost/test/gears/ directory.
  108. *
  109. * \section work Work in progress
  110. * \todo write WIP section
  111. */
  112. /** \interface GHOST_ISystem
  113. * Interface for classes that provide access to the operating system.
  114. * There should be only one system class in an application.
  115. * Therefore, the routines to create and dispose the system are static.
  116. * Provides:
  117. * -# Time(r) management.
  118. * -# Display/window management (windows are only created on the main display).
  119. * -# Event management.
  120. * -# Cursor shape management (no custom cursors for now).
  121. * -# Access to the state of the mouse buttons and the keyboard.
  122. * -# Menus for windows with events generated when they are accessed (this is
  123. * work in progress).
  124. */
  125. class GHOST_ISystem {
  126. public:
  127. /**
  128. * Creates the one and only system.
  129. * \return An indication of success.
  130. */
  131. static GHOST_TSuccess createSystem();
  132. /**
  133. * Disposes the one and only system.
  134. * \return An indication of success.
  135. */
  136. static GHOST_TSuccess disposeSystem();
  137. /**
  138. * Returns a pointer to the one and only system (nil if it hasn't been created).
  139. * \return A pointer to the system.
  140. */
  141. static GHOST_ISystem *getSystem();
  142. protected:
  143. /**
  144. * Constructor.
  145. * Protected default constructor to force use of static createSystem member.
  146. */
  147. GHOST_ISystem()
  148. {
  149. }
  150. /**
  151. * Destructor.
  152. * Protected default constructor to force use of static dispose member.
  153. */
  154. virtual ~GHOST_ISystem()
  155. {
  156. }
  157. public:
  158. /***************************************************************************************
  159. * Time(r) functionality
  160. ***************************************************************************************/
  161. /**
  162. * Returns the system time.
  163. * Returns the number of milliseconds since the start of the system process.
  164. * Based on ANSI clock() routine.
  165. * \return The number of milliseconds.
  166. */
  167. virtual GHOST_TUns64 getMilliSeconds() const = 0;
  168. /**
  169. * Installs a timer.
  170. * Note that, on most operating systems, messages need to be processed in order
  171. * for the timer callbacks to be invoked.
  172. * \param delay The time to wait for the first call to the timerProc (in milliseconds)
  173. * \param interval The interval between calls to the timerProc (in milliseconds)
  174. * \param timerProc The callback invoked when the interval expires,
  175. * \param userData Placeholder for user data.
  176. * \return A timer task (0 if timer task installation failed).
  177. */
  178. virtual GHOST_ITimerTask *installTimer(GHOST_TUns64 delay,
  179. GHOST_TUns64 interval,
  180. GHOST_TimerProcPtr timerProc,
  181. GHOST_TUserDataPtr userData = NULL) = 0;
  182. /**
  183. * Removes a timer.
  184. * \param timerTask Timer task to be removed.
  185. * \return Indication of success.
  186. */
  187. virtual GHOST_TSuccess removeTimer(GHOST_ITimerTask *timerTask) = 0;
  188. /***************************************************************************************
  189. * Display/window management functionality
  190. ***************************************************************************************/
  191. /**
  192. * Returns the number of displays on this system.
  193. * \return The number of displays.
  194. */
  195. virtual GHOST_TUns8 getNumDisplays() const = 0;
  196. /**
  197. * Returns the dimensions of the main display on this system.
  198. * \return The dimension of the main display.
  199. */
  200. virtual void getMainDisplayDimensions(GHOST_TUns32 &width, GHOST_TUns32 &height) const = 0;
  201. /**
  202. * Returns the combine dimensions of all monitors.
  203. * \return The dimension of the workspace.
  204. */
  205. virtual void getAllDisplayDimensions(GHOST_TUns32 &width, GHOST_TUns32 &height) const = 0;
  206. /**
  207. * Create a new window.
  208. * The new window is added to the list of windows managed.
  209. * Never explicitly delete the window, use disposeWindow() instead.
  210. * \param title: The name of the window
  211. * (displayed in the title bar of the window if the OS supports it).
  212. * \param left: The coordinate of the left edge of the window.
  213. * \param top: The coordinate of the top edge of the window.
  214. * \param width: The width the window.
  215. * \param height: The height the window.
  216. * \param state: The state of the window when opened.
  217. * \param type: The type of drawing context installed in this window.
  218. * \param glSettings: Misc OpenGL settings.
  219. * \param exclusive: Use to show the window on top and ignore others (used fullscreen).
  220. * \param parentWindow: Parent (embedder) window
  221. * \return The new window (or 0 if creation failed).
  222. */
  223. virtual GHOST_IWindow *createWindow(const STR_String &title,
  224. GHOST_TInt32 left,
  225. GHOST_TInt32 top,
  226. GHOST_TUns32 width,
  227. GHOST_TUns32 height,
  228. GHOST_TWindowState state,
  229. GHOST_TDrawingContextType type,
  230. GHOST_GLSettings glSettings,
  231. const bool exclusive = false,
  232. const GHOST_TEmbedderWindowID parentWindow = 0) = 0;
  233. /**
  234. * Dispose a window.
  235. * \param window Pointer to the window to be disposed.
  236. * \return Indication of success.
  237. */
  238. virtual GHOST_TSuccess disposeWindow(GHOST_IWindow *window) = 0;
  239. /**
  240. * Create a new offscreen context.
  241. * Never explicitly delete the context, use disposeContext() instead.
  242. * \return The new context (or 0 if creation failed).
  243. */
  244. virtual GHOST_IContext *createOffscreenContext() = 0;
  245. /**
  246. * Dispose of a context.
  247. * \param context Pointer to the context to be disposed.
  248. * \return Indication of success.
  249. */
  250. virtual GHOST_TSuccess disposeContext(GHOST_IContext *context) = 0;
  251. /**
  252. * Returns whether a window is valid.
  253. * \param window Pointer to the window to be checked.
  254. * \return Indication of validity.
  255. */
  256. virtual bool validWindow(GHOST_IWindow *window) = 0;
  257. /**
  258. * Begins full screen mode.
  259. * \param setting The new setting of the display.
  260. * \param window Window displayed in full screen.
  261. * This window is invalid after full screen has been ended.
  262. * \return Indication of success.
  263. */
  264. virtual GHOST_TSuccess beginFullScreen(const GHOST_DisplaySetting &setting,
  265. GHOST_IWindow **window,
  266. const bool stereoVisual,
  267. const bool alphaBackground = 0) = 0;
  268. /**
  269. * Updates the resolution while in fullscreen mode.
  270. * \param setting The new setting of the display.
  271. * \param window Window displayed in full screen.
  272. *
  273. * \return Indication of success.
  274. */
  275. virtual GHOST_TSuccess updateFullScreen(const GHOST_DisplaySetting &setting,
  276. GHOST_IWindow **window) = 0;
  277. /**
  278. * Ends full screen mode.
  279. * \return Indication of success.
  280. */
  281. virtual GHOST_TSuccess endFullScreen(void) = 0;
  282. /**
  283. * Returns current full screen mode status.
  284. * \return The current status.
  285. */
  286. virtual bool getFullScreen(void) = 0;
  287. /**
  288. * Native pixel size support (MacBook 'retina').
  289. */
  290. virtual bool useNativePixel(void) = 0;
  291. /**
  292. * Focus window after opening, or put them in the background.
  293. */
  294. virtual void useWindowFocus(const bool use_focus) = 0;
  295. /***************************************************************************************
  296. * Event management functionality
  297. ***************************************************************************************/
  298. /**
  299. * Retrieves events from the system and stores them in the queue.
  300. * \param waitForEvent Flag to wait for an event (or return immediately).
  301. * \return Indication of the presence of events.
  302. */
  303. virtual bool processEvents(bool waitForEvent) = 0;
  304. /**
  305. * Retrieves events from the queue and send them to the event consumers.
  306. */
  307. virtual void dispatchEvents() = 0;
  308. /**
  309. * Adds the given event consumer to our list.
  310. * \param consumer The event consumer to add.
  311. * \return Indication of success.
  312. */
  313. virtual GHOST_TSuccess addEventConsumer(GHOST_IEventConsumer *consumer) = 0;
  314. /**
  315. * Removes the given event consumer to our list.
  316. * \param consumer The event consumer to remove.
  317. * \return Indication of success.
  318. */
  319. virtual GHOST_TSuccess removeEventConsumer(GHOST_IEventConsumer *consumer) = 0;
  320. /***************************************************************************************
  321. * Cursor management functionality
  322. ***************************************************************************************/
  323. /**
  324. * Returns the current location of the cursor (location in screen coordinates)
  325. * \param x The x-coordinate of the cursor.
  326. * \param y The y-coordinate of the cursor.
  327. * \return Indication of success.
  328. */
  329. virtual GHOST_TSuccess getCursorPosition(GHOST_TInt32 &x, GHOST_TInt32 &y) const = 0;
  330. /**
  331. * Updates the location of the cursor (location in screen coordinates).
  332. * Not all operating systems allow the cursor to be moved (without the input device being moved).
  333. * \param x The x-coordinate of the cursor.
  334. * \param y The y-coordinate of the cursor.
  335. * \return Indication of success.
  336. */
  337. virtual GHOST_TSuccess setCursorPosition(GHOST_TInt32 x, GHOST_TInt32 y) = 0;
  338. /***************************************************************************************
  339. * Access to mouse button and keyboard states.
  340. ***************************************************************************************/
  341. /**
  342. * Returns the state of a modifier key (ouside the message queue).
  343. * \param mask The modifier key state to retrieve.
  344. * \param isDown The state of a modifier key (true == pressed).
  345. * \return Indication of success.
  346. */
  347. virtual GHOST_TSuccess getModifierKeyState(GHOST_TModifierKeyMask mask, bool &isDown) const = 0;
  348. /**
  349. * Returns the state of a mouse button (ouside the message queue).
  350. * \param mask The button state to retrieve.
  351. * \param isDown Button state.
  352. * \return Indication of success.
  353. */
  354. virtual GHOST_TSuccess getButtonState(GHOST_TButtonMask mask, bool &isDown) const = 0;
  355. /**
  356. * Set which tablet API to use. Only affects Windows, other platforms have a single API.
  357. * \param api Enum indicating which API to use.
  358. */
  359. virtual void setTabletAPI(GHOST_TTabletAPI api) = 0;
  360. #ifdef WITH_INPUT_NDOF
  361. /**
  362. * Sets 3D mouse deadzone
  363. * \param deadzone: Deadzone of the 3D mouse (both for rotation and pan) relative to full range
  364. */
  365. virtual void setNDOFDeadZone(float deadzone) = 0;
  366. #endif
  367. /**
  368. * Toggles console
  369. * \param action
  370. * - 0: Hides
  371. * - 1: Shows
  372. * - 2: Toggles
  373. * - 3: Hides if it runs not from command line
  374. * - *: Does nothing
  375. * \return current status (1 -visible, 0 - hidden)
  376. */
  377. virtual int toggleConsole(int action) = 0;
  378. /***************************************************************************************
  379. * Access to clipboard.
  380. ***************************************************************************************/
  381. /**
  382. * Returns the selection buffer
  383. * \return "unsigned char" from X11 XA_CUT_BUFFER0 buffer
  384. *
  385. */
  386. virtual GHOST_TUns8 *getClipboard(bool selection) const = 0;
  387. /**
  388. * Put data to the Clipboard
  389. */
  390. virtual void putClipboard(GHOST_TInt8 *buffer, bool selection) const = 0;
  391. protected:
  392. /**
  393. * Initialize the system.
  394. * \return Indication of success.
  395. */
  396. virtual GHOST_TSuccess init() = 0;
  397. /**
  398. * Shut the system down.
  399. * \return Indication of success.
  400. */
  401. virtual GHOST_TSuccess exit() = 0;
  402. /** The one and only system */
  403. static GHOST_ISystem *m_system;
  404. #ifdef WITH_CXX_GUARDEDALLOC
  405. MEM_CXX_CLASS_ALLOC_FUNCS("GHOST:GHOST_ISystem")
  406. #endif
  407. };
  408. #endif // __GHOST_ISYSTEM_H__