ShellMacOSX.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "Shell.h"
  29. #include <RmlUi/Core.h>
  30. #include "ShellFileInterface.h"
  31. #include "macosx/InputMacOSX.h"
  32. #include <Carbon/Carbon.h>
  33. #include <AGL/agl.h>
  34. #include <sys/time.h>
  35. #include <stdio.h>
  36. #include <stdarg.h>
  37. static const EventTypeSpec INPUT_EVENTS[] = {
  38. { kEventClassKeyboard, kEventRawKeyDown },
  39. { kEventClassKeyboard, kEventRawKeyUp },
  40. { kEventClassKeyboard, kEventRawKeyModifiersChanged },
  41. { kEventClassMouse, kEventMouseDown },
  42. { kEventClassMouse, kEventMouseUp },
  43. { kEventClassMouse, kEventMouseMoved },
  44. { kEventClassMouse, kEventMouseDragged },
  45. { kEventClassMouse, kEventMouseWheelMoved },
  46. };
  47. static const EventTypeSpec WINDOW_EVENTS[] = {
  48. { kEventClassWindow, kEventWindowClose },
  49. { kEventClassWindow, kEventWindowBoundsChanged },
  50. };
  51. static WindowRef window;
  52. static timeval start_time;
  53. static Rml::Core::String clipboard_text;
  54. static std::unique_ptr<ShellFileInterface> file_interface;
  55. static void IdleTimerCallback(EventLoopTimerRef timer, EventLoopIdleTimerMessage inState, void* p);
  56. static OSStatus EventHandler(EventHandlerCallRef next_handler, EventRef event, void* p);
  57. bool Shell::Initialise()
  58. {
  59. gettimeofday(&start_time, nullptr);
  60. InputMacOSX::Initialise();
  61. Rml::Core::String root = FindSamplesRoot();
  62. file_interface = std::make_unique<ShellFileInterface>(root);
  63. Rml::Core::SetFileInterface(file_interface.get());
  64. return true;
  65. }
  66. void Shell::Shutdown()
  67. {
  68. file_interface.reset();
  69. }
  70. Rml::Core::String Shell::FindSamplesRoot()
  71. {
  72. Rml::Core::String path = "../../Samples/";
  73. // Find the location of the executable.
  74. CFBundleRef bundle = CFBundleGetMainBundle();
  75. CFURLRef executable_url = CFBundleCopyExecutableURL(bundle);
  76. CFStringRef executable_posix_file_name = CFURLCopyFileSystemPath(executable_url, kCFURLPOSIXPathStyle);
  77. CFIndex max_length = CFStringGetMaximumSizeOfFileSystemRepresentation(executable_posix_file_name);
  78. char* executable_file_name = new char[max_length];
  79. if (!CFStringGetFileSystemRepresentation(executable_posix_file_name, executable_file_name, max_length))
  80. executable_file_name[0] = 0;
  81. Rml::Core::String executable_path = Rml::Core::String(executable_file_name);
  82. executable_path = executable_path.substr(0, executable_path.rfind("/") + 1);
  83. delete[] executable_file_name;
  84. CFRelease(executable_posix_file_name);
  85. CFRelease(executable_url);
  86. return (executable_path + "../../../" + path);
  87. }
  88. static ShellRenderInterfaceExtensions *shell_renderer;
  89. bool Shell::OpenWindow(const char* name, ShellRenderInterfaceExtensions *_shell_renderer, unsigned int width, unsigned int height, bool allow_resize)
  90. {
  91. shell_renderer = _shell_renderer;
  92. Rect content_bounds = { 60, 20, 60 + height, 20 + width };
  93. OSStatus result = CreateNewWindow(kDocumentWindowClass,
  94. (allow_resize ? (kWindowStandardDocumentAttributes | kWindowLiveResizeAttribute) :
  95. kWindowCloseBoxAttribute) | kWindowStandardHandlerAttribute,
  96. &content_bounds,
  97. &window);
  98. if (result != noErr)
  99. return false;
  100. CFStringRef window_title = CFStringCreateWithCString(nullptr, name, kCFStringEncodingUTF8);
  101. if (result != noErr)
  102. return false;
  103. result = SetWindowTitleWithCFString(window, window_title);
  104. if (result != noErr)
  105. {
  106. CFRelease(window_title);
  107. return false;
  108. }
  109. CFRelease(window_title);
  110. ShowWindow(window);
  111. if(shell_renderer != nullptr) {
  112. shell_renderer->AttachToNative(window);
  113. }
  114. return true;
  115. }
  116. void Shell::CloseWindow()
  117. {
  118. if(shell_renderer) {
  119. shell_renderer->DetachFromNative();
  120. }
  121. // Close the window.
  122. HideWindow(window);
  123. ReleaseWindow(window);
  124. }
  125. void Shell::EventLoop(ShellIdleFunction idle_function)
  126. {
  127. OSStatus error;
  128. error = InstallApplicationEventHandler(NewEventHandlerUPP(InputMacOSX::EventHandler),
  129. GetEventTypeCount(INPUT_EVENTS),
  130. INPUT_EVENTS,
  131. nullptr,
  132. nullptr);
  133. if (error != noErr)
  134. DisplayError("Unable to install handler for input events, error: %d.", error);
  135. error = InstallWindowEventHandler(window,
  136. NewEventHandlerUPP(EventHandler),
  137. GetEventTypeCount(WINDOW_EVENTS),
  138. WINDOW_EVENTS,
  139. nullptr,
  140. nullptr);
  141. if (error != noErr)
  142. DisplayError("Unable to install handler for window events, error: %d.", error);
  143. EventLoopTimerRef timer;
  144. error = InstallEventLoopIdleTimer(GetMainEventLoop(), // inEventLoop
  145. 0, // inFireDelay
  146. 5 * kEventDurationMillisecond, // inInterval (200 Hz)
  147. NewEventLoopIdleTimerUPP(IdleTimerCallback), // inTimerProc
  148. (void*) idle_function, // inTimerData,
  149. &timer // outTimer
  150. );
  151. if (error != noErr)
  152. DisplayError("Unable to install Carbon event loop timer, error: %d.", error);
  153. RunApplicationEventLoop();
  154. }
  155. void Shell::RequestExit()
  156. {
  157. EventRef event;
  158. OSStatus result = CreateEvent(nullptr, // default allocator
  159. kEventClassApplication,
  160. kEventAppQuit,
  161. 0,
  162. kEventAttributeNone,
  163. &event);
  164. if (result == noErr)
  165. PostEventToQueue(GetMainEventQueue(), event, kEventPriorityStandard);
  166. }
  167. void Shell::DisplayError(const char* fmt, ...)
  168. {
  169. const int buffer_size = 1024;
  170. char buffer[buffer_size];
  171. va_list argument_list;
  172. // Print the message to the buffer.
  173. va_start(argument_list, fmt);
  174. int len = vsnprintf(buffer, buffer_size - 2, fmt, argument_list);
  175. if (len < 0 || len > buffer_size - 2)
  176. {
  177. len = buffer_size - 2;
  178. }
  179. buffer[len] = '\n';
  180. buffer[len + 1] = '\0';
  181. va_end(argument_list);
  182. fprintf(stderr, "%s", buffer);
  183. }
  184. void Shell::Log(const char* fmt, ...)
  185. {
  186. const int buffer_size = 1024;
  187. char buffer[buffer_size];
  188. va_list argument_list;
  189. // Print the message to the buffer.
  190. va_start(argument_list, fmt);
  191. int len = vsnprintf(buffer, buffer_size - 2, fmt, argument_list);
  192. if ( len < 0 || len > buffer_size - 2 )
  193. {
  194. len = buffer_size - 2;
  195. }
  196. buffer[len] = '\n';
  197. buffer[len + 1] = '\0';
  198. va_end(argument_list);
  199. printf("%s", buffer);
  200. }
  201. double Shell::GetElapsedTime()
  202. {
  203. struct timeval now;
  204. gettimeofday(&now, nullptr);
  205. double sec = now.tv_sec - start_time.tv_sec;
  206. double usec = now.tv_usec;
  207. double result = sec + (usec / 1000000.0);
  208. return result;
  209. }
  210. void Shell::SetMouseCursor(const Rml::Core::String& cursor_name)
  211. {
  212. // Not implemented
  213. }
  214. void Shell::SetClipboardText(const Rml::Core::String& text)
  215. {
  216. // Todo: interface with system clipboard
  217. clipboard_text = text;
  218. }
  219. void Shell::GetClipboardText(Rml::Core::String& text)
  220. {
  221. // Todo: interface with system clipboard
  222. text = clipboard_text;
  223. }
  224. static void IdleTimerCallback(EventLoopTimerRef timer, EventLoopIdleTimerMessage inState, void* p)
  225. {
  226. Shell::ShellIdleFunction function = (Shell::ShellIdleFunction) p;
  227. function();
  228. }
  229. static OSStatus EventHandler(EventHandlerCallRef next_handler, EventRef event, void* p)
  230. {
  231. switch (GetEventClass(event))
  232. {
  233. case kEventClassWindow:
  234. {
  235. switch (GetEventKind(event))
  236. {
  237. case kEventWindowClose:
  238. Shell::RequestExit();
  239. break;
  240. case kEventWindowBoundsChanged:
  241. // Window resized, update the rmlui context
  242. UInt32 attributes;
  243. GetEventParameter(event, kEventParamAttributes, typeUInt32, nullptr, sizeof(UInt32), nullptr, &attributes);
  244. if(attributes & kWindowBoundsChangeSizeChanged)
  245. {
  246. Rect bounds;
  247. GetEventParameter(event, kEventParamCurrentBounds, typeQDRectangle, nullptr, sizeof(Rect), nullptr, &bounds);
  248. UInt32 width = bounds.right - bounds.left;
  249. UInt32 height = bounds.bottom - bounds.top;
  250. shell_renderer->SetViewport(width, height);
  251. }
  252. break;
  253. }
  254. }
  255. break;
  256. }
  257. // InputMacOSX::ProcessCarbonEvent(event);
  258. return CallNextEventHandler(next_handler, event);
  259. }