suwako / UnixCalc アーカイブ

このリポジトリはアーカイブされています。読むだけで、プッシュやイシューの変更は出来ません。

シンプル化

諏訪子 <suwako@076.moe>、2026-01-19 00:20
コミット ffa9c692707c14ee3c1107c10b6c4ab41770fca0 ・親 4a9266f ・ファイルを見る

12個のファイル、+794 -736

変更 Makefile
@@ -16,13 +16,16 @@
1616
1717 # cc | clang | gcc
1818 CC = cc
19+IMPLEMENTS = -DSUWAUI_IMPLEMENTS_SUWAWINDOW \
20+ -DSUWAUI_IMPLEMENTS_SUWALABEL \
21+ -DSUWAUI_IMPLEMENTS_SUWABUTTON
1922 # lldb | gdb
2023 DEBUGGER = lldb
21-FILES = main.c src/*.c
24+FILES = *.c
2225
2326 CFLAGS = -Wall -Wextra \
24- -I./dep/include -I/usr/include -I/usr/local/include -I/usr/X11R6/include \
25- -L./dep/lib/${OS} -L/usr/lib -L/usr/local/lib -L/usr/X11R6/lib \
27+ -I./include -I/usr/include -I/usr/local/include -I/usr/X11R6/include \
28+ -L/usr/lib -L/usr/local/lib -L/usr/X11R6/lib \
2629 -I/usr/X11R6/include/freetype2 -I/usr/local/include/freetype2
2730
2831 LDFLAGS = -lc -lX11 -lXft
@@ -39,15 +42,15 @@
3942 all: debug
4043
4144 debug:
42- ${CC} -O0 -g ${CFLAGS} -o ${NAME} ${FILES} ${LDFLAGS}
45+ ${CC} ${IMPLEMENTS} -O0 -g ${CFLAGS} -o ${NAME} ${FILES} ${LDFLAGS}
4346 ${DEBUGGER} -o run ${NAME}
4447
4548 develop:
46- ${CC} -O3 -g ${CFLAGS} -o ${NAME} ${FILES} ${LDFLAGS} ${SLIB}
49+ ${CC} ${IMPLEMENTS} -O3 -g ${CFLAGS} -o ${NAME} ${FILES} ${LDFLAGS} ${SLIB}
4750 ./${NAME}
4851
4952 release:
50- ${CC} -O3 ${CFLAGS} -o ${NAME} ${FILES} -static ${LDFLAGS} ${SLIB}
53+ ${CC} ${IMPLEMENTS} -O3 ${CFLAGS} -o ${NAME} ${FILES} -static ${LDFLAGS} ${SLIB}
5154 strip ${NAME}
5255 ./${NAME}
5356
追加 control.c
@@ -0,0 +1,382 @@
1+#include <X11/keysym.h>
2+#include <math.h>
3+#include <ctype.h>
4+
5+#include "control.h"
6+
7+#define NUM_ROWS 6
8+#define NUM_COLS 4
9+
10+#define DESIGN_WIDTH 382
11+#define DESIGN_HEIGHT 534
12+
13+#define DESIGN_BUTTON_AREA_Y 224
14+#define DESIGN_BUTTON_WIDTH 93
15+#define DESIGN_BUTTON_HEIGHT 60
16+#define DESIGN_PADDING 2
17+
18+char curinput[64] = {0};
19+int input_pos = 0;
20+
21+static const char *btn_labels[][10] = {
22+ { NULL }, // 数字表示
23+ { "C", "±", "%", "÷" },
24+ { "7", "8", "9", "×" },
25+ { "4", "5", "6", "-" },
26+ { "1", "2", "3", "+" },
27+ { "0", ".", "=", "<" },
28+};
29+
30+static SuwaButton *find_button_at(SuwaWindow *window, int mx, int my) {
31+ static SuwaButton found = {0};
32+
33+ float scale = get_scale(window);
34+ if (scale <= 0) scale = 1.0f;
35+
36+ int btn_w = (int)(DESIGN_BUTTON_WIDTH * scale + .5f);
37+ int btn_h = (int)(DESIGN_BUTTON_HEIGHT * scale + .5f);
38+ int padding = (int)(DESIGN_PADDING * scale + .5f);;
39+ int start_y = (int)(DESIGN_BUTTON_AREA_Y * scale + .5f);;
40+
41+ int row = -1;
42+ int col = -1;
43+
44+ for (int r = 1; r < NUM_ROWS; ++r) {
45+ int y_start = start_y + (r - 1) * btn_h + padding;
46+ if (my >= y_start && my < y_start + btn_h) {
47+ row = r;
48+ break;
49+ }
50+ }
51+
52+ if (row < 0) return NULL;
53+
54+ for (int c = 0; c < NUM_COLS; ++c) {
55+ int x_start = padding + c * (btn_w + padding);
56+ if (mx >= x_start && mx < x_start + btn_w) {
57+ col = c;
58+ break;
59+ }
60+ }
61+
62+ if (col < 0) return NULL;
63+
64+ found.x = padding + col * (btn_w + padding);
65+ found.y = start_y + (row - 1) * (btn_h + padding);
66+ found.width = btn_w;
67+ found.height = btn_h;
68+ found.text = btn_labels[row][col];
69+ found.pressed = 1;
70+
71+ return &found;
72+}
73+
74+void append_to_input(CtrlLabels *labels, char c) {
75+ if ((unsigned long)input_pos >= sizeof(curinput) - 2) return;
76+ strcpy(labels->problem->text, "");
77+ curinput[input_pos++] = c;
78+ curinput[input_pos] = '\0';
79+
80+ strncpy(labels->res->text, curinput, sizeof(labels->res->text) - 1);
81+ labels->res->text[sizeof(labels->res->text) - 1] = '\0';
82+}
83+
84+void clear_calculator(CtrlLabels *labels) {
85+ strcpy(labels->problem->text, "");
86+ curinput[0] = '\0';
87+ input_pos = 0;
88+ strcpy(labels->res->text, "0");
89+}
90+
91+double evaluate_simple(CtrlLabels *labels, const char *expr) {
92+ double res = 0.0;
93+ double cur = 0.0;
94+ char op = '+';
95+ int i = 0;
96+ snprintf(labels->problem->text, sizeof(labels->problem->text), "%s=", expr);
97+
98+ while (expr[i]) {
99+ if (expr[i] == ' ') { i++; continue; }
100+
101+ if (isdigit(expr[i]) || expr[i] == '.') {
102+ char numbuf[32];
103+ int j = 0;
104+ while (isdigit(expr[i]) || expr[i] == '.') {
105+ if (j < 31) numbuf[j++] = expr[i];
106+ ++i;
107+ }
108+ numbuf[j] = '\0';
109+ cur = atof(numbuf);
110+ } else {
111+ if (op == '+') res += cur;
112+ else if (op == '-') res -= cur;
113+ else if (op == '*') res *= cur;
114+ else if (op == '/') {
115+ if (cur == 0) return NAN;
116+ res /= cur;
117+ }
118+
119+ op = expr[i];
120+ ++i;
121+ cur = 0.0;
122+ }
123+ }
124+
125+ if (op == '+') res += cur;
126+ else if (op == '-') res -= cur;
127+ else if (op == '*') res *= cur;
128+ else if (op == '/') {
129+ if (cur == 0) return NAN;
130+ res /= cur;
131+ }
132+
133+ return res;
134+}
135+
136+void control_expose(SuwaWindow *window, CtrlLabels *labels) {
137+ if (window->event.type != Expose && window->event.type != ConfigureNotify) return;
138+
139+ XWindowAttributes attr;
140+ XGetWindowAttributes(window->display, window->xwindow, &attr);
141+ int w = attr.width;
142+ int h = attr.height;
143+
144+ if (window->backbuf != None) XFreePixmap(window->display, window->backbuf);
145+ window->backbuf = XCreatePixmap(window->display, window->xwindow, w, h,
146+ DefaultDepth(window->display, DefaultScreen(window->display)));
147+
148+ if (window->backbuf == None) window->backbuf = window->xwindow;
149+ window->target = window->backbuf;
150+
151+ XftDraw *backdraw = XftDrawCreate(window->display, window->backbuf,
152+ DefaultVisual(window->display, DefaultScreen(window->display)),
153+ DefaultColormap(window->display, DefaultScreen(window->display)));
154+ if (!backdraw) {
155+ fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n");
156+ XFreePixmap(window->display, window->backbuf);
157+ window->backbuf = None;
158+ return;
159+ }
160+
161+ XSetForeground(window->display, window->gc, BGCOL);
162+ XFillRectangle(window->display, window->backbuf, window->gc, 0, 0, w, h);
163+
164+ float scale = get_scale(window);
165+ if (scale <= 0) scale = 1.f;
166+
167+ int label_padding = (int)(20 * scale + .5f);
168+
169+ // 出力
170+ {
171+ XGlyphInfo extents;
172+ XftTextExtentsUtf8(window->display, labels->res->font,
173+ (const FcChar8 *)labels->res->text,
174+ strlen(labels->res->text), &extents);
175+
176+ int tx = w - label_padding - extents.xOff;
177+ int ty = (int)(180 * scale + .5f);
178+
179+ XftDrawStringUtf8(
180+ backdraw, &labels->res->fg_color, labels->res->font,
181+ tx, ty, (const FcChar8 *)labels->res->text, 64);
182+ }
183+
184+ {
185+ XGlyphInfo extents;
186+ XftTextExtentsUtf8(window->display, labels->problem->font,
187+ (const FcChar8 *)labels->problem->text,
188+ strlen(labels->problem->text), &extents);
189+
190+ int tx = w - label_padding - extents.xOff;
191+ int ty = (int)(80 * scale + .5f);
192+
193+ XftDrawStringUtf8(
194+ backdraw, &labels->problem->fg_color, labels->problem->font,
195+ tx, ty, (const FcChar8 *)labels->problem->text, 64);
196+ }
197+
198+ int bw = (int)(DESIGN_BUTTON_WIDTH * scale + .5f);
199+ int bh = (int)(DESIGN_BUTTON_HEIGHT * scale + .5f);
200+ int bp = (int)(DESIGN_PADDING * scale + .5f);
201+ int by = (int)(DESIGN_BUTTON_AREA_Y * scale + .5f);
202+
203+ XftColor buttonColor;
204+#if defined(__OpenBSD__)
205+ buttonColor = suwaui_set_button_fgcolor(window, "#232320");
206+#elif defined(__FreeBSD__)
207+ buttonColor = suwaui_set_button_fgcolor(window, "#232020");
208+#endif
209+
210+ for (int row = 1; row < NUM_ROWS; ++row) {
211+ for (int col = 0; col < NUM_COLS; ++col) {
212+ SuwaButton btn = suwaui_add_button(bp + col * (bw + bp),
213+ by + (row - 1) * (bh + bp),
214+ bw, bh, btn_labels[row][col], window->font, BTCOL, buttonColor);
215+
216+ suwaui_draw_button(window, &btn, backdraw);
217+ }
218+ }
219+
220+ XCopyArea(window->display, window->backbuf, window->xwindow, window->gc,
221+ 0, 0, w, h, 0, 0);
222+ XftDrawDestroy(backdraw);
223+ XFlush(window->display);
224+}
225+
226+void handle_button_press(SuwaWindow *window, int mx, int my) {
227+ SuwaButton *btn = find_button_at(window, mx, my);
228+ if (!btn) return;
229+
230+ btn->pressed = 1;
231+ XftDraw *backdraw = XftDrawCreate(window->display, window->backbuf,
232+ DefaultVisual(window->display, DefaultScreen(window->display)),
233+ DefaultColormap(window->display, DefaultScreen(window->display)));
234+ if (!backdraw) {
235+ fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n");
236+ XFreePixmap(window->display, window->backbuf);
237+ window->backbuf = None;
238+ return;
239+ }
240+
241+ suwaui_draw_button(window, btn, backdraw);
242+ XFlush(window->display);
243+}
244+
245+void handle_button_release(SuwaWindow *window, CtrlLabels *labels, int mx, int my) {
246+ SuwaButton *btn = find_button_at(window, mx, my);
247+ if (!btn) return;
248+
249+ btn->pressed = 0;
250+ XftDraw *backdraw = XftDrawCreate(window->display, window->backbuf,
251+ DefaultVisual(window->display, DefaultScreen(window->display)),
252+ DefaultColormap(window->display, DefaultScreen(window->display)));
253+ if (!backdraw) {
254+ fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n");
255+ XFreePixmap(window->display, window->backbuf);
256+ window->backbuf = None;
257+ return;
258+ }
259+
260+ window->target = window->backbuf;
261+ suwaui_draw_button(window, btn, backdraw);
262+ XftDrawDestroy(backdraw);
263+ XFlush(window->display);
264+
265+ const char *label = btn->text;
266+ if (strcmp(label, "C") == 0) {
267+ clear_calculator(labels);
268+ } else if (strcmp(label, "×") == 0) {
269+ append_to_input(labels, '*');
270+ } else if (strcmp(label, "÷") == 0) {
271+ append_to_input(labels, '/');
272+ } else if (strcmp(label, "<") == 0) {
273+ curinput[--input_pos] = '\0';
274+ strcpy(labels->res->text, curinput[0] ? curinput : "0");
275+ } else if (strcmp(label, "=") == 0) {
276+ double res = evaluate_simple(labels, curinput);
277+ if (isnan(res)) strncpy(labels->res->text, "Error", 5);
278+ else snprintf(labels->res->text, sizeof(labels->res->text), "%.8g", res);
279+ } else if (strlen(label) == 1) {
280+ append_to_input(labels, label[0]);
281+ }
282+
283+ window->event = (XEvent){.type = Expose};
284+ control_expose(window, labels);
285+}
286+
287+void handle_key_press(SuwaWindow *window, CtrlLabels *labels) {
288+ KeySym keysym;
289+ char buf[32];
290+ int len;
291+ (void)len;
292+
293+ keysym = XLookupKeysym(&window->event.xkey, 0);
294+ len = XLookupString(&window->event.xkey, buf, sizeof(buf), &keysym, NULL);
295+
296+ if (keysym == XK_Q) {
297+ window->isrunning = 0;
298+ return;
299+ }
300+
301+ if (keysym == XK_B) {
302+ puts("標準電卓画面");
303+ }
304+
305+ if (keysym == XK_P) {
306+ puts("プログラマー電卓画面");
307+ }
308+
309+ if (keysym == XK_H) {
310+ puts("履歴画面");
311+ }
312+
313+ if (keysym == XK_h) {
314+ puts("ヘルプ画面");
315+ puts("-----");
316+ puts("Q = 終了 Exit");
317+ puts("X = 1文字の消し Remove 1 character");
318+ puts("C = クリ了 Clear");
319+ puts("h = ヘルプ画面 Help screen");
320+ puts("H = 履歴画面 History screen");
321+ puts("B = 標準電卓画面 Basic calculator screen");
322+ puts("P = プログラマー電卓画面 Programmer calculator screen");
323+ }
324+
325+ if (keysym == XK_C) {
326+ clear_calculator(labels);
327+ goto redraw;
328+ }
329+
330+ if (keysym == XK_Return || keysym == XK_KP_Enter) {
331+ double res = evaluate_simple(labels, curinput);
332+ if (isnan(res)) strncpy(labels->res->text, "Error", 5);
333+ else snprintf(labels->res->text, sizeof(labels->res->text), "%.8g", res);
334+ goto redraw;
335+ }
336+
337+ if ((keysym == XK_BackSpace && input_pos > 0)
338+ || (keysym == XK_Delete && input_pos > 0)
339+ || (keysym == XK_X && input_pos > 0)) {
340+ curinput[--input_pos] = '\0';
341+ strcpy(labels->res->text, curinput[0] ? curinput : "0");
342+ goto redraw;
343+ }
344+
345+ char c = 0;
346+ if (keysym >= XK_0 && keysym <= XK_9) c = '0' + (keysym - XK_0);
347+ else if (keysym >= XK_KP_0 && keysym <= XK_KP_9) c = '0' + (keysym - XK_0);
348+ else if (keysym == XK_KP_Add || keysym == XK_plus) c = '+';
349+ else if (keysym == XK_KP_Subtract || keysym == XK_minus) c = '-';
350+ else if (keysym == XK_KP_Multiply || keysym == XK_asterisk) c = '*';
351+ else if (keysym == XK_KP_Divide || keysym == XK_slash) c = '/';
352+ else if (keysym == XK_KP_Decimal || keysym == XK_period) c = '.';
353+
354+ if (c) {
355+ append_to_input(labels, c);
356+ goto redraw;
357+ }
358+
359+ return;
360+
361+redraw:
362+ window->event = (XEvent){.type = Expose};
363+ control_expose(window, labels);
364+}
365+
366+// void handle_mouse_hover(SuwaWindow *window) {
367+// int x = window->event,xmotion.x;
368+// int y = window->event,xmotion.y;
369+// SuwaButton *hover = find_button_at(x, y);
370+
371+// if (hover != hovered_btn) {
372+// if (hovered_btn) {
373+// hovered_btn->pressed = 0;
374+// redraw_single_button(&window, hovered_btn);
375+// }
376+// hovered_btn = hover;
377+
378+// if (hovered_btn) {
379+// redraw_single_button(&window, hovered_btn);
380+// }
381+// }
382+// }
追加 control.h
@@ -0,0 +1,14 @@
1+#pragma once
2+
3+#include <suwaui.h>
4+
5+typedef struct {
6+ SuwaLabel *res;
7+ SuwaLabel *problem;
8+} CtrlLabels;
9+
10+void control_expose(SuwaWindow *window, CtrlLabels *labels);
11+void handle_button_press(SuwaWindow *window, int mx, int my);
12+void handle_button_release(SuwaWindow *window, CtrlLabels *labels, int mx, int my);
13+void handle_key_press(SuwaWindow *window, CtrlLabels *labels);
14+// void handle_mouse_hover(SuwaWindow *window);
@@ -0,0 +1,344 @@
1+#pragma once
2+
3+#include <X11/Xlib.h>
4+#include <X11/Xft/Xft.h>
5+#include <X11/Xatom.h>
6+
7+#include <stdio.h>
8+
9+#define FGCOL 0xfcfcfc
10+#if defined(__OpenBSD__)
11+#define BGCOL 0x232320
12+#define BTSEL 0xb8b515
13+#define BTCOL 0xf1ed25
14+#define BTHVR 0xecea71
15+#elif defined(__FreeBSD__)
16+#define BGCOL 0x232020
17+#define BTSEL 0xb61729
18+#define BTCOL 0xee4030
19+#define BTHVR 0xf35869
20+#endif
21+
22+#if defined(__cplusplus)
23+extern "C" {
24+#endif
25+
26+#if defined(SUWAUI_IMPLEMENTS_SUWAWINDOW)
27+#include <string.h>
28+
29+typedef struct {
30+ int x, y, width, height;
31+ int start_width, start_height;
32+ int isrunning;
33+ const char *name;
34+ const char *version;
35+ const char *res_name;
36+ const char *class_name;
37+ Display *display;
38+ Window xwindow;
39+ int screen;
40+ Drawable target;
41+ GC gc;
42+ Visual visual;
43+ XftColor color;
44+ XftFont *font;
45+ Colormap colormap;
46+ Pixmap backbuf;
47+ XEvent event;
48+} SuwaWindow;
49+
50+__attribute__((unused)) static void suwaui_destroy_window(SuwaWindow *w);
51+
52+__attribute__((unused)) static inline float get_scale(SuwaWindow *w) {
53+ XWindowAttributes attr;
54+ XGetWindowAttributes(w->display, w->xwindow, &attr);
55+ float sx = (float)attr.width / w->start_width;
56+ float sy = (float)attr.height / w->start_height;
57+ return sx < sy ? sx : sy;
58+}
59+
60+__attribute__((unused)) static SuwaWindow suwaui_create_window(int x, int y, int width, int height,
61+ const char *software_name, const char *software_version,
62+ const char *res_name, const char *class_name,
63+ const char *font, const char *fg_color,
64+ int is_centered, int cannot_shrink, int cannot_grow) {
65+ (void)cannot_grow;
66+
67+ SuwaWindow w = {0};
68+ w.width = width;
69+ w.height = height;
70+ w.start_width = width;
71+ w.start_height = height;
72+ w.name = software_name;
73+ w.version = software_version;
74+ w.res_name = res_name;
75+ w.class_name = class_name;
76+ w.isrunning = 1;
77+
78+ XGCValues values;
79+
80+ w.display = XOpenDisplay(NULL);
81+ if (w.display == NULL) {
82+ fprintf(stderr, "screen open fail\n");
83+ return (SuwaWindow){0};
84+ }
85+
86+ w.screen = DefaultScreen(w.display);
87+
88+ if (is_centered > 0) {
89+ int sw = DisplayWidth(w.display, w.screen);
90+ int sh = DisplayHeight(w.display, w.screen);
91+ w.x = (sw - w.width) / 2;
92+ w.y = (sh - w.height) / 2;
93+ } else {
94+ w.x = x;
95+ w.y = y;
96+ }
97+
98+ w.xwindow = XCreateSimpleWindow(w.display,
99+ RootWindow(w.display, w.screen),
100+ w.x, w.y,
101+ w.width, w.height,
102+ 1, BTCOL, BGCOL);
103+ if (!w.xwindow) {
104+ suwaui_destroy_window(&w);
105+ fprintf(stderr, "window create fail\n");
106+ return (SuwaWindow){0};
107+ }
108+
109+ if (cannot_shrink) {
110+ XSizeHints *sizeHint = XAllocSizeHints();
111+ if (sizeHint) {
112+ sizeHint->flags = PMinSize;
113+ sizeHint->min_width = width;
114+ sizeHint->min_height = height;
115+ XSetWMNormalHints(w.display, w.xwindow, sizeHint);
116+ XFree(sizeHint);
117+ }
118+ }
119+
120+ w.backbuf = XCreatePixmap(w.display, w.xwindow,
121+ w.width, w.height,
122+ DefaultDepth(w.display, w.screen));
123+ w.target = w.backbuf;
124+
125+ Atom net_wm_window_type = XInternAtom(w.display, "_NET_WM_WINDOW_TYPE", False);
126+ Atom dialog = XInternAtom(w.display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
127+
128+ XChangeProperty(w.display, w.xwindow, net_wm_window_type, XA_ATOM, 32,
129+ PropModeReplace, (unsigned char *)&dialog, 1);
130+
131+ XStoreName(w.display, w.xwindow, w.name);
132+ Atom net_wm_name = XInternAtom(w.display, "_NET_WM_NAME", False);
133+ char displayname[16];
134+ snprintf(displayname, 16, "%s %s", w.name, w.version);
135+ XChangeProperty(w.display, w.xwindow, net_wm_name,
136+ XInternAtom(w.display, "UTF8_STRING", False), 8,
137+ PropModeReplace, (unsigned char *)displayname, strlen(displayname));
138+
139+ XClassHint *classHint = XAllocClassHint();
140+ if (classHint) {
141+ classHint->res_name = strdup(w.res_name);
142+ classHint->res_class = strdup(w.class_name);
143+ XSetClassHint(w.display, w.xwindow, classHint);
144+ XFree(classHint);
145+ }
146+
147+ XSetWindowBackground(w.display, w.xwindow, BGCOL);
148+
149+ XSelectInput(w.display, w.xwindow,
150+ ExposureMask
151+ | ButtonPressMask
152+ | ButtonReleaseMask
153+ | KeyPressMask
154+ // | PointerMotionMask
155+ // | ButtonMotionMask
156+ // | StructureNotifyMask
157+ );
158+
159+ w.gc = XCreateGC(w.display, w.xwindow, 0, &values);
160+ if (!w.gc) {
161+ suwaui_destroy_window(&w);
162+ fprintf(stderr, "gc create fail\n");
163+ return (SuwaWindow){0};
164+ }
165+
166+ w.visual = *DefaultVisual(w.display, w.screen);
167+
168+ w.colormap = XCreateColormap(w.display, w.xwindow,
169+ &w.visual, AllocNone);
170+ if (w.colormap == None) {
171+ suwaui_destroy_window(&w);
172+ fprintf(stderr, "calor map create fail\n");
173+ return (SuwaWindow){0};
174+ }
175+
176+ w.font = XftFontOpenName(w.display, w.screen, font);
177+ if (!w.font) {
178+ suwaui_destroy_window(&w);
179+ fprintf(stderr, "font create fail\n");
180+ return (SuwaWindow){0};
181+ }
182+
183+ if (!XftColorAllocName(w.display, &w.visual,
184+ w.colormap, fg_color, &w.color)) {
185+ suwaui_destroy_window(&w);
186+ fprintf(stderr, "color estimate fail\n");
187+ return (SuwaWindow){0};
188+ }
189+
190+ return w;
191+}
192+
193+__attribute__((unused)) static void suwaui_destroy_window(SuwaWindow *w) {
194+ if (w->font) XftFontClose(w->display, w->font);
195+
196+ if (w->gc) XFreeGC(w->display, w->gc);
197+ if (w->backbuf) {
198+ XFreePixmap(w->display, w->backbuf);
199+ w->backbuf = None;
200+ }
201+ if (w->xwindow) XDestroyWindow(w->display, w->xwindow);
202+ if (w->display) XCloseDisplay(w->display);
203+}
204+#endif // SUWAUI_IMPLEMENTS_SUWAWINDOW
205+
206+#if defined(SUWAUI_IMPLEMENTS_SUWATABS)
207+typedef struct {
208+ int x, y, width, height;
209+} SuwaTab;
210+
211+typedef struct {
212+ int x, y, width, height;
213+} SuwaTabs;
214+#endif // SUWAUI_IMPLEMENTS_SUWATABS
215+
216+#if defined(SUWAUI_IMPLEMENTS_SUWABUTTON)
217+typedef struct {
218+ int x, y, width, height;
219+ const char *text;
220+ XftFont *font;
221+ int bg_color;
222+ XftColor fg_color;
223+ int pressed; // 0 = 普通、1 = 押している
224+} SuwaButton;
225+
226+// __attribute__((unused)) static void suwaui_del_button(SuwaWindow *window, SuwaButton *button);
227+
228+__attribute__((unused)) static XftColor suwaui_set_button_fgcolor(SuwaWindow *window, const char *color) {
229+ XftColor xcolor;
230+ XftColorAllocName(window->display,
231+ DefaultVisual(window->display, DefaultScreen(window->display)),
232+ DefaultColormap(window->display, DefaultScreen(window->display)),
233+ color, &xcolor);
234+
235+ return xcolor;
236+}
237+
238+__attribute__((unused)) static void suwaui_draw_button(SuwaWindow *window, SuwaButton *btn, XftDraw *xftdraw) {
239+ unsigned long curbg = btn->pressed ? BTSEL : btn->bg_color;
240+ XSetForeground(window->display, window->gc, curbg);
241+ XFillRectangle(window->display, window->target, window->gc, btn->x, btn->y,
242+ btn->width, btn->height);
243+
244+ // 文字の中央に
245+ if (btn->text && window->font && xftdraw) {
246+ const FcChar8 *str = (const FcChar8 *)btn->text;
247+ int len = strlen((const char *)str);
248+
249+ XGlyphInfo extents;
250+ XftTextExtentsUtf8(window->display, window->font, str, len, &extents);
251+
252+ int text_w = extents.xOff;
253+ int text_h = window->font->ascent + window->font->descent;
254+
255+ int tx = btn->x + (btn->width - text_w) / 2;
256+ int ty = btn->y + (btn->height - text_h) / 2 + window->font->ascent;
257+
258+ XftDrawStringUtf8(xftdraw, &btn->fg_color, window->font, tx, ty,
259+ (FcChar8 *)btn->text, len);
260+ }
261+}
262+
263+__attribute__((unused)) static SuwaButton suwaui_add_button(int x, int y, int width, int height,
264+ const char *text, XftFont *font, int bg_color, XftColor fg_color) {
265+ SuwaButton button = {
266+ .x = x,
267+ .y = y,
268+ .width = width,
269+ .height = height,
270+ .text = text,
271+ .font = font,
272+ .bg_color = bg_color,
273+ .fg_color = fg_color,
274+ .pressed = 0
275+ };
276+
277+ return button;
278+}
279+#endif // SUWAUI_IMPLEMENTS_SUWABUTTON
280+
281+#if defined(SUWAUI_IMPLEMENTS_SUWALABEL)
282+typedef struct {
283+ int x, y, width, height;
284+ char text[64];
285+ XftFont *font;
286+ XftColor fg_color;
287+} SuwaLabel;
288+
289+__attribute__((unused)) static void suwaui_del_label(SuwaWindow *window, SuwaLabel *label);
290+
291+__attribute__((unused)) static SuwaLabel suwaui_add_label(SuwaWindow *window,
292+ int x, int y, int width, int height, char text[64], const char *font,
293+ const char *fg_color, int rtl) {
294+ (void)width;
295+ (void)height;
296+ SuwaLabel label = {
297+ .x = x,
298+ .y = y,
299+ .width = width,
300+ .height = height
301+ };
302+
303+ label.font = XftFontOpenName(window->display, window->screen, font);
304+ if (!label.font) {
305+ fprintf(stderr, "font create fail\n");
306+ return (SuwaLabel){0};
307+ }
308+
309+ strncpy(label.text, text ? text : "", sizeof(label.text) - 1);
310+ label.text[sizeof(label.text)-1] = '\0';
311+
312+ const FcChar8 *txt = (const FcChar8 *)label.text;
313+ int len = strlen((const char *)txt);
314+
315+ XGlyphInfo extents;
316+ XftTextExtentsUtf8(window->display, label.font, txt, len, &extents);
317+
318+ if (rtl > 0) {
319+ XWindowAttributes attr;
320+ XGetWindowAttributes(window->display, window->xwindow, &attr);
321+ int w = attr.width;
322+ label.x = w - label.x - extents.xOff;
323+ }
324+
325+ if (!XftColorAllocName(window->display,
326+ DefaultVisual(window->display, DefaultScreen(window->display)),
327+ DefaultColormap(window->display, DefaultScreen(window->display)),
328+ fg_color, &label.fg_color)) {
329+ suwaui_del_label(window, &label);
330+ fprintf(stderr, "color estimate fail\n");
331+ return (SuwaLabel){0};
332+ }
333+
334+ return label;
335+}
336+
337+__attribute__((unused)) static void suwaui_del_label(SuwaWindow *window, SuwaLabel *label) {
338+ if (label->font) XftFontClose(window->display, label->font);
339+}
340+#endif // SUWAUI_IMPLEMENTS_SUWALABEL
341+
342+#if defined(__cplusplus)
343+}
344+#endif
変更 main.c
@@ -1,243 +1,79 @@
11 #include <stdio.h>
22
33 #include <X11/Xatom.h>
4-#include "src/utils.h"
5-#include "src/control.h"
4+#include "control.h"
65
76 const char *sofname = "ucalc";
87 const char *version = "0.0.0";
98 const char *disname = "Unix Calc";
109
1110 int main() {
12- UiSystem ui;
13- ui.isrunning = 1;
14- ui.window.width = 382;
15- ui.window.height = 534;
16- XEvent event;
17- int screen;
18- XGCValues values;
19-
20- ui.display = XOpenDisplay(NULL);
21- if (ui.display == NULL) {
22- fprintf(stderr, "画面を開けられません。\n");
23- exit(1);
24- }
25-
26- screen = DefaultScreen(ui.display);
27-
28- int sw = DisplayWidth(ui.display, screen);
29- int sh = DisplayHeight(ui.display, screen);
30- ui.window.x = (sw - ui.window.width) / 3;
31- ui.window.y = (sh - ui.window.height) / 2;
32-
33- ui.xwindow = XCreateSimpleWindow(ui.display,
34- RootWindow(ui.display, screen),
35- ui.window.x, ui.window.y,
36- ui.window.width, ui.window.height,
37- 1, BTCOL, BGCOL);
38- if (!ui.xwindow) {
39- cleanup(&ui);
40- fprintf(stderr, "ウィンドウを作成に失敗。\n");
41- exit(1);
42- }
43-
44- XSizeHints *sizeHint = XAllocSizeHints();
45- if (sizeHint) {
46- sizeHint->flags = PMinSize;
47- sizeHint->min_width = 382;
48- sizeHint->min_height = 534;
49- XSetWMNormalHints(ui.display, ui.xwindow, sizeHint);
50- XFree(sizeHint);
51- }
52-
53- ui.backbuf = XCreatePixmap(ui.display, ui.xwindow,
54- ui.window.width, ui.window.height,
55- DefaultDepth(ui.display, screen));
56- ui.target = ui.backbuf;
57-
58- Atom net_wm_window_type = XInternAtom(ui.display, "_NET_WM_WINDOW_TYPE", False);
59- Atom dialog = XInternAtom(ui.display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
60-
61- XChangeProperty(ui.display, ui.xwindow, net_wm_window_type, XA_ATOM, 32,
62- PropModeReplace, (unsigned char *)&dialog, 1);
63-
64- XStoreName(ui.display, ui.xwindow, disname);
65- Atom net_wm_name = XInternAtom(ui.display, "_NET_WM_NAME", False);
66- char displayname[16];
67- snprintf(displayname, 16, "%s %s", disname, version);
68- XChangeProperty(ui.display, ui.xwindow, net_wm_name,
69- XInternAtom(ui.display, "UTF8_STRING", False), 8,
70- PropModeReplace, (unsigned char *)displayname, strlen(displayname));
71-
72- XClassHint *classHint = XAllocClassHint();
73- if (classHint) {
74- classHint->res_name = "unixcalc";
75- classHint->res_class = "UnixCalc";
76- XSetClassHint(ui.display, ui.xwindow, classHint);
77- XFree(classHint);
78- }
79-
80- XSetWindowBackground(ui.display, ui.xwindow, BGCOL);
81-
82- XSelectInput(ui.display, ui.xwindow,
83- ExposureMask
84- | ButtonPressMask
85- | ButtonReleaseMask
86- | KeyPressMask
87- // | PointerMotionMask
88- // | ButtonMotionMask
89- // | StructureNotifyMask
90- );
91-
92- ui.gc = XCreateGC(ui.display, ui.xwindow, 0, &values);
93- if (!ui.gc) {
94- cleanup(&ui);
95- fprintf(stderr, "GCを作成に失敗。\n");
96- exit(1);
97- }
98-
99- ui.visual = *DefaultVisual(ui.display, screen);
100-
101- ui.colormap = XCreateColormap(ui.display, ui.xwindow, &ui.visual, AllocNone);
102- if (ui.colormap == None) {
103- cleanup(&ui);
104- fprintf(stderr, "カラーマップを作成に失敗。\n");
105- exit(1);
106- }
107-
108- XWindowAttributes attr;
109- XGetWindowAttributes(ui.display, ui.xwindow, &attr);
110- int w = attr.width;
111-
112- {
11311 #if defined(__OpenBSD__)
114- ui.resLabel.font = XftFontOpenName(ui.display, screen, "Noto Sans CJK-48");
12+ SuwaWindow window = suwaui_create_window(
13+ 500, 400, 382, 534,
14+ disname, version, "unixcalc", "UnixCalc",
15+ "Noto Sans CJK-8", "#232320",
16+ 0, 1, 0);
17+ SuwaLabel resLabel = suwaui_add_label(&window, 20, 180, 0, 0, "0",
18+ "Noto Sans CJK-48", "#f1ed25", 1);
19+ SuwaLabel problemLabel = suwaui_add_label(&window, 20, 80, 0, 0, "",
20+ "Noto Sans CJK-12", "#b8b515", 1);
11521 #elif defined(__FreeBSD__)
116- ui.resLabel.font = XftFontOpenName(ui.display, screen, "Noto Sans CJK-72");
22+ SuwaWindow window = suwaui_create_window(
23+ 20, 40, 382, 534,
24+ disname, version, "unixcalc", "UnixCalc",
25+ "Noto Sans CJK-12", "#232020",
26+ 0, 1, 0);
27+ SuwaLabel resLabel = suwaui_add_label(&window, 20, 180, 0, 0, "0",
28+ "Noto Sans CJK-72", "#ee4030", 1);
29+ SuwaLabel problemLabel = suwaui_add_label(&window, 20, 80, 0, 0, "",
30+ "Noto Sans CJK-24", "#b61729", 1);
11731 #endif
118- if (!ui.resLabel.font) {
119- cleanup(&ui);
120- fprintf(stderr, "解決フォントの読み込みに失敗。\n");
121- exit(1);
122- }
12332
124- ui.resLabel.text[0] = '0';
125- ui.resLabel.text[1] = '\0';
33+ CtrlLabels lbl = {
34+ .res = &resLabel,
35+ .problem = &problemLabel
36+ };
12637
127- const FcChar8 *text = (const FcChar8 *)ui.resLabel.text;
128- int len = strlen((const char *)text);
129-
130- XGlyphInfo extents;
131- XftTextExtentsUtf8(ui.display, ui.resLabel.font, text, len, &extents);
132-
133- ui.resLabel.x = w - 20 - extents.xOff;
134- ui.resLabel.y = 180;
135- if (!XftColorAllocName(ui.display,
136- DefaultVisual(ui.display, DefaultScreen(ui.display)),
137- DefaultColormap(ui.display, DefaultScreen(ui.display)),
138-#if defined(__OpenBSD__)
139- "#f1ed25", &ui.resLabel.fg_color)) {
140-#elif defined(__FreeBSD__)
141- "#ee4030", &ui.resLabel.fg_color)) {
142-#endif
143- cleanup(&ui);
144- fprintf(stderr, "色の役割に失敗。\n");
145- exit(1);
146- }
147- }
148-
38+ XMapWindow(window.display, window.xwindow);
14939 {
150-#if defined(__OpenBSD__)
151- ui.problemLabel.font = XftFontOpenName(ui.display, screen, "Noto Sans CJK-12");
152-#elif defined(__FreeBSD__)
153- ui.problemLabel.font = XftFontOpenName(ui.display, screen, "Noto Sans CJK-24");
154-#endif
155- if (!ui.problemLabel.font) {
156- cleanup(&ui);
157- fprintf(stderr, "問題フォントの読み込みに失敗。\n");
158- exit(1);
159- }
160-
161- ui.problemLabel.text[0] = '\0';
162-
163- const FcChar8 *text = (const FcChar8 *)ui.problemLabel.text;
164- int len = strlen((const char *)text);
165-
166- XGlyphInfo extents;
167- XftTextExtentsUtf8(ui.display, ui.problemLabel.font, text, len, &extents);
168-
169- ui.problemLabel.x = w - 20 - extents.xOff;
170- ui.problemLabel.y = 80;
171- if (!XftColorAllocName(ui.display,
172- DefaultVisual(ui.display, DefaultScreen(ui.display)),
173- DefaultColormap(ui.display, DefaultScreen(ui.display)),
174-#if defined(__OpenBSD__)
175- "#b8b515", &ui.problemLabel.fg_color)) {
176-#elif defined(__FreeBSD__)
177- "#b61729", &ui.problemLabel.fg_color)) {
178-#endif
179- cleanup(&ui);
180- fprintf(stderr, "色の役割に失敗。\n");
181- exit(1);
182- }
183- }
184-
185-#if defined(__OpenBSD__)
186- ui.font = XftFontOpenName(ui.display, screen, "Noto Sans CJK-8");
187-#elif defined(__FreeBSD__)
188- ui.font = XftFontOpenName(ui.display, screen, "Noto Sans CJK-12");
189-#endif
190- if (!ui.font) {
191- cleanup(&ui);
192- fprintf(stderr, "フォントの読み込みに失敗。\n");
193- exit(1);
194- }
195-
196-#if defined(__OpenBSD__)
197- if (!XftColorAllocName(ui.display, &ui.visual, ui.colormap, "#232320", &ui.color)) {
198-#elif defined(__FreeBSD__)
199- if (!XftColorAllocName(ui.display, &ui.visual, ui.colormap, "#232020", &ui.color)) {
200-#endif
201- cleanup(&ui);
202- fprintf(stderr, "色の役割に失敗。\n");
203- exit(1);
204- }
205-
206- XMapWindow(ui.display, ui.xwindow);
207- {
20840 XWindowAttributes attr;
209- XGetWindowAttributes(ui.display, ui.xwindow, &attr);
41+ XGetWindowAttributes(window.display, window.xwindow, &attr);
21042 XEvent fake = { .type = Expose };
211- fake.xexpose.window = ui.xwindow;
43+ fake.xexpose.window = window.xwindow;
21244 fake.xexpose.width = attr.width;
21345 fake.xexpose.height = attr.height;
214- control_expose(&ui, &event);
46+ control_expose(&window, &lbl);
21547 }
21648
217- while (ui.isrunning) {
218- XNextEvent(ui.display, &event);
49+ while (window.isrunning) {
50+ XNextEvent(window.display, &window.event);
21951
220- switch (event.type) {
52+ switch (window.event.type) {
22153 case Expose:
22254 case ConfigureNotify:
223- XClearWindow(ui.display, ui.xwindow);
224- control_expose(&ui, &event);
55+ XClearWindow(window.display, window.xwindow);
56+ control_expose(&window, &lbl);
22557 break;
22658 case ButtonPress:
227- if (event.xbutton.button == Button1) {
228- handle_button_press(&ui, event.xbutton.x, event.xbutton.y);
59+ if (window.event.xbutton.button == Button1) {
60+ int x = window.event.xbutton.x;
61+ int y = window.event.xbutton.y;
62+ handle_button_press(&window, x, y);
22963 break;
23064 }
23165 case ButtonRelease:
232- if (event.xbutton.button == Button1) {
233- handle_button_release(&ui, event.xbutton.x, event.xbutton.y);
66+ if (window.event.xbutton.button == Button1) {
67+ int x = window.event.xbutton.x;
68+ int y = window.event.xbutton.y;
69+ handle_button_release(&window, &lbl, x, y);
23470 break;
23571 }
23672 case KeyPress:
237- handle_key_press(&ui, &event);
73+ handle_key_press(&window, &lbl);
23874 break;
23975 // case MotionNotify:
240- // handle_mouse_hover(&ui, &event);
76+ // handle_mouse_hover(&window);
24177 // break;
24278 case ClientMessage:
24379 // WM_DELETE_WINDOW
@@ -245,7 +81,9 @@
24581 }
24682 }
24783
248- cleanup(&ui);
84+ suwaui_del_label(&window, &problemLabel);
85+ suwaui_del_label(&window, &resLabel);
86+ suwaui_destroy_window(&window);
24987
25088 return 0;
25189 }
削除 src/control.c
@@ -1,406 +0,0 @@
1-#include <X11/keysym.h>
2-#include <math.h>
3-#include <ctype.h>
4-
5-#include "control.h"
6-#include "display.h"
7-
8-#define NUM_ROWS 6
9-#define NUM_COLS 4
10-
11-#define DESIGN_WIDTH 382
12-#define DESIGN_HEIGHT 534
13-
14-#define DESIGN_BUTTON_AREA_Y 224
15-#define DESIGN_BUTTON_WIDTH 93
16-#define DESIGN_BUTTON_HEIGHT 60
17-#define DESIGN_PADDING 2
18-
19-char curinput[64] = {0};
20-int input_pos = 0;
21-int initialized = 0;
22-XftColor buttonColor;
23-
24-static const char *btn_labels[][10] = {
25- { NULL }, // 数字表示
26- { "C", "±", "%", "÷" },
27- { "7", "8", "9", "×" },
28- { "4", "5", "6", "-" },
29- { "1", "2", "3", "+" },
30- { "0", ".", "=", "<" },
31-};
32-
33-static inline float get_scale(UiSystem *ui) {
34- XWindowAttributes attr;
35- XGetWindowAttributes(ui->display, ui->xwindow, &attr);
36- float sx = (float)attr.width / DESIGN_WIDTH;
37- float sy = (float)attr.height / DESIGN_HEIGHT;
38- return sx < sy ? sx : sy;
39-}
40-
41-static SuwaButton *find_button_at(UiSystem *ui, int mx, int my) {
42- static SuwaButton found = {0};
43-
44- float scale = get_scale(ui);
45- if (scale <= 0) scale = 1.0f;
46-
47- int btn_w = (int)(DESIGN_BUTTON_WIDTH * scale + .5f);
48- int btn_h = (int)(DESIGN_BUTTON_HEIGHT * scale + .5f);
49- int padding = (int)(DESIGN_PADDING * scale + .5f);;
50- int start_y = (int)(DESIGN_BUTTON_AREA_Y * scale + .5f);;
51-
52- int row = -1;
53- int col = -1;
54-
55- for (int r = 1; r < NUM_ROWS; ++r) {
56- int y_start = start_y + (r - 1) * btn_h + padding;
57- if (my >= y_start && my < y_start + btn_h) {
58- row = r;
59- break;
60- }
61- }
62-
63- if (row < 0) return NULL;
64-
65- for (int c = 0; c < NUM_COLS; ++c) {
66- int x_start = padding + c * (btn_w + padding);
67- if (mx >= x_start && mx < x_start + btn_w) {
68- col = c;
69- break;
70- }
71- }
72-
73- if (col < 0) return NULL;
74-
75- found.x = padding + col * (btn_w + padding);
76- found.y = start_y + (row - 1) * (btn_h + padding);
77- found.width = btn_w;
78- found.height = btn_h;
79- found.text = btn_labels[row][col];
80- found.pressed = 1;
81-
82- return &found;
83-}
84-
85-void append_to_input(UiSystem *ui, char c) {
86- if ((unsigned long)input_pos >= sizeof(curinput) - 2) return;
87- strcpy(ui->problemLabel.text, "");
88- curinput[input_pos++] = c;
89- curinput[input_pos] = '\0';
90-
91- strncpy(ui->resLabel.text, curinput, sizeof(ui->resLabel.text) - 1);
92- ui->resLabel.text[sizeof(ui->resLabel.text) - 1] = '\0';
93-}
94-
95-void clear_calculator(UiSystem *ui) {
96- strcpy(ui->problemLabel.text, "");
97- curinput[0] = '\0';
98- input_pos = 0;
99- strcpy(ui->resLabel.text, "0");
100-}
101-
102-double evaluate_simple(UiSystem *ui, const char *expr) {
103- double res = 0.0;
104- double cur = 0.0;
105- char op = '+';
106- int i = 0;
107- snprintf(ui->problemLabel.text, sizeof(ui->problemLabel.text), "%s=", expr);
108-
109- while (expr[i]) {
110- if (expr[i] == ' ') { i++; continue; }
111-
112- if (isdigit(expr[i]) || expr[i] == '.') {
113- char numbuf[32];
114- int j = 0;
115- while (isdigit(expr[i]) || expr[i] == '.') {
116- if (j < 31) numbuf[j++] = expr[i];
117- ++i;
118- }
119- numbuf[j] = '\0';
120- cur = atof(numbuf);
121- } else {
122- if (op == '+') res += cur;
123- else if (op == '-') res -= cur;
124- else if (op == '*') res *= cur;
125- else if (op == '/') {
126- if (cur == 0) return NAN;
127- res /= cur;
128- }
129-
130- op = expr[i];
131- ++i;
132- cur = 0.0;
133- }
134- }
135-
136- if (op == '+') res += cur;
137- else if (op == '-') res -= cur;
138- else if (op == '*') res *= cur;
139- else if (op == '/') {
140- if (cur == 0) return NAN;
141- res /= cur;
142- }
143-
144- return res;
145-}
146-
147-void control_expose(UiSystem *ui, XEvent *e) {
148- if (e->type != Expose && e->type != ConfigureNotify) return;
149-
150- XWindowAttributes attr;
151- XGetWindowAttributes(ui->display, ui->xwindow, &attr);
152- int w = attr.width;
153- int h = attr.height;
154-
155- if (ui->backbuf != None) XFreePixmap(ui->display, ui->backbuf);
156- ui->backbuf = XCreatePixmap(ui->display, ui->xwindow, w, h,
157- DefaultDepth(ui->display, DefaultScreen(ui->display)));
158-
159- if (ui->backbuf == None) ui->backbuf = ui->xwindow;
160- ui->target = ui->backbuf;
161-
162- XftDraw *backdraw = XftDrawCreate(ui->display, ui->backbuf,
163- DefaultVisual(ui->display, DefaultScreen(ui->display)),
164- DefaultColormap(ui->display, DefaultScreen(ui->display)));
165- if (!backdraw) {
166- fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n");
167- XFreePixmap(ui->display, ui->backbuf);
168- ui->backbuf = None;
169- return;
170- }
171-
172- XSetForeground(ui->display, ui->gc, BGCOL);
173- XFillRectangle(ui->display, ui->backbuf, ui->gc, 0, 0, w, h);
174-
175- float scale = get_scale(ui);
176- if (scale <= 0) scale = 1.f;
177-
178- int label_padding = (int)(20 * scale + .5f);
179-
180- // 出力
181- {
182- XGlyphInfo extents;
183- XftTextExtentsUtf8(ui->display, ui->resLabel.font,
184- (const FcChar8 *)ui->resLabel.text,
185- strlen(ui->resLabel.text), &extents);
186-
187- int tx = w - label_padding - extents.xOff;
188- int ty = (int)(180 * scale + .5f);
189-
190- XftDrawStringUtf8(
191- backdraw, &ui->resLabel.fg_color, ui->resLabel.font,
192- tx, ty, (const FcChar8 *)ui->resLabel.text, 64);
193- }
194-
195- {
196- XGlyphInfo extents;
197- XftTextExtentsUtf8(ui->display, ui->problemLabel.font,
198- (const FcChar8 *)ui->problemLabel.text,
199- strlen(ui->problemLabel.text), &extents);
200-
201- int tx = w - label_padding - extents.xOff;
202- int ty = (int)(80 * scale + .5f);
203-
204- XftDrawStringUtf8(
205- backdraw, &ui->problemLabel.fg_color, ui->problemLabel.font,
206- tx, ty, (const FcChar8 *)ui->problemLabel.text, 64);
207- }
208-
209- int width = (int)(DESIGN_BUTTON_WIDTH * scale + .5f);
210- int height = (int)(DESIGN_BUTTON_HEIGHT * scale + .5f);
211- int padding = (int)(DESIGN_PADDING * scale + .5f);
212- int start_y = (int)(DESIGN_BUTTON_AREA_Y * scale + .5f);
213-
214- if (initialized == 0) {
215- XftColorAllocName(ui->display,
216- DefaultVisual(ui->display, DefaultScreen(ui->display)),
217- DefaultColormap(ui->display, DefaultScreen(ui->display)),
218-#if defined(__OpenBSD__)
219- "#232320", &buttonColor);
220-#elif defined(__FreeBSD__)
221- "#232020", &buttonColor);
222-#endif
223- }
224- initialized = 1;
225-
226- for (int row = 1; row < NUM_ROWS; ++row) {
227- for (int col = 0; col < NUM_COLS; ++col) {
228- SuwaButton btn = {
229- .x = padding + col * (width + padding),
230- .y = start_y + (row - 1) * (height + padding),
231- .width = width,
232- .height = height,
233- .text = btn_labels[row][col],
234- .font = ui->font,
235- .bg_color = BTCOL,
236- .fg_color = buttonColor,
237- .pressed = 0
238- };
239-
240- drawbuttons(ui, &btn, backdraw);
241- }
242- }
243-
244- XCopyArea(ui->display, ui->backbuf, ui->xwindow, ui->gc, 0, 0, w, h, 0, 0);
245- XftDrawDestroy(backdraw);
246- XFlush(ui->display);
247-}
248-
249-void handle_button_press(UiSystem *ui, int mx, int my) {
250- SuwaButton *btn = find_button_at(ui, mx, my);
251- if (!btn) return;
252-
253- btn->pressed = 1;
254- XftDraw *backdraw = XftDrawCreate(ui->display, ui->backbuf,
255- DefaultVisual(ui->display, DefaultScreen(ui->display)),
256- DefaultColormap(ui->display, DefaultScreen(ui->display)));
257- if (!backdraw) {
258- fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n");
259- XFreePixmap(ui->display, ui->backbuf);
260- ui->backbuf = None;
261- return;
262- }
263-
264- drawbuttons(ui, btn, backdraw);
265- XFlush(ui->display);
266-}
267-
268-void handle_button_release(UiSystem *ui, int mx, int my) {
269- SuwaButton *btn = find_button_at(ui, mx, my);
270- if (!btn) return;
271-
272- btn->pressed = 0;
273- XftDraw *backdraw = XftDrawCreate(ui->display, ui->backbuf,
274- DefaultVisual(ui->display, DefaultScreen(ui->display)),
275- DefaultColormap(ui->display, DefaultScreen(ui->display)));
276- if (!backdraw) {
277- fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n");
278- XFreePixmap(ui->display, ui->backbuf);
279- ui->backbuf = None;
280- return;
281- }
282-
283- ui->target = ui->backbuf;
284- drawbuttons(ui, btn, backdraw);
285- XftDrawDestroy(backdraw);
286- XFlush(ui->display);
287-
288- const char *label = btn->text;
289- if (strcmp(label, "C") == 0) {
290- clear_calculator(ui);
291- } else if (strcmp(label, "×") == 0) {
292- append_to_input(ui, '*');
293- } else if (strcmp(label, "÷") == 0) {
294- append_to_input(ui, '/');
295- } else if (strcmp(label, "<") == 0) {
296- curinput[--input_pos] = '\0';
297- strcpy(ui->resLabel.text, curinput[0] ? curinput : "0");
298- } else if (strcmp(label, "=") == 0) {
299- double res = evaluate_simple(ui, curinput);
300- if (isnan(res)) strncpy(ui->resLabel.text, "Error", 5);
301- else snprintf(ui->resLabel.text, sizeof(ui->resLabel.text), "%.8g", res);
302- } else if (strlen(label) == 1) {
303- append_to_input(ui, label[0]);
304- }
305-
306- control_expose(ui, &(XEvent){.type = Expose});
307-}
308-
309-void handle_key_press(UiSystem *ui, XEvent *event) {
310- KeySym keysym;
311- char buf[32];
312- int len;
313- (void)len;
314-
315- keysym = XLookupKeysym(&event->xkey, 0);
316- len = XLookupString(&event->xkey, buf, sizeof(buf), &keysym, NULL);
317-
318- if (keysym == XK_Q) {
319- ui->isrunning = 0;
320- return;
321- }
322-
323- if (keysym == XK_B) {
324- puts("標準電卓画面");
325- }
326-
327- if (keysym == XK_S) {
328- puts("関数電卓画面");
329- }
330-
331- if (keysym == XK_P) {
332- puts("プログラマー電卓画面");
333- }
334-
335- if (keysym == XK_H) {
336- puts("履歴画面");
337- }
338-
339- if (keysym == XK_h) {
340- puts("ヘルプ画面");
341- puts("-----");
342- puts("Q = 終了 Exit");
343- puts("X = 1文字の消し Remove 1 character");
344- puts("C = クリ了 Clear");
345- puts("h = ヘルプ画面 Help screen");
346- puts("H = 履歴画面 History screen");
347- puts("B = 標準電卓画面 Basic calculator screen");
348- puts("S = 関数電卓画面 Scientific calculator screen");
349- puts("P = プログラマー電卓画面 Programmer calculator screen");
350- }
351-
352- if (keysym == XK_C) {
353- clear_calculator(ui);
354- goto redraw;
355- }
356-
357- if (keysym == XK_Return || keysym == XK_KP_Enter) {
358- double res = evaluate_simple(ui, curinput);
359- if (isnan(res)) strncpy(ui->resLabel.text, "Error", 5);
360- else snprintf(ui->resLabel.text, sizeof(ui->resLabel.text), "%.8g", res);
361- goto redraw;
362- }
363-
364- if ((keysym == XK_BackSpace && input_pos > 0)
365- || (keysym == XK_Delete && input_pos > 0)
366- || (keysym == XK_X && input_pos > 0)) {
367- curinput[--input_pos] = '\0';
368- strcpy(ui->resLabel.text, curinput[0] ? curinput : "0");
369- goto redraw;
370- }
371-
372- char c = 0;
373- if (keysym >= XK_0 && keysym <= XK_9) c = '0' + (keysym - XK_0);
374- else if (keysym >= XK_KP_0 && keysym <= XK_KP_9) c = '0' + (keysym - XK_0);
375- else if (keysym == XK_KP_Add || keysym == XK_plus) c = '+';
376- else if (keysym == XK_KP_Subtract || keysym == XK_minus) c = '-';
377- else if (keysym == XK_KP_Multiply || keysym == XK_asterisk) c = '*';
378- else if (keysym == XK_KP_Divide || keysym == XK_slash) c = '/';
379- else if (keysym == XK_KP_Decimal || keysym == XK_period) c = '.';
380-
381- if (c) {
382- append_to_input(ui, c);
383- goto redraw;
384- }
385-
386- return;
387-
388-redraw:
389- control_expose(ui, &(XEvent){.type = Expose});
390-}
391-
392-// void handle_mouse_hover(UiSystem *ui, XEvent *event) {
393-// SuwaButton *hover = find_button_at(event->xmotion.x, event->xmotion.y);
394-
395-// if (hover != hovered_btn) {
396-// if (hovered_btn) {
397-// hovered_btn->pressed = 0;
398-// redraw_single_button(&ui, hovered_btn);
399-// }
400-// hovered_btn = hover;
401-
402-// if (hovered_btn) {
403-// redraw_single_button(&ui, hovered_btn);
404-// }
405-// }
406-// }
削除 src/control.h
@@ -1,9 +0,0 @@
1-#pragma once
2-
3-#include "ui.h"
4-
5-void control_expose(UiSystem *ui, XEvent *event);
6-void handle_button_press(UiSystem *ui, int mx, int my);
7-void handle_button_release(UiSystem *ui, int mx, int my);
8-void handle_key_press(UiSystem *ui, XEvent *event);
9-void handle_mouse_hover(UiSystem *ui, XEvent *event);
削除 src/display.c
@@ -1,28 +0,0 @@
1-#include "display.h"
2-
3-#include <string.h>
4-
5-void drawbuttons(UiSystem *ui, SuwaButton *btn, XftDraw *xftdraw) {
6- unsigned long curbg = btn->pressed ? BTSEL : btn->bg_color;
7- XSetForeground(ui->display, ui->gc, curbg);
8- XFillRectangle(ui->display, ui->target, ui->gc, btn->x, btn->y,
9- btn->width, btn->height);
10-
11- // 文字の中央に
12- if (btn->text && ui->font && xftdraw) {
13- const FcChar8 *str = (const FcChar8 *)btn->text;
14- int len = strlen((const char *)str);
15-
16- XGlyphInfo extents;
17- XftTextExtentsUtf8(ui->display, ui->font, str, len, &extents);
18-
19- int text_w = extents.xOff;
20- int text_h = ui->font->ascent + ui->font->descent;
21-
22- int tx = btn->x + (btn->width - text_w) / 2;
23- int ty = btn->y + (btn->height - text_h) / 2 + ui->font->ascent;
24-
25- XftDrawStringUtf8(xftdraw, &btn->fg_color, ui->font, tx, ty,
26- (FcChar8 *)btn->text, len);
27- }
28-}
削除 src/display.h
@@ -1,5 +0,0 @@
1-#pragma once
2-
3-#include "ui.h"
4-
5-void drawbuttons(UiSystem *ui, SuwaButton *btn, XftDraw *xftdraw);
削除 src/ui.h
@@ -1,53 +0,0 @@
1-#pragma once
2-
3-#include <X11/Xlib.h>
4-#include <X11/Xft/Xft.h>
5-
6-#define FGCOL 0xfcfcfc
7-#if defined(__OpenBSD__)
8-#define BGCOL 0x232320
9-#define BTSEL 0xb8b515
10-#define BTCOL 0xf1ed25
11-#define BTHVR 0xecea71
12-#elif defined(__FreeBSD__)
13-#define BGCOL 0x232020
14-#define BTSEL 0xb61729
15-#define BTCOL 0xee4030
16-#define BTHVR 0xf35869
17-#endif
18-
19-typedef struct {
20- int x, y, width, height;
21-} SuwaWindow;
22-
23-typedef struct {
24- int x, y, width, height;
25- const char *text;
26- XftFont *font;
27- int bg_color;
28- XftColor fg_color;
29- int pressed; // 0 = 普通、1 = 押している
30-} SuwaButton;
31-
32-typedef struct {
33- int x, y, width, height;
34- char text[64];
35- XftFont *font;
36- XftColor fg_color;
37-} SuwaLabel;
38-
39-typedef struct {
40- int isrunning;
41- Display *display;
42- Window xwindow;
43- Drawable target;
44- GC gc;
45- Visual visual;
46- XftColor color;
47- XftFont *font;
48- Colormap colormap;
49- Pixmap backbuf;
50- SuwaWindow window;
51- SuwaLabel resLabel;
52- SuwaLabel problemLabel;
53-} UiSystem;
削除 src/utils.c
@@ -1,17 +0,0 @@
1-#include "utils.h"
2-
3-void cleanup(UiSystem *ui) {
4- // フォント
5- if (ui->resLabel.font) XftFontClose(ui->display, ui->resLabel.font);
6- if (ui->problemLabel.font) XftFontClose(ui->display, ui->problemLabel.font);
7- if (ui->font) XftFontClose(ui->display, ui->font);
8-
9- // その他
10- if (ui->gc) XFreeGC(ui->display, ui->gc);
11- if (ui->backbuf) {
12- XFreePixmap(ui->display, ui->backbuf);
13- ui->backbuf = None;
14- }
15- if (ui->xwindow) XDestroyWindow(ui->display, ui->xwindow);
16- if (ui->display) XCloseDisplay(ui->display);
17-}
削除 src/utils.h
@@ -1,5 +0,0 @@
1-#pragma once
2-
3-#include "ui.h"
4-
5-void cleanup(UiSystem *ui);