最初コミット
諏訪子 <suwako@076.moe>、2026-01-19 00:31
コミット 4779cec2c4fff75c928f9b535a01f406f7a10b32
・ファイルを見る
2個のファイル、+388 -0
追加
LICENSE.md
| @@ -0,0 +1,21 @@ | ||
| 1 | +# 076 Free License | |
| 2 | + | |
| 3 | +Copyright (c) 2026 テクニカル諏訪子 | |
| 4 | + | |
| 5 | +Permission is hereby granted to any person obtaining a copy of the software | |
| 6 | +SuwaUI (the "Software") to use, modify, merge, copy, publish, distribute, | |
| 7 | +sublicense, and/or sell copies of the Software, subject to the following conditions: | |
| 8 | + | |
| 9 | + 1. **Origin Attribution**: | |
| 10 | + - You must not misrepresent the origin of the Software; you must not claim | |
| 11 | + you created the original Software. | |
| 12 | + 2. **Notice Preservation**: | |
| 13 | + - This license and the above copyright notice must remain intact in all copies | |
| 14 | + of the source code. | |
| 15 | + | |
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
| 17 | +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
| 18 | +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
| 19 | +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | |
| 20 | +CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE | |
| 21 | +OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
追加
suwaui.h
| @@ -0,0 +1,367 @@ | ||
| 1 | +/************************************************************************************** | |
| 2 | +# 076 Free License | |
| 3 | + | |
| 4 | +Copyright (c) 2026 テクニカル諏訪子 | |
| 5 | + | |
| 6 | +Permission is hereby granted to any person obtaining a copy of the software | |
| 7 | +SuwaUI (the "Software") to use, modify, merge, copy, publish, distribute, | |
| 8 | +sublicense, and/or sell copies of the Software, subject to the following conditions: | |
| 9 | + | |
| 10 | + 1. **Origin Attribution**: | |
| 11 | + - You must not misrepresent the origin of the Software; you must not claim | |
| 12 | + you created the original Software. | |
| 13 | + 2. **Notice Preservation**: | |
| 14 | + - This license and the above copyright notice must remain intact in all copies | |
| 15 | + of the source code. | |
| 16 | + | |
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
| 18 | +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
| 19 | +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
| 20 | +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF | |
| 21 | +CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE | |
| 22 | +OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| 23 | +**************************************************************************************/ | |
| 24 | +#pragma once | |
| 25 | + | |
| 26 | +#include <X11/Xlib.h> | |
| 27 | +#include <X11/Xft/Xft.h> | |
| 28 | +#include <X11/Xatom.h> | |
| 29 | + | |
| 30 | +#include <stdio.h> | |
| 31 | + | |
| 32 | +#define FGCOL 0xfcfcfc | |
| 33 | +#if defined(__OpenBSD__) | |
| 34 | +#define BGCOL 0x232320 | |
| 35 | +#define BTSEL 0xb8b515 | |
| 36 | +#define BTCOL 0xf1ed25 | |
| 37 | +#define BTHVR 0xecea71 | |
| 38 | +#elif defined(__FreeBSD__) | |
| 39 | +#define BGCOL 0x232020 | |
| 40 | +#define BTSEL 0xb61729 | |
| 41 | +#define BTCOL 0xee4030 | |
| 42 | +#define BTHVR 0xf35869 | |
| 43 | +#endif | |
| 44 | + | |
| 45 | +#if defined(__cplusplus) | |
| 46 | +extern "C" { | |
| 47 | +#endif | |
| 48 | + | |
| 49 | +#if defined(SUWAUI_IMPLEMENTS_SUWAWINDOW) | |
| 50 | +#include <string.h> | |
| 51 | + | |
| 52 | +typedef struct { | |
| 53 | + int x, y, width, height; | |
| 54 | + int start_width, start_height; | |
| 55 | + int isrunning; | |
| 56 | + const char *name; | |
| 57 | + const char *version; | |
| 58 | + const char *res_name; | |
| 59 | + const char *class_name; | |
| 60 | + Display *display; | |
| 61 | + Window xwindow; | |
| 62 | + int screen; | |
| 63 | + Drawable target; | |
| 64 | + GC gc; | |
| 65 | + Visual visual; | |
| 66 | + XftColor color; | |
| 67 | + XftFont *font; | |
| 68 | + Colormap colormap; | |
| 69 | + Pixmap backbuf; | |
| 70 | + XEvent event; | |
| 71 | +} SuwaWindow; | |
| 72 | + | |
| 73 | +__attribute__((unused)) static void suwaui_destroy_window(SuwaWindow *w); | |
| 74 | + | |
| 75 | +__attribute__((unused)) static inline float get_scale(SuwaWindow *w) { | |
| 76 | + XWindowAttributes attr; | |
| 77 | + XGetWindowAttributes(w->display, w->xwindow, &attr); | |
| 78 | + float sx = (float)attr.width / w->start_width; | |
| 79 | + float sy = (float)attr.height / w->start_height; | |
| 80 | + return sx < sy ? sx : sy; | |
| 81 | +} | |
| 82 | + | |
| 83 | +__attribute__((unused)) static SuwaWindow suwaui_create_window(int x, int y, int width, int height, | |
| 84 | + const char *software_name, const char *software_version, | |
| 85 | + const char *res_name, const char *class_name, | |
| 86 | + const char *font, const char *fg_color, | |
| 87 | + int is_centered, int cannot_shrink, int cannot_grow) { | |
| 88 | + (void)cannot_grow; | |
| 89 | + | |
| 90 | + SuwaWindow w = {0}; | |
| 91 | + w.width = width; | |
| 92 | + w.height = height; | |
| 93 | + w.start_width = width; | |
| 94 | + w.start_height = height; | |
| 95 | + w.name = software_name; | |
| 96 | + w.version = software_version; | |
| 97 | + w.res_name = res_name; | |
| 98 | + w.class_name = class_name; | |
| 99 | + w.isrunning = 1; | |
| 100 | + | |
| 101 | + XGCValues values; | |
| 102 | + | |
| 103 | + w.display = XOpenDisplay(NULL); | |
| 104 | + if (w.display == NULL) { | |
| 105 | + fprintf(stderr, "screen open fail\n"); | |
| 106 | + return (SuwaWindow){0}; | |
| 107 | + } | |
| 108 | + | |
| 109 | + w.screen = DefaultScreen(w.display); | |
| 110 | + | |
| 111 | + if (is_centered > 0) { | |
| 112 | + int sw = DisplayWidth(w.display, w.screen); | |
| 113 | + int sh = DisplayHeight(w.display, w.screen); | |
| 114 | + w.x = (sw - w.width) / 2; | |
| 115 | + w.y = (sh - w.height) / 2; | |
| 116 | + } else { | |
| 117 | + w.x = x; | |
| 118 | + w.y = y; | |
| 119 | + } | |
| 120 | + | |
| 121 | + w.xwindow = XCreateSimpleWindow(w.display, | |
| 122 | + RootWindow(w.display, w.screen), | |
| 123 | + w.x, w.y, | |
| 124 | + w.width, w.height, | |
| 125 | + 1, BTCOL, BGCOL); | |
| 126 | + if (!w.xwindow) { | |
| 127 | + suwaui_destroy_window(&w); | |
| 128 | + fprintf(stderr, "window create fail\n"); | |
| 129 | + return (SuwaWindow){0}; | |
| 130 | + } | |
| 131 | + | |
| 132 | + if (cannot_shrink) { | |
| 133 | + XSizeHints *sizeHint = XAllocSizeHints(); | |
| 134 | + if (sizeHint) { | |
| 135 | + sizeHint->flags = PMinSize; | |
| 136 | + sizeHint->min_width = width; | |
| 137 | + sizeHint->min_height = height; | |
| 138 | + XSetWMNormalHints(w.display, w.xwindow, sizeHint); | |
| 139 | + XFree(sizeHint); | |
| 140 | + } | |
| 141 | + } | |
| 142 | + | |
| 143 | + w.backbuf = XCreatePixmap(w.display, w.xwindow, | |
| 144 | + w.width, w.height, | |
| 145 | + DefaultDepth(w.display, w.screen)); | |
| 146 | + w.target = w.backbuf; | |
| 147 | + | |
| 148 | + Atom net_wm_window_type = XInternAtom(w.display, "_NET_WM_WINDOW_TYPE", False); | |
| 149 | + Atom dialog = XInternAtom(w.display, "_NET_WM_WINDOW_TYPE_DIALOG", False); | |
| 150 | + | |
| 151 | + XChangeProperty(w.display, w.xwindow, net_wm_window_type, XA_ATOM, 32, | |
| 152 | + PropModeReplace, (unsigned char *)&dialog, 1); | |
| 153 | + | |
| 154 | + XStoreName(w.display, w.xwindow, w.name); | |
| 155 | + Atom net_wm_name = XInternAtom(w.display, "_NET_WM_NAME", False); | |
| 156 | + char displayname[16]; | |
| 157 | + snprintf(displayname, 16, "%s %s", w.name, w.version); | |
| 158 | + XChangeProperty(w.display, w.xwindow, net_wm_name, | |
| 159 | + XInternAtom(w.display, "UTF8_STRING", False), 8, | |
| 160 | + PropModeReplace, (unsigned char *)displayname, strlen(displayname)); | |
| 161 | + | |
| 162 | + XClassHint *classHint = XAllocClassHint(); | |
| 163 | + if (classHint) { | |
| 164 | + classHint->res_name = strdup(w.res_name); | |
| 165 | + classHint->res_class = strdup(w.class_name); | |
| 166 | + XSetClassHint(w.display, w.xwindow, classHint); | |
| 167 | + XFree(classHint); | |
| 168 | + } | |
| 169 | + | |
| 170 | + XSetWindowBackground(w.display, w.xwindow, BGCOL); | |
| 171 | + | |
| 172 | + XSelectInput(w.display, w.xwindow, | |
| 173 | + ExposureMask | |
| 174 | + | ButtonPressMask | |
| 175 | + | ButtonReleaseMask | |
| 176 | + | KeyPressMask | |
| 177 | + // | PointerMotionMask | |
| 178 | + // | ButtonMotionMask | |
| 179 | + // | StructureNotifyMask | |
| 180 | + ); | |
| 181 | + | |
| 182 | + w.gc = XCreateGC(w.display, w.xwindow, 0, &values); | |
| 183 | + if (!w.gc) { | |
| 184 | + suwaui_destroy_window(&w); | |
| 185 | + fprintf(stderr, "gc create fail\n"); | |
| 186 | + return (SuwaWindow){0}; | |
| 187 | + } | |
| 188 | + | |
| 189 | + w.visual = *DefaultVisual(w.display, w.screen); | |
| 190 | + | |
| 191 | + w.colormap = XCreateColormap(w.display, w.xwindow, | |
| 192 | + &w.visual, AllocNone); | |
| 193 | + if (w.colormap == None) { | |
| 194 | + suwaui_destroy_window(&w); | |
| 195 | + fprintf(stderr, "calor map create fail\n"); | |
| 196 | + return (SuwaWindow){0}; | |
| 197 | + } | |
| 198 | + | |
| 199 | + w.font = XftFontOpenName(w.display, w.screen, font); | |
| 200 | + if (!w.font) { | |
| 201 | + suwaui_destroy_window(&w); | |
| 202 | + fprintf(stderr, "font create fail\n"); | |
| 203 | + return (SuwaWindow){0}; | |
| 204 | + } | |
| 205 | + | |
| 206 | + if (!XftColorAllocName(w.display, &w.visual, | |
| 207 | + w.colormap, fg_color, &w.color)) { | |
| 208 | + suwaui_destroy_window(&w); | |
| 209 | + fprintf(stderr, "color estimate fail\n"); | |
| 210 | + return (SuwaWindow){0}; | |
| 211 | + } | |
| 212 | + | |
| 213 | + return w; | |
| 214 | +} | |
| 215 | + | |
| 216 | +__attribute__((unused)) static void suwaui_destroy_window(SuwaWindow *w) { | |
| 217 | + if (w->font) XftFontClose(w->display, w->font); | |
| 218 | + | |
| 219 | + if (w->gc) XFreeGC(w->display, w->gc); | |
| 220 | + if (w->backbuf) { | |
| 221 | + XFreePixmap(w->display, w->backbuf); | |
| 222 | + w->backbuf = None; | |
| 223 | + } | |
| 224 | + if (w->xwindow) XDestroyWindow(w->display, w->xwindow); | |
| 225 | + if (w->display) XCloseDisplay(w->display); | |
| 226 | +} | |
| 227 | +#endif // SUWAUI_IMPLEMENTS_SUWAWINDOW | |
| 228 | + | |
| 229 | +#if defined(SUWAUI_IMPLEMENTS_SUWATABS) | |
| 230 | +typedef struct { | |
| 231 | + int x, y, width, height; | |
| 232 | +} SuwaTab; | |
| 233 | + | |
| 234 | +typedef struct { | |
| 235 | + int x, y, width, height; | |
| 236 | +} SuwaTabs; | |
| 237 | +#endif // SUWAUI_IMPLEMENTS_SUWATABS | |
| 238 | + | |
| 239 | +#if defined(SUWAUI_IMPLEMENTS_SUWABUTTON) | |
| 240 | +typedef struct { | |
| 241 | + int x, y, width, height; | |
| 242 | + const char *text; | |
| 243 | + XftFont *font; | |
| 244 | + int bg_color; | |
| 245 | + XftColor fg_color; | |
| 246 | + int pressed; // 0 = 普通、1 = 押している | |
| 247 | +} SuwaButton; | |
| 248 | + | |
| 249 | +// __attribute__((unused)) static void suwaui_del_button(SuwaWindow *window, SuwaButton *button); | |
| 250 | + | |
| 251 | +__attribute__((unused)) static XftColor suwaui_set_button_fgcolor(SuwaWindow *window, const char *color) { | |
| 252 | + XftColor xcolor; | |
| 253 | + XftColorAllocName(window->display, | |
| 254 | + DefaultVisual(window->display, DefaultScreen(window->display)), | |
| 255 | + DefaultColormap(window->display, DefaultScreen(window->display)), | |
| 256 | + color, &xcolor); | |
| 257 | + | |
| 258 | + return xcolor; | |
| 259 | +} | |
| 260 | + | |
| 261 | +__attribute__((unused)) static void suwaui_draw_button(SuwaWindow *window, SuwaButton *btn, XftDraw *xftdraw) { | |
| 262 | + unsigned long curbg = btn->pressed ? BTSEL : btn->bg_color; | |
| 263 | + XSetForeground(window->display, window->gc, curbg); | |
| 264 | + XFillRectangle(window->display, window->target, window->gc, btn->x, btn->y, | |
| 265 | + btn->width, btn->height); | |
| 266 | + | |
| 267 | + // 文字の中央に | |
| 268 | + if (btn->text && window->font && xftdraw) { | |
| 269 | + const FcChar8 *str = (const FcChar8 *)btn->text; | |
| 270 | + int len = strlen((const char *)str); | |
| 271 | + | |
| 272 | + XGlyphInfo extents; | |
| 273 | + XftTextExtentsUtf8(window->display, window->font, str, len, &extents); | |
| 274 | + | |
| 275 | + int text_w = extents.xOff; | |
| 276 | + int text_h = window->font->ascent + window->font->descent; | |
| 277 | + | |
| 278 | + int tx = btn->x + (btn->width - text_w) / 2; | |
| 279 | + int ty = btn->y + (btn->height - text_h) / 2 + window->font->ascent; | |
| 280 | + | |
| 281 | + XftDrawStringUtf8(xftdraw, &btn->fg_color, window->font, tx, ty, | |
| 282 | + (FcChar8 *)btn->text, len); | |
| 283 | + } | |
| 284 | +} | |
| 285 | + | |
| 286 | +__attribute__((unused)) static SuwaButton suwaui_add_button(int x, int y, int width, int height, | |
| 287 | + const char *text, XftFont *font, int bg_color, XftColor fg_color) { | |
| 288 | + SuwaButton button = { | |
| 289 | + .x = x, | |
| 290 | + .y = y, | |
| 291 | + .width = width, | |
| 292 | + .height = height, | |
| 293 | + .text = text, | |
| 294 | + .font = font, | |
| 295 | + .bg_color = bg_color, | |
| 296 | + .fg_color = fg_color, | |
| 297 | + .pressed = 0 | |
| 298 | + }; | |
| 299 | + | |
| 300 | + return button; | |
| 301 | +} | |
| 302 | +#endif // SUWAUI_IMPLEMENTS_SUWABUTTON | |
| 303 | + | |
| 304 | +#if defined(SUWAUI_IMPLEMENTS_SUWALABEL) | |
| 305 | +typedef struct { | |
| 306 | + int x, y, width, height; | |
| 307 | + char text[64]; | |
| 308 | + XftFont *font; | |
| 309 | + XftColor fg_color; | |
| 310 | +} SuwaLabel; | |
| 311 | + | |
| 312 | +__attribute__((unused)) static void suwaui_del_label(SuwaWindow *window, SuwaLabel *label); | |
| 313 | + | |
| 314 | +__attribute__((unused)) static SuwaLabel suwaui_add_label(SuwaWindow *window, | |
| 315 | + int x, int y, int width, int height, char text[64], const char *font, | |
| 316 | + const char *fg_color, int rtl) { | |
| 317 | + (void)width; | |
| 318 | + (void)height; | |
| 319 | + SuwaLabel label = { | |
| 320 | + .x = x, | |
| 321 | + .y = y, | |
| 322 | + .width = width, | |
| 323 | + .height = height | |
| 324 | + }; | |
| 325 | + | |
| 326 | + label.font = XftFontOpenName(window->display, window->screen, font); | |
| 327 | + if (!label.font) { | |
| 328 | + fprintf(stderr, "font create fail\n"); | |
| 329 | + return (SuwaLabel){0}; | |
| 330 | + } | |
| 331 | + | |
| 332 | + strncpy(label.text, text ? text : "", sizeof(label.text) - 1); | |
| 333 | + label.text[sizeof(label.text)-1] = '\0'; | |
| 334 | + | |
| 335 | + const FcChar8 *txt = (const FcChar8 *)label.text; | |
| 336 | + int len = strlen((const char *)txt); | |
| 337 | + | |
| 338 | + XGlyphInfo extents; | |
| 339 | + XftTextExtentsUtf8(window->display, label.font, txt, len, &extents); | |
| 340 | + | |
| 341 | + if (rtl > 0) { | |
| 342 | + XWindowAttributes attr; | |
| 343 | + XGetWindowAttributes(window->display, window->xwindow, &attr); | |
| 344 | + int w = attr.width; | |
| 345 | + label.x = w - label.x - extents.xOff; | |
| 346 | + } | |
| 347 | + | |
| 348 | + if (!XftColorAllocName(window->display, | |
| 349 | + DefaultVisual(window->display, DefaultScreen(window->display)), | |
| 350 | + DefaultColormap(window->display, DefaultScreen(window->display)), | |
| 351 | + fg_color, &label.fg_color)) { | |
| 352 | + suwaui_del_label(window, &label); | |
| 353 | + fprintf(stderr, "color estimate fail\n"); | |
| 354 | + return (SuwaLabel){0}; | |
| 355 | + } | |
| 356 | + | |
| 357 | + return label; | |
| 358 | +} | |
| 359 | + | |
| 360 | +__attribute__((unused)) static void suwaui_del_label(SuwaWindow *window, SuwaLabel *label) { | |
| 361 | + if (label->font) XftFontClose(window->display, label->font); | |
| 362 | +} | |
| 363 | +#endif // SUWAUI_IMPLEMENTS_SUWALABEL | |
| 364 | + | |
| 365 | +#if defined(__cplusplus) | |
| 366 | +} | |
| 367 | +#endif |