aboutsummaryrefslogtreecommitdiff
path: root/subprojects/d2tk/example
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/d2tk/example')
-rw-r--r--subprojects/d2tk/example/custom_cairo.c25
-rw-r--r--subprojects/d2tk/example/custom_nanovg.c28
-rw-r--r--subprojects/d2tk/example/d2tk_fbdev.c2
-rw-r--r--subprojects/d2tk/example/d2tk_glfw.c118
-rw-r--r--subprojects/d2tk/example/d2tk_pugl.c2
-rw-r--r--subprojects/d2tk/example/example.c130
-rw-r--r--subprojects/d2tk/example/example.h4
7 files changed, 303 insertions, 6 deletions
diff --git a/subprojects/d2tk/example/custom_cairo.c b/subprojects/d2tk/example/custom_cairo.c
new file mode 100644
index 0000000..56904ed
--- /dev/null
+++ b/subprojects/d2tk/example/custom_cairo.c
@@ -0,0 +1,25 @@
+#include <stdint.h>
+
+#include <d2tk/core.h>
+
+#include <cairo.h>
+
+static void
+_draw_custom(void *_ctx, const d2tk_rect_t *rect, const void *data)
+{
+ cairo_t *ctx = _ctx;
+ (void)data;
+
+ d2tk_rect_t bnd = *rect;
+ bnd.x += bnd.w/4;
+ bnd.y += bnd.h/4;
+ bnd.w /= 2;
+ bnd.h /= 2;
+
+ cairo_new_sub_path(ctx);
+ cairo_rectangle(ctx, bnd.x, bnd.y, bnd.w, bnd.h);
+ cairo_set_source_rgba(ctx, 1.f, 1.f, 1.f, 0.5f);
+ cairo_fill(ctx);
+}
+
+d2tk_core_custom_t draw_custom = _draw_custom;
diff --git a/subprojects/d2tk/example/custom_nanovg.c b/subprojects/d2tk/example/custom_nanovg.c
new file mode 100644
index 0000000..7bd2db3
--- /dev/null
+++ b/subprojects/d2tk/example/custom_nanovg.c
@@ -0,0 +1,28 @@
+#include <stdint.h>
+#include <math.h>
+
+#include <d2tk/core.h>
+
+#include <nanovg.h>
+
+static void
+_draw_custom(void *_ctx, const d2tk_rect_t *rect, const void *data)
+{
+ NVGcontext *ctx = _ctx;
+ (void)data;
+
+ d2tk_rect_t bnd = *rect;
+ bnd.x += bnd.w/4;
+ bnd.y += bnd.h/4;
+ bnd.w /= 2;
+ bnd.h /= 2;
+
+ const NVGcolor col = nvgRGBA(0xff, 0xff, 0xff, 0x7f);
+
+ nvgBeginPath(ctx);
+ nvgRect(ctx, bnd.x, bnd.y, bnd.w, bnd.h);
+ nvgFillColor(ctx, col);
+ nvgFill(ctx);
+}
+
+d2tk_core_custom_t draw_custom = _draw_custom;
diff --git a/subprojects/d2tk/example/d2tk_fbdev.c b/subprojects/d2tk/example/d2tk_fbdev.c
index f44e47d..47baed8 100644
--- a/subprojects/d2tk/example/d2tk_fbdev.c
+++ b/subprojects/d2tk/example/d2tk_fbdev.c
@@ -51,7 +51,7 @@ _expose(void *data, d2tk_coord_t w, d2tk_coord_t h)
d2tk_frontend_t *fbdev = app->fbdev;
d2tk_base_t *base = d2tk_frontend_get_base(fbdev);
- d2tk_example_run(base, w, h);
+ d2tk_example_run(fbdev, base, w, h);
if(app->show_cursor)
{
diff --git a/subprojects/d2tk/example/d2tk_glfw.c b/subprojects/d2tk/example/d2tk_glfw.c
new file mode 100644
index 0000000..31ce905
--- /dev/null
+++ b/subprojects/d2tk/example/d2tk_glfw.c
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2018-2019 Hanspeter Portner (dev@open-music-kontrollers.ch)
+ *
+ * This is free software: you can redistribute it and/or modify
+ * it under the terms of the Artistic License 2.0 as published by
+ * The Perl Foundation.
+ *
+ * This source is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Artistic License 2.0 for more details.
+ *
+ * You should have received a copy of the Artistic License 2.0
+ * along the source as a COPYING file. If not, obtain it from
+ * http://www.perlfoundation.org/artistic_license_2_0.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <string.h>
+#include <signal.h>
+
+#include <d2tk/frontend_glfw.h>
+#include "example/example.h"
+
+typedef struct _app_t app_t;
+
+struct _app_t {
+ d2tk_frontend_t *dglfw;
+};
+
+static sig_atomic_t done = false;
+
+static void
+_sig(int signum __attribute__((unused)))
+{
+ done = true;
+}
+
+static inline int
+_expose(void *data, d2tk_coord_t w, d2tk_coord_t h)
+{
+ app_t *app = data;
+ d2tk_frontend_t *dglfw = app->dglfw;
+ d2tk_base_t *base = d2tk_frontend_get_base(dglfw);
+
+ d2tk_example_run(dglfw, base, w, h);
+
+ return EXIT_SUCCESS;
+}
+
+int
+main(int argc, char **argv)
+{
+ static app_t app;
+
+ const float scale = d2tk_frontend_get_scale();
+ d2tk_coord_t w = scale * 1280;
+ d2tk_coord_t h = scale * 720;
+
+ int c;
+ while( (c = getopt(argc, argv, "w:h:")) != -1)
+ {
+ switch(c)
+ {
+ case 'w':
+ {
+ w = atoi(optarg);
+ } break;
+ case 'h':
+ {
+ h = atoi(optarg);
+ } break;
+
+ default:
+ {
+ fprintf(stderr, "Usage: %s\n"
+ " -w width\n"
+ " -h height\n\n",
+ argv[0]);
+ } return EXIT_FAILURE;
+ }
+ }
+
+ const d2tk_glfw_config_t config = {
+ .w = w,
+ .h = h,
+ .bundle_path = "./",
+ .expose = _expose,
+ .data = &app
+ };
+
+ signal(SIGINT, _sig);
+ signal(SIGTERM, _sig);
+#if !defined(_WIN32)
+ signal(SIGQUIT, _sig);
+ signal(SIGKILL, _sig);
+#endif
+
+ app.dglfw = d2tk_glfw_new(&config);
+ if(app.dglfw)
+ {
+ d2tk_example_init();
+
+ d2tk_frontend_run(app.dglfw, &done);
+
+ d2tk_frontend_free(app.dglfw);
+
+ d2tk_example_deinit();
+
+ return EXIT_SUCCESS;
+ }
+
+ return EXIT_FAILURE;
+}
diff --git a/subprojects/d2tk/example/d2tk_pugl.c b/subprojects/d2tk/example/d2tk_pugl.c
index 70ab967..4132547 100644
--- a/subprojects/d2tk/example/d2tk_pugl.c
+++ b/subprojects/d2tk/example/d2tk_pugl.c
@@ -45,7 +45,7 @@ _expose(void *data, d2tk_coord_t w, d2tk_coord_t h)
d2tk_frontend_t *dpugl = app->dpugl;
d2tk_base_t *base = d2tk_frontend_get_base(dpugl);
- d2tk_example_run(base, w, h);
+ d2tk_example_run(dpugl, base, w, h);
return EXIT_SUCCESS;
}
diff --git a/subprojects/d2tk/example/example.c b/subprojects/d2tk/example/example.c
index a029469..4b6a0c4 100644
--- a/subprojects/d2tk/example/example.c
+++ b/subprojects/d2tk/example/example.c
@@ -23,9 +23,10 @@
#include <dirent.h>
#include <string.h>
-#include <d2tk/frontend_pugl.h>
#include "example/example.h"
+#include <d2tk/util.h>
+
typedef union _val_t val_t;
union _val_t {
@@ -50,9 +51,14 @@ typedef enum _bar_t {
BAR_METER,
BAR_FRAME,
BAR_UTF8,
+ BAR_CUSTOM,
+ BAR_COPYPASTE,
#if D2TK_PTY
BAR_PTY,
#endif
+#if D2TK_SPAWN
+ BAR_SPAWN,
+#endif
#if !defined(_WIN32) && !defined(__APPLE__)
BAR_BROWSER,
#endif
@@ -76,9 +82,14 @@ static const char *bar_lbl [BAR_MAX] = {
[BAR_FLOWMATRIX] = "Flowmatrix",
[BAR_METER] = "Meter",
[BAR_FRAME] = "Frame",
+ [BAR_CUSTOM] = "Custom",
+ [BAR_COPYPASTE] = "Copy&Paste",
[BAR_UTF8] = "UTF-8",
#if D2TK_PTY
- [BAR_PTY] = "PTY",
+ [BAR_PTY] = "PTY",
+#endif
+#if D2TK_SPAWN
+ [BAR_SPAWN] = "SPAWN",
#endif
#if !defined(_WIN32) && !defined(__APPLE__)
[BAR_BROWSER] = "Browser",
@@ -919,6 +930,74 @@ _render_c_utf8(d2tk_base_t *base, const d2tk_rect_t *rect)
#undef N
}
+extern d2tk_core_custom_t draw_custom;
+
+static inline void
+_render_c_custom(d2tk_base_t *base, const d2tk_rect_t *rect)
+{
+ static uint32_t dummy [1] = { 0 };
+
+ d2tk_base_custom(base, sizeof(dummy), dummy, rect, draw_custom);
+}
+
+static inline void
+_render_c_copypaste_set(d2tk_frontend_t *frontend, d2tk_base_t *base,
+ const d2tk_rect_t *rect)
+{
+ if(d2tk_base_button_is_changed(base, D2TK_ID, rect))
+ {
+ static unsigned int count = 0;
+ static const char *type = "text/plain";
+ char buf [32];
+
+ const size_t buf_len = snprintf(buf, sizeof(buf), "d2tk #%u", count++);
+
+ d2tk_frontend_set_clipboard(frontend, type, buf, buf_len);
+ }
+}
+
+static inline void
+_render_c_copypaste_get(d2tk_frontend_t *frontend, d2tk_base_t *base,
+ const d2tk_rect_t *rect)
+{
+ if(d2tk_base_button_is_changed(base, D2TK_ID, rect))
+ {
+ const char *type = NULL;
+ size_t lbl_len = 0;
+ const char *lbl = d2tk_frontend_get_clipboard(frontend, &type, &lbl_len);
+
+ if(lbl && lbl_len && type)
+ {
+ fprintf(stderr, "clip: (%zu) %s (%s)\n", lbl_len, lbl, type);
+ }
+ }
+}
+
+static inline void
+_render_c_copypaste(d2tk_frontend_t *frontend, d2tk_base_t *base,
+ const d2tk_rect_t *rect)
+{
+ static d2tk_coord_t hfrac [2] = { 1, 1 };
+
+ D2TK_BASE_LAYOUT(rect, 2, hfrac, D2TK_FLAG_LAYOUT_X_REL, hlay)
+ {
+ const d2tk_rect_t *hrect = d2tk_layout_get_rect(hlay);
+ const d2tk_coord_t x = d2tk_layout_get_index(hlay);
+
+ switch(x)
+ {
+ case 0:
+ _render_c_copypaste_set(frontend, base, hrect);
+ {
+ } break;
+ case 1:
+ {
+ _render_c_copypaste_get(frontend, base, hrect);
+ } break;
+ }
+ }
+}
+
#if D2TK_PTY
static inline void
_render_c_pty(d2tk_base_t *base, const d2tk_rect_t *rect)
@@ -981,6 +1060,36 @@ _render_c_pty(d2tk_base_t *base, const d2tk_rect_t *rect)
}
#endif
+#if D2TK_SPAWN
+static inline void
+_render_c_spawn(d2tk_base_t *base, const d2tk_rect_t *rect)
+{
+ static char lbl [] = "spawn";
+ static int kid = -1;
+
+ if(d2tk_base_button_label_is_changed(
+ base, D2TK_ID, sizeof(lbl), lbl, D2TK_ALIGN_CENTERED, rect))
+ {
+ char *argv [] = {
+ "xdg-open",
+ "https://git.open-music-kontrollers.ch/lad/d2tk/about",
+ NULL
+ };
+ kid = d2tk_util_spawn(argv);
+
+ if(kid <= 0)
+ {
+ fprintf(stderr, "failed to spawn kid\n");
+ }
+ }
+
+ if(d2tk_util_wait(&kid))
+ {
+ fprintf(stderr, "kid still running\n");
+ }
+}
+#endif
+
#if !defined(_WIN32) && !defined(__APPLE__)
static int
strcasenumcmp(const char *s1, const char *s2)
@@ -1249,7 +1358,8 @@ d2tk_example_deinit(void)
}
D2TK_API void
-d2tk_example_run(d2tk_base_t *base, d2tk_coord_t w, d2tk_coord_t h)
+d2tk_example_run(d2tk_frontend_t *frontend, d2tk_base_t *base,
+ d2tk_coord_t w, d2tk_coord_t h)
{
static d2tk_coord_t vfrac [2] = { 1, 19 };
@@ -1330,12 +1440,26 @@ d2tk_example_run(d2tk_base_t *base, d2tk_coord_t w, d2tk_coord_t h)
{
_render_c_utf8(base, vrect);
} break;
+ case BAR_CUSTOM:
+ {
+ _render_c_custom(base, vrect);
+ } break;
+ case BAR_COPYPASTE:
+ {
+ _render_c_copypaste(frontend, base, vrect);
+ } break;
#if D2TK_PTY
case BAR_PTY:
{
_render_c_pty(base, vrect);
} break;
#endif
+#if D2TK_SPAWN
+ case BAR_SPAWN:
+ {
+ _render_c_spawn(base, vrect);
+ } break;
+#endif
#if !defined(_WIN32) && !defined(__APPLE__)
case BAR_BROWSER:
{
diff --git a/subprojects/d2tk/example/example.h b/subprojects/d2tk/example/example.h
index 84f430c..bbb013b 100644
--- a/subprojects/d2tk/example/example.h
+++ b/subprojects/d2tk/example/example.h
@@ -19,6 +19,7 @@
#define _D2TK_EXAMPLE_H
#include <d2tk/base.h>
+#include <d2tk/frontend.h>
#ifdef __cplusplus
extern "C" {
@@ -31,7 +32,8 @@ D2TK_API void
d2tk_example_deinit();
D2TK_API void
-d2tk_example_run(d2tk_base_t *base, d2tk_coord_t w, d2tk_coord_t h);
+d2tk_example_run(d2tk_frontend_t *frontend, d2tk_base_t *base,
+ d2tk_coord_t w, d2tk_coord_t h);
#ifdef __cplusplus
}