Added pointer and keyboard handlers
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user