FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
PluginWindowMac.mm
1 /**********************************************************\
2 Original Author: Eric Herrmann
3 
4 Created: Feb 8, 2011
5 License: Dual license model; choose one of two:
6  New BSD License
7  http://www.opensource.org/licenses/bsd-license.php
8  - or -
9  GNU Lesser General Public License, version 2.1
10  http://www.gnu.org/licenses/lgpl-2.1.html
11 
12 Copyright 2011 Eric Herrmann, Firebreath development team
13 \**********************************************************/
14 
15 #include "global/config.h"
16 #include "FactoryBase.h"
17 
18 #include "PluginWindowMacICA.h"
19 #include "PluginWindowMacCA.h"
20 #include "PluginWindowMacCG.h"
21 #include "PluginWindowMacQD.h"
22 #include "PluginEventMacCocoa.h"
23 #include "PluginEventMacCarbon.h"
24 #include "PluginEvents/GeneralEvents.h"
25 #include "PluginEvents/DrawingEvents.h"
26 #include "Mac/NpapiPluginMac.h"
27 #include "NpapiPluginModule.h"
28 #include "PluginEventMac.h"
29 
30 #include "PluginWindowMac.h"
31 
32 using namespace FB;
33 
34 @interface PluginWindowMac_helper : NSObject
35 {
36  PluginWindowMac *m_ica;
37 
38 }
39 
40 - (void)setIca:(PluginWindowMac *)ica;
41 - (PluginWindowMac *)ica;
42 
43 - (void)fired:(NSTimer*)timer;
44 
45 @end
46 
47 @implementation PluginWindowMac_helper
48 
49 - (void)setIca:(PluginWindowMac *)ica {
50  m_ica = ica;
51 }
52 - (PluginWindowMac *)ica {
53  return m_ica;
54 }
55 
56 - (void)fired:(NSTimer*)timer {
57  m_ica->InvalidateWindow();
58 }
59 
60 @end
61 
62 #if FBMAC_USE_INVALIDATINGCOREANIMATION || FBMAC_USE_COREANIMATION || FBMAC_USE_COREGRAPHICS || FBMAC_USE_QUICKDRAW
63 static bool supports(const FB::Npapi::NpapiBrowserHostPtr &host, NPNVariable what) {
64  NPBool value = FALSE;
65  // err tells us if the browser supports model negotiation
66  NPError err = host->GetValue(what, &value);
67  return (NPERR_NO_ERROR == err && value) ? true : false;
68 }
69 
70 static bool set(const FB::Npapi::NpapiBrowserHostPtr &host, NPPVariable what, void* value) {
71  NPError err = host->SetValue(what, value);
72  return (NPERR_NO_ERROR == err) ? true : false;
73 }
74 #endif
75 
76 #if FBMAC_USE_INVALIDATINGCOREANIMATION
77 static bool initPluginWindowMac_ICA(const FB::Npapi::NpapiBrowserHostPtr &host, NPDrawingModel& drawingModel) {
78  drawingModel = (NPDrawingModel) -1;
79  if (supports(host, NPNVsupportsInvalidatingCoreAnimationBool) && set(host, NPPVpluginDrawingModel, (void*)NPDrawingModelInvalidatingCoreAnimation)) {
80  FBLOG_INFO("PluginCore", "NPDrawingModel=NPDrawingModelInvalidatingCoreAnimation");
81  drawingModel = NPDrawingModelInvalidatingCoreAnimation;
82  return true;
83  }
84  return false;
85 }
86 #endif
87 #if FBMAC_USE_COREANIMATION
88 static bool initPluginWindowMac_CA(const FB::Npapi::NpapiBrowserHostPtr &host, NPDrawingModel& drawingModel) {
89  drawingModel = (NPDrawingModel) -1;
90  if (supports(host, NPNVsupportsCoreAnimationBool) && set(host, NPPVpluginDrawingModel, (void*)NPDrawingModelCoreAnimation)) {
91  FBLOG_INFO("PluginCore", "NPDrawingModel=NPDrawingModelCoreAnimation");
92  drawingModel = NPDrawingModelCoreAnimation;
93  return true;
94  }
95  return false;
96 }
97 #endif
98 #if FBMAC_USE_COREGRAPHICS
99 static bool initPluginWindowMac_CG(const FB::Npapi::NpapiBrowserHostPtr &host, NPDrawingModel& drawingModel) {
100  drawingModel = (NPDrawingModel) -1;
101  if (supports(host, NPNVsupportsCoreGraphicsBool) && set(host, NPPVpluginDrawingModel, (void*)NPDrawingModelCoreGraphics)) {
102  FBLOG_INFO("PluginCore", "NPDrawingModel=NPDrawingModelCoreGraphics");
103  drawingModel = NPDrawingModelCoreGraphics;
104  return true;
105  }
106  return false;
107 }
108 #endif
109 #if FBMAC_USE_QUICKDRAW
110 #ifndef NP_NO_QUICKDRAW
111 static bool initPluginWindowMac_QD(const FB::Npapi::NpapiBrowserHostPtr &host, NPDrawingModel& drawingModel) {
112  drawingModel = (NPDrawingModel) -1;
113  if (supports(host, NPNVsupportsQuickDrawBool) && set(host, NPPVpluginDrawingModel, (void*)NPDrawingModelQuickDraw)) {
114  FBLOG_INFO("PluginCore", "NPDrawingModel=NPDrawingModelQuickDraw");
115  drawingModel = NPDrawingModelQuickDraw;
116  return true;
117  }
118  return false;
119 }
120 #endif
121 #endif
122 
123 NPDrawingModel PluginWindowMac::initPluginWindowMac(const FB::Npapi::NpapiBrowserHostPtr &host, const std::string& drawingModel) {
124  NPDrawingModel rval = (NPDrawingModel) -1;
125 #if FBMAC_USE_COREANIMATION
126  if (0 == strcmp(drawingModel.c_str(), "NPDrawingModelCoreAnimation"))
127  (void) initPluginWindowMac_CA(host, rval);
128  else
129 #endif
130 #if FBMAC_USE_INVALIDATINGCOREANIMATION
131  if (0 == strcmp(drawingModel.c_str(), "NPDrawingModelInvalidatingCoreAnimation"))
132  (void) initPluginWindowMac_ICA(host, rval);
133  else
134 #endif
135 #if FBMAC_USE_COREGRAPHICS
136  if (0 == strcmp(drawingModel.c_str(), "NPDrawingModelCoreGraphics"))
137  (void) initPluginWindowMac_CG(host, rval);
138  else
139 #endif
140 #if FBMAC_USE_QUICKDRAW
141 #ifndef NP_NO_QUICKDRAW
142  if (0 == strcmp(drawingModel.c_str(), "NPDrawingModelQuickDraw"))
143  (void) initPluginWindowMac_QD(host, rval);
144  else
145 #endif
146 #endif
147  FBLOG_INFO("PluginCore", "NPDrawingModel=NONE");
148  return rval;
149 }
150 
151 NPDrawingModel PluginWindowMac::initPluginWindowMac(const FB::Npapi::NpapiBrowserHostPtr &host) {
152  NPDrawingModel rval = (NPDrawingModel) -1;
153 #if FBMAC_USE_INVALIDATINGCOREANIMATION
154  if (initPluginWindowMac_ICA(host, rval))
155  return rval;
156  else
157 #endif
158 #if FBMAC_USE_COREANIMATION
159  if (initPluginWindowMac_CA(host, rval))
160  return rval;
161  else
162 #endif
163 #if FBMAC_USE_COREGRAPHICS
164  if (initPluginWindowMac_CG(host, rval))
165  return rval;
166  else
167 #endif
168 #if FBMAC_USE_QUICKDRAW
169 #ifndef NP_NO_QUICKDRAW
170  if (initPluginWindowMac_QD(host, rval))
171  return rval;
172  else
173 #endif
174 #endif
175  FBLOG_INFO("PluginCore", "NPDrawingModel=NONE");
176  return rval;
177 }
178 
179 FB::PluginWindowMac* PluginWindowMac::createPluginWindowMac(NPDrawingModel drawingModel) {
180  FB::PluginWindowMac* rval = NULL;
181  switch (drawingModel) {
182 #if FBMAC_USE_INVALIDATINGCOREANIMATION
183  case NPDrawingModelInvalidatingCoreAnimation:
184  rval = getFactoryInstance()->createPluginWindowMacICA();
185  break;
186 #endif
187 #if FBMAC_USE_COREANIMATION
188  case NPDrawingModelCoreAnimation:
189  rval = getFactoryInstance()->createPluginWindowMacCA();
190  break;
191 #endif
192 #if FBMAC_USE_COREGRAPHICS
193  case NPDrawingModelCoreGraphics:
194  rval = getFactoryInstance()->createPluginWindowMacCG();
195  break;
196 #endif
197 #if FBMAC_USE_QUICKDRAW
198 #ifndef NP_NO_QUICKDRAW
199  case NPDrawingModelQuickDraw:
200  rval = getFactoryInstance()->createPluginWindowMacQD();
201  break;
202 #endif
203 #endif
204  case NPDrawingModelOpenGL:
205  default:
206  break;
207  }
208  return rval;
209 }
210 
211 PluginWindowMac::PluginWindowMac()
212  : PluginWindow(), m_npHost()
213  , m_x(0), m_y(0), m_width(0), m_height(0)
214  , m_clipTop(0), m_clipLeft(0), m_clipBottom(0), m_clipRight(0)
215  , m_timer(NULL), m_helper(NULL), m_drawLabel(false)
216 {
217  PluginWindowMac_helper *mhelper = [PluginWindowMac_helper new];
218  [mhelper setIca:this];
219  m_helper = mhelper;
220 }
221 
222 PluginWindowMac::~PluginWindowMac()
223 {
224  StopAutoInvalidate();
225 
226  PluginWindowMac_helper *mhelper = (PluginWindowMac_helper*) m_helper;
227  if (mhelper) {
228  [mhelper release];
229  mhelper = NULL;
230  }
231 }
232 
233 NPError PluginWindowMac::SetWindow(NPWindow* window) {
234  if ((m_x != window->x)
235  || (m_y != window->y)
236  || (m_width != window->width)
237  || (m_height != window->height)) {
238  m_x = window->x;
239  m_y = window->y;
240  m_width = window->width;
241  m_height = window->height;
242 
243  ResizedEvent evt;
244  SendEvent(&evt);
245  }
246  if ((m_clipTop != window->clipRect.top)
247  || (m_clipLeft != window->clipRect.left)
248  || (m_clipBottom != window->clipRect.bottom)
249  || (m_clipRight != window->clipRect.right)) {
250  m_clipTop = window->clipRect.top;
251  m_clipLeft = window->clipRect.left;
252  m_clipBottom = window->clipRect.bottom;
253  m_clipRight = window->clipRect.right;
254 
255  ClipChangedEvent evt;
256  SendEvent(&evt);
257  }
258  return NPERR_NO_ERROR;
259 }
260 
261 FB::Rect PluginWindowMac::getWindowPosition() const {
262  FB::Rect r = { static_cast<int32_t>(m_y), static_cast<int32_t>(m_x),
263  static_cast<int32_t>(m_y + m_height), static_cast<int32_t>(m_x + m_width) };
264  return r;
265 }
266 
267 FB::Rect PluginWindowMac::getWindowClipping() const {
268  FB::Rect r = { m_clipTop, m_clipLeft, m_clipBottom, m_clipRight };
269  return r;
270 }
271 
272 uint32_t PluginWindowMac::getWindowHeight() const {
273  return m_height;
274 }
275 
276 uint32_t PluginWindowMac::getWindowWidth() const {
277  return m_width;
278 }
279 
280 static void timerCallback(NPP npp, uint32_t timerID) {
281  boost::shared_ptr<FB::Npapi::NpapiPluginMac> plugin = FB::ptr_cast<FB::Npapi::NpapiPluginMac>(FB::Npapi::getPlugin(npp));
282  if (plugin)
283  plugin->HandleTimerEvent();
284 }
285 
286 int PluginWindowMac::scheduleTimer(int interval, bool repeat) {
287  return m_npHost->ScheduleTimer(interval, repeat, timerCallback);
288 }
289 
290 void PluginWindowMac::unscheduleTimer(int timerId) {
291  m_npHost->UnscheduleTimer(timerId);
292 }
293 
294 void PluginWindowMac::handleTimerEvent() {
295  TimerEvent ev(0, NULL);
296  SendEvent(&ev);
297 }
298 
299 void PluginWindowMac::StartAutoInvalidate(double rate) {
300  StopAutoInvalidate();
301 
302  FBLOG_INFO("PluginCore", "AutoInvalidate STARTED " << 1/rate << "fps");
303  PluginWindowMac_helper *mhelper = (PluginWindowMac_helper*) m_helper;
304  m_timer = [[NSTimer scheduledTimerWithTimeInterval:rate target:mhelper selector:@selector(fired:) userInfo:NULL repeats:YES] retain];
305 }
306 
307 void PluginWindowMac::StopAutoInvalidate() {
308  FBLOG_INFO("PluginCore", "AutoInvalidate STOPPED");
309  NSTimer *mtimer = (NSTimer*) m_timer;
310  if (mtimer) {
311  [mtimer invalidate];
312  [mtimer release];
313  mtimer = NULL;
314  m_timer = NULL;
315  }
316 }
317 
318 void PluginWindowMac::InvalidateWindow() const {
319  FBLOG_TRACE("PluginCore", "InvalidateWindow");
320  NPRect r = { 0, 0, static_cast<uint16_t>(m_height), static_cast<uint16_t>(m_width) };
321  if (!m_npHost->isMainThread())
322  m_npHost->ScheduleOnMainThread(m_npHost, boost::bind(&Npapi::NpapiBrowserHost::InvalidateRect2, m_npHost, r));
323  else
324  m_npHost->InvalidateRect(&r);
325 }
boost::shared_ptr< T > ptr_cast(boost::shared_ptr< U > const &r)
Convenience function for doing a dynamic cast of one boost::shared_ptr to another.
Definition: APITypes.h:339
Fired when the clipping of the plugin drawing area changes.
Definition: DrawingEvents.h:45
Fired when a timer event is received; currently this only works on windows and is fired when WM_TIMER...
Definition: GeneralEvents.h:29
PluginWindow is the base class for all PluginWindow objects.
Definition: PluginWindow.h:33
Fired when the plugin window is resized.
Definition: DrawingEvents.h:36