1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qplatformintegration.h"
5
6#include <qpa/qplatformfontdatabase.h>
7#include <qpa/qplatformclipboard.h>
8#include <qpa/qplatformaccessibility.h>
9#include <qpa/qplatformtheme.h>
10#include <QtGui/private/qguiapplication_p.h>
11#include <QtGui/private/qpixmap_raster_p.h>
12
13#if QT_CONFIG(draganddrop)
14#include <private/qdnd_p.h>
15#include <private/qsimpledrag_p.h>
16#endif
17
18#ifndef QT_NO_SESSIONMANAGER
19# include <qpa/qplatformsessionmanager.h>
20#endif
21
22QT_BEGIN_NAMESPACE
23
24/*!
25 Accessor for the platform integration's fontdatabase.
26
27 Default implementation returns a default QPlatformFontDatabase.
28
29 \sa QPlatformFontDatabase
30*/
31QPlatformFontDatabase *QPlatformIntegration::fontDatabase() const
32{
33 static QPlatformFontDatabase *db = nullptr;
34 if (!db) {
35 db = new QPlatformFontDatabase;
36 }
37 return db;
38}
39
40/*!
41 Accessor for the platform integration's clipboard.
42
43 Default implementation returns a default QPlatformClipboard.
44
45 \sa QPlatformClipboard
46
47*/
48
49#ifndef QT_NO_CLIPBOARD
50
51QPlatformClipboard *QPlatformIntegration::clipboard() const
52{
53 static QPlatformClipboard *clipboard = nullptr;
54 if (!clipboard) {
55 clipboard = new QPlatformClipboard;
56 }
57 return clipboard;
58}
59
60#endif
61
62#if QT_CONFIG(draganddrop)
63/*!
64 Accessor for the platform integration's drag object.
65
66 Default implementation returns QSimpleDrag. This class supports only drag
67 and drop operations within the same Qt application.
68*/
69QPlatformDrag *QPlatformIntegration::drag() const
70{
71 static QSimpleDrag *drag = nullptr;
72 if (!drag) {
73 drag = new QSimpleDrag;
74 }
75 return drag;
76}
77#endif // QT_CONFIG(draganddrop)
78
79QPlatformNativeInterface * QPlatformIntegration::nativeInterface() const
80{
81 return nullptr;
82}
83
84QPlatformServices *QPlatformIntegration::services() const
85{
86 return nullptr;
87}
88
89/*!
90 \class QPlatformIntegration
91 \since 4.8
92 \internal
93 \preliminary
94 \ingroup qpa
95 \brief The QPlatformIntegration class is the entry for WindowSystem specific functionality.
96
97 QPlatformIntegration is the single entry point for windowsystem specific functionality when
98 using the QPA platform. It has factory functions for creating platform specific pixmaps and
99 windows. The class also controls the font subsystem.
100
101 QPlatformIntegration is a singleton class which gets instantiated in the QGuiApplication
102 constructor. The QPlatformIntegration instance do not have ownership of objects it creates in
103 functions where the name starts with create. However, functions which don't have a name
104 starting with create acts as accessors to member variables.
105
106 It is not trivial to create or build a platform plugin outside of the Qt source tree. Therefore
107 the recommended approach for making new platform plugin is to copy an existing plugin inside
108 the QTSRCTREE/src/plugins/platform and develop the plugin inside the source tree.
109
110 The minimal platform integration is the smallest platform integration it is possible to make,
111 which makes it an ideal starting point for new plugins. For a slightly more advanced plugin,
112 consider reviewing the directfb plugin, or the testlite plugin.
113*/
114
115/*!
116 \fn QPlatformPixmap *QPlatformIntegration::createPlatformPixmap(QPlatformPixmap::PixelType type) const
117
118 Factory function for QPlatformPixmap. PixelType can be either PixmapType or BitmapType.
119 \sa QPlatformPixmap
120*/
121
122/*!
123 \fn QPlatformWindow *QPlatformIntegration::createPlatformWindow(QWindow *window) const
124
125 Factory function for QPlatformWindow. The \a window parameter is a pointer to the window
126 which the QPlatformWindow is supposed to be created for.
127
128 All windows have to have a QPlatformWindow, and it will be created on-demand when the
129 QWindow is made visible for the first time, or explicitly through calling QWindow::create().
130
131 In the constructor, of the QPlatformWindow, the window flags, state, title and geometry
132 of the \a window should be applied to the underlying window. If the resulting flags or state
133 differs, the resulting values should be set on the \a window using QWindow::setWindowFlags()
134 or QWindow::setWindowState(), respectively.
135
136 \sa QPlatformWindow, QPlatformWindowFormat
137 \sa createPlatformBackingStore()
138*/
139
140/*!
141 \fn QPlatformBackingStore *QPlatformIntegration::createPlatformBackingStore(QWindow *window) const
142
143 Factory function for QPlatformBackingStore. The QWindow parameter is a pointer to the
144 top level widget(tlw) the window surface is created for. A QPlatformWindow is always created
145 before the QPlatformBackingStore for tlw where the widget also requires a backing store.
146
147 \sa QBackingStore
148 \sa createPlatformWindow()
149*/
150
151/*!
152 \enum QPlatformIntegration::Capability
153
154 Capabilities are used to determine specific features of a platform integration
155
156 \value ThreadedPixmaps The platform uses a pixmap implementation that is reentrant
157 and can be used from multiple threads, like the raster paint engine and QImage based
158 pixmaps.
159
160 \value OpenGL The platform supports OpenGL
161
162 \value ThreadedOpenGL The platform supports using OpenGL outside the GUI thread.
163
164 \value SharedGraphicsCache The platform supports a shared graphics cache
165
166 \value BufferQueueingOpenGL Deprecated. The OpenGL implementation on the platform will
167 queue up buffers when swapBuffers() is called and block only when its buffer pipeline
168 is full, rather than block immediately.
169
170 \value MultipleWindows The platform supports multiple QWindows, i.e. does some kind
171 of compositing either client or server side. Some platforms might only support a
172 single fullscreen window.
173
174 \value ApplicationState The platform handles the application state explicitly.
175 This means that QEvent::ApplicationActivate and QEvent::ApplicationDeativate
176 will not be posted automatically. Instead, the platform must handle application
177 state explicitly by using QWindowSystemInterface::handleApplicationStateChanged().
178 If not set, application state will follow window activation, which is the normal
179 behavior for desktop platforms.
180
181 \value ForeignWindows The platform allows creating QWindows which represent
182 native windows created by other processes or by using native libraries.
183
184 \value NonFullScreenWindows The platform supports top-level windows which do not
185 fill the screen. The default implementation returns \c true. Returning false for
186 this will cause all windows, including dialogs and popups, to be resized to fill the
187 screen.
188
189 \value WindowManagement The platform is based on a system that performs window
190 management. This includes the typical desktop platforms. Can be set to false on
191 platforms where no window management is available, meaning for example that windows
192 are never repositioned by the window manager. The default implementation returns \c true.
193
194 \value AllGLFunctionsQueryable Deprecated. Used to indicate whether the QOpenGLContext
195 backend provided by the platform is
196 able to return function pointers from getProcAddress() even for standard OpenGL
197 functions, for example OpenGL 1 functions like glClear() or glDrawArrays(). This is
198 important because the OpenGL specifications do not require this ability from the
199 getProcAddress implementations of the windowing system interfaces (EGL, WGL, GLX). The
200 platform plugins may however choose to enhance the behavior in the backend
201 implementation for QOpenGLContext::getProcAddress() and support returning a function
202 pointer also for the standard, non-extension functions. This capability is a
203 prerequisite for dynamic OpenGL loading. Starting with Qt 5.7, the platform plugin
204 is required to have this capability.
205
206 \value ApplicationIcon The platform supports setting the application icon. (since 5.5)
207
208 \value TopStackedNativeChildWindows The platform supports native child windows via
209 QWindowContainer without having to punch a transparent hole in the
210 backingstore. (since 5.10)
211
212 \value OpenGLOnRasterSurface The platform supports making a QOpenGLContext current
213 in combination with a QWindow of type RasterSurface.
214
215 \value PaintEvents The platform sends paint events instead of expose events when
216 the window needs repainting. Expose events are only sent when a window is toggled
217 from a non-exposed to exposed state or back.
218
219 \value RhiBasedRendering The platform supports one or more of the 3D rendering APIs
220 that Qt Quick and other components can use via the Qt Rendering Hardware Interface. On
221 platforms where it is clear upfront that the platform cannot, or does not want to,
222 support rendering via 3D graphics APIs such as OpenGL, Vulkan, Direct 3D, or Metal,
223 this capability can be reported as \c false. That in effect means that in modules
224 where there is an alternative, such as Qt Quick with its \c software backend, an
225 automatic fallback to that alternative may occur, if applicable. The default
226 implementation of hasCapability() returns \c true.
227
228 \value ScreenWindowGrabbing The platform supports grabbing window on screen.
229 On Wayland, this capability can be reported as \c false. The default implementation
230 of hasCapability() returns \c true.
231 */
232
233/*!
234
235 \fn QAbstractEventDispatcher *QPlatformIntegration::createEventDispatcher() const = 0
236
237 Factory function for the GUI event dispatcher. The platform plugin should create
238 and return a QAbstractEventDispatcher subclass when this function is called.
239
240 If the platform plugin for some reason creates the event dispatcher outside of
241 this function (for example in the constructor), it needs to handle the case
242 where this function is never called, ensuring that the event dispatcher is
243 still deleted at some point (typically in the destructor).
244
245 Note that the platform plugin should never explicitly set the event dispatcher
246 itself, using QCoreApplication::setEventDispatcher(), but let QCoreApplication
247 decide when and which event dispatcher to create.
248
249 \since 5.2
250*/
251
252bool QPlatformIntegration::hasCapability(Capability cap) const
253{
254 return cap == NonFullScreenWindows || cap == NativeWidgets || cap == WindowManagement
255 || cap == TopStackedNativeChildWindows || cap == WindowActivation
256 || cap == RhiBasedRendering || cap == ScreenWindowGrabbing;
257}
258
259QPlatformPixmap *QPlatformIntegration::createPlatformPixmap(QPlatformPixmap::PixelType type) const
260{
261 return new QRasterPlatformPixmap(type);
262}
263
264#ifndef QT_NO_OPENGL
265/*!
266 Factory function for QPlatformOpenGLContext. The \a context parameter is a pointer to
267 the context for which a platform-specific context backend needs to be
268 created. Configuration settings like the format, share context and screen have to be
269 taken from this QOpenGLContext and the resulting platform context is expected to be
270 backed by a native context that fulfills these criteria.
271
272 If the context has native handles set, no new native context is expected to be created.
273 Instead, the provided handles have to be used. In this case the ownership of the handle
274 must not be taken and the platform implementation is not allowed to destroy the native
275 context. Configuration parameters like the format are also to be ignored. Instead, the
276 platform implementation is responsible for querying the configuriation from the provided
277 native context.
278
279 Returns a pointer to a QPlatformOpenGLContext instance or \nullptr if the context could
280 not be created.
281
282 \sa QOpenGLContext
283*/
284QPlatformOpenGLContext *QPlatformIntegration::createPlatformOpenGLContext(QOpenGLContext *context) const
285{
286 Q_UNUSED(context);
287 qWarning(msg: "This plugin does not support createPlatformOpenGLContext!");
288 return nullptr;
289}
290#endif // QT_NO_OPENGL
291
292/*!
293 Factory function for QPlatformSharedGraphicsCache. This function will return 0 if the platform
294 integration does not support any shared graphics cache mechanism for the given \a cacheId.
295*/
296QPlatformSharedGraphicsCache *QPlatformIntegration::createPlatformSharedGraphicsCache(const char *cacheId) const
297{
298 qWarning(msg: "This plugin does not support createPlatformSharedGraphicsBuffer for cacheId: %s!",
299 cacheId);
300 return nullptr;
301}
302
303/*!
304 Factory function for QPaintEngine. This function will return 0 if the platform
305 integration does not support creating any paint engine the given \a paintDevice.
306*/
307QPaintEngine *QPlatformIntegration::createImagePaintEngine(QPaintDevice *paintDevice) const
308{
309 Q_UNUSED(paintDevice);
310 return nullptr;
311}
312
313/*!
314 Performs initialization steps that depend on having an event dispatcher
315 available. Called after the event dispatcher has been created.
316
317 Tasks that require an event dispatcher, for example creating socket notifiers, cannot be
318 performed in the constructor. Instead, they should be performed here. The default
319 implementation does nothing.
320*/
321void QPlatformIntegration::initialize()
322{
323}
324
325/*!
326 Called before the platform integration is deleted. Useful when cleanup relies on virtual
327 functions.
328
329 \since 5.5
330*/
331void QPlatformIntegration::destroy()
332{
333}
334
335/*!
336 Returns the platforms input context.
337
338 The default implementation returns \nullptr, implying no input method support.
339*/
340QPlatformInputContext *QPlatformIntegration::inputContext() const
341{
342 return nullptr;
343}
344
345#if QT_CONFIG(accessibility)
346
347/*!
348 Returns the platforms accessibility.
349
350 The default implementation returns QPlatformAccessibility which
351 delegates handling of accessibility to accessiblebridge plugins.
352*/
353QPlatformAccessibility *QPlatformIntegration::accessibility() const
354{
355 static QPlatformAccessibility *accessibility = nullptr;
356 if (Q_UNLIKELY(!accessibility)) {
357 accessibility = new QPlatformAccessibility;
358 }
359 return accessibility;
360}
361
362#endif
363
364QVariant QPlatformIntegration::styleHint(StyleHint hint) const
365{
366 switch (hint) {
367 case CursorFlashTime:
368 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::CursorFlashTime);
369 case KeyboardInputInterval:
370 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::KeyboardInputInterval);
371 case KeyboardAutoRepeatRate:
372 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::KeyboardAutoRepeatRate);
373 case MouseDoubleClickInterval:
374 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::MouseDoubleClickInterval);
375 case StartDragDistance:
376 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::StartDragDistance);
377 case StartDragTime:
378 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::StartDragTime);
379 case ShowIsFullScreen:
380 return false;
381 case ShowIsMaximized:
382 return false;
383 case ShowShortcutsInContextMenus:
384 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::ShowShortcutsInContextMenus);
385 case PasswordMaskDelay:
386 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::PasswordMaskDelay);
387 case PasswordMaskCharacter:
388 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::PasswordMaskCharacter);
389 case FontSmoothingGamma:
390 return qreal(1.7);
391 case StartDragVelocity:
392 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::StartDragVelocity);
393 case UseRtlExtensions:
394 return QVariant(false);
395 case SetFocusOnTouchRelease:
396 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::SetFocusOnTouchRelease);
397 case MousePressAndHoldInterval:
398 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::MousePressAndHoldInterval);
399 case TabFocusBehavior:
400 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::TabFocusBehavior);
401 case ReplayMousePressOutsidePopup:
402 return true;
403 case ItemViewActivateItemOnSingleClick:
404 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::ItemViewActivateItemOnSingleClick);
405 case UiEffects:
406 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::UiEffects);
407 case WheelScrollLines:
408 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::WheelScrollLines);
409 case MouseQuickSelectionThreshold:
410 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::MouseQuickSelectionThreshold);
411 case MouseDoubleClickDistance:
412 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::MouseDoubleClickDistance);
413 case FlickStartDistance:
414 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::FlickStartDistance);
415 case FlickMaximumVelocity:
416 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::FlickMaximumVelocity);
417 case FlickDeceleration:
418 return QPlatformTheme::defaultThemeHint(hint: QPlatformTheme::FlickDeceleration);
419 case UnderlineShortcut:
420 return true;
421 }
422
423 return 0;
424}
425
426Qt::WindowState QPlatformIntegration::defaultWindowState(Qt::WindowFlags flags) const
427{
428 // Leave popup-windows as is
429 if (flags & Qt::Popup & ~Qt::Window)
430 return Qt::WindowNoState;
431
432 if (flags & Qt::SubWindow)
433 return Qt::WindowNoState;
434
435 if (styleHint(hint: QPlatformIntegration::ShowIsFullScreen).toBool())
436 return Qt::WindowFullScreen;
437 else if (styleHint(hint: QPlatformIntegration::ShowIsMaximized).toBool())
438 return Qt::WindowMaximized;
439
440 return Qt::WindowNoState;
441}
442
443Qt::KeyboardModifiers QPlatformIntegration::queryKeyboardModifiers() const
444{
445 return QGuiApplication::keyboardModifiers();
446}
447
448/*!
449 Should be used to obtain a list of possible shortcuts for the given key
450 event. Shortcuts should be encoded as int(Qt::Key + Qt::KeyboardModifiers).
451
452 One example for more than one possibility is the key combination of Shift+5.
453 That one might trigger a shortcut which is set as "Shift+5" as well as one
454 using %. These combinations depend on the currently set keyboard layout.
455
456 \note This function should be called only from key event handlers.
457*/
458QList<int> QPlatformIntegration::possibleKeys(const QKeyEvent *) const
459{
460 return QList<int>();
461}
462
463QStringList QPlatformIntegration::themeNames() const
464{
465 return QStringList();
466}
467
468class QPlatformTheme *QPlatformIntegration::createPlatformTheme(const QString &name) const
469{
470 Q_UNUSED(name);
471 return new QPlatformTheme;
472}
473
474/*!
475 Factory function for QOffscreenSurface. An offscreen surface will typically be implemented with a
476 pixel buffer (pbuffer). If the platform doesn't support offscreen surfaces, an invisible window
477 will be used by QOffscreenSurface instead.
478*/
479QPlatformOffscreenSurface *QPlatformIntegration::createPlatformOffscreenSurface(QOffscreenSurface *surface) const
480{
481 Q_UNUSED(surface);
482 return nullptr;
483}
484
485#ifndef QT_NO_SESSIONMANAGER
486/*!
487 \since 5.2
488
489 Factory function for QPlatformSessionManager. The default QPlatformSessionManager provides the same
490 functionality as the QSessionManager.
491*/
492QPlatformSessionManager *QPlatformIntegration::createPlatformSessionManager(const QString &id, const QString &key) const
493{
494 return new QPlatformSessionManager(id, key);
495}
496#endif
497
498/*!
499 \since 5.2
500
501 Function to sync the platform integrations state with the window system.
502
503 This is often implemented as a roundtrip from the platformintegration to the window system.
504
505 This function should not call QWindowSystemInterface::flushWindowSystemEvents() or
506 QCoreApplication::processEvents()
507*/
508void QPlatformIntegration::sync()
509{
510}
511
512/*!
513 \since 5.7
514
515 Should sound a bell, using the default volume and sound.
516
517 \sa QApplication::beep()
518*/
519void QPlatformIntegration::beep() const
520{
521}
522
523/*!
524 \since 6.0
525
526 Asks the platform to terminate the application.
527
528 Overrides should ensure there's a callback into the QWSI
529 function handleApplicationTermination so that the quit can
530 be propagated to QtGui and the application.
531*/
532void QPlatformIntegration::quit() const
533{
534 QWindowSystemInterface::handleApplicationTermination<QWindowSystemInterface::SynchronousDelivery>();
535}
536
537#ifndef QT_NO_OPENGL
538/*!
539 Platform integration function for querying the OpenGL implementation type.
540
541 Used only when dynamic OpenGL implementation loading is enabled.
542
543 Subclasses should reimplement this function and return a value based on
544 the OpenGL implementation they have chosen to load.
545
546 \note The return value does not indicate or limit the types of
547 contexts that can be created by a given implementation. For example
548 a desktop OpenGL implementation may be capable of creating OpenGL
549 ES-compatible contexts too.
550
551 \sa QOpenGLContext::openGLModuleType(), QOpenGLContext::isOpenGLES()
552
553 \since 5.3
554 */
555QOpenGLContext::OpenGLModuleType QPlatformIntegration::openGLModuleType()
556{
557 qWarning(msg: "This plugin does not support dynamic OpenGL loading!");
558 return QOpenGLContext::LibGL;
559}
560#endif
561
562/*!
563 \since 5.5
564
565 Platform integration function for setting the application icon.
566
567 \sa QGuiApplication::setWindowIcon()
568*/
569void QPlatformIntegration::setApplicationIcon(const QIcon &icon) const
570{
571 Q_UNUSED(icon);
572}
573
574/*!
575 \since 6.5
576
577 Should set the application's badge to \a number.
578
579 If the number is 0 the badge should be cleared.
580
581 \sa QGuiApplication::setBadge()
582*/
583void QPlatformIntegration::setApplicationBadge(qint64 number)
584{
585 Q_UNUSED(number);
586}
587
588#if QT_CONFIG(vulkan) || defined(Q_QDOC)
589
590/*!
591 Factory function for QPlatformVulkanInstance. The \a instance parameter is a
592 pointer to the instance for which a platform-specific backend needs to be
593 created.
594
595 Returns a pointer to a QPlatformOpenGLContext instance or \nullptr if the context could
596 not be created.
597
598 \sa QVulkanInstance
599 \since 5.10
600*/
601QPlatformVulkanInstance *QPlatformIntegration::createPlatformVulkanInstance(QVulkanInstance *instance) const
602{
603 Q_UNUSED(instance);
604 qWarning(msg: "This plugin does not support createPlatformVulkanInstance");
605 return nullptr;
606}
607
608#endif // QT_CONFIG(vulkan)
609
610QT_END_NAMESPACE
611

source code of qtbase/src/gui/kernel/qplatformintegration.cpp