📄 qx11embed_x11.cpp
字号:
#endif // In this message's l[2] we have the max version // supported by both the client and the // container. QX11EmbedWidget does not use this field. // We have been embedded, so we set our // client's embedded flag. d->setEmbedded(); emit embedded(); } break; case XEMBED_FOCUS_IN: // in case we embed more than one topLevel window inside the same // host window. if (window() != qApp->activeWindow()) qApp->setActiveWindow(this); switch (event->xclient.data.l[2]) { case XEMBED_FOCUS_CURRENT: // The container sends us this message if it wants // us to focus on the widget that last had focus. // This is the reply when XEMBED_REQUEST_FOCUS is // sent to the container. if (!d->currentFocus.isNull()) { if (!d->currentFocus->hasFocus()) d->currentFocus->setFocus(Qt::OtherFocusReason); } else { // No widget currently has focus. We set focus // on the first widget next to the // client widget. Since the setFocus will not work // if the window is disabled, set the currentFocus // directly so that it's set on window activate. d->currentFocus = d->getFocusWidget(QX11EmbedWidgetPrivate::FirstFocusWidget); d->currentFocus->setFocus(Qt::OtherFocusReason); } break; case XEMBED_FOCUS_FIRST: // The container sends this message when it wants // us to focus on the first widget in our focus // chain (typically because of a tab). d->currentFocus = d->getFocusWidget(QX11EmbedWidgetPrivate::FirstFocusWidget); d->currentFocus->setFocus(Qt::TabFocusReason); break; case XEMBED_FOCUS_LAST: // The container sends this message when it wants // us to focus on the last widget in our focus // chain (typically because of a backtab). d->currentFocus = d->getFocusWidget(QX11EmbedWidgetPrivate::LastFocusWidget); d->currentFocus->setFocus(Qt::BacktabFocusReason); break; default: // Ignore any other XEMBED_FOCUS_IN details. break; } break; case XEMBED_FOCUS_OUT: // The container sends us this message when it wants us // to lose focus and forget about the widget that last // had focus. Typically sent by the container when it // loses focus because of mouse or tab activity. We do // then not want to set focus on anything if we're // activated. d->clearFocus(); break; default: // Ignore any other XEMBED messages. break; }; } else { // Non-XEMBED client messages are not interesting. } break; default: // Ignore all other x11 events. break; } // Allow default handling. return QWidget::x11Event(event);}/*! \reimp*/bool QX11EmbedWidget::event(QEvent *event){ if (event->type() == QEvent::ParentChange) { XSelectInput(x11Info().display(), internalWinId(), KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | KeymapStateMask | ButtonMotionMask | PointerMotionMask | FocusChangeMask | ExposureMask | StructureNotifyMask | SubstructureNotifyMask | PropertyChangeMask); } return QWidget::event(event);}/*! \reimp*/void QX11EmbedWidget::resizeEvent(QResizeEvent *event){ if (layout()) layout()->update(); QWidget::resizeEvent(event);}/*! If the widget is embedded, returns the window ID of the container; otherwize returns 0.*/WId QX11EmbedWidget::containerWinId() const{ Q_D(const QX11EmbedWidget); return d->container;}class QX11EmbedContainerPrivate : public QWidgetPrivate{ Q_DECLARE_PUBLIC(QX11EmbedContainer)public: inline QX11EmbedContainerPrivate() { client = 0; focusProxy = 0; clientIsXEmbed = false; xgrab = false; } bool isEmbedded() const; void moveInputToProxy(); void acceptClient(WId window); void rejectClient(WId window); void checkGrab(); WId topLevelParentWinId() const; void emitError(QX11EmbedContainer::Error error) { Q_Q(QX11EmbedContainer); lastError = error; emit q->error(error); } WId client; QWidget *focusProxy; bool clientIsXEmbed; bool xgrab; QRect clientOriginalRect; QSize wmMinimumSizeHint; QX11EmbedContainer::Error lastError;};/*! Creates a QX11EmbedContainer object with the given \a parent.*/QX11EmbedContainer::QX11EmbedContainer(QWidget *parent) : QWidget(*new QX11EmbedContainerPrivate, parent, 0){ Q_D(QX11EmbedContainer); XSetErrorHandler(x11ErrorHandler); initXEmbedAtoms(x11Info().display()); createWinId(); setFocusPolicy(Qt::StrongFocus); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); // ### PORT setKeyCompression(false); setAcceptDrops(true); setEnabled(false); // Everybody gets a focus proxy, but only one toplevel container's // focus proxy is actually in use. d->focusProxy = new QWidget(this); d->focusProxy->setGeometry(-1, -1, 1, 1); // We need events from the window (activation status) and // from qApp (keypress/release). qApp->installEventFilter(this); window()->installEventFilter(this); // Install X11 event filter. if (!oldX11EventFilter) oldX11EventFilter = QCoreApplication::instance()->setEventFilter(x11EventFilter); XSelectInput(x11Info().display(), internalWinId(), KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | KeymapStateMask | PointerMotionMask | EnterWindowMask | LeaveWindowMask | FocusChangeMask | ExposureMask | StructureNotifyMask | SubstructureNotifyMask); // Make sure our new event mask takes effect as soon as possible. XFlush(x11Info().display()); // Move input to our focusProxy if this widget is active, and not // shaded by a modal dialog (in which case isActiveWindow() would // still return true, but where we must not move input focus). if (qApp->activeWindow() == window() && !d->isEmbedded()) d->moveInputToProxy();#ifdef QX11EMBED_DEBUG qDebug() << "QX11EmbedContainer::QX11EmbedContainer: constructed container" << (void *)this << "with winId" << winId();#endif}/*! Destructs a QX11EmbedContainer.*/QX11EmbedContainer::~QX11EmbedContainer(){ Q_D(QX11EmbedContainer); if (d->client) { XUnmapWindow(x11Info().display(), d->client); XReparentWindow(x11Info().display(), d->client, x11Info().appRootWindow(), 0, 0); } if (d->xgrab) XUngrabButton(x11Info().display(), AnyButton, AnyModifier, internalWinId());}QX11EmbedContainer::Error QX11EmbedContainer::error() const { return d_func()->lastError;}/*! \reimp*/void QX11EmbedContainer::paintEvent(QPaintEvent *){}/*! \internal Returns wether or not the windows' embedded flag is set.*/bool QX11EmbedContainerPrivate::isEmbedded() const{ Q_Q(const QX11EmbedContainer); return ((QHackWidget *)q->window())->topData()->embedded == 1;}/*! \internal Returns the parentWinId of the window.*/WId QX11EmbedContainerPrivate::topLevelParentWinId() const{ Q_Q(const QX11EmbedContainer); return ((QHackWidget *)q->window())->topData()->parentWinId;}/*! If the container has an embedded widget, this function returns the X11 window ID of the client; otherwise it returns 0.*/WId QX11EmbedContainer::clientWinId() const{ Q_D(const QX11EmbedContainer); return d->client;}/*! Instructs the container to embed the X11 window with window ID \a id. The client widget will then move on top of the container window and be resized to fit into the container. The \a id should be the ID of a window controlled by an XEmbed enabled application, but this is not mandatory. If \a id does not belong to an XEmbed client widget, then focus handling, activation, accelerators and other features will not work properly.*/void QX11EmbedContainer::embedClient(WId id){ Q_D(QX11EmbedContainer); if (id == 0) { d->emitError(InvalidWindowID); return; } // Walk up the tree of parent windows to prevent embedding of ancestors. WId thisId = internalWinId(); Window rootReturn; Window parentReturn; Window *childrenReturn = 0; unsigned int nchildrenReturn; do { if (XQueryTree(x11Info().display(), thisId, &rootReturn, &parentReturn, &childrenReturn, &nchildrenReturn) == 0) { d->emitError(InvalidWindowID); return; } if (childrenReturn) { XFree(childrenReturn); childrenReturn = 0; } thisId = parentReturn; if (id == thisId) { d->emitError(InvalidWindowID); return; } } while (thisId != rootReturn); // watch for property notify events (see below) XGrabServer(x11Info().display()); XWindowAttributes attrib; if (!XGetWindowAttributes(x11Info().display(), id, &attrib)) { XUngrabServer(x11Info().display()); d->emitError(InvalidWindowID); return; } XSelectInput(x11Info().display(), id, attrib.your_event_mask | PropertyChangeMask | StructureNotifyMask); XUngrabServer(x11Info().display()); // Put the window into WithdrawnState XUnmapWindow(x11Info().display(), id); XSync(x11Info().display(), False); // make sure the window is hidden /* Wait for notification from the window manager that the window is in withdrawn state. According to the ICCCM section 4.1.3.1, we should wait for the WM_STATE property to either be deleted or set to WithdrawnState. For safety, we will not wait more than 500 ms, so that we can preemptively workaround buggy window managers. */ QTime t; t.start(); functorData data; data.id = id; data.rootWindow = attrib.root; data.clearedWmState = false; data.reparentedToRoot = false; do { if (t.elapsed() > 500) // time-out after 500 ms break; XEvent event; if (!XCheckIfEvent(x11Info().display(), &event, functor, (XPointer) &data)) { XSync(x11Info().display(), False); usleep(50000); continue; } qApp->x11ProcessEvent(&event); } while (!data.clearedWmState || !data.reparentedToRoot); // restore the event mask XSelectInput(x11Info().display(), id, attrib.your_event_mask); switch (XReparentWindow(x11Info().display(), id, internalWinId(), 0, 0)) { case BadWindow: case BadMatch: d->emitError(InvalidWindowID); break; default: break; }}/*! \internal Handles key, activation and focus events for the container.*/bool QX11EmbedContainer::eventFilter(QObject *o, QEvent *event){ Q_D(QX11EmbedContainer); switch (event->type()) { case QEvent::KeyPress: // Forward any keypresses to our client. if (o == this && d->client) { lastKeyEvent.window = d->client; XSendEvent(x11Info().display(), d->client, false, KeyPressMask, (XEvent *) &lastKeyEvent); return true; } break; case QEvent::KeyRelease: // Forward any keyreleases to our client. if (o == this && d->client) { lastKeyEvent.window = d->client; XSendEvent(x11Info().display(), d->client, false, KeyReleaseMask, (XEvent *) &lastKeyEvent); return true; } break; case QEvent::WindowActivate: // When our container window is activated, we pass the // activation message on to our client. Note that X input // focus is set to our focus proxy. We want to intercept all // keypresses. if (o == window() && d->client) { if (!d->isEmbedded()) d->moveInputToProxy(); if (d->clientIsXEmbed) { sendXEmbedMessage(d->client, x11Info().display(), XEMBED_WINDOW_ACTIVATE); } else { d->checkGrab(); if (hasFocus()) XSetInputFocus(x11Info().display(), d->client, XRevertToParent, x11Time()); } } break; case QEvent::WindowDeactivate: // When our container window is deactivated, we pass the // deactivation message to our client. if (o == window() && d->client) { if (d->clientIsXEmbed) sendXEmbedMessage(d->client, x11Info().display(), XEMBED_WINDOW_DEACTIVATE); else d->checkGrab(); } break; case QEvent::FocusIn: // When receiving FocusIn events generated by Tab or Backtab, // we pass focus on to our client. Any mouse activity is sent // directly to the client, and it will ask us for focus with // XEMBED_REQUEST_FOCUS. if (o == this && d->client) { if (d->clientIsXEmbed) { if (!d->isEmbedded()) d->moveInputToProxy(); QFocusEvent *fe = (QFocusEvent *)event; switch (fe->reason()) { case Qt::TabFocusReason: sendXEmbedMessage(d->client, x11Info().display(), XEMBED_FOCUS_IN, XEMBED_FOCUS_FIRST); break; case Qt::BacktabFocusReason: sendXEmbedMessage(d->client, x11Info().display(), XEMBED_FOCUS_IN, XEMBED_FOCUS_LAST); break; default: break; } } else { d->checkGrab(); XSetInputFocus(x11Info().display(), d->client, XRevertToParent, x11Time());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -