Skip to content

Commit db5ab5f

Browse files
joevtdingusdev
authored andcommitted
display_sdl: Make auto grab mouse optional.
Add a command line option to enable auto grab. Default to the previous behavior before the auto grab feature was added. Add a debugger command to enable or disable auto grab. Notes: Auto grab mouse currently only works for graphics controllers and guest OSs that use a hardware cursor and only if the emulation sets cursor_dirty to true (i.e. not emulation that uses cursor_ovl_cb).
1 parent d542d9e commit db5ab5f

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

‎debugger/debugger.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ using namespace std;
5858
#define COUT08X COUT0_X(8)
5959
#define COUT04X COUT0_X(4)
6060

61+
extern bool g_auto_grab_mouse;
62+
6163
static uint32_t str2addr(string& addr_str) {
6264
try {
6365
return static_cast<uint32_t>(stoul(addr_str, NULL, 0));
@@ -116,6 +118,8 @@ static void show_help() {
116118
cout << " printenv -- print current NVRAM settings." << endl;
117119
cout << " setenv V N -- set NVRAM variable V to value N." << endl;
118120
cout << endl;
121+
cout << " autograbmouse H -- auto grab the mouse if H is not zero." << endl;
122+
cout << endl;
119123
cout << " restart -- restart the machine" << endl;
120124
cout << " quit -- quit the debugger" << endl;
121125
cout << endl;
@@ -1169,6 +1173,18 @@ void DppcDebugger::enter_debugger() {
11691173
via_obj->assert_int(irq_bit);
11701174
});
11711175
#endif
1176+
} else if (cmd == "autograbmouse") {
1177+
cmd = "";
1178+
string value;
1179+
int num;
1180+
ss >> value;
1181+
try {
1182+
num = str2num(value);
1183+
} catch (invalid_argument& exc) {
1184+
cout << exc.what() << endl;
1185+
continue;
1186+
}
1187+
g_auto_grab_mouse = num;
11721188
} else {
11731189
if (!cmd.empty()) {
11741190
cout << "Unknown command: " << cmd << endl;

‎devices/video/display_sdl.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
2626
#include <cmath>
2727
#include <string>
2828

29+
bool g_auto_grab_mouse = false;
30+
2931
static const char * get_full_screen_mode_string(int scale_mode) {
3032
#define onemode(x) case Display::x: return #x ;
3133
switch(scale_mode) {
@@ -316,6 +318,7 @@ void Display::handle_events(const WindowEvent& wnd_event) {
316318
if (wnd_event.window_id == impl->disp_wnd_id) {
317319
SDL_SetHint(SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED, "0");
318320
SDL_SetWindowKeyboardGrab(impl->display_wnd, SDL_TRUE);
321+
if (g_auto_grab_mouse) {
319322
// When the window (re)gains focus (e.g. the user clicks it), SDL
320323
// may have dropped the relative mouse grab on focus loss. Re-grab
321324
// it so the guest cursor keeps working; this is the reliable
@@ -324,6 +327,7 @@ void Display::handle_events(const WindowEvent& wnd_event) {
324327
SDL_SetRelativeMouseMode(SDL_TRUE);
325328
this->update_window_title();
326329
}
330+
}
327331
}
328332
break;
329333

@@ -449,7 +453,7 @@ void Display::update_window_title()
449453
+ " " + std::to_string(int(std::round(impl->renderer_scale_x * 100))) + "%";
450454
if (is_grabbed)
451455
new_window_title += " (Grabbed, Ctrl+G to release)";
452-
else if (impl->guest_cursor_drawn)
456+
else if (!g_auto_grab_mouse || impl->guest_cursor_drawn)
453457
new_window_title += " (Press Ctrl+G to grab)";
454458

455459
if (new_window_title != old_window_title)
@@ -482,6 +486,7 @@ void Display::update(std::function<void(uint8_t *dst_buf, int dst_pitch)> conver
482486
SDL_RenderClear(impl->renderer);
483487
SDL_RenderCopy(impl->renderer, impl->disp_texture, NULL, &impl->dest_rect);
484488

489+
if (g_auto_grab_mouse) {
485490
bool is_grabbed = SDL_GetRelativeMouseMode();
486491

487492
// The guest draws its own cursor into the framebuffer (hardware cursor).
@@ -520,6 +525,7 @@ void Display::update(std::function<void(uint8_t *dst_buf, int dst_pitch)> conver
520525
impl->guest_cursor_drawn = draw_hw_cursor;
521526
this->update_window_title();
522527
}
528+
}
523529

524530
// draw HW cursor if enabled
525531
if (draw_hw_cursor) {

‎main.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
4949
#endif
5050
using namespace std;
5151

52+
extern bool g_auto_grab_mouse;
53+
5254
static void sigint_handler(int signum) {
5355
power_on = false;
5456
power_off_reason = po_signal_interrupt;
@@ -168,6 +170,9 @@ int main(int argc, char** argv) {
168170
CLI::Option* machine_opt = emu->add_option("-m,--machine",
169171
machine_str, "Specify machine ID");
170172

173+
emu->add_flag("--auto-grab-mouse", g_auto_grab_mouse,
174+
"The guest cursor causes mouse to be grabbed");
175+
171176
auto list_cmd = app.add_subcommand("list",
172177
"Display available machine configurations and exit");
173178

0 commit comments

Comments
 (0)