Skip to content

Commit

Permalink
Unify the rawfb demos
Browse files Browse the repository at this point in the history
  • Loading branch information
RobLoach committed Apr 4, 2024
1 parent 969fa94 commit 4aba6dd
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 62 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CFLAGS=`sdl2-config --cflags --libs` -std=c89 -Wall -Wextra -pedantic -Wno-unu

.PHONY: clean

demo: main.c sdl2surface_rawfb.h
demo: main.c nuklear_sdl2_rawfb.h
$(CC) -o demo *.c $(CFLAGS) -lrt -lm

clean:
Expand Down
4 changes: 2 additions & 2 deletions demo/sdl2surface_rawfb/main.c → demo/rawfb/sdl2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_INCLUDE_SOFTWARE_FONT
#include "../../nuklear.h"
#include "../../../nuklear.h"
#define NK_RAWFB_IMPLEMENTATION
#include "sdl2surface_rawfb.h"
#include "nuklear_sdl2_rawfb.h"

/* ===============================================================
*
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
114 changes: 57 additions & 57 deletions demo/wayland_rawfb/main.c → demo/rawfb/wayland/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
#include <time.h>
#include <sys/time.h>

#include "../../nuklear.h"
#include "../../../nuklear.h"

#include "xdg-shell.h"
#include "../../demo/common/nuklear_rawfb.h"
#include "../nuklear_rawfb.h"

struct nk_wayland {
/*wayland vars*/
Expand Down Expand Up @@ -137,7 +137,7 @@ static const struct wl_output_listener nk_wayland_output_listener =
//-------------------------------------------------------------------- endof WAYLAND OUTPUT INTERFACE

//WAYLAND POINTER INTERFACE (mouse/touchpad)
static void nk_wayland_pointer_enter (void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t surface_x, wl_fixed_t surface_y)
static void nk_wayland_pointer_enter (void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t surface_x, wl_fixed_t surface_y)
{
NK_UNUSED(data);
NK_UNUSED(pointer);
Expand All @@ -147,15 +147,15 @@ static void nk_wayland_pointer_enter (void *data, struct wl_pointer *pointer, ui
NK_UNUSED(surface_y);
}

static void nk_wayland_pointer_leave (void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface)
static void nk_wayland_pointer_leave (void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface)
{
NK_UNUSED(data);
NK_UNUSED(pointer);
NK_UNUSED(serial);
NK_UNUSED(surface);
}

static void nk_wayland_pointer_motion (void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t x, wl_fixed_t y)
static void nk_wayland_pointer_motion (void *data, struct wl_pointer *pointer, uint32_t time, wl_fixed_t x, wl_fixed_t y)
{
struct nk_wayland* win = (struct nk_wayland*)data;

Expand All @@ -164,11 +164,11 @@ static void nk_wayland_pointer_motion (void *data, struct wl_pointer *pointer, u

win->mouse_pointer_x = wl_fixed_to_int(x);
win->mouse_pointer_y = wl_fixed_to_int(y);

nk_input_motion(&(win->rawfb->ctx), win->mouse_pointer_x, win->mouse_pointer_y);
}

static void nk_wayland_pointer_button (void *data, struct wl_pointer *pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state)
static void nk_wayland_pointer_button (void *data, struct wl_pointer *pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state)
{
struct nk_wayland* win = (struct nk_wayland*)data;

Expand All @@ -180,14 +180,14 @@ static void nk_wayland_pointer_button (void *data, struct wl_pointer *pointer, u
if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
// printf("nk_input_button x=%d, y=%d press: 1 \n", win->mouse_pointer_x, win->mouse_pointer_y);
nk_input_button(&(win->rawfb->ctx), NK_BUTTON_LEFT, win->mouse_pointer_x, win->mouse_pointer_y, 1);

} else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
nk_input_button(&(win->rawfb->ctx), NK_BUTTON_LEFT, win->mouse_pointer_x, win->mouse_pointer_y, 0);
}
}
}

static void nk_wayland_pointer_axis (void *data, struct wl_pointer *pointer, uint32_t time, uint32_t axis, wl_fixed_t value)
static void nk_wayland_pointer_axis (void *data, struct wl_pointer *pointer, uint32_t time, uint32_t axis, wl_fixed_t value)
{
NK_UNUSED(data);
NK_UNUSED(pointer);
Expand All @@ -196,12 +196,12 @@ static void nk_wayland_pointer_axis (void *data, struct wl_pointer *pointer, uin
NK_UNUSED(value);
}

static struct wl_pointer_listener nk_wayland_pointer_listener =
static struct wl_pointer_listener nk_wayland_pointer_listener =
{
&nk_wayland_pointer_enter,
&nk_wayland_pointer_leave,
&nk_wayland_pointer_motion,
&nk_wayland_pointer_button,
&nk_wayland_pointer_enter,
&nk_wayland_pointer_leave,
&nk_wayland_pointer_motion,
&nk_wayland_pointer_button,
&nk_wayland_pointer_axis,
NULL,
NULL,
Expand All @@ -211,7 +211,7 @@ static struct wl_pointer_listener nk_wayland_pointer_listener =
//-------------------------------------------------------------------- endof WAYLAND POINTER INTERFACE

//WAYLAND KEYBOARD INTERFACE
static void nk_wayland_keyboard_keymap (void *data, struct wl_keyboard *keyboard, uint32_t format, int32_t fd, uint32_t size)
static void nk_wayland_keyboard_keymap (void *data, struct wl_keyboard *keyboard, uint32_t format, int32_t fd, uint32_t size)
{
NK_UNUSED(data);
NK_UNUSED(keyboard);
Expand All @@ -220,7 +220,7 @@ static void nk_wayland_keyboard_keymap (void *data, struct wl_keyboard *keyboard
NK_UNUSED(size);
}

static void nk_wayland_keyboard_enter (void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys)
static void nk_wayland_keyboard_enter (void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys)
{
NK_UNUSED(data);
NK_UNUSED(keyboard);
Expand All @@ -229,15 +229,15 @@ static void nk_wayland_keyboard_enter (void *data, struct wl_keyboard *keyboard,
NK_UNUSED(keys);
}

static void nk_wayland_keyboard_leave (void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface)
static void nk_wayland_keyboard_leave (void *data, struct wl_keyboard *keyboard, uint32_t serial, struct wl_surface *surface)
{
NK_UNUSED(data);
NK_UNUSED(keyboard);
NK_UNUSED(serial);
NK_UNUSED(surface);
}

static void nk_wayland_keyboard_key (void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
static void nk_wayland_keyboard_key (void *data, struct wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
{
NK_UNUSED(data);
NK_UNUSED(keyboard);
Expand All @@ -258,22 +258,22 @@ static void nk_wayland_keyboard_modifiers (void *data, struct wl_keyboard *keybo
NK_UNUSED(group);
}

static struct wl_keyboard_listener nk_wayland_keyboard_listener =
static struct wl_keyboard_listener nk_wayland_keyboard_listener =
{
&nk_wayland_keyboard_keymap,
&nk_wayland_keyboard_enter,
&nk_wayland_keyboard_leave,
&nk_wayland_keyboard_key,
&nk_wayland_keyboard_keymap,
&nk_wayland_keyboard_enter,
&nk_wayland_keyboard_leave,
&nk_wayland_keyboard_key,
&nk_wayland_keyboard_modifiers,
NULL
};
//-------------------------------------------------------------------- endof WAYLAND KEYBOARD INTERFACE

//WAYLAND SEAT INTERFACE
static void seat_capabilities (void *data, struct wl_seat *seat, uint32_t capabilities)
static void seat_capabilities (void *data, struct wl_seat *seat, uint32_t capabilities)
{
struct nk_wayland* win = (struct nk_wayland*)data;

if (capabilities & WL_SEAT_CAPABILITY_POINTER) {
struct wl_pointer *pointer = wl_seat_get_pointer (seat);
wl_pointer_add_listener (pointer, &nk_wayland_pointer_listener, win);
Expand Down Expand Up @@ -338,7 +338,7 @@ static struct xdg_toplevel_listener nk_wayland_xdg_toplevel_listener =


// WAYLAND REGISTRY INTERFACE
static void nk_wayland_registry_add_object (void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version)
static void nk_wayland_registry_add_object (void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version)
{
struct nk_wayland* win = (struct nk_wayland*)data;

Expand All @@ -347,38 +347,38 @@ static void nk_wayland_registry_add_object (void *data, struct wl_registry *regi
//printf("looking for %s interface \n", interface);
if (!strcmp(interface,"wl_compositor")) {
win->compositor = wl_registry_bind (registry, name, &wl_compositor_interface, 1);

} else if (!strcmp(interface,"xdg_wm_base")) {
win->xdg_wm_base = wl_registry_bind (registry, name, &xdg_wm_base_interface, 1);
xdg_wm_base_add_listener (win->xdg_wm_base, &nk_wayland_xdg_wm_base_listener, win);
} else if (!strcmp(interface,"wl_shm")) {
win->wl_shm = wl_registry_bind (registry, name, &wl_shm_interface, 1);

} else if (!strcmp(interface,"wl_seat")) {
win->seat = wl_registry_bind (registry, name, &wl_seat_interface, 1);
wl_seat_add_listener (win->seat, &seat_listener, win);

} else if (!strcmp(interface, "wl_output")) {
struct wl_output *wl_output = wl_registry_bind(registry, name, &wl_output_interface, 1);
wl_output_add_listener(wl_output, &nk_wayland_output_listener, NULL);
}
}

static void nk_wayland_registry_remove_object (void *data, struct wl_registry *registry, uint32_t name)
static void nk_wayland_registry_remove_object (void *data, struct wl_registry *registry, uint32_t name)
{
NK_UNUSED(data);
NK_UNUSED(registry);
NK_UNUSED(name);
}

static struct wl_registry_listener nk_wayland_registry_listener =
static struct wl_registry_listener nk_wayland_registry_listener =
{
&nk_wayland_registry_add_object,
&nk_wayland_registry_add_object,
&nk_wayland_registry_remove_object
};
//------------------------------------------------------------------------------------------------ endof WAYLAND REGISTRY INTERFACE

static void nk_wayland_deinit(struct nk_wayland *win)
static void nk_wayland_deinit(struct nk_wayland *win)
{
xdg_toplevel_destroy (win->xdg_toplevel);
xdg_surface_destroy (win->xdg_surface);
Expand Down Expand Up @@ -407,16 +407,16 @@ static void nk_wayland_surf_clear(struct nk_wayland* win)
{
int x, y;
int pix_idx;

for (y = 0; y < HEIGHT; y++){
for (x = 0; x < WIDTH; x++){
for (x = 0; x < WIDTH; x++){
pix_idx = y * WIDTH + x;
win->data[pix_idx] = 0xFF000000;
}
}
}

//This causes the screen to refresh
//This causes the screen to refresh
static const struct wl_callback_listener frame_listener;

static void redraw(void *data, struct wl_callback *callback, uint32_t time)
Expand All @@ -428,42 +428,42 @@ static void redraw(void *data, struct wl_callback *callback, uint32_t time)
NK_UNUSED(time);

wl_callback_destroy(win->frame_callback);
wl_surface_damage(win->surface, 0, 0, WIDTH, HEIGHT);
wl_surface_damage(win->surface, 0, 0, WIDTH, HEIGHT);



win->frame_callback = wl_surface_frame(win->surface);
wl_surface_attach(win->surface, win->front_buffer, 0, 0);
wl_callback_add_listener(win->frame_callback, &frame_listener, win);
wl_surface_commit(win->surface);

}


static const struct wl_callback_listener frame_listener = {
redraw
};

int main ()
int main ()
{
long dt;
long started;
struct nk_wayland nk_wayland_ctx;
struct wl_registry *registry;
int running = 1;

//1. Initialize display
nk_wayland_ctx.display = wl_display_connect (NULL);
if (nk_wayland_ctx.display == NULL) {
printf("no wayland display found. do you have wayland composer running? \n");
return -1;
}

registry = wl_display_get_registry (nk_wayland_ctx.display);
wl_registry_add_listener (registry, &nk_wayland_registry_listener, &nk_wayland_ctx);
wl_display_roundtrip (nk_wayland_ctx.display);


//2. Create Window
nk_wayland_ctx.surface = wl_compositor_create_surface (nk_wayland_ctx.compositor);

Expand Down Expand Up @@ -491,19 +491,19 @@ int main ()
wl_display_roundtrip (nk_wayland_ctx.display);


//3. Clear window and start rendering loop
//3. Clear window and start rendering loop
nk_wayland_surf_clear(&nk_wayland_ctx);
wl_surface_attach (nk_wayland_ctx.surface, nk_wayland_ctx.front_buffer, 0, 0);
wl_surface_commit (nk_wayland_ctx.surface);

nk_rawfb_init(nk_wayland_ctx.data, nk_wayland_ctx.tex_scratch, WIDTH, HEIGHT, WIDTH*4, PIXEL_LAYOUT_XRGB_8888);


//4. rendering UI
while (running) {
started = timestamp();
// GUI
started = timestamp();

// GUI
if (nk_begin(&(nk_wayland_ctx.rawfb->ctx), "Demo", nk_rect(50, 50, 200, 200),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|
NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) {
Expand All @@ -522,7 +522,7 @@ int main ()
nk_property_int(&(nk_wayland_ctx.rawfb->ctx), "Compression:", 0, &property, 100, 10, 1);
}
nk_end(&(nk_wayland_ctx.rawfb->ctx));

if (nk_window_is_closed(&(nk_wayland_ctx.rawfb->ctx), "Demo")) break;

/* -------------- EXAMPLES ---------------- */
Expand All @@ -540,21 +540,21 @@ int main ()
#endif
/* ----------------------------------------- */

// Draw framebuffer
// Draw framebuffer
nk_rawfb_render(nk_wayland_ctx.rawfb, nk_rgb(30,30,30), 1);


//handle wayland stuff (send display to FB & get inputs)
nk_input_begin(&(nk_wayland_ctx.rawfb->ctx));
wl_display_dispatch(nk_wayland_ctx.display);
nk_input_end(&(nk_wayland_ctx.rawfb->ctx));
// Timing

// Timing
dt = timestamp() - started;
if (dt < DTIME)
sleep_for(DTIME - dt);
}

nk_wayland_deinit (&nk_wayland_ctx);
wl_display_disconnect (nk_wayland_ctx.display);
return 0;
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions demo/x11_rawfb/main.c → demo/rawfb/x11/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
#define NK_INCLUDE_DEFAULT_FONT
#define NK_INCLUDE_SOFTWARE_FONT

#include "../../nuklear.h"
#include "../../demo/common/nuklear_rawfb.h"
#include "../../../nuklear.h"
#include "../nuklear_rawfb.h"
#include "nuklear_xlib.h"

#define DTIME 20
Expand Down
File renamed without changes.

0 comments on commit 4aba6dd

Please sign in to comment.