wlroots/examples/shared.c

685 lines
22 KiB
C
Raw Normal View History

2017-06-21 18:33:39 +00:00
#define _POSIX_C_SOURCE 200112L
2017-06-13 14:13:11 +00:00
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <limits.h>
2017-06-13 14:13:11 +00:00
#include <xkbcommon/xkbcommon.h>
#include <wayland-server-protocol.h>
#include <wlr/backend.h>
2017-07-11 07:18:34 +00:00
#include <wlr/backend/session.h>
2017-08-05 07:49:34 +00:00
#include <wlr/backend/multi.h>
2017-06-21 14:27:45 +00:00
#include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_output_layout.h>
2017-06-21 14:27:45 +00:00
#include <wlr/types/wlr_input_device.h>
2017-06-21 18:15:06 +00:00
#include <wlr/util/log.h>
2017-06-13 14:13:11 +00:00
#include "shared.h"
static void usage(const char *name, int ret) {
fprintf(stderr,
"usage: %s [-d <name> [-r <rotation> | -f]]*\n"
"\n"
" -o <output> The name of the DRM display. e.g. DVI-I-1.\n"
" -r <rotation> The rotation counter clockwise. Valid values are 90, 180, 270.\n"
" -x <position> The X-axis coordinate position of this output in the layout.\n"
" -y <position> The Y-axis coordinate position of this output in the layout.\n"
" -f Flip the output along the vertical axis.\n", name);
exit(ret);
}
struct example_config *parse_args(int argc, char *argv[]) {
struct example_config *config = calloc(1, sizeof(struct example_config));
wl_list_init(&config->outputs);
struct output_config *oc = NULL;
int c;
while ((c = getopt(argc, argv, "o:r:x:y:fh")) != -1) {
switch (c) {
case 'o':
oc = calloc(1, sizeof(*oc));
oc->name = optarg;
oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
wl_list_insert(&config->outputs, &oc->link);
break;
case 'r':
if (!oc) {
fprintf(stderr, "You must specify an output first\n");
usage(argv[0], 1);
}
if (oc->transform != WL_OUTPUT_TRANSFORM_NORMAL
&& oc->transform != WL_OUTPUT_TRANSFORM_FLIPPED) {
fprintf(stderr, "Rotation for %s already specified\n", oc->name);
usage(argv[0], 1);
}
if (strcmp(optarg, "90") == 0) {
oc->transform += WL_OUTPUT_TRANSFORM_90;
} else if (strcmp(optarg, "180") == 0) {
oc->transform += WL_OUTPUT_TRANSFORM_180;
} else if (strcmp(optarg, "270") == 0) {
oc->transform += WL_OUTPUT_TRANSFORM_270;
} else {
fprintf(stderr, "Invalid rotation '%s'\n", optarg);
usage(argv[0], 1);
}
break;
case 'x':
if (!oc) {
fprintf(stderr, "You must specify an output first\n");
usage(argv[0], 1);
}
oc->x = strtol(optarg, NULL, 0);
break;
case 'y':
if (!oc) {
fprintf(stderr, "You must specify an output first\n");
usage(argv[0], 1);
}
oc->y = strtol(optarg, NULL, 0);
break;
case 'f':
if (!oc) {
fprintf(stderr, "You must specify an output first\n");
usage(argv[0], 1);
}
if (oc->transform >= WL_OUTPUT_TRANSFORM_FLIPPED) {
fprintf(stderr, "Flip for %s already specified\n", oc->name);
usage(argv[0], 1);
}
oc->transform += WL_OUTPUT_TRANSFORM_FLIPPED;
break;
case 'h':
case '?':
usage(argv[0], c != 'h');
}
}
return config;
}
void example_config_destroy(struct example_config *config) {
struct output_config *oc, *tmp = NULL;
wl_list_for_each_safe(oc, tmp, &config->outputs, link) {
free(oc);
}
free(config);
}
struct wlr_output_layout *configure_layout(struct example_config *config, struct wl_list *outputs) {
struct wlr_output_layout *layout = wlr_output_layout_init();
int max_x = INT_MIN;
int max_x_y = INT_MIN; // y value for the max_x output
// first add all the configured outputs
struct output_state *output;
wl_list_for_each(output, outputs, link) {
struct output_config *conf;
wl_list_for_each(conf, &config->outputs, link) {
if (strcmp(conf->name, output->output->name) == 0) {
wlr_output_layout_add(layout, output->output,
conf->x, conf->y);
wlr_output_transform(output->output, conf->transform);
int width, height;
wlr_output_effective_resolution(output->output, &width, &height);
if (conf->x + width > max_x) {
max_x = conf->x + width;
max_x_y = conf->y;
}
break;
}
}
}
if (max_x == INT_MIN) {
// couldn't find a configured output
max_x = 0;
max_x_y = 0;
}
// now add all the other configured outputs in a sensible position
wl_list_for_each(output, outputs, link) {
if (wlr_output_layout_get(layout, output->output)) {
continue;
}
wlr_output_layout_add(layout, output->output, max_x, max_x_y);
int width, height;
wlr_output_effective_resolution(output->output, &width, &height);
max_x += width;
}
return layout;
}
2017-06-19 19:15:37 +00:00
static void keyboard_led_update(struct keyboard_state *kbstate) {
uint32_t leds = 0;
for (uint32_t i = 0; i < WLR_LED_LAST; ++i) {
if (xkb_state_led_index_is_active(kbstate->xkb_state, kbstate->leds[i])) {
leds |= (1 << i);
}
}
wlr_keyboard_led_update(kbstate->device->keyboard, leds);
}
2017-06-13 14:13:11 +00:00
static void keyboard_key_notify(struct wl_listener *listener, void *data) {
2017-06-21 18:07:09 +00:00
struct wlr_event_keyboard_key *event = data;
2017-06-13 14:13:11 +00:00
struct keyboard_state *kbstate = wl_container_of(listener, kbstate, key);
uint32_t keycode = event->keycode + 8;
enum wlr_key_state key_state = event->state;
const xkb_keysym_t *syms;
int nsyms = xkb_state_key_get_syms(kbstate->xkb_state, keycode, &syms);
xkb_state_update_key(kbstate->xkb_state, keycode,
event->state == WLR_KEY_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP);
keyboard_led_update(kbstate);
2017-06-13 14:13:11 +00:00
for (int i = 0; i < nsyms; ++i) {
xkb_keysym_t sym = syms[i];
char name[64];
int l = xkb_keysym_get_name(sym, name, sizeof(name));
if (l != -1 && l != sizeof(name)) {
2017-06-21 18:15:06 +00:00
wlr_log(L_DEBUG, "Key event: %s %s", name,
2017-06-13 14:13:11 +00:00
key_state == WLR_KEY_PRESSED ? "pressed" : "released");
}
if (kbstate->compositor->keyboard_key_cb) {
kbstate->compositor->keyboard_key_cb(kbstate, event->keycode, sym, key_state);
2017-06-13 14:13:11 +00:00
}
if (sym == XKB_KEY_Escape) {
wl_display_terminate(kbstate->compositor->display);
2017-08-06 01:37:49 +00:00
} else if (key_state == WLR_KEY_PRESSED &&
sym >= XKB_KEY_XF86Switch_VT_1 &&
sym <= XKB_KEY_XF86Switch_VT_12) {
if (wlr_backend_is_multi(kbstate->compositor->backend)) {
struct wlr_session *session =
wlr_multi_get_session(kbstate->compositor->backend);
if (session) {
wlr_session_change_vt(session, sym - XKB_KEY_XF86Switch_VT_1 + 1);
}
2017-08-06 01:37:49 +00:00
}
}
2017-06-13 14:13:11 +00:00
}
}
2017-06-13 16:21:36 +00:00
static void keyboard_add(struct wlr_input_device *device, struct compositor_state *state) {
2017-06-13 14:13:11 +00:00
struct keyboard_state *kbstate = calloc(sizeof(struct keyboard_state), 1);
kbstate->device = device;
kbstate->compositor = state;
wl_list_init(&kbstate->key.link);
kbstate->key.notify = keyboard_key_notify;
wl_signal_add(&device->keyboard->events.key, &kbstate->key);
wl_list_insert(&state->keyboards, &kbstate->link);
struct xkb_rule_names rules;
memset(&rules, 0, sizeof(rules));
rules.rules = getenv("XKB_DEFAULT_RULES");
rules.model = getenv("XKB_DEFAULT_MODEL");
rules.layout = getenv("XKB_DEFAULT_LAYOUT");
rules.variant = getenv("XKB_DEFAULT_VARIANT");
rules.options = getenv("XKB_DEFAULT_OPTIONS");
struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
if (!context) {
2017-06-21 18:15:06 +00:00
wlr_log(L_ERROR, "Failed to create XKB context");
2017-06-13 14:13:11 +00:00
exit(1);
}
kbstate->keymap = xkb_map_new_from_names(
context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS);
if (!kbstate->keymap) {
2017-06-21 18:15:06 +00:00
wlr_log(L_ERROR, "Failed to create XKB keymap");
2017-06-13 14:13:11 +00:00
exit(1);
}
xkb_context_unref(context);
kbstate->xkb_state = xkb_state_new(kbstate->keymap);
if (!kbstate->xkb_state) {
2017-06-21 18:15:06 +00:00
wlr_log(L_ERROR, "Failed to create XKB state");
2017-06-13 14:13:11 +00:00
exit(1);
}
2017-06-19 19:15:37 +00:00
const char *led_names[3] = {
XKB_LED_NAME_NUM,
XKB_LED_NAME_CAPS,
XKB_LED_NAME_SCROLL
};
for (uint32_t i = 0; i < 3; ++i) {
kbstate->leds[i] = xkb_map_led_get_index(kbstate->keymap, led_names[i]);
}
2017-06-13 14:13:11 +00:00
}
2017-06-13 16:21:36 +00:00
static void pointer_motion_notify(struct wl_listener *listener, void *data) {
2017-06-21 18:07:09 +00:00
struct wlr_event_pointer_motion *event = data;
2017-06-13 16:21:36 +00:00
struct pointer_state *pstate = wl_container_of(listener, pstate, motion);
if (pstate->compositor->pointer_motion_cb) {
pstate->compositor->pointer_motion_cb(pstate,
event->delta_x, event->delta_y);
}
}
static void pointer_motion_absolute_notify(struct wl_listener *listener, void *data) {
struct wlr_event_pointer_motion_absolute *event = data;
struct pointer_state *pstate = wl_container_of(listener, pstate, motion_absolute);
if (pstate->compositor->pointer_motion_absolute_cb) {
pstate->compositor->pointer_motion_absolute_cb(pstate,
event->x_mm, event->y_mm);
}
}
static void pointer_button_notify(struct wl_listener *listener, void *data) {
2017-06-21 18:07:09 +00:00
struct wlr_event_pointer_button *event = data;
struct pointer_state *pstate = wl_container_of(listener, pstate, button);
if (pstate->compositor->pointer_button_cb) {
pstate->compositor->pointer_button_cb(pstate,
event->button, event->state);
2017-06-13 16:21:36 +00:00
}
}
2017-06-13 16:38:57 +00:00
static void pointer_axis_notify(struct wl_listener *listener, void *data) {
2017-06-21 18:07:09 +00:00
struct wlr_event_pointer_axis *event = data;
2017-06-13 16:38:57 +00:00
struct pointer_state *pstate = wl_container_of(listener, pstate, axis);
if (pstate->compositor->pointer_axis_cb) {
pstate->compositor->pointer_axis_cb(pstate,
event->source, event->orientation, event->delta);
}
}
2017-06-13 16:21:36 +00:00
static void pointer_add(struct wlr_input_device *device, struct compositor_state *state) {
struct pointer_state *pstate = calloc(sizeof(struct pointer_state), 1);
pstate->device = device;
pstate->compositor = state;
wl_list_init(&pstate->motion.link);
wl_list_init(&pstate->motion_absolute.link);
wl_list_init(&pstate->button.link);
wl_list_init(&pstate->axis.link);
pstate->motion.notify = pointer_motion_notify;
pstate->motion_absolute.notify = pointer_motion_absolute_notify;
pstate->button.notify = pointer_button_notify;
2017-06-13 16:38:57 +00:00
pstate->axis.notify = pointer_axis_notify;
2017-06-13 16:21:36 +00:00
wl_signal_add(&device->pointer->events.motion, &pstate->motion);
wl_signal_add(&device->pointer->events.motion_absolute, &pstate->motion_absolute);
wl_signal_add(&device->pointer->events.button, &pstate->button);
2017-06-13 16:38:57 +00:00
wl_signal_add(&device->pointer->events.axis, &pstate->axis);
2017-06-13 16:21:36 +00:00
wl_list_insert(&state->pointers, &pstate->link);
}
2017-06-14 18:50:09 +00:00
static void touch_down_notify(struct wl_listener *listener, void *data) {
2017-06-21 18:07:09 +00:00
struct wlr_event_touch_down *event = data;
2017-06-14 18:50:09 +00:00
struct touch_state *tstate = wl_container_of(listener, tstate, down);
if (tstate->compositor->touch_down_cb) {
tstate->compositor->touch_down_cb(tstate, event->slot,
event->x_mm, event->y_mm, event->width_mm, event->height_mm);
}
}
static void touch_motion_notify(struct wl_listener *listener, void *data) {
2017-06-21 18:07:09 +00:00
struct wlr_event_touch_motion *event = data;
2017-06-14 18:50:09 +00:00
struct touch_state *tstate = wl_container_of(listener, tstate, motion);
if (tstate->compositor->touch_motion_cb) {
tstate->compositor->touch_motion_cb(tstate, event->slot,
event->x_mm, event->y_mm, event->width_mm, event->height_mm);
}
}
static void touch_up_notify(struct wl_listener *listener, void *data) {
2017-06-21 18:07:09 +00:00
struct wlr_event_touch_up *event = data;
2017-06-14 18:50:09 +00:00
struct touch_state *tstate = wl_container_of(listener, tstate, up);
if (tstate->compositor->touch_up_cb) {
tstate->compositor->touch_up_cb(tstate, event->slot);
}
}
static void touch_cancel_notify(struct wl_listener *listener, void *data) {
2017-06-21 18:07:09 +00:00
struct wlr_event_touch_cancel *event = data;
2017-06-14 18:50:09 +00:00
struct touch_state *tstate = wl_container_of(listener, tstate, cancel);
if (tstate->compositor->touch_cancel_cb) {
tstate->compositor->touch_cancel_cb(tstate, event->slot);
}
}
static void touch_add(struct wlr_input_device *device, struct compositor_state *state) {
struct touch_state *tstate = calloc(sizeof(struct touch_state), 1);
tstate->device = device;
tstate->compositor = state;
wl_list_init(&tstate->down.link);
wl_list_init(&tstate->motion.link);
wl_list_init(&tstate->up.link);
wl_list_init(&tstate->cancel.link);
tstate->down.notify = touch_down_notify;
tstate->motion.notify = touch_motion_notify;
tstate->up.notify = touch_up_notify;
tstate->cancel.notify = touch_cancel_notify;
wl_signal_add(&device->touch->events.down, &tstate->down);
wl_signal_add(&device->touch->events.motion, &tstate->motion);
wl_signal_add(&device->touch->events.up, &tstate->up);
wl_signal_add(&device->touch->events.cancel, &tstate->cancel);
wl_list_insert(&state->touch, &tstate->link);
}
2017-06-15 20:15:12 +00:00
static void tablet_tool_axis_notify(struct wl_listener *listener, void *data) {
2017-06-21 18:07:09 +00:00
struct wlr_event_tablet_tool_axis *event = data;
2017-06-15 20:15:12 +00:00
struct tablet_tool_state *tstate = wl_container_of(listener, tstate, axis);
if (tstate->compositor->tool_axis_cb) {
tstate->compositor->tool_axis_cb(tstate, event);
}
}
static void tablet_tool_proximity_notify(struct wl_listener *listener, void *data) {
2017-06-21 18:07:09 +00:00
struct wlr_event_tablet_tool_proximity *event = data;
2017-06-15 20:15:12 +00:00
struct tablet_tool_state *tstate = wl_container_of(listener, tstate, proximity);
if (tstate->compositor->tool_proximity_cb) {
tstate->compositor->tool_proximity_cb(tstate, event->state);
}
}
static void tablet_tool_button_notify(struct wl_listener *listener, void *data) {
2017-06-21 18:07:09 +00:00
struct wlr_event_tablet_tool_button *event = data;
2017-06-15 20:15:12 +00:00
struct tablet_tool_state *tstate = wl_container_of(listener, tstate, button);
if (tstate->compositor->tool_button_cb) {
tstate->compositor->tool_button_cb(tstate, event->button, event->state);
}
}
static void tablet_tool_add(struct wlr_input_device *device,
struct compositor_state *state) {
struct tablet_tool_state *tstate = calloc(sizeof(struct tablet_tool_state), 1);
tstate->device = device;
tstate->compositor = state;
wl_list_init(&tstate->axis.link);
wl_list_init(&tstate->proximity.link);
wl_list_init(&tstate->tip.link);
wl_list_init(&tstate->button.link);
tstate->axis.notify = tablet_tool_axis_notify;
tstate->proximity.notify = tablet_tool_proximity_notify;
//tstate->tip.notify = tablet_tool_tip_notify;
tstate->button.notify = tablet_tool_button_notify;
wl_signal_add(&device->tablet_tool->events.axis, &tstate->axis);
wl_signal_add(&device->tablet_tool->events.proximity, &tstate->proximity);
//wl_signal_add(&device->tablet_tool->events.tip, &tstate->tip);
wl_signal_add(&device->tablet_tool->events.button, &tstate->button);
wl_list_insert(&state->tablet_tools, &tstate->link);
}
2017-06-19 18:49:07 +00:00
static void tablet_pad_button_notify(struct wl_listener *listener, void *data) {
2017-06-21 18:07:09 +00:00
struct wlr_event_tablet_pad_button *event = data;
2017-06-19 18:49:07 +00:00
struct tablet_pad_state *pstate = wl_container_of(listener, pstate, button);
if (pstate->compositor->pad_button_cb) {
pstate->compositor->pad_button_cb(pstate, event->button, event->state);
}
}
static void tablet_pad_add(struct wlr_input_device *device,
struct compositor_state *state) {
struct tablet_pad_state *pstate = calloc(sizeof(struct tablet_pad_state), 1);
pstate->device = device;
pstate->compositor = state;
wl_list_init(&pstate->button.link);
pstate->button.notify = tablet_pad_button_notify;
wl_signal_add(&device->tablet_pad->events.button, &pstate->button);
wl_list_insert(&state->tablet_pads, &pstate->link);
}
2017-06-13 16:21:36 +00:00
static void input_add_notify(struct wl_listener *listener, void *data) {
2017-06-13 14:13:11 +00:00
struct wlr_input_device *device = data;
struct compositor_state *state = wl_container_of(listener, state, input_add);
2017-06-13 16:21:36 +00:00
switch (device->type) {
case WLR_INPUT_DEVICE_KEYBOARD:
keyboard_add(device, state);
break;
case WLR_INPUT_DEVICE_POINTER:
pointer_add(device, state);
break;
2017-06-14 18:50:09 +00:00
case WLR_INPUT_DEVICE_TOUCH:
touch_add(device, state);
break;
2017-06-15 20:15:12 +00:00
case WLR_INPUT_DEVICE_TABLET_TOOL:
tablet_tool_add(device, state);
2017-06-19 18:49:07 +00:00
break;
case WLR_INPUT_DEVICE_TABLET_PAD:
tablet_pad_add(device, state);
break;
2017-06-13 16:21:36 +00:00
default:
break;
2017-06-13 14:13:11 +00:00
}
2017-08-20 20:02:39 +00:00
if (state->input_add_cb) {
state->input_add_cb(state, device);
}
2017-06-13 16:21:36 +00:00
}
static void keyboard_remove(struct wlr_input_device *device, struct compositor_state *state) {
2017-06-13 14:13:11 +00:00
struct keyboard_state *kbstate = NULL, *_kbstate;
wl_list_for_each(_kbstate, &state->keyboards, link) {
if (_kbstate->device == device) {
2017-06-13 16:21:36 +00:00
kbstate = _kbstate;
2017-06-13 14:13:11 +00:00
break;
}
}
if (!kbstate) {
2017-06-13 16:21:36 +00:00
return;
2017-06-13 14:13:11 +00:00
}
xkb_state_unref(kbstate->xkb_state);
xkb_map_unref(kbstate->keymap);
2017-06-13 14:13:11 +00:00
wl_list_remove(&kbstate->link);
wl_list_remove(&kbstate->key.link);
free(kbstate);
2017-06-13 14:13:11 +00:00
}
2017-06-13 16:21:36 +00:00
static void pointer_remove(struct wlr_input_device *device, struct compositor_state *state) {
struct pointer_state *pstate = NULL, *_pstate;
wl_list_for_each(_pstate, &state->pointers, link) {
if (_pstate->device == device) {
pstate = _pstate;
break;
}
}
if (!pstate) {
return;
}
wl_list_remove(&pstate->link);
2017-06-14 18:50:09 +00:00
wl_list_remove(&pstate->motion.link);
wl_list_remove(&pstate->motion_absolute.link);
2017-06-14 18:50:09 +00:00
wl_list_remove(&pstate->button.link);
wl_list_remove(&pstate->axis.link);
free(pstate);
2017-06-14 18:50:09 +00:00
}
static void touch_remove(struct wlr_input_device *device, struct compositor_state *state) {
struct touch_state *tstate = NULL, *_tstate;
wl_list_for_each(_tstate, &state->touch, link) {
if (_tstate->device == device) {
tstate = _tstate;
break;
}
}
if (!tstate) {
return;
}
wl_list_remove(&tstate->link);
wl_list_remove(&tstate->down.link);
wl_list_remove(&tstate->motion.link);
wl_list_remove(&tstate->up.link);
wl_list_remove(&tstate->cancel.link);
free(tstate);
2017-06-13 16:21:36 +00:00
}
2017-06-15 20:15:12 +00:00
static void tablet_tool_remove(struct wlr_input_device *device, struct compositor_state *state) {
struct tablet_tool_state *tstate = NULL, *_tstate;
wl_list_for_each(_tstate, &state->tablet_tools, link) {
if (_tstate->device == device) {
tstate = _tstate;
break;
}
}
if (!tstate) {
return;
}
wl_list_remove(&tstate->link);
wl_list_remove(&tstate->axis.link);
wl_list_remove(&tstate->proximity.link);
//wl_list_remove(&tstate->tip.link);
wl_list_remove(&tstate->button.link);
free(tstate);
2017-06-15 20:15:12 +00:00
}
static void tablet_pad_remove(struct wlr_input_device *device, struct compositor_state *state) {
struct tablet_pad_state *pstate = NULL, *_pstate;
wl_list_for_each(_pstate, &state->tablet_pads, link) {
if (_pstate->device ==device) {
pstate = _pstate;
break;
}
}
if (!pstate) {
return;
}
wl_list_remove(&pstate->button.link);
free(pstate);
}
2017-06-13 16:21:36 +00:00
static void input_remove_notify(struct wl_listener *listener, void *data) {
struct wlr_input_device *device = data;
struct compositor_state *state = wl_container_of(listener, state, input_remove);
2017-06-13 16:21:36 +00:00
switch (device->type) {
case WLR_INPUT_DEVICE_KEYBOARD:
keyboard_remove(device, state);
break;
case WLR_INPUT_DEVICE_POINTER:
pointer_remove(device, state);
break;
2017-06-14 18:50:09 +00:00
case WLR_INPUT_DEVICE_TOUCH:
touch_remove(device, state);
break;
2017-06-15 20:15:12 +00:00
case WLR_INPUT_DEVICE_TABLET_TOOL:
tablet_tool_remove(device, state);
break;
case WLR_INPUT_DEVICE_TABLET_PAD:
tablet_pad_remove(device, state);
break;
2017-06-13 16:21:36 +00:00
default:
break;
}
}
2017-06-13 14:13:11 +00:00
static void output_frame_notify(struct wl_listener *listener, void *data) {
struct output_state *output = wl_container_of(listener, output, frame);
struct compositor_state *compositor = output->compositor;
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
if (compositor->output_frame_cb) {
compositor->output_frame_cb(output, &now);
}
output->last_frame = now;
compositor->last_frame = now;
}
static void output_resolution_notify(struct wl_listener *listener, void *data) {
struct output_state *output = wl_container_of(listener, output, resolution);
struct compositor_state *compositor = output->compositor;
if (compositor->output_resolution_cb) {
2017-08-17 20:19:08 +00:00
compositor->output_resolution_cb(compositor, output);
}
}
2017-06-13 14:13:11 +00:00
static void output_add_notify(struct wl_listener *listener, void *data) {
struct wlr_output *output = data;
struct compositor_state *state = wl_container_of(listener, state, output_add);
2017-06-21 18:15:06 +00:00
wlr_log(L_DEBUG, "Output '%s' added", output->name);
wlr_log(L_DEBUG, "%s %s %"PRId32"mm x %"PRId32"mm", output->make, output->model,
2017-06-13 14:13:11 +00:00
output->phys_width, output->phys_height);
if (output->modes->length > 0) {
wlr_output_set_mode(output, output->modes->items[0]);
}
2017-06-13 14:13:11 +00:00
struct output_state *ostate = calloc(1, sizeof(struct output_state));
clock_gettime(CLOCK_MONOTONIC, &ostate->last_frame);
ostate->output = output;
ostate->compositor = state;
ostate->frame.notify = output_frame_notify;
ostate->resolution.notify = output_resolution_notify;
2017-06-13 14:13:11 +00:00
wl_list_init(&ostate->frame.link);
wl_signal_add(&output->events.frame, &ostate->frame);
wl_signal_add(&output->events.resolution, &ostate->resolution);
2017-06-13 14:13:11 +00:00
wl_list_insert(&state->outputs, &ostate->link);
if (state->output_add_cb) {
state->output_add_cb(ostate);
}
}
static void output_remove_notify(struct wl_listener *listener, void *data) {
struct wlr_output *output = data;
struct compositor_state *state = wl_container_of(listener, state, output_remove);
struct output_state *ostate = NULL, *_ostate;
wl_list_for_each(_ostate, &state->outputs, link) {
if (_ostate->output == output) {
ostate = _ostate;
break;
}
}
if (!ostate) {
return; // We are unfamiliar with this output
}
if (state->output_remove_cb) {
state->output_remove_cb(ostate);
}
wl_list_remove(&ostate->link);
wl_list_remove(&ostate->frame.link);
wl_list_remove(&ostate->resolution.link);
free(ostate);
2017-06-13 14:13:11 +00:00
}
void compositor_init(struct compositor_state *state) {
state->display = wl_display_create();
state->event_loop = wl_display_get_event_loop(state->display);
wl_list_init(&state->keyboards);
2017-06-13 16:21:36 +00:00
wl_list_init(&state->pointers);
2017-06-14 18:50:09 +00:00
wl_list_init(&state->touch);
2017-06-15 20:15:12 +00:00
wl_list_init(&state->tablet_tools);
2017-06-19 18:49:07 +00:00
wl_list_init(&state->tablet_pads);
2017-06-13 14:13:11 +00:00
wl_list_init(&state->input_add.link);
state->input_add.notify = input_add_notify;
wl_list_init(&state->input_remove.link);
state->input_remove.notify = input_remove_notify;
wl_list_init(&state->outputs);
wl_list_init(&state->output_add.link);
state->output_add.notify = output_add_notify;
wl_list_init(&state->output_remove.link);
state->output_remove.notify = output_remove_notify;
struct wlr_backend *wlr = wlr_backend_autocreate(state->display);
2017-06-13 14:13:11 +00:00
if (!wlr) {
exit(1);
}
wl_signal_add(&wlr->events.input_add, &state->input_add);
wl_signal_add(&wlr->events.input_remove, &state->input_remove);
wl_signal_add(&wlr->events.output_add, &state->output_add);
wl_signal_add(&wlr->events.output_remove, &state->output_remove);
state->backend = wlr;
clock_gettime(CLOCK_MONOTONIC, &state->last_frame);
2017-06-21 18:33:39 +00:00
const char *socket = wl_display_add_socket_auto(state->display);
if (!socket) {
wlr_log_errno(L_ERROR, "Unable to open wayland socket");
wlr_backend_destroy(wlr);
exit(1);
}
2017-06-21 18:33:39 +00:00
wlr_log(L_INFO, "Running compositor on wayland display '%s'", socket);
setenv("_WAYLAND_DISPLAY", socket, true);
if (!wlr_backend_start(state->backend)) {
wlr_log(L_ERROR, "Failed to start backend");
wlr_backend_destroy(wlr);
2017-06-13 14:13:11 +00:00
exit(1);
}
}
2017-06-13 14:13:11 +00:00
void compositor_fini(struct compositor_state *state) {
2017-06-13 14:13:11 +00:00
wlr_backend_destroy(state->backend);
wl_display_destroy(state->display);
}