Skip to content

Commit

Permalink
Manipulate mouse cursor with a minimal image
Browse files Browse the repository at this point in the history
This commit makes mouse cursor manipulation configurable, allowing it to
be turned off completely for the SDL backend and to be featured in the
upcoming Linux framebuffer backend with a minimal mouse image rendered.
Currently, only one minimalist dot is provided, but the default cursor
image should be improved later.
  • Loading branch information
jserv committed Jul 30, 2024
1 parent 009f4dc commit d4ad37f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ libtwin.a_includes-y := \
include \
src

# Features
libtwin.a_files-$(CONFIG_CURSOR) += src/cursor.c

# Image loaders

ifeq ($(CONFIG_LOADER_JPEG), y)
Expand Down
7 changes: 7 additions & 0 deletions apps/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ int main(void)
atexit(cleanup);
signal(SIGINT, sigint_handler);

#if defined(CONFIG_CURSOR)
int hx, hy;
twin_pixmap_t *cursor = twin_make_cursor(&hx, &hy);
if (cursor)
twin_screen_set_cursor(tx->screen, cursor, hx, hy);
#endif

twin_screen_set_background(
tx->screen, load_background(tx->screen, ASSET_PATH "/tux.png"));

Expand Down
8 changes: 8 additions & 0 deletions configs/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ config BACKEND_SDL

endchoice

menu "Features"

config CURSOR
bool "Manipulate cursor"
default n

endmenu

menu "Image Loaders"

config LOADER_PNG
Expand Down
6 changes: 6 additions & 0 deletions include/twin.h
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,12 @@ void twin_path_convolve(twin_path_t *dest,
twin_path_t *stroke,
twin_path_t *pen);

/*
* cursor.c
*/

twin_pixmap_t *twin_make_cursor(int *hx, int *hy);

/*
* dispatch.c
*/
Expand Down
27 changes: 27 additions & 0 deletions src/cursor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Twin - A Tiny Window System
* Copyright (c) 2024 National Cheng Kung University, Taiwan
* All rights reserved.
*/

/* Manipulating cursor images */

#include <stddef.h>
#include <twin.h>

/* FIXME: Improve the default cursor instead of using a minimalist dot. */
static uint32_t _twin_cursor_default[] = {
0x00000000, 0x88ffffff, 0x88ffffff, 0x00000000, 0x88ffffff, 0xff000000,
0xff000000, 0x88ffffff, 0x88ffffff, 0xff000000, 0xff000000, 0x88ffffff,
0x00000000, 0x88ffffff, 0x88ffffff, 0x00000000,
};

twin_pixmap_t *twin_make_cursor(int *hx, int *hy)
{
twin_pointer_t data = {.argb32 = _twin_cursor_default};
twin_pixmap_t *cur = twin_pixmap_create_const(TWIN_ARGB32, 4, 4, 16, data);
if (!cur)
return NULL;
*hx = *hy = 4;
return cur;
}
6 changes: 6 additions & 0 deletions src/screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,11 @@ void twin_screen_update(twin_screen_t *screen)
twin_screen_span_pixmap(screen, span, p, y, left, right, pop16,
pop32);

#if defined(CONFIG_CURSOR)
if (screen->cursor)
twin_screen_span_pixmap(screen, span, screen->cursor, y, left,
right, pop16, pop32);
#endif

(*screen->put_span)(left, y, right, span, screen->closure);
}
Expand Down Expand Up @@ -253,6 +255,7 @@ twin_pixmap_t *twin_screen_get_background(twin_screen_t *screen)
return screen->background;
}

#if defined(CONFIG_CURSOR)
static void twin_screen_damage_cursor(twin_screen_t *screen)
{
twin_screen_damage(screen, screen->cursor->x, screen->cursor->y,
Expand Down Expand Up @@ -302,6 +305,7 @@ static void twin_screen_update_cursor(twin_screen_t *screen,

twin_screen_enable_update(screen);
}
#endif /* CONFIG_CURSOR */

static void _twin_adj_mouse_evt(twin_event_t *event, twin_pixmap_t *pixmap)
{
Expand All @@ -320,9 +324,11 @@ bool twin_screen_dispatch(twin_screen_t *screen, twin_event_t *event)
case TwinEventMotion:
case TwinEventButtonDown:
case TwinEventButtonUp:
#if defined(CONFIG_CURSOR)
/* update mouse cursor */
twin_screen_update_cursor(screen, event->u.pointer.screen_x,
event->u.pointer.screen_y);
#endif

/* if target is tracking the mouse, check for mouse up, if not,
* just pass the event along
Expand Down

0 comments on commit d4ad37f

Please sign in to comment.