Skip to content

Commit

Permalink
Ctrl-Q to quit
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanGriffiths committed Dec 4, 2023
1 parent 2f5548c commit 16d5662
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
48 changes: 39 additions & 9 deletions examples/client/mir_shell_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
#include <wayland-client.h>
#include <wayland-client-core.h>

#include <linux/input-event-codes.h>
#include <X11/X.h>
#include <getopt.h>
#include <linux/input-event-codes.h>

#include <memory>
#include <string.h>
Expand Down Expand Up @@ -188,6 +189,14 @@ class window
}

virtual void handle_mouse_button(wl_pointer*, uint32_t serial, uint32_t time, uint32_t button, uint32_t state);
virtual void handle_keyboard_key(wl_keyboard* keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state);
virtual void handle_keyboard_modifiers(
wl_keyboard* keyboard,
uint32_t serial,
uint32_t mods_depressed,
uint32_t mods_latched,
uint32_t mods_locked,
uint32_t group);

bool has_mouse_focus() { return mouse_focus == this; }
bool has_keyboard_focus() { return keyboard_focus == this; }
Expand Down Expand Up @@ -215,14 +224,6 @@ class window
void handle_keyboard_keymap(wl_keyboard* keyboard, uint32_t format, int32_t fd, uint32_t size);
void handle_keyboard_enter(wl_keyboard* keyboard, uint32_t serial, wl_surface* surface, wl_array* keys);
void handle_keyboard_leave(wl_keyboard* keyboard, uint32_t serial, wl_surface* surface);
void handle_keyboard_key(wl_keyboard* keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state);
void handle_keyboard_modifiers(
wl_keyboard* keyboard,
uint32_t serial,
uint32_t mods_depressed,
uint32_t mods_latched,
uint32_t mods_locked,
uint32_t group);
void handle_keyboard_repeat_info(wl_keyboard* wl_keyboard, int32_t rate, int32_t delay);

void update_free_buffers(wl_buffer* buffer);
Expand Down Expand Up @@ -282,8 +283,18 @@ class normal_window : public grey_window
normal_window(int32_t width, int32_t height);
~normal_window();

protected:
void
handle_keyboard_key(wl_keyboard* keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) override;

void handle_keyboard_modifiers(
wl_keyboard* keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked,
uint32_t group) override;

private:
zmir_mir_normal_surface_v1* const mir_normal_surface;

uint32_t modifiers = 0;
};

void handle_registry_global(
Expand Down Expand Up @@ -710,6 +721,25 @@ normal_window::~normal_window()
}
}

void normal_window::handle_keyboard_key(wl_keyboard* keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
{
grey_window::handle_keyboard_key(keyboard, serial, time, key, state);

if (modifiers == ControlMask && key == KEY_Q)
{
mir::client::WaylandRunner::quit();
}
}

void normal_window::handle_keyboard_modifiers(
wl_keyboard* keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked,
uint32_t group)
{
grey_window::handle_keyboard_modifiers(keyboard, serial, mods_depressed, mods_latched, mods_locked, group);

modifiers = mods_depressed;
}

class satellite : public grey_window
{
public:
Expand Down
19 changes: 17 additions & 2 deletions examples/client/wayland_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,23 @@ mir::client::WaylandRunner::WaylandRunner()
sigaction(SIGHUP, &sig_handler_new, NULL);
}

mir::client::WaylandRunner mir::client::WaylandRunner::instance;

void mir::client::WaylandRunner::run(wl_display* display)
{
static WaylandRunner the_instance;
the_instance(display);
instance(display);
}

void mir::client::WaylandRunner::quit_()
{
if (eventfd_write(shutdown_signal, 1) == -1)
{
printf("Failed to execute a shutdown");
exit(-1);
}
}

void mir::client::WaylandRunner::quit()
{
instance.quit_();
}
3 changes: 3 additions & 0 deletions examples/client/wayland_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ class WaylandRunner
{
public:
static void run(wl_display* display);
static void quit();
private:
static WaylandRunner instance;
void quit_();
void operator()(wl_display* display);
WaylandRunner();
};
Expand Down

0 comments on commit 16d5662

Please sign in to comment.