WebClient.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #include <SDL/include/SDL.h>
  2. #include <ThirdParty/SDL/include/SDL_syswm.h>
  3. #include <ThirdParty/CEF/include/cef_app.h>
  4. #include <ThirdParty/CEF/include/cef_client.h>
  5. #include <ThirdParty/CEF/include/cef_browser.h>
  6. #include <ThirdParty/CEF/include/wrapper/cef_helpers.h>
  7. #include <ThirdParty/CEF/include/base/cef_bind.h>
  8. #include <ThirdParty/CEF/include/wrapper/cef_closure_task.h>
  9. #include <Atomic/Core/ProcessUtils.h>
  10. #include <Atomic/Core/CoreEvents.h>
  11. #include <Atomic/IO/Log.h>
  12. #include <Atomic/Input/Input.h>
  13. #include <Atomic/Graphics/Graphics.h>
  14. #include "WebBrowserHost.h"
  15. #include "WebClient.h"
  16. #include "WebKeyboardSDL.h"
  17. namespace Atomic
  18. {
  19. #ifdef ATOMIC_PLATFORM_OSX
  20. void* GetNSWindowContentView(void* window);
  21. #endif
  22. class WebClientPrivate : public CefClient, public CefLifeSpanHandler
  23. {
  24. friend class WebClient;
  25. public:
  26. WebClientPrivate(WebClient* client)
  27. {
  28. webClient_ = client;
  29. webBrowserHost_ = webClient_->GetSubsystem<WebBrowserHost>();
  30. }
  31. CefRefPtr<CefRenderHandler> GetRenderHandler() OVERRIDE
  32. {
  33. if (webClient_->renderHandler_.Null())
  34. return nullptr;
  35. return webClient_->renderHandler_->GetCEFRenderHandler();
  36. }
  37. virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE
  38. {
  39. return this;
  40. }
  41. bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
  42. CefProcessId source_process,
  43. CefRefPtr<CefProcessMessage> message) OVERRIDE
  44. {
  45. return false;
  46. }
  47. bool CreateBrowser(const String& initialURL, int width, int height)
  48. {
  49. if (webClient_->renderHandler_.Null())
  50. {
  51. LOGERROR("WebClient::CreateBrowser - No render handler specified");
  52. return false;
  53. }
  54. CefWindowInfo windowInfo;
  55. CefBrowserSettings browserSettings;
  56. //browserSettings.webgl = STATE_ENABLED;
  57. windowInfo.width = width;
  58. windowInfo.height = height;
  59. Graphics* graphics = webClient_->GetSubsystem<Graphics>();
  60. SDL_Window* sdlWindow = static_cast<SDL_Window*>(graphics->GetSDLWindow());
  61. SDL_SysWMinfo info;
  62. SDL_VERSION(&info.version);
  63. if(SDL_GetWindowWMInfo(sdlWindow, &info))
  64. {
  65. NSView* view = (NSView*) GetNSWindowContentView(info.info.cocoa.window);
  66. windowInfo.SetAsWindowless(view, false);
  67. webClient_->renderHandler_->SetSize(width, height);
  68. CefRefPtr<CefBrowser> browser = CefBrowserHost::CreateBrowserSync(windowInfo, this,
  69. initialURL.CString(), browserSettings, nullptr);
  70. if (!browser.get())
  71. return false;
  72. browser_ = browser;
  73. return true;
  74. }
  75. return false;
  76. }
  77. // CefLifeSpanHandler methods:
  78. virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE
  79. {
  80. CEF_REQUIRE_UI_THREAD();
  81. }
  82. virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  83. {
  84. return false;
  85. }
  86. virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  87. {
  88. CEF_REQUIRE_UI_THREAD();
  89. if (browser->IsSame(browser_))
  90. browser_ = nullptr;
  91. }
  92. void CloseBrowser(bool force_close)
  93. {
  94. if (!CefCurrentlyOn(TID_UI))
  95. {
  96. // Execute on the UI thread.
  97. CefPostTask(TID_UI,
  98. base::Bind(&WebClientPrivate::CloseBrowser, this, force_close));
  99. return;
  100. }
  101. if (!browser_.get())
  102. return;
  103. browser_->GetHost()->CloseBrowser(force_close);
  104. }
  105. IMPLEMENT_REFCOUNTING(WebClientPrivate);
  106. private:
  107. CefRefPtr<CefBrowser> browser_;
  108. WeakPtr<WebBrowserHost> webBrowserHost_;
  109. WeakPtr<WebClient> webClient_;
  110. };
  111. WebClient::WebClient(Context* context) : Object(context)
  112. {
  113. d_ = new WebClientPrivate(this);
  114. }
  115. WebClient::~WebClient()
  116. {
  117. renderHandler_ = 0;
  118. //d_->Release();
  119. }
  120. void WebClient::SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier) const
  121. {
  122. if (!d_->browser_.get())
  123. return;
  124. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  125. CefMouseEvent mevent;
  126. mevent.x = x;
  127. mevent.y = y;
  128. mevent.modifiers = 0;
  129. //MBT_LEFT = 0,
  130. //MBT_MIDDLE,
  131. //MBT_RIGHT,
  132. host->SendMouseClickEvent(mevent, (CefBrowserHost::MouseButtonType) button, mouseUp, 1);
  133. }
  134. void WebClient::SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave) const
  135. {
  136. if (!d_->browser_.get())
  137. return;
  138. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  139. CefMouseEvent mevent;
  140. mevent.x = x;
  141. mevent.y = y;
  142. mevent.modifiers = 0;
  143. Input* input = GetSubsystem<Input>();
  144. if (input->GetMouseButtonDown(MOUSEB_LEFT))
  145. mevent.modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
  146. if (input->GetMouseButtonDown(MOUSEB_MIDDLE))
  147. mevent.modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
  148. if (input->GetMouseButtonDown(MOUSEB_RIGHT))
  149. mevent.modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
  150. host->SendMouseMoveEvent(mevent, mouseLeave);
  151. }
  152. void WebClient::SendMouseWheelEvent(int x, int y, unsigned modifier,int deltaX, int deltaY) const
  153. {
  154. if (!d_->browser_.get())
  155. return;
  156. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  157. CefMouseEvent mevent;
  158. mevent.x = x;
  159. mevent.y = y;
  160. mevent.modifiers = 0;
  161. #ifdef ATOMIC_PLATFORM_OSX
  162. deltaY = -deltaY;
  163. #endif
  164. host->SendMouseWheelEvent(mevent, deltaX, deltaY * 5);
  165. }
  166. /*
  167. EVENTFLAG_CAPS_LOCK_ON = 1 << 0,
  168. EVENTFLAG_SHIFT_DOWN = 1 << 1,
  169. EVENTFLAG_CONTROL_DOWN = 1 << 2,
  170. EVENTFLAG_ALT_DOWN = 1 << 3,
  171. EVENTFLAG_LEFT_MOUSE_BUTTON = 1 << 4,
  172. EVENTFLAG_MIDDLE_MOUSE_BUTTON = 1 << 5,
  173. EVENTFLAG_RIGHT_MOUSE_BUTTON = 1 << 6,
  174. // Mac OS-X command key.
  175. EVENTFLAG_COMMAND_DOWN = 1 << 7,
  176. EVENTFLAG_NUM_LOCK_ON = 1 << 8,
  177. EVENTFLAG_IS_KEY_PAD = 1 << 9,
  178. EVENTFLAG_IS_LEFT = 1 << 10,
  179. EVENTFLAG_IS_RIGHT = 1 << 11,
  180. } cef_event_flags_t;
  181. */
  182. void WebClient::SendKeyEvent(int key, bool keyUp, int scanCode, unsigned raw, int buttons, int qual)
  183. {
  184. if (!d_->browser_.get())
  185. return;
  186. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  187. CefKeyEvent keyEvent;
  188. // handle return special
  189. if (scanCode == SDL_SCANCODE_RETURN)
  190. {
  191. if (keyUp)
  192. return;
  193. keyEvent.type = KEYEVENT_CHAR;
  194. keyEvent.character = 13;
  195. keyEvent.unmodified_character = 13;
  196. host->SendKeyEvent(keyEvent);
  197. return;
  198. }
  199. unsigned modifiers = EVENTFLAG_NONE;
  200. if (qual & QUAL_SHIFT)
  201. modifiers |= EVENTFLAG_SHIFT_DOWN;
  202. if (qual & QUAL_ALT)
  203. modifiers |= EVENTFLAG_ALT_DOWN;
  204. if (qual & QUAL_CTRL)
  205. modifiers |= EVENTFLAG_CONTROL_DOWN;
  206. #ifdef ATOMIC_PLATFORM_OSX
  207. Input* input = GetSubsystem<Input>();
  208. if (input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI))
  209. {
  210. modifiers |= EVENTFLAG_COMMAND_DOWN;
  211. }
  212. #endif
  213. keyEvent.modifiers = modifiers;
  214. int nativeKeyCode = GetNativeKeyFromSDLScanCode(scanCode);
  215. if (nativeKeyCode == -1)
  216. return;
  217. /*
  218. target->type = src->type;
  219. target->modifiers = src->modifiers;
  220. target->windows_key_code = src->windows_key_code;
  221. target->native_key_code = src->native_key_code;
  222. target->is_system_key = src->is_system_key;
  223. target->character = src->character;
  224. target->unmodified_character = src->unmodified_character;
  225. */
  226. keyEvent.type = keyUp ? KEYEVENT_KEYUP : KEYEVENT_KEYDOWN;
  227. keyEvent.native_key_code = nativeKeyCode;
  228. host->SendKeyEvent(keyEvent);
  229. }
  230. void WebClient::SendTextEvent(const String& text, unsigned modifiers)
  231. {
  232. if (!d_->browser_.get())
  233. return;
  234. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  235. CefKeyEvent keyEvent;
  236. keyEvent.type = KEYEVENT_CHAR;
  237. keyEvent.character = text[0];
  238. host->SendKeyEvent(keyEvent);
  239. }
  240. void WebClient::WasResized()
  241. {
  242. if (!d_->browser_.get())
  243. return;
  244. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  245. host->WasResized();;
  246. }
  247. bool WebClient::CreateBrowser(const String& initialURL, int width, int height)
  248. {
  249. return d_->CreateBrowser(initialURL, width, height);
  250. }
  251. void WebClient::SetSize(int width, int height)
  252. {
  253. if (renderHandler_.Null())
  254. return;
  255. if (renderHandler_->GetWidth() == width && renderHandler_->GetHeight() == height)
  256. return;
  257. renderHandler_->SetSize(width, height);
  258. WasResized();
  259. }
  260. void WebClient::SetWebRenderHandler(WebRenderHandler* handler)
  261. {
  262. handler->SetWebClient(this);
  263. renderHandler_ = handler;
  264. }
  265. CefClient* WebClient::GetCefClient()
  266. {
  267. return d_;
  268. }
  269. }