initial import
[vuplus_webkit] / Source / WebCore / plugins / qt / PluginViewQt.cpp
1 /*
2  * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
3  * Copyright (C) 2008 Collabora Ltd. All rights reserved.
4  * Copyright (C) 2009 Girish Ramakrishnan <girish@forwardbias.in>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
26  */
27
28 #include "config.h"
29 #include "PluginView.h"
30
31 #if USE(JSC)
32 #include "BridgeJSC.h"
33 #endif
34 #include "Chrome.h"
35 #include "ChromeClient.h"
36 #include "Document.h"
37 #include "DocumentLoader.h"
38 #include "Element.h"
39 #include "FloatPoint.h"
40 #include "FocusController.h"
41 #include "Frame.h"
42 #include "FrameLoadRequest.h"
43 #include "FrameLoader.h"
44 #include "FrameTree.h"
45 #include "FrameView.h"
46 #include "GraphicsContext.h"
47 #include "HTMLNames.h"
48 #include "HTMLPlugInElement.h"
49 #include "HostWindow.h"
50 #include "IFrameShimSupport.h"
51 #include "Image.h"
52 #if USE(JSC)
53 #include "JSDOMBinding.h"
54 #endif
55 #include "KeyboardEvent.h"
56 #include "MouseEvent.h"
57 #include "NotImplemented.h"
58 #include "Page.h"
59 #include "PlatformMouseEvent.h"
60 #include "PlatformKeyboardEvent.h"
61 #include "PluginContainerQt.h"
62 #include "PluginDebug.h"
63 #include "PluginPackage.h"
64 #include "PluginMainThreadScheduler.h"
65 #include "QWebPageClient.h"
66 #include "RenderLayer.h"
67 #include "Settings.h"
68 #include "npruntime_impl.h"
69 #include "qwebpage_p.h"
70 #if USE(JSC)
71 #include "runtime_root.h"
72 #endif
73
74 #include <QApplication>
75 #include <QDesktopWidget>
76 #include <QGraphicsWidget>
77 #include <QKeyEvent>
78 #include <QPainter>
79 #include <QStyleOptionGraphicsItem>
80 #include <QWidget>
81 #include <QX11Info>
82 #include <X11/X.h>
83 #ifndef QT_NO_XRENDER
84 #define Bool int
85 #define Status int
86 #include <X11/extensions/Xrender.h>
87 #endif
88 #include <runtime/JSLock.h>
89 #include <runtime/JSValue.h>
90
91 using JSC::ExecState;
92 #if USE(JSC)
93 using JSC::Interpreter;
94 #endif
95 using JSC::JSLock;
96 using JSC::JSObject;
97 using JSC::UString;
98
99 using std::min;
100
101 using namespace WTF;
102
103 namespace WebCore {
104
105 using namespace HTMLNames;
106
107 #if USE(ACCELERATED_COMPOSITING)
108 // Qt's GraphicsLayer (GraphicsLayerQt) requires layers to be QGraphicsWidgets
109 class PluginGraphicsLayerQt : public QGraphicsWidget {
110 public:
111     PluginGraphicsLayerQt(PluginView* view) : m_view(view) { }
112     ~PluginGraphicsLayerQt() { }
113
114     void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget = 0)
115     {
116         Q_UNUSED(widget);
117         m_view->paintUsingXPixmap(painter, option->exposedRect.toRect());
118     }
119
120 private:
121     PluginView* m_view;
122 };
123
124 bool PluginView::shouldUseAcceleratedCompositing() const
125 {
126     return m_parentFrame->page()->chrome()->client()->allowsAcceleratedCompositing()
127            && m_parentFrame->page()->settings()
128            && m_parentFrame->page()->settings()->acceleratedCompositingEnabled();
129 }
130 #endif
131
132 void PluginView::updatePluginWidget()
133 {
134     if (!parent())
135         return;
136
137     ASSERT(parent()->isFrameView());
138     FrameView* frameView = static_cast<FrameView*>(parent());
139
140     IntRect oldWindowRect = m_windowRect;
141     IntRect oldClipRect = m_clipRect;
142
143     m_windowRect = IntRect(frameView->contentsToWindow(frameRect().location()), frameRect().size());
144     m_clipRect = windowClipRect();
145     m_clipRect.move(-m_windowRect.x(), -m_windowRect.y());
146
147     if (m_windowRect == oldWindowRect && m_clipRect == oldClipRect)
148         return;
149
150     // The plugin had a zero width or height before but was resized, we need to show it again.
151     if (oldWindowRect.isEmpty())
152         show();
153
154     if (!m_isWindowed && m_windowRect.size() != oldWindowRect.size()) {
155 #if defined(MOZ_PLATFORM_MAEMO) && (MOZ_PLATFORM_MAEMO >= 5)
156         // On Maemo5, Flash always renders to 16-bit buffer
157         if (m_renderToImage)
158             m_image = QImage(m_windowRect.width(), m_windowRect.height(), QImage::Format_RGB16);
159         else
160 #endif
161         {
162             if (m_drawable)
163                 XFreePixmap(QX11Info::display(), m_drawable);
164
165             m_drawable = XCreatePixmap(QX11Info::display(), QX11Info::appRootWindow(), m_windowRect.width(), m_windowRect.height(), 
166                                        ((NPSetWindowCallbackStruct*)m_npWindow.ws_info)->depth);
167             QApplication::syncX(); // make sure that the server knows about the Drawable
168         }
169     }
170
171     // do not call setNPWindowIfNeeded immediately, will be called on paint()
172     m_hasPendingGeometryChange = true;
173
174     // (i) in order to move/resize the plugin window at the same time as the
175     // rest of frame during e.g. scrolling, we set the window geometry
176     // in the paint() function, but as paint() isn't called when the
177     // plugin window is outside the frame which can be caused by a
178     // scroll, we need to move/resize immediately.
179     // (ii) if we are running layout tests from DRT, paint() won't ever get called
180     // so we need to call setNPWindowIfNeeded() if window geometry has changed
181     if (!m_windowRect.intersects(frameView->frameRect())
182         || (QWebPagePrivate::drtRun && platformPluginWidget() && (m_windowRect != oldWindowRect || m_clipRect != oldClipRect)))
183         setNPWindowIfNeeded();
184
185     if (!m_platformLayer) {
186         // Make sure we get repainted afterwards. This is necessary for downward
187         // scrolling to move the plugin widget properly.
188         // Note that we don't invalidate the frameRect() here. This is because QWebFrame::renderRelativeCoords()
189         // imitates ScrollView and adds the scroll offset back on to the rect we damage here (making the co-ordinates absolute
190         // to the frame again) before passing it to FrameView.
191         invalidate();
192     }
193 }
194
195 void PluginView::setFocus(bool focused)
196 {
197     if (platformPluginWidget()) {
198         if (focused)
199             platformPluginWidget()->setFocus(Qt::OtherFocusReason);
200     } else {
201         Widget::setFocus(focused);
202     }
203 }
204
205 void PluginView::show()
206 {
207     Q_ASSERT(platformPluginWidget() == platformWidget());
208     Widget::show();
209 }
210
211 void PluginView::hide()
212 {
213     Q_ASSERT(platformPluginWidget() == platformWidget());
214     Widget::hide();
215 }
216
217 #if defined(MOZ_PLATFORM_MAEMO) && (MOZ_PLATFORM_MAEMO >= 5)
218 void PluginView::paintUsingImageSurfaceExtension(QPainter* painter, const IntRect& exposedRect)
219 {
220     NPImageExpose imageExpose;
221     QPoint offset;
222     QWebPageClient* client = m_parentFrame->view()->hostWindow()->platformPageClient();
223     const bool surfaceHasUntransformedContents = client && qobject_cast<QWidget*>(client->pluginParent());
224
225     QPaintDevice* surface =  QPainter::redirected(painter->device(), &offset);
226
227     // If the surface is a QImage, we can render directly into it
228     if (surfaceHasUntransformedContents && surface && surface->devType() == QInternal::Image) {
229         QImage* image = static_cast<QImage*>(surface);
230         offset = -offset; // negating the offset gives us the offset of the view within the surface
231         imageExpose.data = reinterpret_cast<char*>(image->bits());
232         imageExpose.dataSize.width = image->width();
233         imageExpose.dataSize.height = image->height();
234         imageExpose.stride = image->bytesPerLine();
235         imageExpose.depth = image->depth(); // this is guaranteed to be 16 on Maemo5
236         imageExpose.translateX = offset.x() + m_windowRect.x();
237         imageExpose.translateY = offset.y() + m_windowRect.y();
238         imageExpose.scaleX = 1;
239         imageExpose.scaleY = 1;
240     } else {
241         if (m_isTransparent) {
242             // On Maemo5, Flash expects the buffer to contain the contents that are below it.
243             // We don't support transparency for non-raster graphicssystem, so clean the image 
244             // before giving to Flash.
245             QPainter imagePainter(&m_image);
246             imagePainter.fillRect(exposedRect, Qt::white);
247         }
248
249         imageExpose.data = reinterpret_cast<char*>(m_image.bits());
250         imageExpose.dataSize.width = m_image.width();
251         imageExpose.dataSize.height = m_image.height();
252         imageExpose.stride = m_image.bytesPerLine();
253         imageExpose.depth = m_image.depth();
254         imageExpose.translateX = 0;
255         imageExpose.translateY = 0;
256         imageExpose.scaleX = 1;
257         imageExpose.scaleY = 1;
258     }
259     imageExpose.x = exposedRect.x();
260     imageExpose.y = exposedRect.y();
261     imageExpose.width = exposedRect.width();
262     imageExpose.height = exposedRect.height();
263
264     XEvent xevent;
265     memset(&xevent, 0, sizeof(XEvent));
266     XGraphicsExposeEvent& exposeEvent = xevent.xgraphicsexpose;
267     exposeEvent.type = GraphicsExpose;
268     exposeEvent.display = 0;
269     exposeEvent.drawable = reinterpret_cast<XID>(&imageExpose);
270     exposeEvent.x = exposedRect.x();
271     exposeEvent.y = exposedRect.y();
272     exposeEvent.width = exposedRect.width();
273     exposeEvent.height = exposedRect.height();
274
275     dispatchNPEvent(xevent);
276
277     if (!surfaceHasUntransformedContents || !surface || surface->devType() != QInternal::Image)
278         painter->drawImage(QPoint(frameRect().x() + exposedRect.x(), frameRect().y() + exposedRect.y()), m_image, exposedRect);
279 }
280 #endif
281
282 void PluginView::paintUsingXPixmap(QPainter* painter, const QRect &exposedRect)
283 {
284     QPixmap qtDrawable = QPixmap::fromX11Pixmap(m_drawable, QPixmap::ExplicitlyShared);
285     const int drawableDepth = ((NPSetWindowCallbackStruct*)m_npWindow.ws_info)->depth;
286     ASSERT(drawableDepth == qtDrawable.depth());
287     const bool syncX = m_pluginDisplay && m_pluginDisplay != QX11Info::display();
288
289     // When printing, Qt uses a QPicture to capture the output in preview mode. The
290     // QPicture holds a reference to the X Pixmap. As a result, the print preview would
291     // update itself when the X Pixmap changes. To prevent this, we create a copy.
292     if (m_element->document()->printing())
293         qtDrawable = qtDrawable.copy();
294
295     if (m_isTransparent && drawableDepth != 32) {
296         // Attempt content propagation for drawable with no alpha by copying over from the backing store
297         QPoint offset;
298         QPaintDevice* backingStoreDevice =  QPainter::redirected(painter->device(), &offset);
299         offset = -offset; // negating the offset gives us the offset of the view within the backing store pixmap
300
301         const bool hasValidBackingStore = backingStoreDevice && backingStoreDevice->devType() == QInternal::Pixmap;
302         QPixmap* backingStorePixmap = static_cast<QPixmap*>(backingStoreDevice);
303
304         // We cannot grab contents from the backing store when painting on QGraphicsView items
305         // (because backing store contents are already transformed). What we really mean to do 
306         // here is to check if we are painting on QWebView, but let's be a little permissive :)
307         QWebPageClient* client = m_parentFrame->view()->hostWindow()->platformPageClient();
308         const bool backingStoreHasUntransformedContents = client && qobject_cast<QWidget*>(client->pluginParent());
309
310         if (hasValidBackingStore && backingStorePixmap->depth() == drawableDepth 
311             && backingStoreHasUntransformedContents) {
312             GC gc = XDefaultGC(QX11Info::display(), QX11Info::appScreen());
313             XCopyArea(QX11Info::display(), backingStorePixmap->handle(), m_drawable, gc,
314                 offset.x() + m_windowRect.x() + exposedRect.x(), offset.y() + m_windowRect.y() + exposedRect.y(),
315                 exposedRect.width(), exposedRect.height(), exposedRect.x(), exposedRect.y());
316         } else { // no backing store, clean the pixmap because the plugin thinks its transparent
317             QPainter painter(&qtDrawable);
318             painter.fillRect(exposedRect, Qt::white);
319         }
320
321         if (syncX)
322             QApplication::syncX();
323     }
324
325     XEvent xevent;
326     memset(&xevent, 0, sizeof(XEvent));
327     XGraphicsExposeEvent& exposeEvent = xevent.xgraphicsexpose;
328     exposeEvent.type = GraphicsExpose;
329     exposeEvent.display = QX11Info::display();
330     exposeEvent.drawable = qtDrawable.handle();
331     exposeEvent.x = exposedRect.x();
332     exposeEvent.y = exposedRect.y();
333     exposeEvent.width = exposedRect.x() + exposedRect.width(); // flash bug? it thinks width is the right in transparent mode
334     exposeEvent.height = exposedRect.y() + exposedRect.height(); // flash bug? it thinks height is the bottom in transparent mode
335
336     dispatchNPEvent(xevent);
337
338     if (syncX)
339         XSync(m_pluginDisplay, false); // sync changes by plugin
340
341     painter->drawPixmap(QPoint(exposedRect.x(), exposedRect.y()), qtDrawable, exposedRect);
342 }
343
344 void PluginView::paint(GraphicsContext* context, const IntRect& rect)
345 {
346     if (!m_isStarted) {
347         paintMissingPluginIcon(context, rect);
348         return;
349     }
350
351     if (context->paintingDisabled())
352         return;
353
354     setNPWindowIfNeeded();
355
356     if (m_isWindowed)
357         return;
358
359 #if USE(ACCELERATED_COMPOSITING)
360     if (m_platformLayer)
361         return;
362 #endif
363
364     if (!m_drawable
365 #if defined(MOZ_PLATFORM_MAEMO) && (MOZ_PLATFORM_MAEMO >= 5)
366         && m_image.isNull()
367 #endif
368        )
369         return;
370
371     QPainter* painter = context->platformContext();
372     IntRect exposedRect(rect);
373     exposedRect.intersect(frameRect());
374     exposedRect.move(-frameRect().x(), -frameRect().y());
375
376 #if defined(MOZ_PLATFORM_MAEMO) && (MOZ_PLATFORM_MAEMO >= 5)
377     if (!m_image.isNull()) {
378         paintUsingImageSurfaceExtension(painter, exposedRect);
379         return;
380     }
381 #endif
382
383     painter->translate(frameRect().x(), frameRect().y());
384     paintUsingXPixmap(painter, exposedRect);
385     painter->translate(-frameRect().x(), -frameRect().y());
386 }
387
388 // TODO: Unify across ports.
389 bool PluginView::dispatchNPEvent(NPEvent& event)
390 {
391     if (!m_plugin->pluginFuncs()->event)
392         return false;
393
394     bool shouldPop = false;
395
396     if (m_plugin->pluginFuncs()->version < NPVERS_HAS_POPUPS_ENABLED_STATE
397         && (event.type == ButtonRelease || event.type == 3 /*KeyRelease*/)) {
398         pushPopupsEnabledState(true);
399         shouldPop = true;
400     }
401
402     PluginView::setCurrentPluginView(this);
403 #if USE(JSC)
404     JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
405 #endif
406     setCallingPlugin(true);
407     bool accepted = !m_plugin->pluginFuncs()->event(m_instance, &event);
408     setCallingPlugin(false);
409     PluginView::setCurrentPluginView(0);
410
411     if (shouldPop)
412         popPopupsEnabledState();
413
414     return accepted;
415 }
416
417 void setSharedXEventFields(XEvent* xEvent, QWidget* ownerWidget)
418 {
419     xEvent->xany.serial = 0; // we are unaware of the last request processed by X Server
420     xEvent->xany.send_event = false;
421     xEvent->xany.display = QX11Info::display();
422     // NOTE: event->xany.window doesn't always respond to the .window property of other XEvent's
423     // but does in the case of KeyPress, KeyRelease, ButtonPress, ButtonRelease, and MotionNotify
424     // events; thus, this is right:
425     xEvent->xany.window = ownerWidget ? ownerWidget->window()->handle() : 0;
426 }
427
428 void PluginView::initXEvent(XEvent* xEvent)
429 {
430     memset(xEvent, 0, sizeof(XEvent));
431
432     QWebPageClient* client = m_parentFrame->view()->hostWindow()->platformPageClient();
433     QWidget* ownerWidget = client ? client->ownerWidget() : 0;
434     setSharedXEventFields(xEvent, ownerWidget);
435 }
436
437 void setXKeyEventSpecificFields(XEvent* xEvent, KeyboardEvent* event)
438 {
439     const PlatformKeyboardEvent* keyEvent = event->keyEvent();
440
441     xEvent->type = (event->type() == eventNames().keydownEvent) ? 2 : 3; // ints as Qt unsets KeyPress and KeyRelease
442     xEvent->xkey.root = QX11Info::appRootWindow();
443     xEvent->xkey.subwindow = 0; // we have no child window
444     xEvent->xkey.time = event->timeStamp();
445     xEvent->xkey.state = keyEvent->nativeModifiers();
446     xEvent->xkey.keycode = keyEvent->nativeScanCode();
447
448     // We may not have a nativeScanCode() if the key event is from DRT's eventsender. In that
449     // case fetch the XEvent's keycode from the event's text. The only
450     // place this keycode will be used is in webkit_test_plugin_handle_event().
451     // FIXME: Create Qt API so that we can set the appropriate keycode in DRT EventSender instead.
452     if (QWebPagePrivate::drtRun && !xEvent->xkey.keycode) {
453         QKeyEvent* qKeyEvent = keyEvent->qtEvent();
454         ASSERT(qKeyEvent);
455         QString keyText = qKeyEvent->text().left(1);
456         xEvent->xkey.keycode = XKeysymToKeycode(QX11Info::display(), XStringToKeysym(keyText.toUtf8().constData()));
457     }
458
459     xEvent->xkey.same_screen = true;
460
461     // NOTE: As the XEvents sent to the plug-in are synthesized and there is not a native window
462     // corresponding to the plug-in rectangle, some of the members of the XEvent structures are not
463     // set to their normal Xserver values. e.g. Key events don't have a position.
464     // source: https://developer.mozilla.org/en/NPEvent
465     xEvent->xkey.x = 0;
466     xEvent->xkey.y = 0;
467     xEvent->xkey.x_root = 0;
468     xEvent->xkey.y_root = 0;
469 }
470
471 void PluginView::handleKeyboardEvent(KeyboardEvent* event)
472 {
473     if (m_isWindowed)
474         return;
475
476     if (event->type() != eventNames().keydownEvent && event->type() != eventNames().keyupEvent)
477         return;
478
479     XEvent npEvent;
480     initXEvent(&npEvent);
481     setXKeyEventSpecificFields(&npEvent, event);
482
483     if (dispatchNPEvent(npEvent))
484         event->setDefaultHandled();
485 }
486
487 static unsigned int inputEventState(MouseEvent* event)
488 {
489     unsigned int state = 0;
490     if (event->ctrlKey())
491         state |= ControlMask;
492     if (event->shiftKey())
493         state |= ShiftMask;
494     if (event->altKey())
495         state |= Mod1Mask;
496     if (event->metaKey())
497         state |= Mod4Mask;
498     return state;
499 }
500
501 static void setXButtonEventSpecificFields(XEvent* xEvent, MouseEvent* event, const IntPoint& postZoomPos)
502 {
503     XButtonEvent& xbutton = xEvent->xbutton;
504     xbutton.type = event->type() == eventNames().mousedownEvent ? ButtonPress : ButtonRelease;
505     xbutton.root = QX11Info::appRootWindow();
506     xbutton.subwindow = 0;
507     xbutton.time = event->timeStamp();
508     xbutton.x = postZoomPos.x();
509     xbutton.y = postZoomPos.y();
510     xbutton.x_root = event->screenX();
511     xbutton.y_root = event->screenY();
512     xbutton.state = inputEventState(event);
513     switch (event->button()) {
514     case MiddleButton:
515         xbutton.button = Button2;
516         break;
517     case RightButton:
518         xbutton.button = Button3;
519         break;
520     case LeftButton:
521     default:
522         xbutton.button = Button1;
523         break;
524     }
525     xbutton.same_screen = true;
526 }
527
528 static void setXMotionEventSpecificFields(XEvent* xEvent, MouseEvent* event, const IntPoint& postZoomPos)
529 {
530     XMotionEvent& xmotion = xEvent->xmotion;
531     xmotion.type = MotionNotify;
532     xmotion.root = QX11Info::appRootWindow();
533     xmotion.subwindow = 0;
534     xmotion.time = event->timeStamp();
535     xmotion.x = postZoomPos.x();
536     xmotion.y = postZoomPos.y();
537     xmotion.x_root = event->screenX();
538     xmotion.y_root = event->screenY();
539     xmotion.state = inputEventState(event);
540     xmotion.is_hint = NotifyNormal;
541     xmotion.same_screen = true;
542 }
543
544 static void setXCrossingEventSpecificFields(XEvent* xEvent, MouseEvent* event, const IntPoint& postZoomPos)
545 {
546     XCrossingEvent& xcrossing = xEvent->xcrossing;
547     xcrossing.type = event->type() == eventNames().mouseoverEvent ? EnterNotify : LeaveNotify;
548     xcrossing.root = QX11Info::appRootWindow();
549     xcrossing.subwindow = 0;
550     xcrossing.time = event->timeStamp();
551     xcrossing.x = postZoomPos.y();
552     xcrossing.y = postZoomPos.x();
553     xcrossing.x_root = event->screenX();
554     xcrossing.y_root = event->screenY();
555     xcrossing.state = inputEventState(event);
556     xcrossing.mode = NotifyNormal;
557     xcrossing.detail = NotifyDetailNone;
558     xcrossing.same_screen = true;
559     xcrossing.focus = false;
560 }
561
562 void PluginView::handleMouseEvent(MouseEvent* event)
563 {
564     if (m_isWindowed)
565         return;
566
567     if (event->button() == RightButton && m_plugin->quirks().contains(PluginQuirkIgnoreRightClickInWindowlessMode))
568         return;
569
570     if (event->type() == eventNames().mousedownEvent) {
571         // Give focus to the plugin on click
572         if (Page* page = m_parentFrame->page())
573             page->focusController()->setActive(true);
574
575         focusPluginElement();
576     }
577
578     XEvent npEvent;
579     initXEvent(&npEvent);
580
581     IntPoint postZoomPos = roundedIntPoint(m_element->renderer()->absoluteToLocal(event->absoluteLocation()));
582
583     if (event->type() == eventNames().mousedownEvent || event->type() == eventNames().mouseupEvent)
584         setXButtonEventSpecificFields(&npEvent, event, postZoomPos);
585     else if (event->type() == eventNames().mousemoveEvent)
586         setXMotionEventSpecificFields(&npEvent, event, postZoomPos);
587     else if (event->type() == eventNames().mouseoutEvent || event->type() == eventNames().mouseoverEvent)
588         setXCrossingEventSpecificFields(&npEvent, event, postZoomPos);
589     else
590         return;
591
592     if (dispatchNPEvent(npEvent))
593         event->setDefaultHandled();
594 }
595
596 void PluginView::handleFocusInEvent()
597 {
598     XEvent npEvent;
599     initXEvent(&npEvent);
600
601     XFocusChangeEvent& event = npEvent.xfocus;
602     event.type = 9; /* int as Qt unsets FocusIn */
603     event.mode = NotifyNormal;
604     event.detail = NotifyDetailNone;
605
606     dispatchNPEvent(npEvent);
607 }
608
609 void PluginView::handleFocusOutEvent()
610 {
611     XEvent npEvent;
612     initXEvent(&npEvent);
613
614     XFocusChangeEvent& event = npEvent.xfocus;
615     event.type = 10; /* int as Qt unsets FocusOut */
616     event.mode = NotifyNormal;
617     event.detail = NotifyDetailNone;
618
619     dispatchNPEvent(npEvent);
620 }
621
622 void PluginView::setParent(ScrollView* parent)
623 {
624     Widget::setParent(parent);
625
626     if (parent)
627         init();
628 }
629
630 void PluginView::setNPWindowRect(const IntRect&)
631 {
632     if (!m_isWindowed)
633         setNPWindowIfNeeded();
634 }
635
636 void PluginView::setNPWindowIfNeeded()
637 {
638     if (!m_isStarted || !parent() || !m_plugin->pluginFuncs()->setwindow)
639         return;
640
641     // If the plugin didn't load sucessfully, no point in calling setwindow
642     if (m_status != PluginStatusLoadedSuccessfully)
643         return;
644
645     // On Unix, only call plugin if it's full-page or windowed
646     if (m_mode != NP_FULL && m_mode != NP_EMBED)
647         return;
648
649     // Check if the platformPluginWidget still exists
650     if (m_isWindowed && !platformPluginWidget())
651         return;
652
653     if (!m_hasPendingGeometryChange)
654         return;
655     m_hasPendingGeometryChange = false;
656
657     if (m_isWindowed) {
658         platformPluginWidget()->setGeometry(m_windowRect);
659
660         // Cut out areas of the plugin occluded by iframe shims
661         Vector<IntRect> cutOutRects;
662         QRegion clipRegion = QRegion(m_clipRect);
663         getPluginOcclusions(m_element, this->parent(), frameRect(), cutOutRects);
664         for (size_t i = 0; i < cutOutRects.size(); i++) {
665             cutOutRects[i].move(-frameRect().x(), -frameRect().y());
666             clipRegion = clipRegion.subtracted(QRegion(cutOutRects[i]));
667         }
668         // if setMask is set with an empty QRegion, no clipping will
669         // be performed, so in that case we hide the plugin view
670         platformPluginWidget()->setVisible(!clipRegion.isEmpty());
671         platformPluginWidget()->setMask(clipRegion);
672
673         m_npWindow.x = m_windowRect.x();
674         m_npWindow.y = m_windowRect.y();
675     } else {
676         m_npWindow.x = 0;
677         m_npWindow.y = 0;
678     }
679
680     // If the width or height are null, set the clipRect to null, indicating that
681     // the plugin is not visible/scrolled out.
682     if (!m_clipRect.width() || !m_clipRect.height()) {
683         m_npWindow.clipRect.left = 0;
684         m_npWindow.clipRect.right = 0;
685         m_npWindow.clipRect.top = 0;
686         m_npWindow.clipRect.bottom = 0;
687     } else {
688         // Clipping rectangle of the plug-in; the origin is the top left corner of the drawable or window. 
689         m_npWindow.clipRect.left = m_npWindow.x + m_clipRect.x();
690         m_npWindow.clipRect.top = m_npWindow.y + m_clipRect.y();
691         m_npWindow.clipRect.right = m_npWindow.x + m_clipRect.x() + m_clipRect.width();
692         m_npWindow.clipRect.bottom = m_npWindow.y + m_clipRect.y() + m_clipRect.height();
693     }
694
695     if (m_plugin->quirks().contains(PluginQuirkDontCallSetWindowMoreThanOnce)) {
696         // FLASH WORKAROUND: Only set initially. Multiple calls to
697         // setNPWindow() cause the plugin to crash in windowed mode.
698         if (!m_isWindowed || m_npWindow.width == -1 || m_npWindow.height == -1) {
699             m_npWindow.width = m_windowRect.width();
700             m_npWindow.height = m_windowRect.height();
701         }
702     } else {
703         m_npWindow.width = m_windowRect.width();
704         m_npWindow.height = m_windowRect.height();
705     }
706
707     PluginView::setCurrentPluginView(this);
708 #if USE(JSC)
709     JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
710 #endif
711     setCallingPlugin(true);
712     m_plugin->pluginFuncs()->setwindow(m_instance, &m_npWindow);
713     setCallingPlugin(false);
714     PluginView::setCurrentPluginView(0);
715 }
716
717 void PluginView::setParentVisible(bool visible)
718 {
719     if (isParentVisible() == visible)
720         return;
721
722     Widget::setParentVisible(visible);
723
724     if (isSelfVisible() && platformPluginWidget())
725         platformPluginWidget()->setVisible(visible);
726 }
727
728 NPError PluginView::handlePostReadFile(Vector<char>& buffer, uint32_t len, const char* buf)
729 {
730     String filename(buf, len);
731
732     if (filename.startsWith("file:///"))
733         filename = filename.substring(8);
734
735     long long size;
736     if (!getFileSize(filename, size))
737         return NPERR_FILE_NOT_FOUND;
738
739     FILE* fileHandle = fopen((filename.utf8()).data(), "r");
740     if (!fileHandle)
741         return NPERR_FILE_NOT_FOUND;
742
743     buffer.resize(size);
744     int bytesRead = fread(buffer.data(), 1, size, fileHandle);
745
746     fclose(fileHandle);
747
748     if (bytesRead <= 0)
749         return NPERR_FILE_NOT_FOUND;
750
751     return NPERR_NO_ERROR;
752 }
753
754 bool PluginView::platformGetValueStatic(NPNVariable variable, void* value, NPError* result)
755 {
756     switch (variable) {
757     case NPNVToolkit:
758         *static_cast<uint32_t*>(value) = 0;
759         *result = NPERR_NO_ERROR;
760         return true;
761
762     case NPNVSupportsXEmbedBool:
763         *static_cast<NPBool*>(value) = true;
764         *result = NPERR_NO_ERROR;
765         return true;
766
767     case NPNVjavascriptEnabledBool:
768         *static_cast<NPBool*>(value) = true;
769         *result = NPERR_NO_ERROR;
770         return true;
771
772     case NPNVSupportsWindowless:
773         *static_cast<NPBool*>(value) = true;
774         *result = NPERR_NO_ERROR;
775         return true;
776
777 #if defined(MOZ_PLATFORM_MAEMO) && (MOZ_PLATFORM_MAEMO >= 5)
778     case NPNVSupportsWindowlessLocal:
779         *static_cast<NPBool*>(value) = true;
780         *result = NPERR_NO_ERROR;
781         return true;
782 #endif
783
784     default:
785         return false;
786     }
787 }
788
789 bool PluginView::platformGetValue(NPNVariable variable, void* value, NPError* result)
790 {
791     switch (variable) {
792     case NPNVxDisplay:
793         *(void **)value = QX11Info::display();
794         *result = NPERR_NO_ERROR;
795         return true;
796
797     case NPNVxtAppContext:
798         *result = NPERR_GENERIC_ERROR;
799         return true;
800
801     case NPNVnetscapeWindow: {
802         void* w = reinterpret_cast<void*>(value);
803         QWebPageClient* client = m_parentFrame->view()->hostWindow()->platformPageClient();
804         *((XID *)w) = client ? client->ownerWidget()->window()->winId() : 0;
805         *result = NPERR_NO_ERROR;
806         return true;
807     }
808
809     case NPNVToolkit:
810         if (m_plugin->quirks().contains(PluginQuirkRequiresGtkToolKit)) {
811             *((uint32_t *)value) = 2;
812             *result = NPERR_NO_ERROR;
813             return true;
814         }
815         return false;
816
817     default:
818         return false;
819     }
820 }
821
822 void PluginView::invalidateRect(const IntRect& rect)
823 {
824 #if USE(ACCELERATED_COMPOSITING) && !USE(TEXTURE_MAPPER)
825     if (m_platformLayer) {
826         m_platformLayer->update(QRectF(rect));
827         return;
828     }
829 #endif
830
831     if (m_isWindowed) {
832         if (platformWidget()) {
833             // update() will schedule a repaint of the widget so ensure
834             // its knowledge of its position on the page is up to date.
835             platformWidget()->setGeometry(m_windowRect);
836             platformWidget()->update(rect);
837         }
838         return;
839     }
840
841     invalidateWindowlessPluginRect(rect);
842 }
843
844 void PluginView::invalidateRect(NPRect* rect)
845 {
846     if (!rect) {
847         invalidate();
848         return;
849     }
850     IntRect r(rect->left, rect->top, rect->right - rect->left, rect->bottom - rect->top);
851     invalidateRect(r);
852 }
853
854 void PluginView::invalidateRegion(NPRegion region)
855 {
856     Q_UNUSED(region);
857     invalidate();
858 }
859
860 void PluginView::forceRedraw()
861 {
862     invalidate();
863 }
864
865 static Display *getPluginDisplay()
866 {
867     // The plugin toolkit might run using a different X connection. At the moment, we only
868     // support gdk based plugins (like flash) that use a different X connection.
869     // The code below has the same effect as this one:
870     // Display *gdkDisplay = gdk_x11_display_get_xdisplay(gdk_display_get_default());
871     QLibrary library(QLatin1String("libgdk-x11-2.0"), 0);
872     if (!library.load())
873         return 0;
874
875     typedef void *(*gdk_init_check_ptr)(void*, void*);
876     gdk_init_check_ptr gdk_init_check = (gdk_init_check_ptr)library.resolve("gdk_init_check");
877     if (!gdk_init_check)
878         return 0;
879
880     typedef void *(*gdk_display_get_default_ptr)();
881     gdk_display_get_default_ptr gdk_display_get_default = (gdk_display_get_default_ptr)library.resolve("gdk_display_get_default");
882     if (!gdk_display_get_default)
883         return 0;
884
885     typedef void *(*gdk_x11_display_get_xdisplay_ptr)(void *);
886     gdk_x11_display_get_xdisplay_ptr gdk_x11_display_get_xdisplay = (gdk_x11_display_get_xdisplay_ptr)library.resolve("gdk_x11_display_get_xdisplay");
887     if (!gdk_x11_display_get_xdisplay)
888         return 0;
889
890     gdk_init_check(0, 0);
891     return (Display*)gdk_x11_display_get_xdisplay(gdk_display_get_default());
892 }
893
894 static void getVisualAndColormap(int depth, Visual **visual, Colormap *colormap)
895 {
896     *visual = 0;
897     *colormap = 0;
898
899 #ifndef QT_NO_XRENDER
900     static const bool useXRender = qgetenv("QT_X11_NO_XRENDER").isNull(); // Should also check for XRender >= 0.5
901 #else
902     static const bool useXRender = false;
903 #endif
904
905     if (!useXRender && depth == 32)
906         return;
907
908     int nvi;
909     XVisualInfo templ;
910     templ.screen  = QX11Info::appScreen();
911     templ.depth   = depth;
912     templ.c_class = TrueColor;
913     XVisualInfo* xvi = XGetVisualInfo(QX11Info::display(), VisualScreenMask | VisualDepthMask | VisualClassMask, &templ, &nvi);
914
915     if (!xvi)
916         return;
917
918 #ifndef QT_NO_XRENDER
919     if (depth == 32) {
920         for (int idx = 0; idx < nvi; ++idx) {
921             XRenderPictFormat* format = XRenderFindVisualFormat(QX11Info::display(), xvi[idx].visual);
922             if (format->type == PictTypeDirect && format->direct.alphaMask) {
923                  *visual = xvi[idx].visual;
924                  break;
925             }
926          }
927     } else
928 #endif // QT_NO_XRENDER
929         *visual = xvi[0].visual;
930
931     XFree(xvi);
932
933     if (*visual)
934         *colormap = XCreateColormap(QX11Info::display(), QX11Info::appRootWindow(), *visual, AllocNone);
935 }
936
937 bool PluginView::platformStart()
938 {
939     ASSERT(m_isStarted);
940     ASSERT(m_status == PluginStatusLoadedSuccessfully);
941
942     if (m_plugin->pluginFuncs()->getvalue) {
943         PluginView::setCurrentPluginView(this);
944 #if USE(JSC)
945         JSC::JSLock::DropAllLocks dropAllLocks(JSC::SilenceAssertionsOnly);
946 #endif
947         setCallingPlugin(true);
948         m_plugin->pluginFuncs()->getvalue(m_instance, NPPVpluginNeedsXEmbed, &m_needsXEmbed);
949         setCallingPlugin(false);
950         PluginView::setCurrentPluginView(0);
951     }
952
953     if (m_isWindowed) {
954         QWebPageClient* client = m_parentFrame->view()->hostWindow()->platformPageClient();
955         if (m_needsXEmbed && client) {
956             setPlatformWidget(new PluginContainerQt(this, client->ownerWidget()));
957             // sync our XEmbed container window creation before sending the xid to plugins.
958             QApplication::syncX();
959         } else {
960             notImplemented();
961             m_status = PluginStatusCanNotLoadPlugin;
962             return false;
963         }
964     } else {
965         setPlatformWidget(0);
966         m_pluginDisplay = getPluginDisplay();
967
968 #if USE(ACCELERATED_COMPOSITING) && !USE(TEXTURE_MAPPER)
969         if (shouldUseAcceleratedCompositing()) {
970             m_platformLayer = adoptPtr(new PluginGraphicsLayerQt(this));
971             // Trigger layer computation in RenderLayerCompositor
972             m_element->setNeedsStyleRecalc(SyntheticStyleChange);
973         }
974 #endif
975     }
976
977     // If the width and the height are not zero we show the PluginView.
978     if (!frameRect().isEmpty())
979         show();
980
981     NPSetWindowCallbackStruct* wsi = new NPSetWindowCallbackStruct();
982     wsi->type = 0;
983
984     if (m_isWindowed) {
985         const QX11Info* x11Info = &platformPluginWidget()->x11Info();
986
987         wsi->display = x11Info->display();
988         wsi->visual = (Visual*)x11Info->visual();
989         wsi->depth = x11Info->depth();
990         wsi->colormap = x11Info->colormap();
991
992         m_npWindow.type = NPWindowTypeWindow;
993         m_npWindow.window = (void*)platformPluginWidget()->winId();
994         m_npWindow.width = -1;
995         m_npWindow.height = -1;
996     } else {
997         const QX11Info* x11Info = &QApplication::desktop()->x11Info();
998
999         if (x11Info->depth() == 32 || !m_plugin->quirks().contains(PluginQuirkRequiresDefaultScreenDepth)) {
1000             getVisualAndColormap(32, &m_visual, &m_colormap);
1001             wsi->depth = 32;
1002         }
1003
1004         if (!m_visual) {
1005             getVisualAndColormap(x11Info->depth(), &m_visual, &m_colormap);
1006             wsi->depth = x11Info->depth();
1007         }
1008
1009         wsi->display = x11Info->display();
1010         wsi->visual = m_visual;
1011         wsi->colormap = m_colormap;
1012
1013         m_npWindow.type = NPWindowTypeDrawable;
1014         m_npWindow.window = 0; // Not used?
1015         m_npWindow.x = 0;
1016         m_npWindow.y = 0;
1017         m_npWindow.width = -1;
1018         m_npWindow.height = -1;
1019     }
1020
1021     m_npWindow.ws_info = wsi;
1022
1023     if (!(m_plugin->quirks().contains(PluginQuirkDeferFirstSetWindowCall))) {
1024         updatePluginWidget();
1025         setNPWindowIfNeeded();
1026     }
1027
1028     return true;
1029 }
1030
1031 void PluginView::platformDestroy()
1032 {
1033     if (platformPluginWidget())
1034         delete platformPluginWidget();
1035
1036     if (m_drawable)
1037         XFreePixmap(QX11Info::display(), m_drawable);
1038
1039     if (m_colormap)
1040         XFreeColormap(QX11Info::display(), m_colormap);
1041 }
1042
1043 #if USE(ACCELERATED_COMPOSITING)
1044 PlatformLayer* PluginView::platformLayer() const
1045 {
1046     return m_platformLayer.get();
1047 }
1048 #endif
1049
1050 } // namespace WebCore