Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibly better solution to detection of current window for nc #325

Merged
merged 4 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Interpreter/interpret.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void restoreContext(Pointer context) {
** be used to both process the message and return.
*/
template <class... T>
int execError(const std::error_code &error_code, T &&... args) {
int execError(const std::error_code &error_code, T &&...args) {
static char msg[MAX_ERR_MSG_LEN];

std::string str = error_code.message();
Expand All @@ -141,7 +141,7 @@ int execError(const std::error_code &error_code, T &&... args) {
}

template <class... T>
int execError(const char *s1, T &&... args) {
int execError(const char *s1, T &&...args) {
static char msg[MAX_ERR_MSG_LEN];

qsnprintf(msg, sizeof(msg), s1, std::forward<T>(args)...);
Expand Down
2 changes: 1 addition & 1 deletion src/ElidedLabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include <QApplication>
#include <QClipboard>
#include <QContextMenuEvent>
#include <QScreen>
#include <QMenu>
#include <QMimeData>
#include <QScreen>
#include <QTextDocument>

#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
Expand Down
2 changes: 1 addition & 1 deletion src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Main::Main(const QStringList &args) {
i = nextArg(args, i);

Settings::serverNameOverride = args[i];
IsServer = true;
IsServer = true;
} else if (opts && (args[i] == QLatin1String("-font") || args[i] == QLatin1String("-fn"))) {
i = nextArg(args, i);

Expand Down
147 changes: 35 additions & 112 deletions src/NeditServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,119 +15,57 @@
#include <QJsonObject>
#include <QLocalServer>
#include <QLocalSocket>
#include <QScreen>
#include <QThread>

#include <memory>

#if defined(QT_X11)
#include <QX11Info>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#endif

namespace {

#if defined(QT_X11)
/**
* @brief queryDesktop
* @param display
* @param window
* @param deskTopAtom
* @return
*/
long queryDesktop(Display *display, Window window, Atom deskTopAtom) {

Atom actualType;
int actualFormat;
unsigned long nItems;
unsigned long bytesAfter;
unsigned char *prop;

if (XGetWindowProperty(display, window, deskTopAtom, 0, 1, False, AnyPropertyType, &actualType, &actualFormat, &nItems, &bytesAfter, &prop) != Success) {
return -1; // Property not found
}

if (actualType == None) {
return -1; // Property does not exist
}

auto _ = gsl::finally([prop]() {
XFree(prop);
});

if (actualFormat != 32 || nItems != 1) {
return -1; // Wrong format
}

return *reinterpret_cast<const long *>(prop);
}

/**
* @brief QueryCurrentDesktop
* @param display
* @param rootWindow
* @return
*/
long QueryCurrentDesktop(Display *display, Window rootWindow) {

static Atom currentDesktopAtom = static_cast<Atom>(-1);

if (currentDesktopAtom == static_cast<Atom>(-1)) {
currentDesktopAtom = XInternAtom(display, "_NET_CURRENT_DESKTOP", True);
}

if (currentDesktopAtom != None) {
return queryDesktop(display, rootWindow, currentDesktopAtom);
QScreen *screenAt(QPoint pos) {
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
for (QScreen *screen : QApplication::screens()) {
if (screen->geometry().contains(pos)) {
return screen;
}
}

return -1; // No desktop information
return nullptr;
#else
return QApplication::screenAt(pos);
#endif
}

/**
* @brief QueryDesktop
* @param display
* @param window
* @brief isLocatedOnDesktop
* @param widget
* @param currentDesktop
* @return
*/
long QueryDesktop(Display *display, Window window) {
static Atom wmDesktopAtom = static_cast<Atom>(-1);

if (wmDesktopAtom == static_cast<Atom>(-1)) {
wmDesktopAtom = XInternAtom(display, "_NET_WM_DESKTOP", True);
bool isLocatedOnDesktop(QWidget *widget, QScreen *currentDesktop) {
if (!currentDesktop) {
return true;
}

if (wmDesktopAtom != None) {
return queryDesktop(display, window, wmDesktopAtom);
}

return -1; // No desktop information
return screenAt(widget->pos()) == currentDesktop;
}
#endif

/**
* @brief isLocatedOnDesktop
* @param widget
* @brief documentForTargetScreen
* @param currentDesktop
* @return
*/
bool isLocatedOnDesktop(QWidget *widget, long currentDesktop) {
if (currentDesktop == -1) {
return true; /* No desktop information available */
}
#if defined(QT_X11)
DocumentWidget *documentForTargetScreen(QScreen *currentDesktop) {
const std::vector<MainWindow *> windows = MainWindow::allWindows();

Display *TheDisplay = QX11Info::display();
long windowDesktop = QueryDesktop(TheDisplay, widget->winId());
// Find a window on the current desktop to hold the new document
for (MainWindow *window : windows) {

// Sticky windows have desktop 0xFFFFFFFF by convention
if (windowDesktop == currentDesktop || windowDesktop == 0xFFFFFFFFL) {
return true; // Desktop matches, or window is sticky
if (isLocatedOnDesktop(window, currentDesktop)) {
return window->currentDocument();
}
}

return false;
#else
return QApplication::desktop()->screenNumber(widget) == currentDesktop;
#endif
return nullptr;
}

/**
Expand All @@ -136,33 +74,23 @@ bool isLocatedOnDesktop(QWidget *widget, long currentDesktop) {
* @param currentDesktop
* @return
*/
DocumentWidget *findDocumentOnDesktop(int tabbed, long currentDesktop) {
if (tabbed == 0 || (tabbed == -1 && !Preferences::GetPrefOpenInTab())) {
DocumentWidget *findDocumentOnDesktop(int tabbed, QScreen *currentDesktop) {

/* A new window is requested, unless we find an untitled unmodified
document on the current desktop */
if (tabbed == 0 || (tabbed == -1 && !Preferences::GetPrefOpenInTab())) {

// A new window is requested, unless we find an untitled unmodified document on the current desktop
const std::vector<DocumentWidget *> documents = DocumentWidget::allDocuments();
for (DocumentWidget *document : documents) {
if (document->filenameSet() || document->fileChanged() || document->macroCmdData_) {
continue;
}
/* No check for top document here! */

if (isLocatedOnDesktop(document, currentDesktop)) {
return document;
}
}
} else {

const std::vector<MainWindow *> windows = MainWindow::allWindows();

// Find a window on the current desktop to hold the new document
for (MainWindow *window : windows) {

if (isLocatedOnDesktop(window, currentDesktop)) {
return window->currentDocument();
}
}
return documentForTargetScreen(currentDesktop);
}

// No window found on current desktop -> create new window
Expand All @@ -173,13 +101,8 @@ DocumentWidget *findDocumentOnDesktop(int tabbed, long currentDesktop) {
* @brief current_desktop
* @return
*/
long current_desktop() {
#if defined(QT_X11)
Display *TheDisplay = QX11Info::display();
return QueryCurrentDesktop(TheDisplay, RootWindow(TheDisplay, DefaultScreen(TheDisplay)));
#else
return QApplication::desktop()->screenNumber(QApplication::activeWindow());
#endif
QScreen *current_desktop() {
return screenAt(QCursor::pos());
}

}
Expand Down Expand Up @@ -242,7 +165,7 @@ void NeditServer::newConnection() {
int lastIconic = 0;

QPointer<DocumentWidget> lastFile;
const long currentDesktop = current_desktop();
QScreen *const currentDesktop = current_desktop();

auto array = jsonDocument.array();
/* If the command string is empty, put up an empty, Untitled window
Expand Down
8 changes: 4 additions & 4 deletions src/TextArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include <QApplication>
#include <QClipboard>
#include <QScreen>
#include <QFocusEvent>
#include <QFontDatabase>
#include <QMenu>
Expand All @@ -30,6 +29,7 @@
#include <QPainterPath>
#include <QPoint>
#include <QResizeEvent>
#include <QScreen>
#include <QScrollBar>
#include <QShortcut>
#include <QTextCodec>
Expand Down Expand Up @@ -3500,11 +3500,11 @@ void TextArea::updateCalltip(int calltipID) {
// If we're not in strict mode try to keep the tip on-screen
if (calltip_.alignMode == TipAlignMode::Sloppy) {

#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
QScreen *currentScreen = window()->windowHandle()->screen();
#else
#else
QScreen *currentScreen = screen();
#endif
#endif
QRect screenGeometry = currentScreen->geometry();

// make sure tip doesn't run off right or left side of screen
Expand Down
4 changes: 2 additions & 2 deletions src/gap_buffer_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class gap_buffer_iterator {
public:
using difference_type = std::ptrdiff_t;
using iterator_category = std::random_access_iterator_tag;
using pointer = Ch*;
using reference = Ch&;
using pointer = Ch *;
using reference = Ch &;
using value_type = Ch;

public:
Expand Down