Complete carve up of main.c

This commit is contained in:
Johan Malm 2019-12-27 22:34:00 +00:00
parent 657f90e09b
commit b5c307c32b
4 changed files with 206 additions and 203 deletions

View file

@ -131,6 +131,7 @@ void xwl_surface_destroy(struct wl_listener *listener, void *data);
void xwl_surface_configure(struct wl_listener *listener, void *data);
void xwl_surface_new(struct wl_listener *listener, void *data);
void view_focus_last_toplevel(struct server *server);
void focus_view(struct view *view, struct wlr_surface *surface);
void view_focus_next_toplevel(struct server *server);
void begin_interactive(struct view *view, enum cursor_mode mode,
@ -143,6 +144,8 @@ struct view *desktop_view_at(struct server *server, double lx, double ly,
/* TODO: try to refactor to remove from header file */
struct view *first_toplevel(struct server *server);
void server_new_input(struct wl_listener *listener, void *data);
void seat_request_cursor(struct wl_listener *listener, void *data);
void server_cursor_motion(struct wl_listener *listener, void *data);
void server_cursor_motion_absolute(struct wl_listener *listener, void *data);
void server_cursor_button(struct wl_listener *listener, void *data);

203
main.c
View file

@ -6,209 +6,6 @@
#include <wlr/types/wlr_gamma_control_v1.h>
#include <wlr/types/wlr_primary_selection_v1.h>
static struct view *last_toplevel(struct server *server)
{
struct view *view;
wl_list_for_each_reverse (view, &server->views, link) {
if (!view->been_mapped) {
continue;
}
if (is_toplevel(view)) {
return view;
}
}
fprintf(stderr, "warn: found no toplevel view (%s)\n", __func__);
return NULL;
}
static void view_focus_last_toplevel(struct server *server)
{
/* TODO: write view_nr_toplevel_views() */
if (wl_list_length(&server->views) < 2) {
return;
}
struct view *view = last_toplevel(server);
focus_view(view, view->surface);
}
static void keyboard_handle_modifiers(struct wl_listener *listener, void *data)
{
/* This event is raised when a modifier key, such as shift or alt, is
* pressed. We simply communicate this to the client. */
struct keyboard *keyboard =
wl_container_of(listener, keyboard, modifiers);
/*
* A seat can only have one keyboard, but this is a limitation of the
* Wayland protocol - not wlroots. We assign all connected keyboards to
* the same seat. You can swap out the underlying wlr_keyboard like this
* and wlr_seat handles this transparently.
*/
wlr_seat_set_keyboard(keyboard->server->seat, keyboard->device);
/* Send modifiers to the client. */
wlr_seat_keyboard_notify_modifiers(
keyboard->server->seat, &keyboard->device->keyboard->modifiers);
}
static bool handle_keybinding(struct server *server, xkb_keysym_t sym)
{
/*
* Here we handle compositor keybindings. This is when the compositor is
* processing keys, rather than passing them on to the client for its
* own processing.
*
* This function assumes Alt is held down.
*/
switch (sym) {
case XKB_KEY_Escape:
wl_display_terminate(server->wl_display);
break;
case XKB_KEY_F1:
case XKB_KEY_F2:
view_focus_last_toplevel(server);
break;
case XKB_KEY_F3:
if (fork() == 0) {
execl("/bin/dmenu_run", "/bin/dmenu_run", (void *)NULL);
}
break;
case XKB_KEY_F6:
begin_interactive(first_toplevel(server), TINYWL_CURSOR_MOVE,
0);
break;
case XKB_KEY_F12:
dbg_show_views(server);
break;
default:
return false;
}
return true;
}
static void keyboard_handle_key(struct wl_listener *listener, void *data)
{
/* This event is raised when a key is pressed or released. */
struct keyboard *keyboard = wl_container_of(listener, keyboard, key);
struct server *server = keyboard->server;
struct wlr_event_keyboard_key *event = data;
struct wlr_seat *seat = server->seat;
/* Translate libinput keycode -> xkbcommon */
uint32_t keycode = event->keycode + 8;
/* Get a list of keysyms based on the keymap for this keyboard */
const xkb_keysym_t *syms;
int nsyms = xkb_state_key_get_syms(
keyboard->device->keyboard->xkb_state, keycode, &syms);
bool handled = false;
uint32_t modifiers =
wlr_keyboard_get_modifiers(keyboard->device->keyboard);
if ((modifiers & WLR_MODIFIER_ALT) && event->state == WLR_KEY_PRESSED) {
/* If alt is held down and this button was _pressed_, we attempt
* to process it as a compositor keybinding. */
for (int i = 0; i < nsyms; i++) {
handled = handle_keybinding(server, syms[i]);
}
}
if (!handled) {
/* Otherwise, we pass it along to the client. */
wlr_seat_set_keyboard(seat, keyboard->device);
wlr_seat_keyboard_notify_key(seat, event->time_msec,
event->keycode, event->state);
}
}
static void server_new_keyboard(struct server *server,
struct wlr_input_device *device)
{
struct keyboard *keyboard = calloc(1, sizeof(struct keyboard));
keyboard->server = server;
keyboard->device = device;
/* We need to prepare an XKB keymap and assign it to the keyboard. This
* assumes the defaults (e.g. layout = "us"). */
struct xkb_rule_names rules = { 0 };
struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
struct xkb_keymap *keymap = xkb_map_new_from_names(
context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS);
wlr_keyboard_set_keymap(device->keyboard, keymap);
xkb_keymap_unref(keymap);
xkb_context_unref(context);
wlr_keyboard_set_repeat_info(device->keyboard, 25, 600);
/* Here we set up listeners for keyboard events. */
keyboard->modifiers.notify = keyboard_handle_modifiers;
wl_signal_add(&device->keyboard->events.modifiers,
&keyboard->modifiers);
keyboard->key.notify = keyboard_handle_key;
wl_signal_add(&device->keyboard->events.key, &keyboard->key);
wlr_seat_set_keyboard(server->seat, device);
/* And add the keyboard to our list of keyboards */
wl_list_insert(&server->keyboards, &keyboard->link);
}
static void server_new_pointer(struct server *server,
struct wlr_input_device *device)
{
/* We don't do anything special with pointers. All of our pointer
* handling is proxied through wlr_cursor. On another compositor, you
* might take this opportunity to do libinput configuration on the
* device to set acceleration, etc. */
wlr_cursor_attach_input_device(server->cursor, device);
}
static void server_new_input(struct wl_listener *listener, void *data)
{
/* This event is raised by the backend when a new input device becomes
* available. */
struct server *server = wl_container_of(listener, server, new_input);
struct wlr_input_device *device = data;
switch (device->type) {
case WLR_INPUT_DEVICE_KEYBOARD:
server_new_keyboard(server, device);
break;
case WLR_INPUT_DEVICE_POINTER:
server_new_pointer(server, device);
break;
default:
break;
}
/* We need to let the wlr_seat know what our capabilities are, which is
* communiciated to the client. In TinyWL we always have a cursor, even
* if there are no pointer devices, so we always include that
* capability. */
uint32_t caps = WL_SEAT_CAPABILITY_POINTER;
if (!wl_list_empty(&server->keyboards)) {
caps |= WL_SEAT_CAPABILITY_KEYBOARD;
}
wlr_seat_set_capabilities(server->seat, caps);
}
static void seat_request_cursor(struct wl_listener *listener, void *data)
{
struct server *server =
wl_container_of(listener, server, request_cursor);
/* This event is rasied by the seat when a client provides a cursor
* image */
struct wlr_seat_pointer_request_set_cursor_event *event = data;
struct wlr_seat_client *focused_client =
server->seat->pointer_state.focused_client;
/* This can be sent by any client, so we check to make sure this one is
* actually has pointer focus first. */
if (focused_client == event->seat_client) {
/* Once we've vetted the client, we can tell the cursor to use
* the provided surface as the cursor image. It will set the
* hardware cursor on the output that it's currently on and
* continue to do so as the cursor moves between outputs. */
wlr_cursor_set_surface(server->cursor, event->surface,
event->hotspot_x, event->hotspot_y);
}
}
int main(int argc, char *argv[])
{
wlr_log_init(WLR_ERROR, NULL);

177
server.c
View file

@ -1,5 +1,182 @@
#include "labwc.h"
static void keyboard_handle_modifiers(struct wl_listener *listener, void *data)
{
/* This event is raised when a modifier key, such as shift or alt, is
* pressed. We simply communicate this to the client. */
struct keyboard *keyboard =
wl_container_of(listener, keyboard, modifiers);
/*
* A seat can only have one keyboard, but this is a limitation of the
* Wayland protocol - not wlroots. We assign all connected keyboards to
* the same seat. You can swap out the underlying wlr_keyboard like this
* and wlr_seat handles this transparently.
*/
wlr_seat_set_keyboard(keyboard->server->seat, keyboard->device);
/* Send modifiers to the client. */
wlr_seat_keyboard_notify_modifiers(
keyboard->server->seat, &keyboard->device->keyboard->modifiers);
}
static bool handle_keybinding(struct server *server, xkb_keysym_t sym)
{
/*
* Here we handle compositor keybindings. This is when the compositor is
* processing keys, rather than passing them on to the client for its
* own processing.
*
* This function assumes Alt is held down.
*/
switch (sym) {
case XKB_KEY_Escape:
wl_display_terminate(server->wl_display);
break;
case XKB_KEY_F1:
case XKB_KEY_F2:
view_focus_last_toplevel(server);
break;
case XKB_KEY_F3:
if (fork() == 0) {
execl("/bin/dmenu_run", "/bin/dmenu_run", (void *)NULL);
}
break;
case XKB_KEY_F6:
begin_interactive(first_toplevel(server), TINYWL_CURSOR_MOVE,
0);
break;
case XKB_KEY_F12:
dbg_show_views(server);
break;
default:
return false;
}
return true;
}
static void keyboard_handle_key(struct wl_listener *listener, void *data)
{
/* This event is raised when a key is pressed or released. */
struct keyboard *keyboard = wl_container_of(listener, keyboard, key);
struct server *server = keyboard->server;
struct wlr_event_keyboard_key *event = data;
struct wlr_seat *seat = server->seat;
/* Translate libinput keycode -> xkbcommon */
uint32_t keycode = event->keycode + 8;
/* Get a list of keysyms based on the keymap for this keyboard */
const xkb_keysym_t *syms;
int nsyms = xkb_state_key_get_syms(
keyboard->device->keyboard->xkb_state, keycode, &syms);
bool handled = false;
uint32_t modifiers =
wlr_keyboard_get_modifiers(keyboard->device->keyboard);
if ((modifiers & WLR_MODIFIER_ALT) && event->state == WLR_KEY_PRESSED) {
/* If alt is held down and this button was _pressed_, we attempt
* to process it as a compositor keybinding. */
for (int i = 0; i < nsyms; i++) {
handled = handle_keybinding(server, syms[i]);
}
}
if (!handled) {
/* Otherwise, we pass it along to the client. */
wlr_seat_set_keyboard(seat, keyboard->device);
wlr_seat_keyboard_notify_key(seat, event->time_msec,
event->keycode, event->state);
}
}
static void server_new_keyboard(struct server *server,
struct wlr_input_device *device)
{
struct keyboard *keyboard = calloc(1, sizeof(struct keyboard));
keyboard->server = server;
keyboard->device = device;
/* We need to prepare an XKB keymap and assign it to the keyboard. This
* assumes the defaults (e.g. layout = "us"). */
struct xkb_rule_names rules = { 0 };
struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
struct xkb_keymap *keymap = xkb_map_new_from_names(
context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS);
wlr_keyboard_set_keymap(device->keyboard, keymap);
xkb_keymap_unref(keymap);
xkb_context_unref(context);
wlr_keyboard_set_repeat_info(device->keyboard, 25, 600);
/* Here we set up listeners for keyboard events. */
keyboard->modifiers.notify = keyboard_handle_modifiers;
wl_signal_add(&device->keyboard->events.modifiers,
&keyboard->modifiers);
keyboard->key.notify = keyboard_handle_key;
wl_signal_add(&device->keyboard->events.key, &keyboard->key);
wlr_seat_set_keyboard(server->seat, device);
/* And add the keyboard to our list of keyboards */
wl_list_insert(&server->keyboards, &keyboard->link);
}
static void server_new_pointer(struct server *server,
struct wlr_input_device *device)
{
/* We don't do anything special with pointers. All of our pointer
* handling is proxied through wlr_cursor. On another compositor, you
* might take this opportunity to do libinput configuration on the
* device to set acceleration, etc. */
wlr_cursor_attach_input_device(server->cursor, device);
}
void server_new_input(struct wl_listener *listener, void *data)
{
/* This event is raised by the backend when a new input device becomes
* available. */
struct server *server = wl_container_of(listener, server, new_input);
struct wlr_input_device *device = data;
switch (device->type) {
case WLR_INPUT_DEVICE_KEYBOARD:
server_new_keyboard(server, device);
break;
case WLR_INPUT_DEVICE_POINTER:
server_new_pointer(server, device);
break;
default:
break;
}
/* We need to let the wlr_seat know what our capabilities are, which is
* communiciated to the client. In TinyWL we always have a cursor, even
* if there are no pointer devices, so we always include that
* capability. */
uint32_t caps = WL_SEAT_CAPABILITY_POINTER;
if (!wl_list_empty(&server->keyboards)) {
caps |= WL_SEAT_CAPABILITY_KEYBOARD;
}
wlr_seat_set_capabilities(server->seat, caps);
}
void seat_request_cursor(struct wl_listener *listener, void *data)
{
struct server *server =
wl_container_of(listener, server, request_cursor);
/* This event is rasied by the seat when a client provides a cursor
* image */
struct wlr_seat_pointer_request_set_cursor_event *event = data;
struct wlr_seat_client *focused_client =
server->seat->pointer_state.focused_client;
/* This can be sent by any client, so we check to make sure this one is
* actually has pointer focus first. */
if (focused_client == event->seat_client) {
/* Once we've vetted the client, we can tell the cursor to use
* the provided surface as the cursor image. It will set the
* hardware cursor on the output that it's currently on and
* continue to do so as the cursor moves between outputs. */
wlr_cursor_set_surface(server->cursor, event->surface,
event->hotspot_x, event->hotspot_y);
}
}
static void process_cursor_move(struct server *server, uint32_t time)
{
/* Move the grabbed view to the new position. */

26
view.c
View file

@ -1,5 +1,31 @@
#include "labwc.h"
static struct view *last_toplevel(struct server *server)
{
struct view *view;
wl_list_for_each_reverse (view, &server->views, link) {
if (!view->been_mapped) {
continue;
}
if (is_toplevel(view)) {
return view;
}
}
fprintf(stderr, "warn: found no toplevel view (%s)\n", __func__);
return NULL;
}
void view_focus_last_toplevel(struct server *server)
{
/* TODO: write view_nr_toplevel_views() */
if (wl_list_length(&server->views) < 2) {
return;
}
struct view *view = last_toplevel(server);
focus_view(view, view->surface);
}
static void activate_view(struct view *view)
{
if (view->type == LAB_XDG_SHELL_VIEW) {