aboutsummaryrefslogtreecommitdiff
path: root/example/example.c
diff options
context:
space:
mode:
Diffstat (limited to 'example/example.c')
-rw-r--r--example/example.c681
1 files changed, 60 insertions, 621 deletions
diff --git a/example/example.c b/example/example.c
index 166c602..2d8b94e 100644
--- a/example/example.c
+++ b/example/example.c
@@ -26,23 +26,6 @@
#include <d2tk/frontend_pugl.h>
#include "example/example.h"
-#if D2TK_EVDEV
-# include <libevdev/libevdev.h>
-# include <libevdev/libevdev-uinput.h>
-
-typedef struct _fake_t fake_t;
-
-struct _fake_t {
- struct libevdev *dev;
- struct libevdev_uinput *uidev;
-};
-
-static fake_t fake = {
- .dev = NULL,
- .uidev = NULL
-};
-#endif
-
typedef union _val_t val_t;
union _val_t {
@@ -56,6 +39,7 @@ union _val_t {
typedef enum _bar_t {
BAR_MIX,
+ BAR_SPINNER,
BAR_SEQ,
BAR_TABLE_ABS,
BAR_SCROLL,
@@ -81,6 +65,7 @@ typedef enum _bar_t {
static bar_t bar = BAR_MIX;
static const char *bar_lbl [BAR_MAX] = {
[BAR_MIX] = "Mix of many",
+ [BAR_SPINNER] = "Spinner",
[BAR_SEQ] = "Sequencer",
[BAR_TABLE_ABS] = "Table Abs",
[BAR_SCROLL] = "Scrollbar",
@@ -265,6 +250,28 @@ _render_c_mix(d2tk_base_t *base, const d2tk_rect_t *rect)
}
static inline void
+_render_c_spinner(d2tk_base_t *base, const d2tk_rect_t *rect)
+{
+#define N 8
+#define M 24
+ static int32_t val [N*M];
+
+ D2TK_BASE_TABLE(rect, N, M, D2TK_FLAG_TABLE_REL, tab)
+ {
+ const d2tk_rect_t *trect = d2tk_table_get_rect(tab);
+ const unsigned k = d2tk_table_get_index(tab);
+ const d2tk_id_t id = D2TK_ID_IDX(k);
+
+ if(d2tk_base_spinner_int32_is_changed(base, id, trect, -99, &val[k], 99))
+ {
+ fprintf(stdout, "spinner %016"PRIx64" %"PRIi32"\n", id, val[k]);
+ }
+ }
+#undef N
+#undef M
+}
+
+static inline void
_render_c_seq(d2tk_base_t *base, const d2tk_rect_t *rect)
{
#define N (8*12)
@@ -805,80 +812,6 @@ _render_c_utf8(d2tk_base_t *base, const d2tk_rect_t *rect)
}
#if D2TK_PTY
-#include <linenoise/linenoise.h>
-
-static void
-_completion(const char *buf, linenoiseCompletions *lc)
-{
- switch(buf[0])
- {
- case 'b':
- {
- linenoiseAddCompletion(lc, "bar");
- } break;
- case 'c':
- {
- linenoiseAddCompletion(lc, "clear");
- } break;
- case 'f':
- {
- linenoiseAddCompletion(lc, "foo");
- } break;
- }
-}
-
-static char *
-_hints(const char *buf, int *color, int *bold)
-{
- static const char *hint = "<cmd>";
-
- switch(buf[0])
- {
- case '\0':
- {
- *color = 36;
- *bold = 0;
- return (char *)hint;
- } break;
- }
-
- return NULL;
-}
-
-static void
-_curses(FILE *err __attribute__((unused)), void *data __attribute__((unused)))
-{
- linenoiseSetMultiLine(0);
- linenoiseSetCompletionCallback(_completion);
- linenoiseSetHintsCallback(_hints);
- linenoiseHistorySetMaxLen(128);
-
- while(true)
- {
- char *line = linenoise("> ");
-
- if(!line)
- {
- break;
- }
-
- if(strlen(line))
- {
- //FIXME
- linenoiseHistoryAdd(line);
- }
-
- if(!strcmp(line, "clear"))
- {
- linenoiseClearScreen();
- }
-
- linenoiseFree(line);
- }
-
- _exit(EXIT_SUCCESS);
-}
-
static inline void
_render_c_pty(d2tk_base_t *base, const d2tk_rect_t *rect)
{
@@ -888,6 +821,10 @@ _render_c_pty(d2tk_base_t *base, const d2tk_rect_t *rect)
NULL
};
+ static uint32_t last_red = 0x0;;
+ static uint32_t last_green = 0x0;;
+ static uint32_t last_blue = 0x0;;
+
static d2tk_coord_t hfrac [2] = { 1, 1 };
D2TK_BASE_LAYOUT(rect, 2, hfrac, D2TK_FLAG_LAYOUT_X_REL, hlay)
@@ -899,11 +836,35 @@ _render_c_pty(d2tk_base_t *base, const d2tk_rect_t *rect)
{
case 0:
{
- d2tk_base_pty(base, D2TK_ID, NULL, argv, HEIGHT, hrect, false);
+ D2TK_BASE_PTY(base, D2TK_ID, argv, HEIGHT, hrect, false, pty)
+ {
+ const uint32_t max_red = d2tk_pty_get_max_red(pty);
+ const uint32_t max_green = d2tk_pty_get_max_green(pty);
+ const uint32_t max_blue = d2tk_pty_get_max_blue(pty);
+
+ if(max_red != last_red)
+ {
+ fprintf(stderr, "max_red: 0x%08x\n", max_red);
+ last_red = max_red;
+ }
+ if(max_green != last_green)
+ {
+ fprintf(stderr, "max_green: 0x%08x\n", max_green);
+ last_green = max_green;
+ }
+ if(max_blue != last_blue)
+ {
+ fprintf(stderr, "max_blue: 0x%08x\n", max_blue);
+ last_blue = max_blue;
+ }
+ }
} break;
case 1:
{
- d2tk_base_pty(base, D2TK_ID, _curses, NULL, HEIGHT, hrect, false);
+ D2TK_BASE_PTY(base, D2TK_ID, argv, HEIGHT, hrect, false, pty)
+ {
+ // nothing to do
+ }
} break;
}
}
@@ -1161,548 +1122,22 @@ _render_c_browser(d2tk_base_t *base, const d2tk_rect_t *rect)
#endif
#if D2TK_EVDEV
-static void
-_fake_event(unsigned type, unsigned code, int value)
-{
- if(fake.uidev)
- {
- libevdev_uinput_write_event(fake.uidev, type, code, value);
- }
-}
-
-static void
-_fake_key_down(unsigned keycode)
-{
- _fake_event(EV_KEY, keycode, 1);
- _fake_event(EV_SYN, SYN_REPORT, 0);
-}
-
-static void
-_fake_key_up(unsigned keycode)
-{
- _fake_event(EV_KEY, keycode, 0);
- _fake_event(EV_SYN, SYN_REPORT, 0);
-}
-
-typedef struct _keybtn_t keybtn_t;
-
-struct _keybtn_t {
- const char *name;
- const char *altn;
- unsigned code;
- float rect [4];
-};
-
-#define W (1.f / 15.f)
-#define W_2 (W / 2)
-#define H (1.f / 6.f)
-
-static const keybtn_t keybtns [] = {
- // row 1
- {
- .name = "Esc",
- .code = KEY_ESC,
- .rect = { 0*W, 0*H, W + W_2, H }
- },
- {
- .name = "F1",
- .code = KEY_F1,
- .rect = { 1*W + W_2, 0*H, W, H }
- },
- {
- .name = "F2",
- .code = KEY_F2,
- .rect = { 2*W + W_2, 0*H, W, H }
- },
- {
- .name = "F3",
- .code = KEY_F3,
- .rect = { 3*W + W_2, 0*H, W, H }
- },
- {
- .name = "F4",
- .code = KEY_F4,
- .rect = { 4*W + W_2, 0*H, W, H }
- },
- {
- .name = "F5",
- .code = KEY_F5,
- .rect = { 5*W + W_2, 0*H, W, H }
- },
- {
- .name = "F6",
- .code = KEY_F6,
- .rect = { 6*W + W_2, 0*H, W, H }
- },
- {
- .name = "F7",
- .code = KEY_F7,
- .rect = { 7*W + W_2, 0*H, W, H }
- },
- {
- .name = "F8",
- .code = KEY_F8,
- .rect = { 8*W + W_2, 0*H, W, H }
- },
- {
- .name = "F9",
- .code = KEY_F9,
- .rect = { 9*W + W_2, 0*H, W, H }
- },
- {
- .name = "F10",
- .code = KEY_F10,
- .rect = { 10*W + W_2, 0*H, W, H }
- },
- {
- .name = "F11",
- .code = KEY_F11,
- .rect = { 11*W + W_2, 0*H, W, H }
- },
- {
- .name = "F12",
- .code = KEY_F12,
- .rect = { 12*W + W_2, 0*H, W, H }
- },
- {
- .name = "Delete",
- .code = KEY_DELETE,
- .rect = { 13*W + W_2, 0*H, W + W_2, H }
- },
-
- // row 2
- {
- .name = "`",
- .altn = "~",
- .code = KEY_GRAVE,
- .rect = { 0*W, 1*H, W, H }
- },
- {
- .name = "1",
- .altn = "!",
- .code = KEY_1,
- .rect = { 1*W, 1*H, W, H }
- },
- {
- .name = "2",
- .altn = "@",
- .code = KEY_2,
- .rect = { 2*W, 1*H, W, H }
- },
- {
- .name = "3",
- .altn = "#",
- .code = KEY_3,
- .rect = { 3*W, 1*H, W, H }
- },
- {
- .name = "4",
- .altn = "$",
- .code = KEY_4,
- .rect = { 4*W, 1*H, W, H }
- },
- {
- .name = "5",
- .altn = "%",
- .code = KEY_5,
- .rect = { 5*W, 1*H, W, H }
- },
- {
- .name = "6",
- .altn = "^",
- .code = KEY_6,
- .rect = { 6*W, 1*H, W, H }
- },
- {
- .name = "7",
- .altn = "&",
- .code = KEY_7,
- .rect = { 7*W, 1*H, W, H }
- },
- {
- .name = "8",
- .altn = "*",
- .code = KEY_8,
- .rect = { 8*W, 1*H, W, H }
- },
- {
- .name = "9",
- .altn = "(",
- .code = KEY_9,
- .rect = { 9*W, 1*H, W, H }
- },
- {
- .name = "0",
- .altn = ")",
- .code = KEY_0,
- .rect = { 10*W, 1*H, W, H }
- },
- {
- .name = "-",
- .altn = "_",
- .code = KEY_MINUS,
- .rect = { 11*W, 1*H, W, H }
- },
- {
- .name = "=",
- .altn = "+",
- .code = KEY_EQUAL,
- .rect = { 12*W, 1*H, W, H }
- },
- {
- .name = "Back",
- .code = KEY_BACKSPACE,
- .rect = { 13*W, 1*H, 2*W, H }
- },
-
- // row 3
- {
- .name = "Tab",
- .code = KEY_TAB,
- .rect = { 0*W, 2*H, W + W_2, H }
- },
- {
- .name = "Q",
- .code = KEY_Q,
- .rect = { 1*W + W_2, 2*H, W, H }
- },
- {
- .name = "W",
- .code = KEY_W,
- .rect = { 2*W + W_2, 2*H, W, H }
- },
- {
- .name = "E",
- .code = KEY_E,
- .rect = { 3*W + W_2, 2*H, W, H }
- },
- {
- .name = "R",
- .code = KEY_R,
- .rect = { 4*W + W_2, 2*H, W, H }
- },
- {
- .name = "T",
- .code = KEY_T,
- .rect = { 5*W + W_2, 2*H, W, H }
- },
- {
- .name = "Y",
- .code = KEY_Y,
- .rect = { 6*W + W_2, 2*H, W, H }
- },
- {
- .name = "U",
- .code = KEY_U,
- .rect = { 7*W + W_2, 2*H, W, H }
- },
- {
- .name = "I",
- .code = KEY_I,
- .rect = { 8*W + W_2, 2*H, W, H }
- },
- {
- .name = "O",
- .code = KEY_O,
- .rect = { 9*W + W_2, 2*H, W, H }
- },
- {
- .name = "P",
- .code = KEY_P,
- .rect = { 10*W + W_2, 2*H, W, H }
- },
- {
- .name = "[",
- .altn = "{",
- .code = KEY_LEFTBRACE,
- .rect = { 11*W + W_2, 2*H, W, H }
- },
- {
- .name = "]",
- .altn = "}",
- .code = KEY_RIGHTBRACE,
- .rect = { 12*W + W_2, 2*H, W, H }
- },
- {
- .name = "\\",
- .altn = "|",
- .code = KEY_BACKSLASH,
- .rect = { 13*W + W_2, 2*H, 2*W - W_2, H }
- },
-
- // row 4
- {
- .name = "Caps",
- .code = KEY_CAPSLOCK,
- .rect = { 0*W, 3*H, W*2, H }
- },
- {
- .name = "A",
- .code = KEY_A,
- .rect = { 2*W, 3*H, W, H }
- },
- {
- .name = "S",
- .code = KEY_S,
- .rect = { 3*W, 3*H, W, H }
- },
- {
- .name = "D",
- .code = KEY_D,
- .rect = { 4*W, 3*H, W, H }
- },
- {
- .name = "F",
- .code = KEY_F,
- .rect = { 5*W, 3*H, W, H }
- },
- {
- .name = "G",
- .code = KEY_G,
- .rect = { 6*W, 3*H, W, H }
- },
- {
- .name = "H",
- .code = KEY_H,
- .rect = { 7*W, 3*H, W, H }
- },
- {
- .name = "J",
- .code = KEY_J,
- .rect = { 8*W, 3*H, W, H }
- },
- {
- .name = "K",
- .code = KEY_K,
- .rect = { 9*W, 3*H, W, H }
- },
- {
- .name = "L",
- .code = KEY_L,
- .rect = { 10*W, 3*H, W, H }
- },
- {
- .name = ";",
- .altn = ":",
- .code = KEY_SEMICOLON,
- .rect = { 11*W, 3*H, W, H }
- },
- {
- .name = "'",
- .altn = "\"",
- .code = KEY_APOSTROPHE,
- .rect = { 12*W, 3*H, W, H }
- },
- {
- .name = "Enter",
- .code = KEY_ENTER,
- .rect = { 13*W, 3*H, W*2, H }
- },
-
- // row 5
- {
- .name = "Shift",
- .code = KEY_LEFTSHIFT,
- .rect = { 0*W, 4*H, W*2 + W_2, H }
- },
- {
- .name = "Z",
- .code = KEY_Z,
- .rect = { 2*W + W_2, 4*H, W, H }
- },
- {
- .name = "X",
- .code = KEY_X,
- .rect = { 3*W + W_2, 4*H, W, H }
- },
- {
- .name = "C",
- .code = KEY_C,
- .rect = { 4*W + W_2, 4*H, W, H }
- },
- {
- .name = "V",
- .code = KEY_V,
- .rect = { 5*W + W_2, 4*H, W, H }
- },
- {
- .name = "B",
- .code = KEY_B,
- .rect = { 6*W + W_2, 4*H, W, H }
- },
- {
- .name = "N",
- .code = KEY_N,
- .rect = { 7*W + W_2, 4*H, W, H }
- },
- {
- .name = "M",
- .code = KEY_M,
- .rect = { 8*W + W_2, 4*H, W, H }
- },
- {
- .name = ",",
- .altn = "<",
- .code = KEY_COMMA,
- .rect = { 9*W + W_2, 4*H, W, H }
- },
- {
- .name = ".",
- .altn = ">",
- .code = KEY_DOT,
- .rect = { 10*W + W_2, 4*H, W, H }
- },
- {
- .name = "/",
- .altn = "?",
- .code = KEY_SLASH,
- .rect = { 11*W + W_2, 4*H, W, H }
- },
- {
- .name = "Shift",
- .code = KEY_RIGHTSHIFT,
- .rect = { 12*W + W_2, 4*H, 3*W - W_2, H }
- },
-
- // row 6
- {
- .name = "Fn",
- .code = KEY_FN,
- .rect = { 0*W, 5*H, W, H }
- },
- {
- .name = "Ctrl",
- .code = KEY_LEFTCTRL,
- .rect = { 1*W, 5*H, W, H }
- },
- {
- .name = "Mta",
- .code = KEY_LEFTMETA,
- .rect = { 2*W, 5*H, W, H }
- },
- {
- .name = "Alt",
- .code = KEY_LEFTALT,
- .rect = { 3*W, 5*H, W, H }
- },
- {
- .name = "Space",
- .code = KEY_SPACE,
- .rect = { 4*W, 5*H, 5*W, H }
- },
- {
- .name = "Alt",
- .code = KEY_RIGHTALT,
- .rect = { 9*W, 5*H, W, H }
- },
- {
- .name = "Mta",
- .code = KEY_RIGHTMETA,
- .rect = { 10*W, 5*H, W, H }
- },
- {
- .name = "Ctrl",
- .code = KEY_RIGHTCTRL,
- .rect = { 11*W, 5*H, W, H }
- },
- {
- .name = "Hom",
- .code = KEY_HOME,
- .rect = { 12*W, 5*H, W, H }
- },
- {
- .name = "End",
- .code = KEY_END,
- .rect = { 13*W, 5*H, W, H }
- },
- {
- .name = "Ins",
- .code = KEY_INSERT,
- .rect = { 14*W, 5*H, W, H }
- },
-
- { // sentinel
- .name = NULL
- }
-};
-
static inline void
_render_c_keyboard(d2tk_base_t *base, const d2tk_rect_t *rect)
{
- for(const keybtn_t *keybtn = keybtns; keybtn->name; keybtn++)
- {
- const d2tk_rect_t bnd = {
- .x = rect->x + keybtn->rect[0]*rect->w,
- .y = rect->y + keybtn->rect[1]*rect->h,
- .w = keybtn->rect[2]*rect->w,
- .h = keybtn->rect[3]*rect->h
- };
-
- const char *lbl = d2tk_base_get_modmask(base, D2TK_MODMASK_SHIFT, false)
- && keybtn->altn
- ? keybtn->altn
- : keybtn->name;
-
- const d2tk_state_t state = d2tk_base_button_label(base,
- D2TK_ID_IDX(keybtn-keybtns), -1, lbl, D2TK_ALIGN_CENTERED, &bnd);
-
- if(d2tk_state_is_down(state))
- {
- _fake_key_down(keybtn->code);
- }
- else if(d2tk_state_is_up(state))
- {
- _fake_key_up(keybtn->code);
- }
- }
+ d2tk_base_vkb(base, D2TK_ID, rect);
}
#endif
D2TK_API int
d2tk_example_init(void)
-{
-#if D2TK_EVDEV
- fake.dev = libevdev_new();
- if(!fake.dev)
- {
- fprintf(stderr, "Error: libevdev_new\n");
- return EXIT_FAILURE;;
- }
- libevdev_set_name(fake.dev, "Fake keyboard");
- libevdev_enable_event_type(fake.dev, EV_SYN);
- libevdev_enable_event_code(fake.dev, EV_SYN, SYN_REPORT, NULL);
- libevdev_enable_event_type(fake.dev, EV_KEY);
- for(const keybtn_t *keybtn = keybtns; keybtn->name; keybtn++)
- {
- libevdev_enable_event_code(fake.dev, EV_KEY, keybtn->code, NULL);
- }
-
- fake.uidev = NULL;
- libevdev_uinput_create_from_device(fake.dev, LIBEVDEV_UINPUT_OPEN_MANAGED,
- &fake.uidev);
- if(!fake.uidev)
- {
- fprintf(stderr, "Warning: libevdev_uinput_create_from_device\n");
- return EXIT_FAILURE;
- }
-#endif
-
+{
return EXIT_SUCCESS;
}
D2TK_API void
d2tk_example_deinit(void)
{
-#if D2TK_EVDEV
- if(fake.uidev)
- {
- libevdev_uinput_destroy(fake.uidev);
- }
- if(fake.dev)
- {
- libevdev_free(fake.dev);
- }
-#endif
}
D2TK_API void
@@ -1743,6 +1178,10 @@ d2tk_example_run(d2tk_base_t *base, d2tk_coord_t w, d2tk_coord_t h)
{
_render_c_mix(base, vrect);
} break;
+ case BAR_SPINNER:
+ {
+ _render_c_spinner(base, vrect);
+ } break;
case BAR_SEQ:
{
_render_c_seq(base, vrect);