Created
August 1, 2022 12:37
-
-
Save watasuke102/408664c73c3b0e59f53a12ae84086cd7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <math.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <time.h> | |
#include <unistd.h> | |
#include <wayland-server-core.h> | |
#include <xkbcommon/xkbcommon.h> | |
#include "../include/wlr/backend.h" | |
#include "../include/wlr/backend/session.h" | |
#include "../include/wlr/render/allocator.h" | |
#include "../include/wlr/render/wlr_renderer.h" | |
#include "../include/wlr/types/wlr_cursor.h" | |
#include "../include/wlr/types/wlr_keyboard.h" | |
#include "../include/wlr/types/wlr_matrix.h" | |
#include "../include/wlr/types/wlr_output_layout.h" | |
#include "../include/wlr/types/wlr_pointer.h" | |
#include "../include/wlr/types/wlr_tablet_tool.h" | |
#include "../include/wlr/types/wlr_touch.h" | |
#include "../include/wlr/types/wlr_xcursor_manager.h" | |
#include "../include/wlr/util/log.h" | |
struct sample_state { | |
struct wl_display *display; | |
struct wlr_renderer *renderer; | |
struct wlr_allocator *allocator; | |
struct wlr_xcursor_manager *xcursor_manager; | |
struct wlr_cursor *cursor; | |
double cur_x, cur_y; | |
struct wlr_output_layout *layout; | |
struct wlr_input_device *pointer_dev; | |
struct wl_listener new_output; | |
struct wl_listener new_input; | |
struct wl_listener cursor_motion; | |
struct wl_listener cursor_button; | |
}; | |
struct sample_output { | |
struct sample_state *state; | |
struct wlr_output *output; | |
struct wl_listener frame; | |
struct wl_listener destroy; | |
}; | |
static void output_frame_notify(struct wl_listener *listener, void *data) { | |
struct sample_output *sample_output = | |
wl_container_of(listener, sample_output, frame); | |
struct sample_state *state = sample_output->state; | |
struct wlr_output *wlr_output = sample_output->output; | |
struct wlr_renderer *renderer = state->renderer; | |
assert(renderer); | |
wlr_output_attach_render(wlr_output, NULL); | |
wlr_renderer_begin(renderer, wlr_output->width, wlr_output->height); | |
wlr_renderer_clear(renderer, (float[4]){0}); | |
wlr_output_render_software_cursors(wlr_output, NULL); | |
wlr_renderer_end(renderer); | |
wlr_output_commit(wlr_output); | |
} | |
static void handle_cursor_button(struct wl_listener *listener, void *data) { | |
exit(0); | |
} | |
static void output_remove_notify(struct wl_listener *listener, void *data) { | |
struct sample_output *sample_output = | |
wl_container_of(listener, sample_output, destroy); | |
struct sample_state *sample = sample_output->state; | |
wlr_output_layout_remove(sample->layout, sample_output->output); | |
wl_list_remove(&sample_output->frame.link); | |
wl_list_remove(&sample_output->destroy.link); | |
free(sample_output); | |
} | |
static void new_output_notify(struct wl_listener *listener, void *data) { | |
struct wlr_output *output = data; | |
struct sample_state *sample = wl_container_of(listener, sample, new_output); | |
wlr_output_init_render(output, sample->allocator, sample->renderer); | |
struct sample_output *sample_output = calloc(1, sizeof(struct sample_output)); | |
sample_output->output = output; | |
sample_output->state = sample; | |
wl_signal_add(&output->events.frame, &sample_output->frame); | |
sample_output->frame.notify = output_frame_notify; | |
wl_signal_add(&output->events.destroy, &sample_output->destroy); | |
sample_output->destroy.notify = output_remove_notify; | |
wlr_output_layout_add_auto(sample->layout, sample_output->output); | |
wlr_xcursor_manager_load(sample->xcursor_manager, output->scale); | |
wlr_xcursor_manager_set_cursor_image(sample->xcursor_manager, "left_ptr", | |
sample->cursor); | |
struct wlr_output_mode *mode = wlr_output_preferred_mode(output); | |
if (mode != NULL) { | |
wlr_output_set_mode(output, mode); | |
} | |
wlr_output_commit(output); | |
wlr_cursor_move(sample->cursor, sample->pointer_dev, 100, 100); | |
} | |
static void new_input_notify(struct wl_listener *listener, void *data) { | |
struct wlr_input_device *device = data; | |
struct sample_state *state = wl_container_of(listener, state, new_input); | |
switch (device->type) { | |
case WLR_INPUT_DEVICE_POINTER: | |
case WLR_INPUT_DEVICE_TOUCH: | |
case WLR_INPUT_DEVICE_TABLET_TOOL: | |
state->pointer_dev = device; | |
wlr_cursor_attach_input_device(state->cursor, device); | |
break; | |
default: | |
break; | |
} | |
} | |
int main(int argc, char *argv[]) { | |
struct wl_display *display = wl_display_create(); | |
struct sample_state state = { | |
.display = display, | |
}; | |
struct wlr_backend *wlr = wlr_backend_autocreate(display); | |
if (!wlr) { | |
exit(1); | |
} | |
state.renderer = wlr_renderer_autocreate(wlr); | |
state.allocator = wlr_allocator_autocreate(wlr, state.renderer); | |
state.cursor = wlr_cursor_create(); | |
state.layout = wlr_output_layout_create(); | |
wlr_cursor_attach_output_layout(state.cursor, state.layout); | |
// for exit | |
wl_signal_add(&state.cursor->events.button, &state.cursor_button); | |
state.cursor_button.notify = handle_cursor_button; | |
// must | |
wl_signal_add(&wlr->events.new_input, &state.new_input); | |
state.new_input.notify = new_input_notify; | |
// must | |
wl_signal_add(&wlr->events.new_output, &state.new_output); | |
state.new_output.notify = new_output_notify; | |
state.xcursor_manager = wlr_xcursor_manager_create("default", 24); | |
if (!state.xcursor_manager) { | |
return 1; | |
} | |
if (!wlr_backend_start(wlr)) { | |
wlr_backend_destroy(wlr); | |
exit(1); | |
} | |
wl_display_run(display); | |
wl_display_destroy(display); | |
wlr_xcursor_manager_destroy(state.xcursor_manager); | |
wlr_cursor_destroy(state.cursor); | |
wlr_output_layout_destroy(state.layout); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment