Added pointer and keyboard handlers

This commit is contained in:
Arek 2024-06-02 19:29:26 +02:00
parent 998164d7f9
commit 61eea2bd43
6 changed files with 144 additions and 54 deletions

View File

@ -1,9 +1,9 @@
cmake_minimum_required(VERSION 3.0 )
set(CMAKE_C_STANDARD 11)
project(wlgol
VERSION 0.0.1
DESCRIPTION "Wayland Game Of Life"
LANGUAGES C)
VERSION 0.0.1
DESCRIPTION "Wayland Game Of Life"
LANGUAGES C)
# Download xdg-shell.xml
# file(DOWNLOAD "https://cgit.freedesktop.org/wayland/wayland-protocols/plain/stable/xdg-shell/xdg-shell.xml" "${CMAKE_CURRENT_BINARY_DIR}/xdg-shell.xml")
@ -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
)

View File

@ -1,28 +0,0 @@
#ifndef PUZZLE_H
#define PUZZLE_H
enum colors
{
NONE = 0,
EL = 1,
T = 2,
POLE = 3,
SQUARE = 4,
LZIG = 5,
RZIG = 6,
};
/**
* @struct puzzle
* Represents a puzzle in the Tetris game.
*/
struct puzzle {
int with; // The width of the puzzle
int height; // The height of the puzzle
unsigned char *data; // The data of the puzzle
enum colors type; // The type of the puzzle
};
#endif

View File

@ -1,9 +1,12 @@
#ifndef TKEYBOARD_H
#define TKEYBOARD_H
//#include <wayland-client-protocol.h>
#include <wayland-client.h>
struct keyboard_data {
struct wl_keyboard *keyboard;
};
extern const struct wl_keyboard_listener keyboard_listener;
#endif

21
include/tpointer.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef TPOINTER_H_
#define TPOINTER_H_
#include <wayland-client.h>
#include <wayland-cursor.h>
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

80
tpointer.c Normal file
View File

@ -0,0 +1,80 @@
#include <stdio.h>
#include <tpointer.h>
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);
}

52
wlgol.c
View File

@ -11,17 +11,19 @@
#include <unistd.h>
#include <xdg-shell.h>
#include <wayland-client-protocol.h>
#include <wayland-client.h>
#include <puzzle.h>
#include <tkeyboard.h>
#include <tpointer.h>
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 +73,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 +132,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 +141,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 +160,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 +187,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;
@ -225,6 +232,7 @@ int main(void)
wl_surface_destroy(canvas.surface);
wl_shm_pool_destroy(pool);
wl_registry_destroy(registry);
wl_display_disconnect(display);
}
unsigned char mycolour = 0;
@ -236,13 +244,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 +305,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;