diff --git a/CMakeLists.txt b/CMakeLists.txt index ec67eca..951ec5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ add_custom_command( set(SOURCES wlgol.c tkeyboard.c + tpointer.c ${CMAKE_CURRENT_BINARY_DIR}/xdg-shell.c ) @@ -38,4 +39,7 @@ target_include_directories(wlgol PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ) -target_link_libraries(wlgol wayland-client) +target_link_libraries(wlgol + wayland-client + wayland-cursor +) diff --git a/include/tkeyboard.h b/include/tkeyboard.h index e6bb8f4..65fd506 100644 --- a/include/tkeyboard.h +++ b/include/tkeyboard.h @@ -1,9 +1,12 @@ #ifndef TKEYBOARD_H #define TKEYBOARD_H -//#include #include +struct keyboard_data { + struct wl_keyboard *keyboard; +}; + extern const struct wl_keyboard_listener keyboard_listener; #endif diff --git a/include/tpointer.h b/include/tpointer.h new file mode 100644 index 0000000..694aeb7 --- /dev/null +++ b/include/tpointer.h @@ -0,0 +1,21 @@ +#ifndef TPOINTER_H_ +#define TPOINTER_H_ + +#include +#include + + +struct pointer_data { + struct wl_cursor_theme *cursor_theme; + struct wl_pointer *pointer; + struct wl_surface *cursor_surface; + struct wl_cursor *cursor; + struct wl_cursor_image *cursor_image; + struct wl_buffer *cursor_buffer; +}; + +extern const struct wl_pointer_listener pointer_listener; + +void pointer_init_cursor(struct pointer_data *pointer_data, struct wl_compositor *compositor, struct wl_shm *shm); + +#endif diff --git a/tpointer.c b/tpointer.c new file mode 100644 index 0000000..80a86ac --- /dev/null +++ b/tpointer.c @@ -0,0 +1,80 @@ +#include + +#include + +static void pointer_enter_handler(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t sx, wl_fixed_t sy) +{ + // not implemented + printf("pointer_enter\n"); + struct pointer_data *pointer_data = data; + + wl_pointer_set_cursor(pointer, serial, pointer_data->cursor_surface, + pointer_data->cursor_image->hotspot_x, pointer_data->cursor_image->hotspot_y); + printf("pointer_enter - end\n"); +} + +static void pointer_leave_handler(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface) +{ + // not implemented + printf("pointer_leave\n"); +} + +static void pointer_motion_handler(void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t sx, wl_fixed_t sy) +{ + // not implemented +} + +static void pointer_button_handler(void *data, struct wl_pointer *pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state) +{ + // not implemented +} + +static void pointer_axis_handler(void *data, struct wl_pointer *pointer, uint32_t time, uint32_t axis, wl_fixed_t value) +{ + // not implemented +} + +static void pointer_frame_handler(void *data, struct wl_pointer *pointer) +{ + // not implemented +} + +static void pointer_axis_source_handler(void *data, struct wl_pointer *pointer, uint32_t axis_source) +{ + // not implemented +} + +static void pointer_axis_discrete_handler(void *data, struct wl_pointer *pointer, uint32_t axis, int32_t discrete) +{ + // not implemented +} + +static void pointer_axis_stop_handler(void *data, struct wl_pointer *pointer, uint32_t time, uint32_t axis) +{ + // not implemented +} + +const struct wl_pointer_listener pointer_listener = { + .enter = pointer_enter_handler, + .leave = pointer_leave_handler, + .motion = pointer_motion_handler, + .button = pointer_button_handler, + .axis = pointer_axis_handler, + .frame = pointer_frame_handler, + .axis_source = pointer_axis_source_handler, + .axis_discrete = pointer_axis_discrete_handler, + .axis_stop = pointer_axis_stop_handler, +}; + +void pointer_init_cursor(struct pointer_data *pointer_data, struct wl_compositor *compositor, struct wl_shm *shm) +{ + pointer_data->cursor_theme = + wl_cursor_theme_load(NULL, 24, shm); + pointer_data->cursor = wl_cursor_theme_get_cursor(pointer_data->cursor_theme, "left_ptr"); + pointer_data->cursor_image = pointer_data->cursor->images[0]; + pointer_data->cursor_buffer = wl_cursor_image_get_buffer(pointer_data->cursor_image); + + pointer_data->cursor_surface = wl_compositor_create_surface(compositor); + wl_surface_attach(pointer_data->cursor_surface, pointer_data->cursor_buffer, 0, 0); + wl_surface_commit(pointer_data->cursor_surface); +} diff --git a/wlgol.c b/wlgol.c index 65f1d1a..7ff1c5f 100644 --- a/wlgol.c +++ b/wlgol.c @@ -16,12 +16,16 @@ #include #include +#include struct wl_compositor *compositor; struct wl_shm *shm; struct xdg_wm_base *xdg_shell; struct wl_seat *wl_seat; +struct keyboard_data keyboard_data; +struct pointer_data pointer_data; + int running = 1; int released = 0; struct timespec mytime; @@ -71,7 +75,7 @@ void registry_global_remove_handler( void *data, struct wl_registry *registry, uint32_t name) { - // notging + // nothing } const struct wl_registry_listener registry_listener = { @@ -130,8 +134,8 @@ void xdg_wm_base_ping_handler(void *data, struct xdg_wm_base *xdg_shell, const struct xdg_wm_base_listener xdg_shell_listener = { .ping = xdg_wm_base_ping_handler}; - -void wl_buffer_release_handler(void *data, struct wl_buffer * wl_buffer) { +void wl_buffer_release_handler(void *data, struct wl_buffer *wl_buffer) +{ // printf("wl_buffer_release\n"); released = 1; } @@ -139,15 +143,17 @@ void wl_buffer_release_handler(void *data, struct wl_buffer * wl_buffer) { const struct wl_buffer_listener wl_buffer_listener = { .release = wl_buffer_release_handler}; - void redraw(); // forward declaration -void wl_callack_done(void *data, struct wl_callback *callback, uint32_t time) { - if (canvas.frame) { +void wl_callack_done(void *data, struct wl_callback *callback, uint32_t time) +{ + if (canvas.frame) + { wl_callback_destroy(canvas.frame); canvas.frame = NULL; } - if (released) { + if (released) + { redraw(); released = 0; } @@ -156,7 +162,6 @@ void wl_callack_done(void *data, struct wl_callback *callback, uint32_t time) { const struct wl_callback_listener wl_surface_frame_listener = { .done = wl_callack_done}; - int main(void) { clock_gettime(CLOCK_MONOTONIC, &mytime); @@ -184,8 +189,12 @@ int main(void) // signal that the surface is ready to be configured wl_surface_commit(canvas.surface); - struct wl_keyboard *wl_keyboard = wl_seat_get_keyboard(wl_seat); - wl_keyboard_add_listener(wl_keyboard, &keyboard_listener, NULL); + keyboard_data.keyboard = wl_seat_get_keyboard(wl_seat); + wl_keyboard_add_listener(keyboard_data.keyboard, &keyboard_listener, &keyboard_data); + + pointer_init_cursor(&pointer_data, compositor, shm); + pointer_data.pointer = wl_seat_get_pointer(wl_seat); + wl_pointer_add_listener(pointer_data.pointer, &pointer_listener, &pointer_data); canvas.width = 400; canvas.height = 400; @@ -236,13 +245,14 @@ void redraw() clock_t start = clock(); mycolour += direction; - switch (mycolour) { - case 0: // lower limit, start to increase - direction = 1; - break; - case 255: // upper limit, start to decrease - direction = -1; - break; + switch (mycolour) + { + case 0: // lower limit, start to increase + direction = 1; + break; + case 255: // upper limit, start to decrease + direction = -1; + break; } // printf("dred=%d\n", dred); // draw into buffer @@ -296,9 +306,10 @@ void redraw() clock_gettime(CLOCK_MONOTONIC, &ntime); double elapsed_time = (ntime.tv_sec - mytime.tv_sec) + (ntime.tv_nsec - mytime.tv_nsec) / 1e9; - if (elapsed_time > 1.0) { + if (elapsed_time > 1.0) + { clock_t end = clock(); - double dur = ((double) end - start) / CLOCKS_PER_SEC; + double dur = ((double)end - start) / CLOCKS_PER_SEC; printf("FPS: %d in %g sec\n", framecounter, dur); mytime = ntime; framecounter = 0;