WebClient.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. if (d_)
  123. d_->CloseBrowser(true);
  124. renderHandler_ = 0;
  125. //d_->Release();
  126. }
  127. void WebClient::SendMouseClickEvent(int x, int y, unsigned button, bool mouseUp, unsigned modifier) const
  128. {
  129. if (!d_->browser_.get())
  130. return;
  131. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  132. CefMouseEvent mevent;
  133. mevent.x = x;
  134. mevent.y = y;
  135. mevent.modifiers = 0;
  136. //MBT_LEFT = 0,
  137. //MBT_MIDDLE,
  138. //MBT_RIGHT,
  139. host->SendMouseClickEvent(mevent, (CefBrowserHost::MouseButtonType) button, mouseUp, 1);
  140. }
  141. void WebClient::SendMouseMoveEvent(int x, int y, unsigned modifier, bool mouseLeave) const
  142. {
  143. if (!d_->browser_.get())
  144. return;
  145. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  146. CefMouseEvent mevent;
  147. mevent.x = x;
  148. mevent.y = y;
  149. mevent.modifiers = 0;
  150. Input* input = GetSubsystem<Input>();
  151. if (input->GetMouseButtonDown(MOUSEB_LEFT))
  152. mevent.modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
  153. if (input->GetMouseButtonDown(MOUSEB_MIDDLE))
  154. mevent.modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
  155. if (input->GetMouseButtonDown(MOUSEB_RIGHT))
  156. mevent.modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
  157. host->SendMouseMoveEvent(mevent, mouseLeave);
  158. }
  159. void WebClient::SendMouseWheelEvent(int x, int y, unsigned modifier,int deltaX, int deltaY) const
  160. {
  161. if (!d_->browser_.get())
  162. return;
  163. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  164. CefMouseEvent mevent;
  165. mevent.x = x;
  166. mevent.y = y;
  167. mevent.modifiers = 0;
  168. #ifdef ATOMIC_PLATFORM_OSX
  169. deltaY = -deltaY;
  170. #endif
  171. host->SendMouseWheelEvent(mevent, deltaX, deltaY * 5);
  172. }
  173. /*
  174. EVENTFLAG_CAPS_LOCK_ON = 1 << 0,
  175. EVENTFLAG_SHIFT_DOWN = 1 << 1,
  176. EVENTFLAG_CONTROL_DOWN = 1 << 2,
  177. EVENTFLAG_ALT_DOWN = 1 << 3,
  178. EVENTFLAG_LEFT_MOUSE_BUTTON = 1 << 4,
  179. EVENTFLAG_MIDDLE_MOUSE_BUTTON = 1 << 5,
  180. EVENTFLAG_RIGHT_MOUSE_BUTTON = 1 << 6,
  181. // Mac OS-X command key.
  182. EVENTFLAG_COMMAND_DOWN = 1 << 7,
  183. EVENTFLAG_NUM_LOCK_ON = 1 << 8,
  184. EVENTFLAG_IS_KEY_PAD = 1 << 9,
  185. EVENTFLAG_IS_LEFT = 1 << 10,
  186. EVENTFLAG_IS_RIGHT = 1 << 11,
  187. } cef_event_flags_t;
  188. */
  189. void WebClient::SendKeyEvent(const StringHash eventType, VariantMap& eventData)
  190. {
  191. if (!d_->browser_.get())
  192. return;
  193. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  194. CefKeyEvent keyEvent;
  195. if (!ConvertKeyEvent(eventType, eventData, keyEvent))
  196. return;
  197. host->SendKeyEvent(keyEvent);
  198. #ifdef ATOMIC_PLATFORM_OSX
  199. // Send an empty key event on OSX, which seems to fix
  200. // keyboard problems on OSX with cefclient
  201. // ./cefclient --off-screen-rendering-enabled
  202. // return does not work at all on cef client with offscreen
  203. // bad interaction with arrow keys (for example here, after
  204. // hitting arrow keys, return/text takes a couple presses to register
  205. if (eventType == "KeyDown")
  206. keyEvent.type = KEYEVENT_KEYDOWN;
  207. else
  208. keyEvent.type = KEYEVENT_KEYUP;
  209. keyEvent.modifiers = 0;
  210. keyEvent.native_key_code = 0;
  211. host->SendKeyEvent(keyEvent);
  212. #endif
  213. }
  214. void WebClient::SendTextInputEvent(const StringHash eventType, VariantMap& eventData)
  215. {
  216. if (!d_->browser_.get())
  217. return;
  218. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  219. CefKeyEvent keyEvent;
  220. if (!ConvertTextInputEvent(eventType, eventData, keyEvent))
  221. return;
  222. host->SendKeyEvent(keyEvent);
  223. }
  224. void WebClient::SendFocusEvent(bool focus)
  225. {
  226. if (!d_->browser_.get())
  227. return;
  228. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  229. host->SendFocusEvent(focus);
  230. }
  231. void WebClient::ShortcutCut()
  232. {
  233. if (!d_->browser_.get())
  234. return;
  235. d_->browser_->GetFocusedFrame()->Cut();
  236. }
  237. void WebClient::ShortcutCopy()
  238. {
  239. if (!d_->browser_.get())
  240. return;
  241. d_->browser_->GetFocusedFrame()->Copy();
  242. }
  243. void WebClient::ShortcutPaste()
  244. {
  245. if (!d_->browser_.get())
  246. return;
  247. d_->browser_->GetFocusedFrame()->Paste();
  248. }
  249. void WebClient::ShortcutSelectAll()
  250. {
  251. if (!d_->browser_.get())
  252. return;
  253. d_->browser_->GetFocusedFrame()->SelectAll();
  254. }
  255. void WebClient::ShortcutUndo()
  256. {
  257. if (!d_->browser_.get())
  258. return;
  259. d_->browser_->GetFocusedFrame()->Undo();
  260. }
  261. void WebClient::ShortcutRedo()
  262. {
  263. if (!d_->browser_.get())
  264. return;
  265. d_->browser_->GetFocusedFrame()->Redo();
  266. }
  267. void WebClient::ShortcutDelete()
  268. {
  269. if (!d_->browser_.get())
  270. return;
  271. d_->browser_->GetFocusedFrame()->Delete();
  272. }
  273. void WebClient::WasResized()
  274. {
  275. if (!d_->browser_.get())
  276. return;
  277. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  278. host->WasResized();;
  279. }
  280. bool WebClient::CreateBrowser(const String& initialURL, int width, int height)
  281. {
  282. return d_->CreateBrowser(initialURL, width, height);
  283. }
  284. void WebClient::SetSize(int width, int height)
  285. {
  286. if (renderHandler_.Null())
  287. return;
  288. if (renderHandler_->GetWidth() == width && renderHandler_->GetHeight() == height)
  289. return;
  290. renderHandler_->SetSize(width, height);
  291. WasResized();
  292. }
  293. void WebClient::SetWebRenderHandler(WebRenderHandler* handler)
  294. {
  295. handler->SetWebClient(this);
  296. renderHandler_ = handler;
  297. }
  298. CefClient* WebClient::GetCefClient()
  299. {
  300. return d_;
  301. }
  302. }