WebClient.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 "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. #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(int scanCode, int qual, bool keyUp)
  188. {
  189. if (!d_->browser_.get())
  190. return;
  191. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  192. CefKeyEvent keyEvent;
  193. if (keyUp)
  194. return;
  195. // handle return special
  196. if (scanCode == SDL_SCANCODE_RETURN)
  197. {
  198. keyEvent.type = KEYEVENT_CHAR;
  199. keyEvent.character = 13;
  200. host->SendKeyEvent(keyEvent);
  201. return;
  202. }
  203. unsigned modifiers = EVENTFLAG_NONE;
  204. if (qual & QUAL_SHIFT)
  205. modifiers |= EVENTFLAG_SHIFT_DOWN;
  206. if (qual & QUAL_ALT)
  207. modifiers |= EVENTFLAG_ALT_DOWN;
  208. if (qual & QUAL_CTRL)
  209. modifiers |= EVENTFLAG_CONTROL_DOWN;
  210. #ifdef ATOMIC_PLATFORM_OSX
  211. Input* input = GetSubsystem<Input>();
  212. if (input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI))
  213. {
  214. modifiers |= EVENTFLAG_COMMAND_DOWN;
  215. }
  216. #endif
  217. keyEvent.modifiers = modifiers;
  218. int nativeKeyCode = GetNativeKeyFromSDLScanCode(scanCode);
  219. if (nativeKeyCode == -1)
  220. return;
  221. /*
  222. target->type = src->type;
  223. target->modifiers = src->modifiers;
  224. target->windows_key_code = src->windows_key_code;
  225. target->native_key_code = src->native_key_code;
  226. target->is_system_key = src->is_system_key;
  227. target->character = src->character;
  228. target->unmodified_character = src->unmodified_character;
  229. */
  230. keyEvent.type = keyUp ? KEYEVENT_KEYUP : KEYEVENT_KEYDOWN;
  231. keyEvent.native_key_code = nativeKeyCode;
  232. host->SendKeyEvent(keyEvent);
  233. #ifdef ATOMIC_PLATFORM_OSX
  234. // Send an empty key event on OSX, which seems to fix
  235. // keyboard problems on OSX with cefclient
  236. // ./cefclient --off-screen-rendering-enabled
  237. // return does not work at all on cef client with offscreen
  238. // bad interaction with arrow keys (for example here, after
  239. // hitting arrow keys, return/text takes a couple presses to register
  240. keyEvent.type = keyUp ? KEYEVENT_KEYUP : KEYEVENT_KEYDOWN;
  241. keyEvent.modifiers = 0;
  242. keyEvent.native_key_code = 0;
  243. host->SendKeyEvent(keyEvent);
  244. #endif
  245. }
  246. void WebClient::SendTextEvent(const String& text, unsigned modifiers)
  247. {
  248. if (!d_->browser_.get())
  249. return;
  250. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  251. CefKeyEvent keyEvent;
  252. keyEvent.type = KEYEVENT_CHAR;
  253. keyEvent.character = text[0];
  254. host->SendKeyEvent(keyEvent);
  255. }
  256. void WebClient::ShortcutCut()
  257. {
  258. if (!d_->browser_.get())
  259. return;
  260. d_->browser_->GetFocusedFrame()->Cut();
  261. }
  262. void WebClient::ShortcutCopy()
  263. {
  264. if (!d_->browser_.get())
  265. return;
  266. d_->browser_->GetFocusedFrame()->Copy();
  267. }
  268. void WebClient::ShortcutPaste()
  269. {
  270. if (!d_->browser_.get())
  271. return;
  272. d_->browser_->GetFocusedFrame()->Paste();
  273. }
  274. void WebClient::ShortcutSelectAll()
  275. {
  276. if (!d_->browser_.get())
  277. return;
  278. d_->browser_->GetFocusedFrame()->SelectAll();
  279. }
  280. void WebClient::ShortcutUndo()
  281. {
  282. if (!d_->browser_.get())
  283. return;
  284. d_->browser_->GetFocusedFrame()->Undo();
  285. }
  286. void WebClient::ShortcutRedo()
  287. {
  288. if (!d_->browser_.get())
  289. return;
  290. d_->browser_->GetFocusedFrame()->Redo();
  291. }
  292. void WebClient::ShortcutDelete()
  293. {
  294. if (!d_->browser_.get())
  295. return;
  296. d_->browser_->GetFocusedFrame()->Delete();
  297. }
  298. void WebClient::WasResized()
  299. {
  300. if (!d_->browser_.get())
  301. return;
  302. CefRefPtr<CefBrowserHost> host = d_->browser_->GetHost();
  303. host->WasResized();;
  304. }
  305. bool WebClient::CreateBrowser(const String& initialURL, int width, int height)
  306. {
  307. return d_->CreateBrowser(initialURL, width, height);
  308. }
  309. void WebClient::SetSize(int width, int height)
  310. {
  311. if (renderHandler_.Null())
  312. return;
  313. if (renderHandler_->GetWidth() == width && renderHandler_->GetHeight() == height)
  314. return;
  315. renderHandler_->SetSize(width, height);
  316. WasResized();
  317. }
  318. void WebClient::SetWebRenderHandler(WebRenderHandler* handler)
  319. {
  320. handler->SetWebClient(this);
  321. renderHandler_ = handler;
  322. }
  323. CefClient* WebClient::GetCefClient()
  324. {
  325. return d_;
  326. }
  327. }