PDA

View Full Version : How to determine desktop number?!?



Szyk
17th November 2018, 16:43
Hi!
I am writing text editor in multiplatform way. In the past I most time spending on Windows, but now I am back to the Linux and I want next version of my text editor will be usable on Linux also.
Now am working with Kubuntu 18.10 with Kde Plasma windows manager. I have 4 virtual desktops. Most of the time I am working only with first of them. But I can image to work with file manager on some other screen and I want to run my editor from file manager many times (intense working with source files in many languages). I do some basic tests and I am failed to find "right Qt way" to determine number of desktop on which my text editor is running. I try:
1. Identify by screen pointer:


MainWindow w;
reinterpret_cast<quint64>(w.windowHandle()->screen()
Which is wrong because it is unique from instance to instance.
2. Find siblings of current screen:

QList<QScreen*> lScreens(w.windowHandle()->screen()->virtualSiblings());
for(QScreen* lScreen : lScreens)
qInfo() << "Detected screen: " << reinterpret_cast<quint64>(lScreen);

But it return only first screen (3 rest are ignored).
3. Identify by primaryScreen pointer:

qInfo() << "Detected screen: " << reinterpret_cast<quint64>(a.primaryScreen());
But it failed also (it is unique from instance to instance).
4. QDesktopWidget::screenNumber()
But it always return 0 (no mater what is current desktop).

Do you have any other ideas?

tuli
18th November 2018, 10:26
Did you pass your text editors widget to QDesktopWidget::screenNumber(QWidget*) ? Show some code.

Szyk
18th November 2018, 13:50
qInfo() << "Detected screen: " << QApplication::desktop()->screenNumber(&w);
Where "w" is MainWindow. It always return just:

Detected screen: 0
More!

qInfo() << "Screen count: " << QApplication::desktop()->screenCount();
Returns:

Screen count: 1

Added after 1 29 minutes:

I don't found Qt solution. Below is X11 specific solution.
Note: God/right programing way is to encapsulate specific code in to easily identified blocks. So, I have Application class which handle local socket and translations. This X11 specific code I exclude in to Application.Linux.cpp (and another Application.Windwos.cpp - for similiar reasons) and in pro file I add conditional directives:


linux: SOURCES += Src/Application.Linux.cpp
win32: SOURCES += Src/Application.Windows.cpp


My Application.Linux.cpp is as follow:


#include "Application.h"
#include <X11/Xlib.h>
#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>

quint32 Application::currentDesktopNumber()
{
Display* lDisplay = XOpenDisplay(nullptr);
int lScreen = DefaultScreen(lDisplay);
Window lRootWindow = RootWindow(lDisplay, lScreen);
Atom lAtom(XInternAtom(lDisplay, "_NET_CURRENT_DESKTOP", True));
if(lAtom == None)
qFatal("no atom\n");

unsigned long nitems, leftover;
unsigned char* lData = nullptr;
int actual_format;
Atom actual_type;

// if(XGetWindowProperty(lDisplay, lRootWindow, lAtom, 0L, 8, False, AnyPropertyType, nullptr, nullptr, nullptr, nullptr, &lData) != Success)
if(XGetWindowProperty(lDisplay, lRootWindow, lAtom, 0L, 8, False, AnyPropertyType, &actual_type, &actual_format, &nitems, &leftover, &lData) != Success)
qFatal("XGetWindowProperty failed!");

quint32 lResult = lData[0];
delete [] lData;

return lResult;
}

void Application::raiseAnotherInstance(quint32 /*aProcesId*/)
{
}

Solution inspired by: https://forums.gentoo.org/viewtopic-t-241716-start-0.html

tuli
18th November 2018, 18:43
Works perfectly fine for me.



log(QString("screen nr: %1").arg(QApplication::desktop()->screenNumber(this)));


Put this in a button-click-handler of mainwindow, it outputs 0 or 1 depending on which screen I drag the window.

Szyk
10th December 2018, 15:17
Great! But: What it will be happen when you create window on specific screen?!? You will get 0!