@@ -45,17 +45,56 @@ namespace {
4545 // -----------------------------------------------------------------------------------------------
4646 QPixmap grabScreenDBusKde ()
4747 {
48- QDBusInterface interface (QStringLiteral (" org.kde.KWin" ),
49- QStringLiteral (" /Screenshot" ),
50- QStringLiteral (" org.kde.kwin.Screenshot" ));
51- QDBusReply<QString> reply = interface.call (QStringLiteral (" screenshotFullscreen" ));
52- QPixmap pm (reply.value ());
53- if (!pm.isNull ()) {
54- QFile::remove (reply.value ());
55- } else {
56- logError (desktop) << LinuxDesktop::tr (" Screenshot via KDE DBus interface failed." );
48+ // Create a temporary file to receive the screenshot data
49+ QTemporaryFile tempFile;
50+ if (!tempFile.open ()) {
51+ qDebug () << " Failed to create temporary file" ;
52+ return QPixmap ();
5753 }
58- return pm;
54+
55+ QDBusUnixFileDescriptor pipe (tempFile.handle ());
56+
57+ QVariantMap options;
58+ options[" include-cursor" ] = false ; // Include mouse cursor in screenshot
59+ options[" include-decoration" ] = false ; // Include window decorations
60+ options[" native-resolution" ] = true ; // Use native resolution
61+
62+ // Create DBus interface
63+ QDBusInterface interface (
64+ QStringLiteral (" org.kde.KWin.ScreenShot2" ),
65+ QStringLiteral (" /org/kde/KWin/ScreenShot2" ),
66+ QStringLiteral (" org.kde.KWin.ScreenShot2" )
67+ );
68+
69+ QList<QVariant> args;
70+ args << QVariant::fromValue (options)
71+ << QVariant::fromValue (pipe);
72+
73+ QDBusReply<QVariantMap> reply = interface.callWithArgumentList (
74+ QDBus::Block,
75+ QStringLiteral (" CaptureActiveScreen" ),
76+ args
77+ );
78+
79+ if (!reply.isValid ()) {
80+ qDebug () << " Screenshot failed:" << reply.error ().message ();
81+ return QPixmap ();
82+ }
83+
84+ QVariantMap results = reply.value ();
85+
86+ if (results[" status" ].toString () != " ok" ) {
87+ qDebug () << " Screenshot failed with status:" << results[" status" ].toString ();
88+ qDebug () << " Error message:" << results[" error" ].toString ();
89+ return QPixmap ();
90+ }
91+
92+ tempFile.seek (0 );
93+
94+ QImage screenshot;
95+ screenshot.load (&tempFile, " PNG" ); // Format should match what KWin writes
96+
97+ return QPixmap::fromImage (screenshot);
5998 }
6099#endif // HAS_Qt_DBus
61100
0 commit comments