Skip to content

Commit

Permalink
Show keyboard+mouse grab status in DesktopWindow's label
Browse files Browse the repository at this point in the history
  • Loading branch information
phd committed Feb 22, 2025
1 parent aab903b commit 27612ee
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 40 deletions.
79 changes: 40 additions & 39 deletions vncviewer/DesktopWindow.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ static rfb::LogWriter vlog("DesktopWindow");
// issue for Fl::event_dispatch.
static std::set<DesktopWindow *> instances;

DesktopWindow::DesktopWindow(int w, int h, const char *name,
DesktopWindow::DesktopWindow(int w, int h, const char *name_,
const rfb::PixelFormat& serverPF,
CConn* cc_)
: Fl_Window(w, h), cc(cc_), offscreen(nullptr), overlay(nullptr),
firstUpdate(true),
name(nullptr), firstUpdate(true),
delayedFullscreen(false), sentDesktopSize(false),
pendingRemoteResize(false), lastResize({0, 0}),
keyboardGrabbed(false), mouseGrabbed(false), forceGrabbed(false),
Expand All @@ -109,7 +109,7 @@ DesktopWindow::DesktopWindow(int w, int h, const char *name,

callback(handleClose, this);

setName(name);
setName(name_);

OptionsDialog::addCallback(handleOptions, this);

Expand Down Expand Up @@ -272,6 +272,8 @@ DesktopWindow::~DesktopWindow()

Fl::event_dispatch(Fl::handle_);

free(name);

// FLTK automatically deletes all child widgets, so we shouldn't touch
// them ourselves here
}
Expand All @@ -282,53 +284,48 @@ const rfb::PixelFormat &DesktopWindow::getPreferredPF()
return viewport->getPreferredPF();
}


void DesktopWindow::setName(const char *name)
void DesktopWindow::setName(const char *new_name)
{
char windowNameStr[100];
const char *labelFormat;
size_t maxNameSize;
char truncatedName[sizeof(windowNameStr)];

labelFormat = "%s - TigerVNC";

// Ignore the length of '%s' since it is
// a format marker which won't take up space
maxNameSize = sizeof(windowNameStr) - 1 - strlen(labelFormat) + 2;

if (maxNameSize > strlen(name)) {
// Guaranteed to fit, no need to truncate
strcpy(truncatedName, name);
} else if (maxNameSize <= strlen("...")) {
// Even an ellipsis won't fit
truncatedName[0] = '\0';
} else {
int offset;

// We need to truncate, add an ellipsis
offset = maxNameSize - strlen("...");
strncpy(truncatedName, name, sizeof(truncatedName));
strcpy(truncatedName + offset, "...");
free(name);
name = nullptr;
if (new_name) {
name = strdup(new_name);
}
updateLabel();
}

void DesktopWindow::updateLabel() {
const char *strTitle = " - TigerVNC";
const char *strGrabbed = " [GRAB]";
const size_t maxNameLen = 100;

char *label = (char*)malloc(maxNameLen + strlen(strGrabbed) + strlen(strTitle) + 1);
strcpy(label, "");

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
if (name) {
if (strlen(name) <= maxNameLen) {
strcat(label, name);
} else {
strncat(label, name, maxNameLen - 3);
strcat(label, "...");
}
} else {
strcat(label, "unknown");
}

if (snprintf(windowNameStr, sizeof(windowNameStr), labelFormat,
truncatedName) >= (int)sizeof(windowNameStr)) {
// This is just to shut up the compiler, as we've already made sure
// we won't truncate anything
if (keyboardGrabbed || mouseGrabbed) {
strcat(label, strGrabbed);
}

#pragma GCC diagnostic pop
strcat(label, strTitle);

copy_label(windowNameStr);
}
copy_label(label);

free(label);
}

// Copy the areas of the framebuffer that have been changed (damaged)
// to the displayed window.

void DesktopWindow::updateWindow()
{
if (firstUpdate) {
Expand Down Expand Up @@ -1198,6 +1195,7 @@ void DesktopWindow::grabKeyboard()
#endif

keyboardGrabbed = true;
updateLabel();

if (contains(Fl::belowmouse()))
grabPointer();
Expand All @@ -1210,6 +1208,7 @@ void DesktopWindow::ungrabKeyboard()

forceGrabbed = false;
keyboardGrabbed = false;
updateLabel();

ungrabPointer();

Expand Down Expand Up @@ -1252,13 +1251,15 @@ void DesktopWindow::grabPointer()
#endif

mouseGrabbed = true;
updateLabel();
}


void DesktopWindow::ungrabPointer()
{
forceGrabbed = false;
mouseGrabbed = false;
updateLabel();

#if !defined(WIN32) && !defined(__APPLE__)
x11_ungrab_pointer(fl_xid(this));
Expand Down
6 changes: 5 additions & 1 deletion vncviewer/DesktopWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Fl_Scrollbar;
class DesktopWindow : public Fl_Window {
public:

DesktopWindow(int w, int h, const char *name,
DesktopWindow(int w, int h, const char *name_,
const rfb::PixelFormat& serverPF, CConn* cc_);
~DesktopWindow();

Expand Down Expand Up @@ -124,6 +124,8 @@ class DesktopWindow : public Fl_Window {

static void handleStatsTimeout(void *data);

void updateLabel();

private:
CConn* cc;
Fl_Scrollbar *hscroll, *vscroll;
Expand All @@ -133,6 +135,8 @@ class DesktopWindow : public Fl_Window {
unsigned char overlayAlpha;
struct timeval overlayStart;

char *name;

bool firstUpdate;
bool delayedFullscreen;
bool sentDesktopSize;
Expand Down

0 comments on commit 27612ee

Please sign in to comment.