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