From c9a461c52b37156f14944caa085bb794c184e5e3 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Sat, 29 Nov 2025 16:55:43 +0800 Subject: Add sdl-ime demo. --- demos/CMakeLists.txt | 5 -- demos/dev/sdl-ime.c | 38 ++++++++++++++ demos/dev/xcb-cairo.cpp | 97 +++++++++++++++++++++++++++++++++++ demos/dev/xcb.cpp | 23 +++++++++ demos/xcb/CMakeLists.txt | 7 --- demos/xcb/cairo-start/CMakeLists.txt | 4 -- demos/xcb/cairo-start/main.cpp | 98 ------------------------------------ demos/xcb/start/CMakeLists.txt | 2 - demos/xcb/start/main.cpp | 23 --------- 9 files changed, 158 insertions(+), 139 deletions(-) create mode 100644 demos/dev/sdl-ime.c create mode 100644 demos/dev/xcb-cairo.cpp create mode 100644 demos/dev/xcb.cpp delete mode 100644 demos/xcb/CMakeLists.txt delete mode 100644 demos/xcb/cairo-start/CMakeLists.txt delete mode 100644 demos/xcb/cairo-start/main.cpp delete mode 100644 demos/xcb/start/CMakeLists.txt delete mode 100644 demos/xcb/start/main.cpp (limited to 'demos') diff --git a/demos/CMakeLists.txt b/demos/CMakeLists.txt index 516ef7ae..a14eee80 100644 --- a/demos/CMakeLists.txt +++ b/demos/CMakeLists.txt @@ -6,10 +6,5 @@ add_subdirectory(platform) add_subdirectory(main) add_subdirectory(ScrollView) add_subdirectory(InputMethod) - -if(UNIX AND NOT APPLE AND NOT EMSCRIPTEN) - add_subdirectory(xcb) -endif() - add_subdirectory(parse) diff --git a/demos/dev/sdl-ime.c b/demos/dev/sdl-ime.c new file mode 100644 index 00000000..0eccfa00 --- /dev/null +++ b/demos/dev/sdl-ime.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include + +int main() { + SDL_Window* window; + SDL_Rect area = {0, 0, 1, 1}; + int cursor = 0; + + window = SDL_CreateWindow("", 400, 200, 0); + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS); + SDL_SetTextInputArea(window, &area, cursor); + SDL_StartTextInput(window); + + SDL_Event e; + while (SDL_WaitEvent(&e)) { + if (e.type == SDL_EVENT_TEXT_INPUT) { + printf("%s: %s\n", "SDL_EVENT_TEXT_INPUT", e.text.text); + fflush(stdout); + } else if (e.type == SDL_EVENT_TEXT_EDITING) { + printf("%s: %s, start %i, length %i\n", "SDL_EVENT_TEXT_EDITING", + e.edit.text, e.edit.start, e.edit.length); + fflush(stdout); + } else if (e.type == SDL_EVENT_TEXT_EDITING_CANDIDATES) { + printf("%s: num_candidates %i, selected %i\n", + "SDL_EVENT_TEXT_EDITING_CANDIDATES", + e.edit_candidates.num_candidates, + e.edit_candidates.selected_candidate); + fflush(stdout); + } else if (e.type == SDL_EVENT_QUIT) { + break; + } + } + + SDL_Quit(); +} diff --git a/demos/dev/xcb-cairo.cpp b/demos/dev/xcb-cairo.cpp new file mode 100644 index 00000000..97461cd8 --- /dev/null +++ b/demos/dev/xcb-cairo.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include + +int main() { + int screen_num; + xcb_connection_t *connection = xcb_connect(NULL, &screen_num); + const xcb_setup_t *setup = xcb_get_setup(connection); + xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup); + xcb_screen_t *screen = iter.data; + + uint32_t mask = XCB_CW_EVENT_MASK; + uint32_t data[] = {XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY}; + + int width = 500; + int height = 500; + + xcb_window_t window = xcb_generate_id(connection); + xcb_create_window(connection, XCB_COPY_FROM_PARENT, window, screen->root, 0, + 0, width, height, 10, XCB_WINDOW_CLASS_INPUT_OUTPUT, + screen->root_visual, mask, data); + xcb_map_window(connection, window); + xcb_flush(connection); + + xcb_visualtype_t *visual_type; + + for (xcb_depth_iterator_t depth_iter = + xcb_screen_allowed_depths_iterator(screen); + depth_iter.rem; xcb_depth_next(&depth_iter)) { + for (xcb_visualtype_iterator_t visual_iter = + xcb_depth_visuals_iterator(depth_iter.data); + visual_iter.rem; xcb_visualtype_next(&visual_iter)) { + if (screen->root_visual == visual_iter.data->visual_id) { + visual_type = visual_iter.data; + break; + } + } + } + + cairo_surface_t *surface = + cairo_xcb_surface_create(connection, window, visual_type, width, height); + + cairo_t *cairo = cairo_create(surface); + + xcb_generic_event_t *e; + + auto paint = [](cairo_t *cairo, int width, int height) { + cairo_set_source_rgb(cairo, 1, 1, 1); + cairo_paint(cairo); + cairo_set_source_rgb(cairo, 1, 1, 0); + cairo_set_line_width(cairo, 5); + cairo_move_to(cairo, width / 2.0, 0); + cairo_curve_to(cairo, width, 0, width, 0, width, height / 2.0); + cairo_curve_to(cairo, width, height, width, height, width / 2.0, height); + cairo_curve_to(cairo, 0, height, 0, height, 0, height / 2.0); + cairo_curve_to(cairo, 0, 0, 0, 0, width / 2.0, 0); + cairo_stroke(cairo); + }; + + while ((e = xcb_wait_for_event(connection))) { + switch (e->response_type & ~0x80) { + case XCB_EXPOSE: { + paint(cairo, width, height); + break; + } + case XCB_CONFIGURE_NOTIFY: { + xcb_configure_notify_event_t *event = + reinterpret_cast(e); + + width = event->width; + height = event->height; + + cairo_xcb_surface_set_size(surface, width, height); + + paint(cairo, width, height); + + break; + } + default: { + /* Unknown event type, ignore it */ + break; + } + } + /* Free the Generic Event */ + free(e); + } + + pause(); + + cairo_destroy(cairo); + cairo_surface_destroy(surface); + xcb_disconnect(connection); + + return 0; +} diff --git a/demos/dev/xcb.cpp b/demos/dev/xcb.cpp new file mode 100644 index 00000000..c4e3761e --- /dev/null +++ b/demos/dev/xcb.cpp @@ -0,0 +1,23 @@ +#include +#include + +int main() { + int screen_num; + xcb_connection_t* connection = xcb_connect(NULL, &screen_num); + const xcb_setup_t* setup = xcb_get_setup(connection); + xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup); + xcb_screen_t* screen = iter.data; + + xcb_window_t window = xcb_generate_id(connection); + xcb_create_window(connection, XCB_COPY_FROM_PARENT, window, screen->root, 0, + 0, 150, 150, 10, XCB_WINDOW_CLASS_INPUT_OUTPUT, + screen->root_visual, 0, NULL); + xcb_map_window(connection, window); + xcb_flush(connection); + + pause(); + + xcb_disconnect(connection); + + return 0; +} diff --git a/demos/xcb/CMakeLists.txt b/demos/xcb/CMakeLists.txt deleted file mode 100644 index 97292467..00000000 --- a/demos/xcb/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -find_library(LIBRARY_XCB xcb REQUIRED) - -add_library(demo-xcb-base INTERFACE) -target_link_libraries(demo-xcb-base INTERFACE ${LIBRARY_XCB}) - -add_subdirectory(start) -add_subdirectory(cairo-start) diff --git a/demos/xcb/cairo-start/CMakeLists.txt b/demos/xcb/cairo-start/CMakeLists.txt deleted file mode 100644 index d08f2c96..00000000 --- a/demos/xcb/cairo-start/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -find_library(LIBRARY_CAIRO cairo REQUIRED) - -add_executable(demo-xcb-cairo-start main.cpp) -target_link_libraries(demo-xcb-cairo-start PUBLIC demo-xcb-base ${LIBRARY_CAIRO}) diff --git a/demos/xcb/cairo-start/main.cpp b/demos/xcb/cairo-start/main.cpp deleted file mode 100644 index 331f90d7..00000000 --- a/demos/xcb/cairo-start/main.cpp +++ /dev/null @@ -1,98 +0,0 @@ -#include -#include -#include -#include -#include -#include - -int main() { - int screen_num; - xcb_connection_t *connection = xcb_connect(NULL, &screen_num); - const xcb_setup_t *setup = xcb_get_setup(connection); - xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup); - xcb_screen_t *screen = iter.data; - - uint32_t mask = XCB_CW_EVENT_MASK; - uint32_t data[] = {XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY}; - - int width = 500; - int height = 500; - - xcb_window_t window = xcb_generate_id(connection); - xcb_create_window(connection, XCB_COPY_FROM_PARENT, window, screen->root, 0, - 0, width, height, 10, XCB_WINDOW_CLASS_INPUT_OUTPUT, - screen->root_visual, mask, data); - xcb_map_window(connection, window); - xcb_flush(connection); - - xcb_visualtype_t *visual_type; - - for (xcb_depth_iterator_t depth_iter = - xcb_screen_allowed_depths_iterator(screen); - depth_iter.rem; xcb_depth_next(&depth_iter)) { - for (xcb_visualtype_iterator_t visual_iter = - xcb_depth_visuals_iterator(depth_iter.data); - visual_iter.rem; xcb_visualtype_next(&visual_iter)) { - if (screen->root_visual == visual_iter.data->visual_id) { - visual_type = visual_iter.data; - break; - } - } - } - - cairo_surface_t *surface = - cairo_xcb_surface_create(connection, window, visual_type, width, height); - - cairo_t *cairo = cairo_create(surface); - - xcb_generic_event_t *e; - - auto paint = [](cairo_t *cairo, int width, int height) { - cairo_set_source_rgb(cairo, 1, 1, 1); - cairo_paint(cairo); - cairo_set_source_rgb(cairo, 1, 1, 0); - cairo_set_line_width(cairo, 5); - cairo_move_to(cairo, width / 2.0, 0); - cairo_curve_to(cairo, width, 0, width, 0, width, height / 2.0); - cairo_curve_to(cairo, width, height, width, height, width / 2.0, height); - cairo_curve_to(cairo, 0, height, 0, height, 0, height / 2.0); - cairo_curve_to(cairo, 0, 0, 0, 0, width / 2.0, 0); - cairo_stroke(cairo); - }; - - while ((e = xcb_wait_for_event(connection))) { - switch (e->response_type & ~0x80) { - case XCB_EXPOSE: { - paint(cairo, width, height); - break; - } - case XCB_CONFIGURE_NOTIFY: { - xcb_configure_notify_event_t *event = - reinterpret_cast(e); - - width = event->width; - height = event->height; - - cairo_xcb_surface_set_size(surface, width, height); - - paint(cairo, width, height); - - break; - } - default: { - /* Unknown event type, ignore it */ - break; - } - } - /* Free the Generic Event */ - free(e); - } - - pause(); - - cairo_destroy(cairo); - cairo_surface_destroy(surface); - xcb_disconnect(connection); - - return 0; -} diff --git a/demos/xcb/start/CMakeLists.txt b/demos/xcb/start/CMakeLists.txt deleted file mode 100644 index 25e37be9..00000000 --- a/demos/xcb/start/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -add_executable(demo-xcb-start main.cpp) -target_link_libraries(demo-xcb-start PUBLIC demo-xcb-base) diff --git a/demos/xcb/start/main.cpp b/demos/xcb/start/main.cpp deleted file mode 100644 index c4e3761e..00000000 --- a/demos/xcb/start/main.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include - -int main() { - int screen_num; - xcb_connection_t* connection = xcb_connect(NULL, &screen_num); - const xcb_setup_t* setup = xcb_get_setup(connection); - xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup); - xcb_screen_t* screen = iter.data; - - xcb_window_t window = xcb_generate_id(connection); - xcb_create_window(connection, XCB_COPY_FROM_PARENT, window, screen->root, 0, - 0, 150, 150, 10, XCB_WINDOW_CLASS_INPUT_OUTPUT, - screen->root_visual, 0, NULL); - xcb_map_window(connection, window); - xcb_flush(connection); - - pause(); - - xcb_disconnect(connection); - - return 0; -} -- cgit v1.2.3