make server a global

dwm isn't afraid of global variables
main
Devin J. Pohly 2020-04-21 14:08:33 -05:00
parent 364b664a6c
commit 49c9e62ff4
1 changed files with 105 additions and 132 deletions

237
dwl.c
View File

@ -69,14 +69,12 @@ struct dwl_server {
struct dwl_output { struct dwl_output {
struct wl_list link; struct wl_list link;
struct dwl_server *server;
struct wlr_output *wlr_output; struct wlr_output *wlr_output;
struct wl_listener frame; struct wl_listener frame;
}; };
struct dwl_view { struct dwl_view {
struct wl_list link; struct wl_list link;
struct dwl_server *server;
struct wlr_xdg_surface *xdg_surface; struct wlr_xdg_surface *xdg_surface;
struct wl_listener map; struct wl_listener map;
struct wl_listener unmap; struct wl_listener unmap;
@ -89,7 +87,6 @@ struct dwl_view {
struct dwl_keyboard { struct dwl_keyboard {
struct wl_list link; struct wl_list link;
struct dwl_server *server;
struct wlr_input_device *device; struct wlr_input_device *device;
struct wl_listener modifiers; struct wl_listener modifiers;
@ -106,57 +103,58 @@ typedef union {
typedef struct { typedef struct {
uint32_t mod; uint32_t mod;
xkb_keysym_t keysym; xkb_keysym_t keysym;
void (*func)(struct dwl_server *, const Arg *); void (*func)(const Arg *);
const Arg arg; const Arg arg;
} Key; } Key;
typedef struct { typedef struct {
unsigned int mod; unsigned int mod;
unsigned int button; unsigned int button;
void (*func)(struct dwl_server *, const Arg *); void (*func)(const Arg *);
const Arg arg; const Arg arg;
} Button; } Button;
/* function declarations */ /* function declarations */
static void axisnotify(struct wl_listener *listener, void *data); static void axisnotify(struct wl_listener *listener, void *data);
static void buttonpress(struct wl_listener *listener, void *data); static void buttonpress(struct wl_listener *listener, void *data);
static void createkeyboard(struct dwl_server *server, static void createkeyboard(struct wlr_input_device *device);
struct wlr_input_device *device);
static void createnotify(struct wl_listener *listener, void *data); static void createnotify(struct wl_listener *listener, void *data);
static void createoutput(struct wl_listener *listener, void *data); static void createoutput(struct wl_listener *listener, void *data);
static void createpointer(struct dwl_server *server, static void createpointer(struct wlr_input_device *device);
struct wlr_input_device *device);
static void cursorframe(struct wl_listener *listener, void *data); static void cursorframe(struct wl_listener *listener, void *data);
static void destroynotify(struct wl_listener *listener, void *data); static void destroynotify(struct wl_listener *listener, void *data);
static void focus(struct dwl_view *view, struct wlr_surface *surface); static void focus(struct dwl_view *view, struct wlr_surface *surface);
static void focusnext(struct dwl_server *server, const Arg *arg); static void focusnext(const Arg *arg);
static void handlemove(struct dwl_server *server, uint32_t time); static void handlemove(uint32_t time);
static void handleresize(struct dwl_server *server, uint32_t time); static void handleresize(uint32_t time);
static void inputdevice(struct wl_listener *listener, void *data); static void inputdevice(struct wl_listener *listener, void *data);
static bool keybinding(struct dwl_server *server, uint32_t mods, xkb_keysym_t sym); static bool keybinding(uint32_t mods, xkb_keysym_t sym);
static void keypress(struct wl_listener *listener, void *data); static void keypress(struct wl_listener *listener, void *data);
static void keypressmod(struct wl_listener *listener, void *data); static void keypressmod(struct wl_listener *listener, void *data);
static void maprequest(struct wl_listener *listener, void *data); static void maprequest(struct wl_listener *listener, void *data);
static void motionabsolute(struct wl_listener *listener, void *data); static void motionabsolute(struct wl_listener *listener, void *data);
static void motionnotify(struct dwl_server *server, uint32_t time); static void motionnotify(uint32_t time);
static void motionrelative(struct wl_listener *listener, void *data); static void motionrelative(struct wl_listener *listener, void *data);
static void movemouse(struct dwl_server *server, const Arg *arg); static void movemouse(const Arg *arg);
static void moverequest(struct wl_listener *listener, void *data); static void moverequest(struct wl_listener *listener, void *data);
static void moveresize(struct dwl_view *view, unsigned int mode, static void moveresize(struct dwl_view *view, unsigned int mode,
uint32_t edges); uint32_t edges);
static void quit(struct dwl_server *server, const Arg *arg); static void quit(const Arg *arg);
static void render(struct wlr_surface *surface, int sx, int sy, void *data); static void render(struct wlr_surface *surface, int sx, int sy, void *data);
static void renderoutput(struct wl_listener *listener, void *data); static void renderoutput(struct wl_listener *listener, void *data);
static void resizemouse(struct dwl_server *server, const Arg *arg); static void resizemouse(const Arg *arg);
static void resizerequest(struct wl_listener *listener, void *data); static void resizerequest(struct wl_listener *listener, void *data);
static void setcursor(struct wl_listener *listener, void *data); static void setcursor(struct wl_listener *listener, void *data);
static void spawn(struct dwl_server *server, const Arg *arg); static void spawn(const Arg *arg);
static void unmapnotify(struct wl_listener *listener, void *data); static void unmapnotify(struct wl_listener *listener, void *data);
static bool xytosurface(struct dwl_view *view, double lx, double ly, static bool xytosurface(struct dwl_view *view, double lx, double ly,
struct wlr_surface **surface, double *sx, double *sy); struct wlr_surface **surface, double *sx, double *sy);
static struct dwl_view * xytoview(struct dwl_server *server, double lx, double ly, static struct dwl_view * xytoview(double lx, double ly,
struct wlr_surface **surface, double *sx, double *sy); struct wlr_surface **surface, double *sx, double *sy);
/* variables */
static struct dwl_server server;
#include "config.h" #include "config.h"
/* Used to move all of the data necessary to render a surface from the top-level /* Used to move all of the data necessary to render a surface from the top-level
@ -173,11 +171,9 @@ axisnotify(struct wl_listener *listener, void *data)
{ {
/* This event is forwarded by the cursor when a pointer emits an axis event, /* This event is forwarded by the cursor when a pointer emits an axis event,
* for example when you move the scroll wheel. */ * for example when you move the scroll wheel. */
struct dwl_server *server =
wl_container_of(listener, server, cursor_axis);
struct wlr_event_pointer_axis *event = data; struct wlr_event_pointer_axis *event = data;
/* Notify the client with pointer focus of the axis event. */ /* Notify the client with pointer focus of the axis event. */
wlr_seat_pointer_notify_axis(server->seat, wlr_seat_pointer_notify_axis(server.seat,
event->time_msec, event->orientation, event->delta, event->time_msec, event->orientation, event->delta,
event->delta_discrete, event->source); event->delta_discrete, event->source);
} }
@ -187,20 +183,18 @@ buttonpress(struct wl_listener *listener, void *data)
{ {
/* This event is forwarded by the cursor when a pointer emits a button /* This event is forwarded by the cursor when a pointer emits a button
* event. */ * event. */
struct dwl_server *server =
wl_container_of(listener, server, cursor_button);
struct wlr_event_pointer_button *event = data; struct wlr_event_pointer_button *event = data;
/* Notify the client with pointer focus that a button press has occurred */ /* Notify the client with pointer focus that a button press has occurred */
wlr_seat_pointer_notify_button(server->seat, wlr_seat_pointer_notify_button(server.seat,
event->time_msec, event->button, event->state); event->time_msec, event->button, event->state);
double sx, sy; double sx, sy;
struct wlr_seat *seat = server->seat; struct wlr_seat *seat = server.seat;
struct wlr_surface *surface; struct wlr_surface *surface;
struct dwl_view *view = xytoview(server, struct dwl_view *view = xytoview(server.cursor->x, server.cursor->y,
server->cursor->x, server->cursor->y, &surface, &sx, &sy); &surface, &sx, &sy);
if (event->state == WLR_BUTTON_RELEASED) { if (event->state == WLR_BUTTON_RELEASED) {
/* If you released any buttons, we exit interactive move/resize mode. */ /* If you released any buttons, we exit interactive move/resize mode. */
server->cursor_mode = CurNormal; server.cursor_mode = CurNormal;
} else { } else {
/* Focus that client if the button was _pressed_ */ /* Focus that client if the button was _pressed_ */
focus(view, surface); focus(view, surface);
@ -211,18 +205,17 @@ buttonpress(struct wl_listener *listener, void *data)
if (event->button == buttons[i].button && if (event->button == buttons[i].button &&
CLEANMASK(mods) == CLEANMASK(buttons[i].mod) && CLEANMASK(mods) == CLEANMASK(buttons[i].mod) &&
buttons[i].func) { buttons[i].func) {
buttons[i].func(server, &buttons[i].arg); buttons[i].func(&buttons[i].arg);
} }
} }
} }
} }
void void
createkeyboard(struct dwl_server *server, struct wlr_input_device *device) createkeyboard(struct wlr_input_device *device)
{ {
struct dwl_keyboard *keyboard = struct dwl_keyboard *keyboard =
calloc(1, sizeof(struct dwl_keyboard)); calloc(1, sizeof(struct dwl_keyboard));
keyboard->server = server;
keyboard->device = device; keyboard->device = device;
/* We need to prepare an XKB keymap and assign it to the keyboard. This /* We need to prepare an XKB keymap and assign it to the keyboard. This
@ -242,10 +235,10 @@ createkeyboard(struct dwl_server *server, struct wlr_input_device *device)
keyboard->key.notify = keypress; keyboard->key.notify = keypress;
wl_signal_add(&device->keyboard->events.key, &keyboard->key); wl_signal_add(&device->keyboard->events.key, &keyboard->key);
wlr_seat_set_keyboard(server->seat, device); wlr_seat_set_keyboard(server.seat, device);
/* And add the keyboard to our list of keyboards */ /* And add the keyboard to our list of keyboards */
wl_list_insert(&server->keyboards, &keyboard->link); wl_list_insert(&server.keyboards, &keyboard->link);
} }
void void
@ -253,8 +246,6 @@ createnotify(struct wl_listener *listener, void *data)
{ {
/* This event is raised when wlr_xdg_shell receives a new xdg surface from a /* This event is raised when wlr_xdg_shell receives a new xdg surface from a
* client, either a toplevel (application window) or popup. */ * client, either a toplevel (application window) or popup. */
struct dwl_server *server =
wl_container_of(listener, server, new_xdg_surface);
struct wlr_xdg_surface *xdg_surface = data; struct wlr_xdg_surface *xdg_surface = data;
if (xdg_surface->role != WLR_XDG_SURFACE_ROLE_TOPLEVEL) { if (xdg_surface->role != WLR_XDG_SURFACE_ROLE_TOPLEVEL) {
return; return;
@ -263,7 +254,6 @@ createnotify(struct wl_listener *listener, void *data)
/* Allocate a dwl_view for this surface */ /* Allocate a dwl_view for this surface */
struct dwl_view *view = struct dwl_view *view =
calloc(1, sizeof(struct dwl_view)); calloc(1, sizeof(struct dwl_view));
view->server = server;
view->xdg_surface = xdg_surface; view->xdg_surface = xdg_surface;
/* Listen to the various events it can emit */ /* Listen to the various events it can emit */
@ -282,7 +272,7 @@ createnotify(struct wl_listener *listener, void *data)
wl_signal_add(&toplevel->events.request_resize, &view->request_resize); wl_signal_add(&toplevel->events.request_resize, &view->request_resize);
/* Add it to the list of views. */ /* Add it to the list of views. */
wl_list_insert(&server->views, &view->link); wl_list_insert(&server.views, &view->link);
} }
void void
@ -290,8 +280,6 @@ createoutput(struct wl_listener *listener, void *data)
{ {
/* This event is rasied by the backend when a new output (aka a display or /* This event is rasied by the backend when a new output (aka a display or
* monitor) becomes available. */ * monitor) becomes available. */
struct dwl_server *server =
wl_container_of(listener, server, new_output);
struct wlr_output *wlr_output = data; struct wlr_output *wlr_output = data;
/* Some backends don't have modes. DRM+KMS does, and we need to set a mode /* Some backends don't have modes. DRM+KMS does, and we need to set a mode
@ -312,17 +300,16 @@ createoutput(struct wl_listener *listener, void *data)
struct dwl_output *output = struct dwl_output *output =
calloc(1, sizeof(struct dwl_output)); calloc(1, sizeof(struct dwl_output));
output->wlr_output = wlr_output; output->wlr_output = wlr_output;
output->server = server;
/* Sets up a listener for the frame notify event. */ /* Sets up a listener for the frame notify event. */
output->frame.notify = renderoutput; output->frame.notify = renderoutput;
wl_signal_add(&wlr_output->events.frame, &output->frame); wl_signal_add(&wlr_output->events.frame, &output->frame);
wl_list_insert(&server->outputs, &output->link); wl_list_insert(&server.outputs, &output->link);
/* Adds this to the output layout. The add_auto function arranges outputs /* Adds this to the output layout. The add_auto function arranges outputs
* from left-to-right in the order they appear. A more sophisticated * from left-to-right in the order they appear. A more sophisticated
* compositor would let the user configure the arrangement of outputs in the * compositor would let the user configure the arrangement of outputs in the
* layout. */ * layout. */
wlr_output_layout_add_auto(server->output_layout, wlr_output); wlr_output_layout_add_auto(server.output_layout, wlr_output);
/* Creating the global adds a wl_output global to the display, which Wayland /* Creating the global adds a wl_output global to the display, which Wayland
* clients can see to find out information about the output (such as * clients can see to find out information about the output (such as
@ -331,13 +318,13 @@ createoutput(struct wl_listener *listener, void *data)
} }
void void
createpointer(struct dwl_server *server, struct wlr_input_device *device) createpointer(struct wlr_input_device *device)
{ {
/* We don't do anything special with pointers. All of our pointer handling /* 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 * is proxied through wlr_cursor. On another compositor, you might take this
* opportunity to do libinput configuration on the device to set * opportunity to do libinput configuration on the device to set
* acceleration, etc. */ * acceleration, etc. */
wlr_cursor_attach_input_device(server->cursor, device); wlr_cursor_attach_input_device(server.cursor, device);
} }
void void
@ -347,10 +334,8 @@ cursorframe(struct wl_listener *listener, void *data)
* event. Frame events are sent after regular pointer events to group * event. Frame events are sent after regular pointer events to group
* multiple events together. For instance, two axis events may happen at the * multiple events together. For instance, two axis events may happen at the
* same time, in which case a frame event won't be sent in between. */ * same time, in which case a frame event won't be sent in between. */
struct dwl_server *server =
wl_container_of(listener, server, cursor_frame);
/* Notify the client with pointer focus of the frame event. */ /* Notify the client with pointer focus of the frame event. */
wlr_seat_pointer_notify_frame(server->seat); wlr_seat_pointer_notify_frame(server.seat);
} }
void void
@ -369,8 +354,7 @@ focus(struct dwl_view *view, struct wlr_surface *surface)
if (view == NULL) { if (view == NULL) {
return; return;
} }
struct dwl_server *server = view->server; struct wlr_seat *seat = server.seat;
struct wlr_seat *seat = server->seat;
struct wlr_surface *prev_surface = seat->keyboard_state.focused_surface; struct wlr_surface *prev_surface = seat->keyboard_state.focused_surface;
if (prev_surface == surface) { if (prev_surface == surface) {
/* Don't re-focus an already focused surface. */ /* Don't re-focus an already focused surface. */
@ -389,7 +373,7 @@ focus(struct dwl_view *view, struct wlr_surface *surface)
struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat); struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat);
/* Move the view to the front */ /* Move the view to the front */
wl_list_remove(&view->link); wl_list_remove(&view->link);
wl_list_insert(&server->views, &view->link); wl_list_insert(&server.views, &view->link);
/* Activate the new surface */ /* Activate the new surface */
wlr_xdg_toplevel_set_activated(view->xdg_surface, true); wlr_xdg_toplevel_set_activated(view->xdg_surface, true);
/* /*
@ -402,32 +386,32 @@ focus(struct dwl_view *view, struct wlr_surface *surface)
} }
void void
focusnext(struct dwl_server *server, const Arg *arg) focusnext(const Arg *arg)
{ {
/* Cycle to the next view */ /* Cycle to the next view */
if (wl_list_length(&server->views) < 2) { if (wl_list_length(&server.views) < 2) {
return; return;
} }
struct dwl_view *current_view = wl_container_of( struct dwl_view *current_view = wl_container_of(
server->views.next, current_view, link); server.views.next, current_view, link);
struct dwl_view *next_view = wl_container_of( struct dwl_view *next_view = wl_container_of(
current_view->link.next, next_view, link); current_view->link.next, next_view, link);
focus(next_view, next_view->xdg_surface->surface); focus(next_view, next_view->xdg_surface->surface);
/* Move the previous view to the end of the list */ /* Move the previous view to the end of the list */
wl_list_remove(&current_view->link); wl_list_remove(&current_view->link);
wl_list_insert(server->views.prev, &current_view->link); wl_list_insert(server.views.prev, &current_view->link);
} }
void void
handlemove(struct dwl_server *server, uint32_t time) handlemove(uint32_t time)
{ {
/* Move the grabbed view to the new position. */ /* Move the grabbed view to the new position. */
server->grabbed_view->x = server->cursor->x - server->grab_x; server.grabbed_view->x = server.cursor->x - server.grab_x;
server->grabbed_view->y = server->cursor->y - server->grab_y; server.grabbed_view->y = server.cursor->y - server.grab_y;
} }
void void
handleresize(struct dwl_server *server, uint32_t time) handleresize(uint32_t time)
{ {
/* /*
* Resizing the grabbed view can be a little bit complicated, because we * Resizing the grabbed view can be a little bit complicated, because we
@ -439,29 +423,29 @@ handleresize(struct dwl_server *server, uint32_t time)
* you'd wait for the client to prepare a buffer at the new size, then * you'd wait for the client to prepare a buffer at the new size, then
* commit any movement that was prepared. * commit any movement that was prepared.
*/ */
struct dwl_view *view = server->grabbed_view; struct dwl_view *view = server.grabbed_view;
double dx = server->cursor->x - server->grab_x; double dx = server.cursor->x - server.grab_x;
double dy = server->cursor->y - server->grab_y; double dy = server.cursor->y - server.grab_y;
double x = view->x; double x = view->x;
double y = view->y; double y = view->y;
int width = server->grab_width; int width = server.grab_width;
int height = server->grab_height; int height = server.grab_height;
if (server->resize_edges & WLR_EDGE_TOP) { if (server.resize_edges & WLR_EDGE_TOP) {
y = server->grab_y + dy; y = server.grab_y + dy;
height -= dy; height -= dy;
if (height < 1) { if (height < 1) {
y += height; y += height;
} }
} else if (server->resize_edges & WLR_EDGE_BOTTOM) { } else if (server.resize_edges & WLR_EDGE_BOTTOM) {
height += dy; height += dy;
} }
if (server->resize_edges & WLR_EDGE_LEFT) { if (server.resize_edges & WLR_EDGE_LEFT) {
x = server->grab_x + dx; x = server.grab_x + dx;
width -= dx; width -= dx;
if (width < 1) { if (width < 1) {
x += width; x += width;
} }
} else if (server->resize_edges & WLR_EDGE_RIGHT) { } else if (server.resize_edges & WLR_EDGE_RIGHT) {
width += dx; width += dx;
} }
view->x = x; view->x = x;
@ -474,15 +458,13 @@ inputdevice(struct wl_listener *listener, void *data)
{ {
/* This event is raised by the backend when a new input device becomes /* This event is raised by the backend when a new input device becomes
* available. */ * available. */
struct dwl_server *server =
wl_container_of(listener, server, new_input);
struct wlr_input_device *device = data; struct wlr_input_device *device = data;
switch (device->type) { switch (device->type) {
case WLR_INPUT_DEVICE_KEYBOARD: case WLR_INPUT_DEVICE_KEYBOARD:
createkeyboard(server, device); createkeyboard(device);
break; break;
case WLR_INPUT_DEVICE_POINTER: case WLR_INPUT_DEVICE_POINTER:
createpointer(server, device); createpointer(device);
break; break;
default: default:
break; break;
@ -491,14 +473,14 @@ inputdevice(struct wl_listener *listener, void *data)
* communiciated to the client. In dwl we always have a cursor, even if * communiciated to the client. In dwl we always have a cursor, even if
* there are no pointer devices, so we always include that capability. */ * there are no pointer devices, so we always include that capability. */
uint32_t caps = WL_SEAT_CAPABILITY_POINTER; uint32_t caps = WL_SEAT_CAPABILITY_POINTER;
if (!wl_list_empty(&server->keyboards)) { if (!wl_list_empty(&server.keyboards)) {
caps |= WL_SEAT_CAPABILITY_KEYBOARD; caps |= WL_SEAT_CAPABILITY_KEYBOARD;
} }
wlr_seat_set_capabilities(server->seat, caps); wlr_seat_set_capabilities(server.seat, caps);
} }
bool bool
keybinding(struct dwl_server *server, uint32_t mods, xkb_keysym_t sym) keybinding(uint32_t mods, xkb_keysym_t sym)
{ {
/* /*
* Here we handle compositor keybindings. This is when the compositor is * Here we handle compositor keybindings. This is when the compositor is
@ -510,7 +492,7 @@ keybinding(struct dwl_server *server, uint32_t mods, xkb_keysym_t sym)
if (sym == keys[i].keysym && if (sym == keys[i].keysym &&
CLEANMASK(mods) == CLEANMASK(keys[i].mod) && CLEANMASK(mods) == CLEANMASK(keys[i].mod) &&
keys[i].func) { keys[i].func) {
keys[i].func(server, &keys[i].arg); keys[i].func(&keys[i].arg);
handled = true; handled = true;
} }
} }
@ -523,9 +505,8 @@ keypress(struct wl_listener *listener, void *data)
/* This event is raised when a key is pressed or released. */ /* This event is raised when a key is pressed or released. */
struct dwl_keyboard *keyboard = struct dwl_keyboard *keyboard =
wl_container_of(listener, keyboard, key); wl_container_of(listener, keyboard, key);
struct dwl_server *server = keyboard->server;
struct wlr_event_keyboard_key *event = data; struct wlr_event_keyboard_key *event = data;
struct wlr_seat *seat = server->seat; struct wlr_seat *seat = server.seat;
/* Translate libinput keycode -> xkbcommon */ /* Translate libinput keycode -> xkbcommon */
uint32_t keycode = event->keycode + 8; uint32_t keycode = event->keycode + 8;
@ -539,7 +520,7 @@ keypress(struct wl_listener *listener, void *data)
if (event->state == WLR_KEY_PRESSED) { if (event->state == WLR_KEY_PRESSED) {
/* On _press_, attempt to process a compositor keybinding. */ /* On _press_, attempt to process a compositor keybinding. */
for (int i = 0; i < nsyms; i++) { for (int i = 0; i < nsyms; i++) {
handled = keybinding(server, mods, syms[i]) || handled; handled = keybinding(mods, syms[i]) || handled;
} }
} }
@ -564,9 +545,9 @@ keypressmod(struct wl_listener *listener, void *data)
* same seat. You can swap out the underlying wlr_keyboard like this and * same seat. You can swap out the underlying wlr_keyboard like this and
* wlr_seat handles this transparently. * wlr_seat handles this transparently.
*/ */
wlr_seat_set_keyboard(keyboard->server->seat, keyboard->device); wlr_seat_set_keyboard(server.seat, keyboard->device);
/* Send modifiers to the client. */ /* Send modifiers to the client. */
wlr_seat_keyboard_notify_modifiers(keyboard->server->seat, wlr_seat_keyboard_notify_modifiers(server.seat,
&keyboard->device->keyboard->modifiers); &keyboard->device->keyboard->modifiers);
} }
@ -588,37 +569,35 @@ motionabsolute(struct wl_listener *listener, void *data)
* move the mouse over the window. You could enter the window from any edge, * move the mouse over the window. You could enter the window from any edge,
* so we have to warp the mouse there. There is also some hardware which * so we have to warp the mouse there. There is also some hardware which
* emits these events. */ * emits these events. */
struct dwl_server *server =
wl_container_of(listener, server, cursor_motion_absolute);
struct wlr_event_pointer_motion_absolute *event = data; struct wlr_event_pointer_motion_absolute *event = data;
wlr_cursor_warp_absolute(server->cursor, event->device, event->x, event->y); wlr_cursor_warp_absolute(server.cursor, event->device, event->x, event->y);
motionnotify(server, event->time_msec); motionnotify(event->time_msec);
} }
void void
motionnotify(struct dwl_server *server, uint32_t time) motionnotify(uint32_t time)
{ {
/* If the mode is non-passthrough, delegate to those functions. */ /* If the mode is non-passthrough, delegate to those functions. */
if (server->cursor_mode == CurMove) { if (server.cursor_mode == CurMove) {
handlemove(server, time); handlemove(time);
return; return;
} else if (server->cursor_mode == CurResize) { } else if (server.cursor_mode == CurResize) {
handleresize(server, time); handleresize(time);
return; return;
} }
/* Otherwise, find the view under the pointer and send the event along. */ /* Otherwise, find the view under the pointer and send the event along. */
double sx, sy; double sx, sy;
struct wlr_seat *seat = server->seat; struct wlr_seat *seat = server.seat;
struct wlr_surface *surface = NULL; struct wlr_surface *surface = NULL;
struct dwl_view *view = xytoview(server, struct dwl_view *view = xytoview(server.cursor->x, server.cursor->y,
server->cursor->x, server->cursor->y, &surface, &sx, &sy); &surface, &sx, &sy);
if (!view) { if (!view) {
/* If there's no view under the cursor, set the cursor image to a /* If there's no view under the cursor, set the cursor image to a
* default. This is what makes the cursor image appear when you move it * default. This is what makes the cursor image appear when you move it
* around the screen, not over any views. */ * around the screen, not over any views. */
wlr_xcursor_manager_set_cursor_image( wlr_xcursor_manager_set_cursor_image(
server->cursor_mgr, "left_ptr", server->cursor); server.cursor_mgr, "left_ptr", server.cursor);
} }
if (surface) { if (surface) {
bool focus_changed = seat->pointer_state.focused_surface != surface; bool focus_changed = seat->pointer_state.focused_surface != surface;
@ -648,26 +627,24 @@ motionrelative(struct wl_listener *listener, void *data)
{ {
/* This event is forwarded by the cursor when a pointer emits a _relative_ /* This event is forwarded by the cursor when a pointer emits a _relative_
* pointer motion event (i.e. a delta) */ * pointer motion event (i.e. a delta) */
struct dwl_server *server =
wl_container_of(listener, server, cursor_motion);
struct wlr_event_pointer_motion *event = data; struct wlr_event_pointer_motion *event = data;
/* The cursor doesn't move unless we tell it to. The cursor automatically /* The cursor doesn't move unless we tell it to. The cursor automatically
* handles constraining the motion to the output layout, as well as any * handles constraining the motion to the output layout, as well as any
* special configuration applied for the specific input device which * special configuration applied for the specific input device which
* generated the event. You can pass NULL for the device if you want to move * generated the event. You can pass NULL for the device if you want to move
* the cursor around without any input. */ * the cursor around without any input. */
wlr_cursor_move(server->cursor, event->device, wlr_cursor_move(server.cursor, event->device,
event->delta_x, event->delta_y); event->delta_x, event->delta_y);
motionnotify(server, event->time_msec); motionnotify(event->time_msec);
} }
void void
movemouse(struct dwl_server *server, const Arg *arg) movemouse(const Arg *arg)
{ {
double sx, sy; double sx, sy;
struct wlr_surface *surface; struct wlr_surface *surface;
struct dwl_view *view = xytoview(server, struct dwl_view *view = xytoview(server.cursor->x, server.cursor->y,
server->cursor->x, server->cursor->y, &surface, &sx, &sy); &surface, &sx, &sy);
if (!view) { if (!view) {
return; return;
} }
@ -692,33 +669,32 @@ moveresize(struct dwl_view *view, unsigned int mode, uint32_t edges)
/* This function sets up an interactive move or resize operation, where the /* This function sets up an interactive move or resize operation, where the
* compositor stops propagating pointer events to clients and instead * compositor stops propagating pointer events to clients and instead
* consumes them itself, to move or resize windows. */ * consumes them itself, to move or resize windows. */
struct dwl_server *server = view->server;
struct wlr_surface *focused_surface = struct wlr_surface *focused_surface =
server->seat->pointer_state.focused_surface; server.seat->pointer_state.focused_surface;
if (view->xdg_surface->surface != focused_surface) { if (view->xdg_surface->surface != focused_surface) {
/* Deny move/resize requests from unfocused clients. */ /* Deny move/resize requests from unfocused clients. */
return; return;
} }
server->grabbed_view = view; server.grabbed_view = view;
server->cursor_mode = mode; server.cursor_mode = mode;
struct wlr_box geo_box; struct wlr_box geo_box;
wlr_xdg_surface_get_geometry(view->xdg_surface, &geo_box); wlr_xdg_surface_get_geometry(view->xdg_surface, &geo_box);
if (mode == CurMove) { if (mode == CurMove) {
server->grab_x = server->cursor->x - view->x; server.grab_x = server.cursor->x - view->x;
server->grab_y = server->cursor->y - view->y; server.grab_y = server.cursor->y - view->y;
} else { } else {
server->grab_x = server->cursor->x + geo_box.x; server.grab_x = server.cursor->x + geo_box.x;
server->grab_y = server->cursor->y + geo_box.y; server.grab_y = server.cursor->y + geo_box.y;
} }
server->grab_width = geo_box.width; server.grab_width = geo_box.width;
server->grab_height = geo_box.height; server.grab_height = geo_box.height;
server->resize_edges = edges; server.resize_edges = edges;
} }
void void
quit(struct dwl_server *server, const Arg *arg) quit(const Arg *arg)
{ {
wl_display_terminate(server->wl_display); wl_display_terminate(server.wl_display);
} }
void void
@ -745,7 +721,7 @@ render(struct wlr_surface *surface, int sx, int sy, void *data)
* output-local coordinates, or (2000 - 1920). */ * output-local coordinates, or (2000 - 1920). */
double ox = 0, oy = 0; double ox = 0, oy = 0;
wlr_output_layout_output_coords( wlr_output_layout_output_coords(
view->server->output_layout, output, &ox, &oy); server.output_layout, output, &ox, &oy);
ox += view->x + sx, oy += view->y + sy; ox += view->x + sx, oy += view->y + sy;
/* We also have to apply the scale factor for HiDPI outputs. This is only /* We also have to apply the scale factor for HiDPI outputs. This is only
@ -790,7 +766,7 @@ renderoutput(struct wl_listener *listener, void *data)
* generally at the output's refresh rate (e.g. 60Hz). */ * generally at the output's refresh rate (e.g. 60Hz). */
struct dwl_output *output = struct dwl_output *output =
wl_container_of(listener, output, frame); wl_container_of(listener, output, frame);
struct wlr_renderer *renderer = output->server->renderer; struct wlr_renderer *renderer = server.renderer;
struct timespec now; struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now); clock_gettime(CLOCK_MONOTONIC, &now);
@ -811,7 +787,7 @@ renderoutput(struct wl_listener *listener, void *data)
/* Each subsequent window we render is rendered on top of the last. Because /* Each subsequent window we render is rendered on top of the last. Because
* our view list is ordered front-to-back, we iterate over it backwards. */ * our view list is ordered front-to-back, we iterate over it backwards. */
struct dwl_view *view; struct dwl_view *view;
wl_list_for_each_reverse(view, &output->server->views, link) { wl_list_for_each_reverse(view, &server.views, link) {
if (!view->mapped) { if (!view->mapped) {
/* An unmapped view should not be rendered. */ /* An unmapped view should not be rendered. */
continue; continue;
@ -843,18 +819,18 @@ renderoutput(struct wl_listener *listener, void *data)
} }
void void
resizemouse(struct dwl_server *server, const Arg *arg) resizemouse(const Arg *arg)
{ {
double sx, sy; double sx, sy;
struct wlr_surface *surface; struct wlr_surface *surface;
struct dwl_view *view = xytoview(server, struct dwl_view *view = xytoview(server.cursor->x, server.cursor->y,
server->cursor->x, server->cursor->y, &surface, &sx, &sy); &surface, &sx, &sy);
if (!view) { if (!view) {
return; return;
} }
struct wlr_box geo_box; struct wlr_box geo_box;
wlr_xdg_surface_get_geometry(view->xdg_surface, &geo_box); wlr_xdg_surface_get_geometry(view->xdg_surface, &geo_box);
wlr_cursor_warp_closest(server->cursor, NULL, wlr_cursor_warp_closest(server.cursor, NULL,
view->x + geo_box.x + geo_box.width, view->x + geo_box.x + geo_box.width,
view->y + geo_box.y + geo_box.height); view->y + geo_box.y + geo_box.height);
moveresize(view, CurResize, WLR_EDGE_BOTTOM|WLR_EDGE_RIGHT); moveresize(view, CurResize, WLR_EDGE_BOTTOM|WLR_EDGE_RIGHT);
@ -876,12 +852,10 @@ resizerequest(struct wl_listener *listener, void *data)
void void
setcursor(struct wl_listener *listener, void *data) setcursor(struct wl_listener *listener, void *data)
{ {
struct dwl_server *server = wl_container_of(
listener, server, request_cursor);
/* This event is rasied by the seat when a client provides a cursor image */ /* 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_pointer_request_set_cursor_event *event = data;
struct wlr_seat_client *focused_client = struct wlr_seat_client *focused_client =
server->seat->pointer_state.focused_client; server.seat->pointer_state.focused_client;
/* This can be sent by any client, so we check to make sure this one is /* This can be sent by any client, so we check to make sure this one is
* actually has pointer focus first. */ * actually has pointer focus first. */
if (focused_client == event->seat_client) { if (focused_client == event->seat_client) {
@ -889,13 +863,13 @@ setcursor(struct wl_listener *listener, void *data)
* provided surface as the cursor image. It will set the hardware cursor * 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 * on the output that it's currently on and continue to do so as the
* cursor moves between outputs. */ * cursor moves between outputs. */
wlr_cursor_set_surface(server->cursor, event->surface, wlr_cursor_set_surface(server.cursor, event->surface,
event->hotspot_x, event->hotspot_y); event->hotspot_x, event->hotspot_y);
} }
} }
void void
spawn(struct dwl_server *server, const Arg *arg) spawn(const Arg *arg)
{ {
if (fork() == 0) { if (fork() == 0) {
setsid(); setsid();
@ -946,13 +920,13 @@ xytosurface(struct dwl_view *view, double lx, double ly,
} }
struct dwl_view * struct dwl_view *
xytoview(struct dwl_server *server, double lx, double ly, xytoview(double lx, double ly,
struct wlr_surface **surface, double *sx, double *sy) struct wlr_surface **surface, double *sx, double *sy)
{ {
/* This iterates over all of our surfaces and attempts to find one under the /* This iterates over all of our surfaces and attempts to find one under the
* cursor. This relies on server->views being ordered from top-to-bottom. */ * cursor. This relies on server.views being ordered from top-to-bottom. */
struct dwl_view *view; struct dwl_view *view;
wl_list_for_each(view, &server->views, link) { wl_list_for_each(view, &server.views, link) {
if (xytosurface(view, lx, ly, surface, sx, sy)) { if (xytosurface(view, lx, ly, surface, sx, sy)) {
return view; return view;
} }
@ -983,7 +957,6 @@ main(int argc, char *argv[])
return 0; return 0;
} }
struct dwl_server server;
/* The Wayland display is managed by libwayland. It handles accepting /* The Wayland display is managed by libwayland. It handles accepting
* clients from the Unix socket, manging Wayland globals, and so on. */ * clients from the Unix socket, manging Wayland globals, and so on. */
server.wl_display = wl_display_create(); server.wl_display = wl_display_create();