suwako / UnixCalc アーカイブ

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

最初コミット

諏訪子 <suwako@076.moe>、2026-01-17 07:24
コミット b20fa6cc3a68def3eaab8c1df83f8db338173121 ・ファイルを見る

17個のファイル、+756 -0

追加 .gitignore
@@ -0,0 +1,3 @@
1+ucalc
2+*.o
3+*.core
追加 CHANGELOG.md
@@ -0,0 +1,2 @@
1+# 0.0.0
2+* 標準電卓
追加 LICENSE.md
追加 Makefile
@@ -0,0 +1,57 @@
1+UNAME_S != uname -s
2+OS = ${UNAME_S}
3+
4+.if ${UNAME_S} == "OpenBSD"
5+OS = openbsd
6+.elif ${UNAME_S} == "FreeBSD"
7+OS = freebsd
8+.endif
9+
10+NAME != cat main.c | grep "const char \*sofname" | awk '{print $$5}' |\
11+ sed "s/\"//g" | sed "s/;//"
12+VERSION != cat main.c | grep "const char \*version" | awk '{print $$5}' |\
13+ sed "s/\"//g" | sed "s/;//"
14+
15+PREFIX = /usr/local
16+
17+CC = cc
18+FILES = main.c src/*.c
19+# FILES = main.c
20+
21+CFLAGS = -Wall -Wextra \
22+ -I./dep/include -I/usr/include -I/usr/local/include -I/usr/X11R6/include \
23+ -L./dep/lib/${OS} -L/usr/lib -L/usr/local/lib -L/usr/X11R6/lib \
24+ -I/usr/X11R6/include/freetype2 -I/usr/local/include/freetype2
25+
26+LDFLAGS = -lc -lX11 -lXft -lsys
27+SLIB = -lxcb -lthr -lfontconfig -lfreetype -lXrender -lXau -lXdmcp -lexpat -lintl \
28+ -lbz2 -lpng16 -lbrotlidec -lz -lm -lbrotlicommon
29+
30+all: debug
31+
32+debug:
33+ ${CC} -O0 -g ${CFLAGS} -o ${NAME} ${FILES} ${LDFLAGS}
34+ lldb -o run ${NAME}
35+
36+develop:
37+ ${CC} -O3 -g ${CFLAGS} -o ${NAME} ${FILES} ${LDFLAGS} ${SLIB}
38+ ./${NAME}
39+
40+release:
41+ ${CC} -O3 ${CFLAGS} -o ${NAME} ${FILES} -static ${LDFLAGS} ${SLIB}
42+ strip ${NAME}
43+ ./${NAME}
44+
45+clean:
46+ rm -rf ${NAME} *.core *.o
47+
48+install:
49+ cp ${NAME} ${PREFIX}/bin
50+ chmod +x ${PREFIX}/bin/${NAME}
51+
52+uninstall:
53+ rm -rf ${PREFIX}/bin/${NAME}
54+
55+dist:
56+
57+.PHONY: all debug develop release clean install uninstall dist
追加 README.md
@@ -0,0 +1,20 @@
1+# Unix Calc
2+
3+XLibre向け電卓。
4+Xorg以外、従属ライブラリを使いません。
5+
6+## インストールする方法 | Installation
7+### BSD
8+```sh
9+make
10+doas make install
11+```
12+
13+### Linux
14+```sh
15+make
16+doas bmake install
17+```
18+
19+![](scrot1.png)\
20+![](scrot2.png)
追加 main.c
@@ -0,0 +1,171 @@
1+#include <stdio.h>
2+
3+#include <X11/Xatom.h>
4+#include "src/utils.h"
5+#include "src/control.h"
6+
7+XftColor color, btncolor;
8+Colormap colormap;
9+XftFont *font;
10+XftFont *prbfont;
11+XftFont *disfont;
12+Visual *visual;
13+
14+const char *sofname = "ucalc";
15+const char *version = "0.0.0";
16+const char *disname = "Unix Calc";
17+
18+int main() {
19+ Display *display;
20+ Window window;
21+ XEvent event;
22+ int screen;
23+ GC gc = NULL;
24+ XGCValues values;
25+
26+ display = XOpenDisplay(NULL);
27+ if (display == NULL) {
28+ fprintf(stderr, "画面を開けられません。\n");
29+ exit(1);
30+ }
31+
32+ screen = DefaultScreen(display);
33+
34+ int sw = DisplayWidth(display, screen);
35+ int sh = DisplayHeight(display, screen);
36+ int window_x = (sw - window_width) / 3;
37+ int window_y = (sh - window_height) / 2;
38+
39+ window = XCreateSimpleWindow(display, RootWindow(display, screen),
40+ window_x, window_y, window_width, window_height, 1, BTCOL, BGCOL);
41+ if (!window) {
42+ cleanup(display, window, gc, &color, &btncolor, font, disfont, prbfont, colormap, visual, backbuf);
43+ fprintf(stderr, "ウィンドウを作成に失敗。\n");
44+ exit(1);
45+ }
46+
47+ backbuf = XCreatePixmap(display, window, window_width, window_height, DefaultDepth(display, screen));
48+
49+ Atom net_wm_window_type = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
50+ Atom dialog = XInternAtom(display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
51+
52+ XChangeProperty(display, window, net_wm_window_type, XA_ATOM, 32,
53+ PropModeReplace, (unsigned char *)&dialog, 1);
54+
55+ XStoreName(display, window, disname);
56+ Atom net_wm_name = XInternAtom(display, "_NET_WM_NAME", False);
57+ char displayname[16];
58+ snprintf(displayname, 16, "%s %s", disname, version);
59+ XChangeProperty(display, window, net_wm_name,
60+ XInternAtom(display, "UTF8_STRING", False), 8,
61+ PropModeReplace, (unsigned char *)displayname, strlen(displayname));
62+
63+ XClassHint *classHint = XAllocClassHint();
64+ if (classHint) {
65+ classHint->res_name = "unixcalc";
66+ classHint->res_class = "UnixCalc";
67+ XSetClassHint(display, window, classHint);
68+ XFree(classHint);
69+ }
70+
71+ XSetWindowBackground(display, window, BGCOL);
72+
73+ XSelectInput(display, window,
74+ ExposureMask
75+ | ButtonPressMask
76+ | ButtonReleaseMask
77+ | KeyPressMask
78+ // | PointerMotionMask
79+ // | ButtonMotionMask
80+ // | StructureNotifyMask
81+ );
82+
83+ gc = XCreateGC(display, window, 0, &values);
84+ if (!gc) {
85+ cleanup(display, window, gc, &color, &btncolor, font, disfont, prbfont, colormap, visual, backbuf);
86+ fprintf(stderr, "GCを作成に失敗。\n");
87+ exit(1);
88+ }
89+
90+ visual = DefaultVisual(display, screen);
91+
92+ colormap = XCreateColormap(display, window, visual, AllocNone);
93+ if (colormap == None) {
94+ cleanup(display, window, gc, &color, &btncolor, font, disfont, prbfont, colormap, visual, backbuf);
95+ fprintf(stderr, "カラーマップを作成に失敗。\n");
96+ exit(1);
97+ }
98+
99+ font = XftFontOpenName(display, screen, "Noto Sans CJK-12");
100+ if (!font) {
101+ cleanup(display, window, gc, &color, &btncolor, font, disfont, prbfont, colormap, visual, backbuf);
102+ fprintf(stderr, "フォントの読み込みに失敗。\n");
103+ exit(1);
104+ }
105+
106+ prbfont = XftFontOpenName(display, screen, "Noto Sans CJK-24");
107+ if (!prbfont) {
108+ cleanup(display, window, gc, &color, &btncolor, font, disfont, prbfont, colormap, visual, backbuf);
109+ fprintf(stderr, "問題フォントの読み込みに失敗。\n");
110+ exit(1);
111+ }
112+
113+ disfont = XftFontOpenName(display, screen, "Noto Sans CJK-72");
114+ if (!disfont) {
115+ cleanup(display, window, gc, &color, &btncolor, font, disfont, prbfont, colormap, visual, backbuf);
116+ fprintf(stderr, "解決フォントの読み込みに失敗。\n");
117+ exit(1);
118+ }
119+
120+ if (!XftColorAllocName(display, visual, colormap, "#232020", &color)) {
121+ cleanup(display, window, gc, &color, &btncolor, font, disfont, prbfont, colormap, visual, backbuf);
122+ fprintf(stderr, "色の役割に失敗。\n");
123+ exit(1);
124+ }
125+
126+ XMapWindow(display, window);
127+ {
128+ XWindowAttributes attr;
129+ XGetWindowAttributes(display, window, &attr);
130+ XEvent fake = { .type = Expose };
131+ fake.xexpose.window = window;
132+ fake.xexpose.width = attr.width;
133+ fake.xexpose.height = attr.height;
134+ control_expose(display, window, gc, &event, &color, font, disfont, prbfont);
135+ }
136+
137+ while (isrunning) {
138+ XNextEvent(display, &event);
139+
140+ switch (event.type) {
141+ case Expose:
142+ case ConfigureNotify:
143+ XClearWindow(display, window);
144+ control_expose(display, window, gc, &event, &color, font, disfont, prbfont);
145+ break;
146+ case ButtonPress:
147+ if (event.xbutton.button == Button1) {
148+ handle_button_press(display, gc, event.xbutton.x, event.xbutton.y, &color, font);
149+ break;
150+ }
151+ case ButtonRelease:
152+ if (event.xbutton.button == Button1) {
153+ handle_button_release(display, window, gc, event.xbutton.x, event.xbutton.y, &color, font, disfont, prbfont);
154+ break;
155+ }
156+ case KeyPress:
157+ handle_key_press(display, window, gc, &event, &color, font, disfont, prbfont);
158+ break;
159+ // case MotionNotify:
160+ // handle_mouse_hover(display, window, gc, &event, &color, font);
161+ // break;
162+ case ClientMessage:
163+ // WM_DELETE_WINDOW
164+ break;
165+ }
166+ }
167+
168+ cleanup(display, window, gc, &color, &btncolor, font, disfont, prbfont, colormap, visual, backbuf);
169+
170+ return 0;
171+}
追加 scrot1.png

バイナリファイル

追加 scrot2.png

バイナリファイル

追加 src/control.c
@@ -0,0 +1,389 @@
1+#include <X11/keysym.h>
2+#include <math.h>
3+#include <ctype.h>
4+
5+#include "control.h"
6+#include "display.h"
7+#include "program.h"
8+
9+static const char *btn_labels[][10] = {
10+ { NULL }, // 数字表示
11+ { "C", "±", "%", "÷" },
12+ { "7", "8", "9", "×" },
13+ { "4", "5", "6", "-" },
14+ { "1", "2", "3", "+" },
15+ { "0", ".", "=", "<" },
16+};
17+
18+#define NUM_ROWS 6
19+#define NUM_COLS 6
20+
21+static SuwaButton *find_button_at(int mx, int my) {
22+ static SuwaButton found = {0};
23+
24+ int btn_w = 93;
25+ int btn_h = 60;
26+ int padding = 2;
27+
28+ int row = -1;
29+ int col = -1;
30+
31+ for (int r = 1; r < NUM_ROWS; ++r) {
32+ int y_start = 224 + (r - 1) * btn_h + padding;
33+ if (my >= y_start && my < y_start + btn_h) {
34+ row = r;
35+ break;
36+ }
37+ }
38+
39+ if (row < 0) return NULL;
40+
41+ for (int c = 0; c < NUM_COLS; ++c) {
42+ int x_start = padding + c * (btn_w + padding);
43+ if (mx >= x_start && mx < x_start + btn_w) {
44+ col = c;
45+ break;
46+ }
47+ }
48+
49+ if (col < 0) return NULL;
50+
51+ found.x = padding + col * (btn_w + padding);
52+ found.y = 224 + (row - 1) * (btn_h + padding);
53+ found.width = btn_w;
54+ found.height = btn_h;
55+ found.label = btn_labels[row][col];
56+ found.pressed = 1;
57+
58+ return &found;
59+}
60+
61+void append_to_input(char c) {
62+ if ((unsigned long)input_pos >= sizeof(curinput) - 2) return;
63+ strcpy(displayprb, "");
64+ curinput[input_pos++] = c;
65+ curinput[input_pos] = '\0';
66+
67+ strncpy(displaytxt, curinput, sizeof(displaytxt) - 1);
68+ displaytxt[sizeof(displaytxt) - 1] = '\0';
69+}
70+
71+void clear_calculator(void) {
72+ strcpy(displayprb, "");
73+ curinput[0] = '\0';
74+ input_pos = 0;
75+ strcpy(displaytxt, "0");
76+}
77+
78+double evaluate_simple(const char *expr) {
79+ double res = 0.0;
80+ double cur = 0.0;
81+ char op = '+';
82+ int i = 0;
83+ snprintf(displayprb, sizeof(displayprb), "%s=", expr);
84+
85+ while (expr[i]) {
86+ if (expr[i] == ' ') { i++; continue; }
87+
88+ if (isdigit(expr[i]) || expr[i] == '.') {
89+ char numbuf[32];
90+ int j = 0;
91+ while (isdigit(expr[i]) || expr[i] == '.') {
92+ if (j < 31) numbuf[j++] = expr[i];
93+ ++i;
94+ }
95+ numbuf[j] = '\0';
96+ cur = atof(numbuf);
97+ } else {
98+ if (op == '+') res += cur;
99+ else if (op == '-') res -= cur;
100+ else if (op == '*') res *= cur;
101+ else if (op == '/') {
102+ if (cur == 0) return NAN;
103+ res /= cur;
104+ }
105+
106+ op = expr[i];
107+ ++i;
108+ cur = 0.0;
109+ }
110+ }
111+
112+ if (op == '+') res += cur;
113+ else if (op == '-') res -= cur;
114+ else if (op == '*') res *= cur;
115+ else if (op == '/') {
116+ if (cur == 0) return NAN;
117+ res /= cur;
118+ }
119+
120+ return res;
121+}
122+
123+void control_expose(Display *dpy, Window wnd, GC gc, XEvent *e, XftColor *color, XftFont *f, XftFont *df, XftFont *pf) {
124+ (void)color;
125+ if (e->type != Expose && e->type != ConfigureNotify) return;
126+
127+ XWindowAttributes attr;
128+ XGetWindowAttributes(dpy, wnd, &attr);
129+ int w = attr.width;
130+ int h = attr.height;
131+
132+ if (backbuf != None) {
133+ XFreePixmap(dpy, backbuf);
134+ }
135+
136+ backbuf = XCreatePixmap(dpy, wnd, w, h, DefaultDepth(dpy, DefaultScreen(dpy)));
137+
138+ if (backbuf == None) {
139+ backbuf = wnd;
140+ }
141+
142+ XftDraw *backdraw = XftDrawCreate(dpy, backbuf,
143+ DefaultVisual(dpy, DefaultScreen(dpy)), DefaultColormap(dpy, DefaultScreen(dpy)));
144+ if (!backdraw) {
145+ fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n");
146+ XFreePixmap(dpy, backbuf);
147+ backbuf = None;
148+ return;
149+ }
150+
151+ XSetForeground(dpy, gc, BGCOL);
152+ XFillRectangle(dpy, backbuf, gc, 0, 0, w, h);
153+
154+ // 出力
155+ if (displaytxt[0] != '\0' && df) {
156+ const FcChar8 *text = (const FcChar8 *)displaytxt;
157+ int len = strlen((const char *)text);
158+
159+ XGlyphInfo extents;
160+ XftTextExtentsUtf8(dpy, df, text, len, &extents);
161+
162+ int tx = w - 20 - extents.xOff;
163+ int ty = 160;
164+
165+ XftColor discol;
166+ XftColorAllocName(dpy, DefaultVisual(dpy, DefaultScreen(dpy)),
167+ DefaultColormap(dpy, DefaultScreen(dpy)),
168+ "#ee4030", &discol);
169+
170+ XftDrawStringUtf8(backdraw, &discol, df, tx, ty, text, len);
171+
172+ XftColorFree(dpy, DefaultVisual(dpy, DefaultScreen(dpy)),
173+ DefaultColormap(dpy, DefaultScreen(dpy)), &discol);
174+ }
175+
176+ if (displayprb[0] != '\0' && pf) {
177+ const FcChar8 *text = (const FcChar8 *)displayprb;
178+ int len = strlen((const char *)text);
179+
180+ XGlyphInfo extents;
181+ XftTextExtentsUtf8(dpy, pf, text, len, &extents);
182+
183+ int tx = w - 20 - extents.xOff;
184+ int ty = 80;
185+
186+ XftColor discol;
187+ XftColorAllocName(dpy, DefaultVisual(dpy, DefaultScreen(dpy)),
188+ DefaultColormap(dpy, DefaultScreen(dpy)),
189+ "#b61729", &discol);
190+
191+ XftDrawStringUtf8(backdraw, &discol, pf, tx, ty, text, len);
192+
193+ XftColorFree(dpy, DefaultVisual(dpy, DefaultScreen(dpy)),
194+ DefaultColormap(dpy, DefaultScreen(dpy)), &discol);
195+ }
196+
197+ int width = 93;
198+ int height = 60;
199+ int padding = 2;
200+ printf("ウィンドウ: (%dx%d)\n", attr.width, attr.height);
201+
202+ for (int row = 0; row < 6; ++row) {
203+ int y = 162 + row * (height + padding);
204+ if (y + height > h) break;
205+
206+ int cols = 4;
207+
208+ for (int col = 0; col < cols; ++col) {
209+ const char *label = btn_labels[row][col];
210+ if (!label) continue;
211+
212+ int x = padding + col * (width + padding);
213+
214+ SuwaButton btn;
215+ btn.x = x;
216+ btn.y = y;
217+ btn.width = width;
218+ btn.height = height;
219+ btn.label = label;
220+ btn.pressed = 0;
221+
222+ drawbuttons(dpy, backbuf, gc, &btn, backdraw, color, f);
223+ }
224+ }
225+
226+ XCopyArea(dpy, backbuf, wnd, gc, 0, 0, w, h, 0, 0);
227+ XftDrawDestroy(backdraw);
228+ XFlush(dpy);
229+}
230+
231+void handle_button_press(Display *dpy, GC gc, int mx, int my, XftColor *col, XftFont *f) {
232+ SuwaButton *btn = find_button_at(mx, my);
233+ if (!btn) return;
234+
235+ btn->pressed = 1;
236+ XftDraw *backdraw = XftDrawCreate(dpy, backbuf,
237+ DefaultVisual(dpy, DefaultScreen(dpy)), DefaultColormap(dpy, DefaultScreen(dpy)));
238+ if (!backdraw) {
239+ fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n");
240+ XFreePixmap(dpy, backbuf);
241+ backbuf = None;
242+ return;
243+ }
244+
245+ drawbuttons(dpy, backbuf, gc, btn, backdraw, col, f);
246+ XFlush(dpy);
247+}
248+
249+void handle_button_release(Display *dpy, Window wnd, GC gc, int mx, int my, XftColor *col, XftFont *f, XftFont *df, XftFont *pf) {
250+ SuwaButton *btn = find_button_at(mx, my);
251+ if (!btn) return;
252+
253+ btn->pressed = 0;
254+
255+ XftDraw *backdraw = XftDrawCreate(dpy, backbuf,
256+ DefaultVisual(dpy, DefaultScreen(dpy)), DefaultColormap(dpy, DefaultScreen(dpy)));
257+ if (!backdraw) {
258+ fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n");
259+ XFreePixmap(dpy, backbuf);
260+ backbuf = None;
261+ return;
262+ }
263+ drawbuttons(dpy, backbuf, gc, btn, backdraw, col, f);
264+ XftDrawDestroy(backdraw);
265+ XFlush(dpy);
266+
267+ const char *label = btn->label;
268+ if (strcmp(label, "C") == 0) {
269+ clear_calculator();
270+ } else if (strcmp(label, "<") == 0) {
271+ curinput[--input_pos] = '\0';
272+ strcpy(displaytxt, curinput[0] ? curinput : "0");
273+ } else if (strcmp(label, "=") == 0) {
274+ double res = evaluate_simple(curinput);
275+ if (isnan(res)) {
276+ strncpy(displaytxt, "Error", 5);
277+ } else {
278+ snprintf(displaytxt, sizeof(displaytxt), "%.8g", res);
279+ strncpy(curinput, displaytxt, strlen(displaytxt));
280+ input_pos = strlen(curinput);
281+ }
282+ } else if (strlen(label) == 1) {
283+ append_to_input(label[0]);
284+ }
285+
286+ control_expose(dpy, wnd, gc, &(XEvent){.type = Expose}, col, f, df, pf);
287+}
288+
289+void handle_key_press(Display *dpy, Window wnd, GC gc, XEvent *event, XftColor *col, XftFont *f, XftFont *df, XftFont *pf) {
290+ KeySym keysym;
291+ char buf[32];
292+ int len;
293+ (void)len;
294+
295+ keysym = XLookupKeysym(&event->xkey, 0);
296+ len = XLookupString(&event->xkey, buf, sizeof(buf), &keysym, NULL);
297+
298+ if (keysym == XK_Q) {
299+ isrunning = 0;
300+ return;
301+ }
302+
303+ if (keysym == XK_B) {
304+ puts("標準電卓画面");
305+ }
306+
307+ if (keysym == XK_S) {
308+ puts("関数電卓画面");
309+ }
310+
311+ if (keysym == XK_P) {
312+ puts("プログラマー電卓画面");
313+ }
314+
315+ if (keysym == XK_H) {
316+ puts("履歴画面");
317+ }
318+
319+ if (keysym == XK_h) {
320+ puts("ヘルプ画面");
321+ puts("-----");
322+ puts("Q = 終了 Exit");
323+ puts("X = 1文字の消し Remove 1 character");
324+ puts("C = クリ了 Clear");
325+ puts("h = ヘルプ画面 Help screen");
326+ puts("H = 履歴画面 History screen");
327+ puts("B = 標準電卓画面 Basic calculator screen");
328+ puts("S = 関数電卓画面 Scientific calculator screen");
329+ puts("P = プログラマー電卓画面 Programmer calculator screen");
330+ }
331+
332+ if (keysym == XK_C) {
333+ clear_calculator();
334+ goto redraw;
335+ }
336+
337+ if (keysym == XK_Return || keysym == XK_KP_Enter) {
338+ double res = evaluate_simple(curinput);
339+ if (isnan(res)) {
340+ strncpy(displaytxt, "Error", 5);
341+ } else {
342+ snprintf(displaytxt, sizeof(displaytxt), "%.8g", res);
343+ }
344+ goto redraw;
345+ }
346+
347+ if ((keysym == XK_BackSpace && input_pos > 0)
348+ || (keysym == XK_Delete && input_pos > 0)
349+ || (keysym == XK_X && input_pos > 0)) {
350+ curinput[--input_pos] = '\0';
351+ strcpy(displaytxt, curinput[0] ? curinput : "0");
352+ goto redraw;
353+ }
354+
355+ char c = 0;
356+ if (keysym >= XK_0 && keysym <= XK_9) c = '0' + (keysym - XK_0);
357+ else if (keysym >= XK_KP_0 && keysym <= XK_KP_9) c = '0' + (keysym - XK_0);
358+ else if (keysym == XK_KP_Add || keysym == XK_plus) c = '+';
359+ else if (keysym == XK_KP_Subtract || keysym == XK_minus) c = '-';
360+ else if (keysym == XK_KP_Multiply || keysym == XK_asterisk) c = '*';
361+ else if (keysym == XK_KP_Divide || keysym == XK_slash) c = '/';
362+ else if (keysym == XK_KP_Decimal || keysym == XK_period) c = '.';
363+
364+ if (c) {
365+ append_to_input(c);
366+ goto redraw;
367+ }
368+
369+ return;
370+
371+redraw:
372+ control_expose(dpy, wnd, gc, &(XEvent){.type = Expose}, col, f, df, pf);
373+}
374+
375+// void handle_mouse_hover(Display *dpy, Window wnd, GC gc, XEvent *event, XftColor *col, XftFont *f) {
376+// SuwaButton *hover = find_button_at(event->xmotion.x, event->xmotion.y);
377+
378+// if (hover != hovered_btn) {
379+// if (hovered_btn) {
380+// hovered_btn->pressed = 0;
381+// redraw_single_button(dpy, wnd, gc, col, f, hovered_btn);
382+// }
383+// hovered_btn = hover;
384+
385+// if (hovered_btn) {
386+// redraw_single_button(dpy, wnd, gc, col, f, hovered_btn);
387+// }
388+// }
389+// }
追加 src/control.h
@@ -0,0 +1,10 @@
1+#pragma once
2+
3+#include <X11/Xlib.h>
4+#include <X11/Xft/Xft.h>
5+
6+void control_expose(Display *dpy, Window wnd, GC gc, XEvent *event, XftColor *color, XftFont *f, XftFont *df, XftFont *pf);
7+void handle_button_press(Display *dpy, GC gc, int mx, int my, XftColor *col, XftFont *f);
8+void handle_button_release(Display *dpy, Window wnd, GC gc, int mx, int my, XftColor *col, XftFont *f, XftFont *df, XftFont *pf);
9+void handle_key_press(Display *dpy, Window wnd, GC gc, XEvent *event, XftColor *col, XftFont *f, XftFont *df, XftFont *pf);
10+void handle_mouse_hover(Display *dpy, Window wnd, GC gc, XEvent *event, XftColor *col, XftFont *f);
追加 src/display.c
@@ -0,0 +1,30 @@
1+#include "display.h"
2+#include "program.h"
3+
4+#include <string.h>
5+
6+void drawbuttons(Display *dpy, Drawable target, GC gc, SuwaButton *btn,
7+ XftDraw *xftdraw, XftColor *textcolor, XftFont *font) {
8+ unsigned long curbg = btn->pressed ? BTSEL : BTCOL;
9+ XSetForeground(dpy, gc, curbg);
10+ XFillRectangle(dpy, target, gc, btn->x, btn->y, btn->width, btn->height);
11+
12+ // 文字の中央に
13+ if (btn->label && font && xftdraw) {
14+ const FcChar8 *str = (const FcChar8 *)btn->label;
15+ int len = strlen((const char *)str);
16+
17+ XGlyphInfo extents;
18+ XftTextExtentsUtf8(dpy, font, str, len, &extents);
19+
20+ int text_w = extents.xOff;
21+ int text_h = font->ascent + font->descent;
22+
23+ int tx = btn->x + (btn->width - text_w) / 2;
24+ int ty = btn->y + (btn->height - text_h) / 2 + font->ascent;
25+
26+ // tx -= extents.x;
27+
28+ XftDrawStringUtf8(xftdraw, textcolor, font, tx, ty, (FcChar8 *)btn->label, len);
29+ }
30+}
追加 src/display.h
@@ -0,0 +1,6 @@
1+#pragma once
2+
3+#include "program.h"
4+
5+void drawbuttons(Display *dpy, Drawable target, GC gc, SuwaButton *btn,
6+ XftDraw *xftdraw, XftColor *textcolor, XftFont *font);
追加 src/program.c
@@ -0,0 +1,12 @@
1+#include "program.h"
2+
3+int window_width = 382;
4+int window_height = 534;
5+int isrunning = 1;
6+char displayprb[128] = "";
7+char curinput[256] = {0};
8+char displaytxt[64] = "0";
9+int input_pos = 0;
10+Pixmap backbuf = None;
11+SuwaButton *hovered_btn = NULL;
12+SuwaButton *pressed_btn = NULL;
追加 src/program.h
@@ -0,0 +1,22 @@
1+#pragma once
2+
3+#include <X11/Xlib.h>
4+#include <X11/Xft/Xft.h>
5+#include "ui.h"
6+
7+#define FGCOL 0xfcfcfc
8+#define BGCOL 0x232020
9+#define BTSEL 0xb61729
10+#define BTCOL 0xee4030
11+#define BTHVR 0xf35869
12+
13+extern int window_width;
14+extern int window_height;
15+extern int isrunning;
16+extern char displayprb[128];
17+extern char curinput[256];
18+extern char displaytxt[64];
19+extern int input_pos;
20+extern Pixmap backbuf;
21+extern SuwaButton *hovered_btn;
22+extern SuwaButton *pressed_btn;
追加 src/ui.h
@@ -0,0 +1,7 @@
1+#pragma once
2+
3+typedef struct {
4+ int x, y, width, height;
5+ const char *label;
6+ int pressed; // 0 = 普通、1 = 押している
7+} SuwaButton;
追加 src/utils.c
@@ -0,0 +1,19 @@
1+#include "utils.h"
2+
3+void cleanup(Display *display, Window window, GC gc,
4+ XftColor *color, XftColor *btncolor, XftFont *font,
5+ XftFont *disfont, XftFont *prbfont, Colormap colormap, Visual *visual,
6+ Pixmap backbuf) {
7+ if (btncolor) XftColorFree(display, visual, colormap, btncolor);
8+ if (color) XftColorFree(display, visual, colormap, color);
9+ if (disfont) XftFontClose(display, disfont);
10+ if (prbfont) XftFontClose(display, prbfont);
11+ if (font) XftFontClose(display, font);
12+ if (gc) XFreeGC(display, gc);
13+ if (backbuf) {
14+ XFreePixmap(display, backbuf);
15+ backbuf = None;
16+ }
17+ if (window) XDestroyWindow(display, window);
18+ if (display) XCloseDisplay(display);
19+}
追加 src/utils.h
@@ -0,0 +1,8 @@
1+#pragma once
2+
3+#include "program.h"
4+
5+void cleanup(Display *display, Window window, GC gc,
6+ XftColor *color, XftColor *btncolor, XftFont *font,
7+ XftFont *disfont, XftFont *prbfont, Colormap colormap, Visual *visual,
8+ Pixmap backbuf);