WebClient.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. #include <SDL/include/SDL.h>
  2. #include <ThirdParty/SDL/include/SDL_syswm.h>
  3. #include <include/cef_app.h>
  4. #include <include/cef_client.h>
  5. #include <include/cef_browser.h>
  6. #include <include/wrapper/cef_helpers.h>
  7. #include <include/base/cef_bind.h>
  8. #include <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 "WebKeyboard.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. #ifdef ATOMIC_PLATFORM_OSX
  66. NSView* view = (NSView*) GetNSWindowContentView(info.info.cocoa.window);
  67. windowInfo.SetAsWindowless(view, false);
  68. #endif
  69. #ifdef ATOMIC_PLATFORM_WINDOWS
  70. windowInfo.SetAsWindowless(info.info.win.window, false);
  71. #endif
  72. webClient_->renderHandler_->SetSize(width, height);
  73. CefRefPtr<CefBrowser> browser = CefBrowserHost::CreateBrowserSync(windowInfo, this,
  74. initialURL.CString(), browserSettings, nullptr);
  75. if (!browser.get())
  76. return false;
  77. browser_ = browser;
  78. return true;
  79. }
  80. return false;
  81. }
  82. // CefLifeSpanHandler methods:
  83. virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE
  84. {
  85. CEF_REQUIRE_UI_THREAD();
  86. }
  87. virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  88. {
  89. return false;
  90. }
  91. virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE
  92. {
  93. CEF_REQUIRE_UI_THREAD();
  94. if (browser->IsSame(browser_))
  95. browser_ = nullptr;
  96. }
  97. void CloseBrowser(bool force_close)
  98. {
  99. if (!CefCurrentlyOn(TID_UI))
  100. {
  101. // Execute on the UI thread.
  102. CefPostTask(TID_UI,
  103. base::Bind(&WebClientPrivate::CloseBrowser, this, force_close));
  104. return;
  105. }
  106. if (!browser_.get())
  107. return;
  108. browser_->GetHost()->CloseBrowser(force_close);
  109. }
  110. IMPLEMENT_REFCOUNTING(WebClientPrivate);
  111. private:
  112. CefRefPtr<CefBrowser> browser_;
  113. WeakPtr<WebBrowserHost> webBrowserHost_;
  114. WeakPtr<WebClient> webClient_;
  115. };
  116. WebClient::WebClient(Context* context) : Object(context)
  117. {
  118. d_ = new WebClientPrivate(this);
  119. }
  120. WebClient::~WebClient()
  121. {
  122. renderHandler_ = 0;
  123. //d_->Release();
  124. }
  125. void WebClient::SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier) const
  126. {
  127. if (!d_->browser_.get())
  128. return;
  129. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  130. CefMouseEvent mevent;
  131. mevent.x = x;
  132. mevent.y = y;
  133. mevent.modifiers = 0;
  134. //MBT_LEFT = 0,
  135. //MBT_MIDDLE,
  136. //MBT_RIGHT,
  137. host->SendMouseClickEvent(mevent, (CefBrowserHost::MouseButtonType) button, mouseUp, 1);
  138. }
  139. void WebClient::SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave) const
  140. {
  141. if (!d_->browser_.get())
  142. return;
  143. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  144. CefMouseEvent mevent;
  145. mevent.x = x;
  146. mevent.y = y;
  147. mevent.modifiers = 0;
  148. Input* input = GetSubsystem<Input>();
  149. if (input->GetMouseButtonDown(MOUSEB_LEFT))
  150. mevent.modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
  151. if (input->GetMouseButtonDown(MOUSEB_MIDDLE))
  152. mevent.modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
  153. if (input->GetMouseButtonDown(MOUSEB_RIGHT))
  154. mevent.modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
  155. host->SendMouseMoveEvent(mevent, mouseLeave);
  156. }
  157. void WebClient::SendMouseWheelEvent(int x, int y, unsigned modifier,int deltaX, int deltaY) const
  158. {
  159. if (!d_->browser_.get())
  160. return;
  161. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  162. CefMouseEvent mevent;
  163. mevent.x = x;
  164. mevent.y = y;
  165. mevent.modifiers = 0;
  166. #ifdef ATOMIC_PLATFORM_OSX
  167. deltaY = -deltaY;
  168. #endif
  169. host->SendMouseWheelEvent(mevent, deltaX, deltaY * 5);
  170. }
  171. /*
  172. EVENTFLAG_CAPS_LOCK_ON = 1 << 0,
  173. EVENTFLAG_SHIFT_DOWN = 1 << 1,
  174. EVENTFLAG_CONTROL_DOWN = 1 << 2,
  175. EVENTFLAG_ALT_DOWN = 1 << 3,
  176. EVENTFLAG_LEFT_MOUSE_BUTTON = 1 << 4,
  177. EVENTFLAG_MIDDLE_MOUSE_BUTTON = 1 << 5,
  178. EVENTFLAG_RIGHT_MOUSE_BUTTON = 1 << 6,
  179. // Mac OS-X command key.
  180. EVENTFLAG_COMMAND_DOWN = 1 << 7,
  181. EVENTFLAG_NUM_LOCK_ON = 1 << 8,
  182. EVENTFLAG_IS_KEY_PAD = 1 << 9,
  183. EVENTFLAG_IS_LEFT = 1 << 10,
  184. EVENTFLAG_IS_RIGHT = 1 << 11,
  185. } cef_event_flags_t;
  186. */
  187. void WebClient::SendKeyEvent(const StringHash eventType, VariantMap& eventData)
  188. {
  189. if (!d_->browser_.get())
  190. return;
  191. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  192. CefKeyEvent keyEvent;
  193. if (!ConvertKeyEvent(eventType, eventData, keyEvent))
  194. return;
  195. host->SendKeyEvent(keyEvent);
  196. #ifdef ATOMIC_PLATFORM_OSX
  197. // Send an empty key event on OSX, which seems to fix
  198. // keyboard problems on OSX with cefclient
  199. // ./cefclient --off-screen-rendering-enabled
  200. // return does not work at all on cef client with offscreen
  201. // bad interaction with arrow keys (for example here, after
  202. // hitting arrow keys, return/text takes a couple presses to register
  203. keyEvent.type = keyUp ? KEYEVENT_KEYUP : KEYEVENT_KEYDOWN;
  204. keyEvent.modifiers = 0;
  205. keyEvent.native_key_code = 0;
  206. host->SendKeyEvent(keyEvent);
  207. #endif
  208. }
  209. void WebClient::SendTextInputEvent(const StringHash eventType, VariantMap& eventData)
  210. {
  211. if (!d_->browser_.get())
  212. return;
  213. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  214. CefKeyEvent keyEvent;
  215. if (!ConvertTextInputEvent(eventType, eventData, keyEvent))
  216. return;
  217. host->SendKeyEvent(keyEvent);
  218. }
  219. void WebClient::SendFocusEvent(bool focus)
  220. {
  221. if (!d_->browser_.get())
  222. return;
  223. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  224. host->SendFocusEvent(focus);
  225. }
  226. void WebClient::ShortcutCut()
  227. {
  228. if (!d_->browser_.get())
  229. return;
  230. d_->browser_->GetFocusedFrame()->Cut();
  231. }
  232. void WebClient::ShortcutCopy()
  233. {
  234. if (!d_->browser_.get())
  235. return;
  236. d_->browser_->GetFocusedFrame()->Copy();
  237. }
  238. void WebClient::ShortcutPaste()
  239. {
  240. if (!d_->browser_.get())
  241. return;
  242. d_->browser_->GetFocusedFrame()->Paste();
  243. }
  244. void WebClient::ShortcutSelectAll()
  245. {
  246. if (!d_->browser_.get())
  247. return;
  248. d_->browser_->GetFocusedFrame()->SelectAll();
  249. }
  250. void WebClient::ShortcutUndo()
  251. {
  252. if (!d_->browser_.get())
  253. return;
  254. d_->browser_->GetFocusedFrame()->Undo();
  255. }
  256. void WebClient::ShortcutRedo()
  257. {
  258. if (!d_->browser_.get())
  259. return;
  260. d_->browser_->GetFocusedFrame()->Redo();
  261. }
  262. void WebClient::ShortcutDelete()
  263. {
  264. if (!d_->browser_.get())
  265. return;
  266. d_->browser_->GetFocusedFrame()->Delete();
  267. }
  268. void WebClient::WasResized()
  269. {
  270. if (!d_->browser_.get())
  271. return;
  272. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  273. host->WasResized();;
  274. }
  275. bool WebClient::CreateBrowser(const String& initialURL, int width, int height)
  276. {
  277. return d_->CreateBrowser(initialURL, width, height);
  278. }
  279. void WebClient::SetSize(int width, int height)
  280. {
  281. if (renderHandler_.Null())
  282. return;
  283. if (renderHandler_->GetWidth() == width && renderHandler_->GetHeight() == height)
  284. return;
  285. renderHandler_->SetSize(width, height);
  286. WasResized();
  287. }
  288. void WebClient::SetWebRenderHandler(WebRenderHandler* handler)
  289. {
  290. handler->SetWebClient(this);
  291. renderHandler_ = handler;
  292. }
  293. CefClient* WebClient::GetCefClient()
  294. {
  295. return d_;
  296. }
  297. }