Skip to content

Commit

Permalink
Options to send keyboard/mouse events only when keyboard/mouse are gr…
Browse files Browse the repository at this point in the history
…abbed
  • Loading branch information
phd committed Feb 24, 2025
1 parent 27612ee commit 50a7d48
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
12 changes: 12 additions & 0 deletions vncviewer/DesktopWindow.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,18 @@ void DesktopWindow::toggleForceGrab() {
}
}

bool DesktopWindow::isKeyboardGrabbed() const {
return keyboardGrabbed;
}

bool DesktopWindow::isMouseGrabbed() const {
return mouseGrabbed;
}

bool DesktopWindow::isForceGrabbed() const {
return forceGrabbed;
}

int DesktopWindow::handle(int event)
{
switch (event) {
Expand Down
4 changes: 4 additions & 0 deletions vncviewer/DesktopWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ class DesktopWindow : public Fl_Window {

void toggleForceGrab();

bool isKeyboardGrabbed() const;
bool isMouseGrabbed() const;
bool isForceGrabbed() const;

private:
static void menuOverlay(void *data);

Expand Down
35 changes: 27 additions & 8 deletions vncviewer/Viewport.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void Viewport::setCursor(int width, int height, const Point& hotspot,

void Viewport::showCursor()
{
if (viewOnly) {
if (viewOnly || ungrabbedGrabOnlyMouse()) {
window()->cursor(FL_CURSOR_DEFAULT);
return;
}
Expand All @@ -249,15 +249,15 @@ void Viewport::showCursor()

void Viewport::handleClipboardRequest()
{
if (viewOnly)
if (viewOnly || ungrabbedGrabOnlyKeyboard())
return;

Fl::paste(*this, clipboardSource);
}

void Viewport::handleClipboardAnnounce(bool available)
{
if (viewOnly)
if (viewOnly || ungrabbedGrabOnlyKeyboard())
return;

if (!acceptClipboard)
Expand Down Expand Up @@ -314,7 +314,7 @@ void Viewport::setLEDState(unsigned int ledState)
return;
}

if (viewOnly)
if (viewOnly || ungrabbedGrabOnlyKeyboard())
return;

if (!hasFocus())
Expand All @@ -327,7 +327,7 @@ void Viewport::pushLEDState()
{
unsigned int ledState;

if (viewOnly)
if (viewOnly || ungrabbedGrabOnlyKeyboard())
return;

// Server support?
Expand Down Expand Up @@ -517,7 +517,7 @@ int Viewport::handle(int event)

void Viewport::sendPointerEvent(const rfb::Point& pos, uint16_t buttonMask)
{
if (viewOnly)
if (viewOnly || ungrabbedGrabOnlyMouse())
return;

if ((pointerEventInterval == 0) || (buttonMask != lastButtonMask)) {
Expand Down Expand Up @@ -553,7 +553,7 @@ void Viewport::handleClipboardChange(int source, void *data)

assert(self);

if (viewOnly)
if (viewOnly || self->ungrabbedGrabOnlyKeyboard())
return;

if (!sendClipboard)
Expand Down Expand Up @@ -672,7 +672,7 @@ void Viewport::handleKeyPress(int systemKeyCode,
return;
}

if (viewOnly)
if (viewOnly || ungrabbedGrabOnlyKeyboard())
return;

try {
Expand All @@ -696,9 +696,13 @@ void Viewport::handleKeyRelease(int systemKeyCode,
// Right Ctrl
if (keySym == FL_Control_R) {
((DesktopWindow*)window())->toggleForceGrab();
showCursor();
return;
}

if (ungrabbedGrabOnlyKeyboard())
return;

try {
cc->sendKeyRelease(systemKeyCode);
} catch (std::exception& e) {
Expand Down Expand Up @@ -824,6 +828,7 @@ void Viewport::popupContextMenu()
window()->fullscreen_off();
else
((DesktopWindow*)window())->fullscreen_on();
showCursor();
break;
case ID_MINIMIZE:
#ifdef __APPLE__
Expand Down Expand Up @@ -900,3 +905,17 @@ void Viewport::handleOptions(void *data)
if (Fl::belowmouse() == self)
self->showCursor();
}

bool Viewport::ungrabbedGrabOnlyKeyboard() const {
if (grabOnly || grabOnlyKeyboard) {
return !((DesktopWindow*)window())->isKeyboardGrabbed();
}
return false;
}

bool Viewport::ungrabbedGrabOnlyMouse() const {
if (grabOnly || grabOnlyMouse) {
return !((DesktopWindow*)window())->isMouseGrabbed();
}
return false;
}
3 changes: 3 additions & 0 deletions vncviewer/Viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ class Viewport : public Fl_Widget, protected EmulateMB,

static void handleOptions(void *data);

bool ungrabbedGrabOnlyKeyboard() const;
bool ungrabbedGrabOnlyMouse() const;

private:
CConn* cc;

Expand Down
12 changes: 12 additions & 0 deletions vncviewer/parameters.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ BoolParameter remoteResize("RemoteResize",
BoolParameter viewOnly("ViewOnly",
"Don't send any mouse or keyboard events to the server",
false);
BoolParameter grabOnlyKeyboard("GrabOnlyKeyboard",
"Send keyboard events to the server only when keyboard grab is active.",
false);
BoolParameter grabOnlyMouse("GrabOnlyMouse",
"Send mouse events to the server only when mouse grab is active.",
false);
BoolParameter grabOnly("GrabOnly",
"Activates both GrabOnlyKeyboard and GrabOnlyMouse.",
false);
BoolParameter shared("Shared",
"Don't disconnect other viewers upon connection - "
"share the desktop instead",
Expand Down Expand Up @@ -207,6 +216,9 @@ static VoidParameter* parameterArray[] = {
&fullScreenSelectedMonitors,
/* Input */
&viewOnly,
&grabOnlyKeyboard,
&grabOnlyMouse,
&grabOnly,
&emulateMiddleButton,
&alwaysCursor,
&cursorType,
Expand Down
3 changes: 3 additions & 0 deletions vncviewer/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ extern rfb::BoolParameter remoteResize;
extern rfb::BoolParameter listenMode;

extern rfb::BoolParameter viewOnly;
extern rfb::BoolParameter grabOnlyKeyboard;
extern rfb::BoolParameter grabOnlyMouse;
extern rfb::BoolParameter grabOnly;
extern rfb::BoolParameter shared;

extern rfb::BoolParameter acceptClipboard;
Expand Down

0 comments on commit 50a7d48

Please sign in to comment.