From 95771fddd94e1e0d4fc6d99db8e0a19afe720e74 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Sun, 2 Nov 2025 20:08:42 +0000 Subject: [PATCH] probably better git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@571 b9cfdab3-6d41-4d17-bbe4-086880011989 --- examples/basic/color_picker.c | 17 +++++++++-- include/Mw/BaseTypes.h | 7 +++++ include/Mw/ColorPicker.h | 18 +++++------ include/Mw/LowLevel.h | 2 +- include/Mw/StringDefs.h | 1 + src/backend/x11.c | 40 +++++-------------------- src/backend/x11.h | 15 ---------- src/color_picker/color_picker.c | 53 ++++++++++++++++----------------- src/color_picker/color_picker.h | 1 - src/math/default.c | 2 ++ src/math/mmx.c | 2 +- 11 files changed, 67 insertions(+), 91 deletions(-) diff --git a/examples/basic/color_picker.c b/examples/basic/color_picker.c index 5390ca5..0e8b41d 100644 --- a/examples/basic/color_picker.c +++ b/examples/basic/color_picker.c @@ -6,15 +6,26 @@ MwWidget cpicker; MwWidget window; MwWidget button; -void color_callback(MwRGB rgb) { +void color_callback(MwWidget handle, void* user_data, void* call_data) { char hexColor[8]; + MwRGB* rgb = call_data; - sprintf(hexColor, "#%02X%02X%02X", rgb.r, rgb.g, rgb.b); + (void)handle; + (void)user_data; + + sprintf(hexColor, "#%02X%02X%02X", rgb->red, rgb->green, rgb->blue); MwSetText(window, MwNbackground, hexColor); } void color_picker(MwWidget handle, void* user_data, void* call_data) { - MwWidget cpicker = MwColorPicker(window, "cpicker", color_callback); + MwWidget cpicker = MwColorPicker(window, "cpicker"); + + (void)handle; + (void)user_data; + (void)call_data; + + MwAddUserHandler(cpicker, MwNcolorChosenHandler, color_callback, NULL); + MwSetText(cpicker, MwNbackground, MwDefaultBackground); } diff --git a/include/Mw/BaseTypes.h b/include/Mw/BaseTypes.h index ecfd1d4..2917a63 100644 --- a/include/Mw/BaseTypes.h +++ b/include/Mw/BaseTypes.h @@ -11,6 +11,7 @@ typedef struct _MwRect MwRect; typedef struct _MwSizeHints MwSizeHints; typedef struct _MwFont MwFont; typedef struct _MwCursor MwCursor; +typedef struct _MwRGB MwRGB; typedef unsigned char MwBool; #define MwTRUE ((MwBool)1) @@ -104,4 +105,10 @@ struct _MwFont { unsigned char data[16]; }; +struct _MwRGB { + MwU32 red; + MwU32 green; + MwU32 blue; +}; + #endif diff --git a/include/Mw/ColorPicker.h b/include/Mw/ColorPicker.h index 51407a8..74214a0 100644 --- a/include/Mw/ColorPicker.h +++ b/include/Mw/ColorPicker.h @@ -14,17 +14,13 @@ extern "C" { #endif -typedef struct _MwRGB MwRGB; - -struct _MwRGB { - MwU32 r; - MwU32 g; - MwU32 b; -}; - -typedef void (*MwColorPickerCallback)(MwRGB rgb); - -MWDECL MwWidget MwColorPicker(MwWidget handle, const char* title, MwColorPickerCallback cb); +/*! + * @brief Creates a color picker + * @param handle Widget + * @param title Title text + * @return Widget + */ +MWDECL MwWidget MwColorPicker(MwWidget handle, const char* title); #ifdef __cplusplus } diff --git a/include/Mw/LowLevel.h b/include/Mw/LowLevel.h index 1775325..49ce7cb 100644 --- a/include/Mw/LowLevel.h +++ b/include/Mw/LowLevel.h @@ -92,7 +92,7 @@ MWDECL void MwLLPolygon(MwLL handle, MwPoint* points, int points_count, MwLLColo MWDECL void MwLLLine(MwLL handle, MwPoint* points, MwLLColor color); MWDECL MwLLColor MwLLAllocColor(MwLL handle, int r, int g, int b); -MWDECL void MwLLColorUpdate(MwLL handle, int r, int g, int b, MwLLColor c); +MWDECL void MwLLColorUpdate(MwLL handle, MwLLColor c, int r, int g, int b); MWDECL void MwLLFreeColor(MwLLColor color); MWDECL void MwLLGetXYWH(MwLL handle, int* x, int* y, unsigned int* w, unsigned int* h); diff --git a/include/Mw/StringDefs.h b/include/Mw/StringDefs.h index 4f0882a..83a5abc 100644 --- a/include/Mw/StringDefs.h +++ b/include/Mw/StringDefs.h @@ -53,5 +53,6 @@ #define MwNfocusInHandler "CfocusIn" /* NULL */ #define MwNfocusOutHandler "CfocusOut" /* NULL */ #define MwNfileChosenHandler "CfileChosen" /* char* */ +#define MwNcolorChosenHandler "CcolorChosen" /* MwRGB* */ #endif diff --git a/src/backend/x11.c b/src/backend/x11.c index 4d47109..6d26caa 100644 --- a/src/backend/x11.c +++ b/src/backend/x11.c @@ -208,32 +208,11 @@ void MwLLLine(MwLL handle, MwPoint* points, MwLLColor color) { MwLLColor MwLLAllocColor(MwLL handle, int r, int g, int b) { MwLLColor c = malloc(sizeof(*c)); - XColor xc; - - if(handle->red_mask == 0) { - if(r > 255) r = 255; - if(g > 255) g = 255; - if(b > 255) b = 255; - if(r < 0) r = 0; - if(g < 0) g = 0; - if(b < 0) b = 0; - - xc.red = 256 * r; - xc.green = 256 * g; - xc.blue = 256 * b; - XAllocColor(handle->display, handle->colormap, &xc); - - c->pixel = xc.pixel; - } else { - c->pixel = generate_color(handle, r, g, b); - } - c->red = r; - c->green = g; - c->blue = b; + MwLLColorUpdate(handle, c, r, g, b); return c; } -void MwLLColorUpdate(MwLL handle, int r, int g, int b, MwLLColor c) { +void MwLLColorUpdate(MwLL handle, MwLLColor c, int r, int g, int b) { XColor xc; if(handle->red_mask == 0) { @@ -455,7 +434,6 @@ MwLLPixmap MwLLCreatePixmap(MwLL handle, unsigned char* data, int width, int hei MwLLPixmap r = malloc(sizeof(*r)); char* di = malloc(4 * width * height); char* dm = malloc(4 * width * height); - int y, x; int evbase, erbase; XWindowAttributes attr; @@ -481,22 +459,20 @@ MwLLPixmap MwLLCreatePixmap(MwLL handle, unsigned char* data, int width, int hei void MwLLPixmapUpdate(MwLL handle, MwLLPixmap r) { int y, x; - MwLLColor c = NULL; for(y = 0; y < r->height; y++) { for(x = 0; x < r->width; x++) { unsigned char* px = &r->data_buf[(y * r->width + x) * 4]; - if(c == NULL) { - c = MwLLAllocColor(handle, px[0], px[1], px[2]); - } else { - MwLLColorUpdate(handle, px[0], px[1], px[2], c); - } - unsigned long p = c->pixel; + MwLLColor c = NULL; + unsigned long p; + + c = MwLLAllocColor(handle, px[0], px[1], px[2]); + p = c->pixel; + MwLLFreeColor(c); XPutPixel(r->image, x, y, p); *(unsigned long*)(&r->data[(y * r->width + x) * sizeof(unsigned long)]) = (px[3] << 24) | p; } } - MwLLFreeColor(c); for(y = 0; y < r->height; y++) { for(x = 0; x < r->width; x++) { diff --git a/src/backend/x11.h b/src/backend/x11.h index e7bd872..5c07641 100644 --- a/src/backend/x11.h +++ b/src/backend/x11.h @@ -65,19 +65,4 @@ struct _MwLLPixmap { XImage* mask; }; -#ifdef HAS_FREETYPE -void print_ft_error(void* ftLib, FT_Error err); -#define MwFreeTypeFontSize 181388 -extern FT_Byte MwFreeTypeFontData[MwFreeTypeFontSize]; - -#define FT_WITH_FUNC(handle, func, block) \ - { \ - _##func = dlsym(handle->ftLib, #func); \ - if(_##func != NULL) { \ - block \ - } else \ - printf("[WARNING] Unable to resolve function " #func ".\n"); \ - } -#endif - #endif diff --git a/src/color_picker/color_picker.c b/src/color_picker/color_picker.c index 6ba092b..2c84437 100644 --- a/src/color_picker/color_picker.c +++ b/src/color_picker/color_picker.c @@ -5,7 +5,9 @@ #include static void hsv2rgb(MwU32 h, MwU32 s, MwU32 v, MwU32* r, MwU32* g, MwU32* b) { - uint8_t sextant = h >> 8; + MwU8 sextant = h >> 8; + MwU16 ww; + MwU32 h_fraction, d; #define HSV_SWAPPTR(a, b) \ do { \ @@ -39,14 +41,12 @@ static void hsv2rgb(MwU32 h, MwU32 s, MwU32 v, MwU32* r, MwU32* g, MwU32* b) { * Bottom level: v * (1.0 - s) * --> (v * (255 - s) + error_corr + 1) / 256 */ - uint16_t ww; // Intermediate result ww = v * (255 - (s)); // We don't use ~s to prevent size-promotion side effects ww += 1; // Error correction ww += ww >> 8; // Error correction *b = ww >> 8; - MwU32 h_fraction = h & 0xff; // 0...255 - MwU32 d; // Intermediate result + h_fraction = h & 0xff; // 0...255 if(!(sextant & 1)) { // *r = ...slope_up...; @@ -62,17 +62,17 @@ static void hsv2rgb(MwU32 h, MwU32 s, MwU32 v, MwU32* r, MwU32* g, MwU32* b) { static void color_picker_image_update(color_picker* picker) { int y, x; - double h; for(y = 0; y < PICKER_SIZE; y++) { for(x = 0; x < PICKER_SIZE; x++) { int i = ((y * PICKER_SIZE) + x) * 4; int _x = x - (PICKER_SIZE / 2); int _y = y - (PICKER_SIZE / 2); + double dist; if(picker->dist_table[y][x] == 0) { picker->dist_table[y][x] = sqrt(_x * _x + _y * _y); } - double dist = picker->dist_table[y][x]; + dist = picker->dist_table[y][x]; if(dist >= 180.) { picker->color_picker_image_data[i] = 0; @@ -101,11 +101,11 @@ static void color_picker_image_update(color_picker* picker) { hsv_v.v = HSV_VAL_MAX - (picker->value * HSV_VAL_MAX); MwRGB color; - hsv2rgb(hsv_v.h, hsv_v.s, hsv_v.v, &color.r, &color.g, &color.b); + hsv2rgb(hsv_v.h, hsv_v.s, hsv_v.v, &color.red, &color.green, &color.blue); - picker->color_picker_image_data[i] = color.r; - picker->color_picker_image_data[i + 1] = color.g; - picker->color_picker_image_data[i + 2] = color.b; + picker->color_picker_image_data[i] = color.red; + picker->color_picker_image_data[i + 1] = color.green; + picker->color_picker_image_data[i + 2] = color.blue; picker->color_picker_image_data[i + 3] = 255; } @@ -137,15 +137,15 @@ static void color_picker_click(MwWidget handle, void* user, void* call) { i = ((mouse->point.y * PICKER_SIZE) + mouse->point.x) * 4; - picker->chosen_color.r = picker->color_picker_image_data[i]; - picker->chosen_color.g = picker->color_picker_image_data[i + 1]; - picker->chosen_color.b = picker->color_picker_image_data[i + 2]; + picker->chosen_color.red = picker->color_picker_image_data[i]; + picker->chosen_color.green = picker->color_picker_image_data[i + 1]; + picker->chosen_color.blue = picker->color_picker_image_data[i + 2]; - sprintf(hexColor, "#%02X%02X%02X", picker->chosen_color.r, picker->chosen_color.g, picker->chosen_color.b); + sprintf(hexColor, "#%02X%02X%02X", picker->chosen_color.red, picker->chosen_color.green, picker->chosen_color.blue); - fr = picker->chosen_color.r > 128 ? 0 : 255; - fg = picker->chosen_color.g > 128 ? 0 : 255; - fb = picker->chosen_color.b > 128 ? 0 : 255; + fr = picker->chosen_color.red > 128 ? 0 : 255; + fg = picker->chosen_color.green > 128 ? 0 : 255; + fb = picker->chosen_color.blue > 128 ? 0 : 255; sprintf(fgColor, "#%02X%02X%02X", fr, fg, fb); MwSetText(picker->color_display, MwNbackground, hexColor); @@ -172,16 +172,14 @@ static void color_picker_on_change_value(MwWidget handle, void* user, static void color_picker_destroy(color_picker* picker) { free(picker->color_picker_image_data); MwLLDestroyPixmap(picker->color_picker_pixmap); - - MwDestroyWidget(picker->color_display_text); - MwDestroyWidget(picker->color_display); - MwDestroyWidget(picker->value_slider); - MwDestroyWidget(picker->color_picker_img); } static void color_picker_close(MwWidget handle, void* user, void* call) { color_picker* picker = (color_picker*)user; + + (void)call; + color_picker_destroy(picker); MwDestroyWidget(handle); } @@ -190,7 +188,9 @@ static void color_picker_finish(MwWidget handle, void* user, void* call) { color_picker* picker = (color_picker*)user; - picker->cb(picker->chosen_color); + (void)call; + + MwDispatchUserHandler(picker->parent, MwNcolorChosenHandler, &picker->chosen_color); color_picker_destroy(picker); MwDestroyWidget(handle->parent); @@ -264,8 +264,9 @@ color_picker* color_picker_setup(MwWidget parent, int w, int h) { memset(picker->hue_table, 0, sizeof(picker->hue_table)); return picker; -}; -MwWidget MwColorPicker(MwWidget handle, const char* title, MwColorPickerCallback cb) { +} + +MwWidget MwColorPicker(MwWidget handle, const char* title) { MwPoint p; color_picker* wheel; MwWidget window; @@ -282,7 +283,5 @@ MwWidget MwColorPicker(MwWidget handle, const char* title, MwColorPickerCallback MwLLDetach(window->lowlevel, &p); MwLLMakePopup(window->lowlevel, handle->lowlevel); - wheel->cb = cb; - return window; } diff --git a/src/color_picker/color_picker.h b/src/color_picker/color_picker.h index cb35f21..0de7cd4 100644 --- a/src/color_picker/color_picker.h +++ b/src/color_picker/color_picker.h @@ -48,7 +48,6 @@ struct _color_picker { MwPoint point; double dist_table[PICKER_SIZE][PICKER_SIZE]; MwHSV hue_table[PICKER_SIZE][PICKER_SIZE]; - MwColorPickerCallback cb; MwRGB chosen_color; }; diff --git a/src/math/default.c b/src/math/default.c index cf8fa73..13ca8b0 100644 --- a/src/math/default.c +++ b/src/math/default.c @@ -68,6 +68,7 @@ MAKE_DEFAULT_TABLE(i16, MwI16); MAKE_DEFAULT_TABLE(i32, MwI32); MAKE_DEFAULT_TABLE(i64, MwI64); +#if 0 static MwLLMathVTable* defaultMultiTable[MwLLVecType_Max] = { &table_u8, /*MwLLVecTypeU8*/ &table_u16, /*MwLLVecTypeU16*/ @@ -78,6 +79,7 @@ static MwLLMathVTable* defaultMultiTable[MwLLVecType_Max] = { &table_i32, /*MwLLVecTypeI32*/ &table_i64, /*MwLLVecTypeI64*/ }; +#endif void default_apply(MwLLVec* v) { switch(v->ty) { case MwLLVecTypeU8: diff --git a/src/math/mmx.c b/src/math/mmx.c index 31743ce..c5398d0 100644 --- a/src/math/mmx.c +++ b/src/math/mmx.c @@ -8,7 +8,7 @@ #define DO_MMX_INTRINSIC(intrin, _ty, _cty) \ int i = 0; \ for(; i < a->size; i += 8) { \ - __m64 m = intrin(*(__m64*)&a->buffer[i], *(__m64*)&b->buffer[i]); \ + __m64 m = intrin(((__m64*)a->buffer)[i], ((__m64*)b->buffer)[i]); \ memcpy(&((_cty*)out->buffer)[i], &m, sizeof(m)); \ }