suwako / UnixCalc アーカイブ

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

最適化

諏訪子 <suwako@076.moe>、2026-01-17 23:52
コミット 5f5f8248d8002419fe5fd934b46269966091323c ・親 c6f2982 ・ファイルを見る

11個のファイル、+213 -172

変更 Makefile
@@ -14,9 +14,11 @@
1414
1515 PREFIX = /usr/local
1616
17+# cc | clang | gcc
1718 CC = cc
19+# lldb | gdb
20+DEBUGGER = lldb
1821 FILES = main.c src/*.c
19-# FILES = main.c
2022
2123 CFLAGS = -Wall -Wextra \
2224 -I./dep/include -I/usr/include -I/usr/local/include -I/usr/X11R6/include \
@@ -31,7 +33,7 @@
3133
3234 debug:
3335 ${CC} -O0 -g ${CFLAGS} -o ${NAME} ${FILES} ${LDFLAGS}
34- lldb -o run ${NAME}
36+ ${DEBUGGER} -o run ${NAME}
3537
3638 develop:
3739 ${CC} -O3 -g ${CFLAGS} -o ${NAME} ${FILES} ${LDFLAGS} ${SLIB}
変更 README.md
@@ -1,19 +1,19 @@
11 # Unix Calc
22
3-XLibre向け電卓。
3+XLibre向け電卓。\
44 Xorg以外、従属ライブラリを使いません。
55
66 ## インストールする方法 | Installation
77 ### BSD
88 ```sh
99 make
10-doas make install
10+doas make install release
1111 ```
1212
1313 ### Linux
1414 ```sh
1515 make
16-doas bmake install
16+doas bmake install release
1717 ```
1818
1919 ![](scrot1.png)\
変更 main.c
@@ -10,6 +10,9 @@
1010
1111 int main() {
1212 UiSystem ui;
13+ ui.isrunning = 1;
14+ ui.window.width = 382;
15+ ui.window.height = 534;
1316 XEvent event;
1417 int screen;
1518 XGCValues values;
@@ -24,33 +27,36 @@
2427
2528 int sw = DisplayWidth(ui.display, screen);
2629 int sh = DisplayHeight(ui.display, screen);
27- int window_x = (sw - window_width) / 3;
28- int window_y = (sh - window_height) / 2;
30+ ui.window.x = (sw - ui.window.width) / 3;
31+ ui.window.y = (sh - ui.window.height) / 2;
2932
30- ui.window = XCreateSimpleWindow(ui.display,
33+ ui.xwindow = XCreateSimpleWindow(ui.display,
3134 RootWindow(ui.display, screen),
32- window_x, window_y, window_width, window_height, 1, BTCOL, BGCOL);
33- if (!ui.window) {
35+ ui.window.x, ui.window.y,
36+ ui.window.width, ui.window.height,
37+ 1, BTCOL, BGCOL);
38+ if (!ui.xwindow) {
3439 cleanup(&ui);
3540 fprintf(stderr, "ウィンドウを作成に失敗。\n");
3641 exit(1);
3742 }
3843
39- ui.backbuf = XCreatePixmap(ui.display, ui.window, window_width, window_height,
44+ ui.backbuf = XCreatePixmap(ui.display, ui.xwindow,
45+ ui.window.width, ui.window.height,
4046 DefaultDepth(ui.display, screen));
4147 ui.target = ui.backbuf;
4248
4349 Atom net_wm_window_type = XInternAtom(ui.display, "_NET_WM_WINDOW_TYPE", False);
4450 Atom dialog = XInternAtom(ui.display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
4551
46- XChangeProperty(ui.display, ui.window, net_wm_window_type, XA_ATOM, 32,
52+ XChangeProperty(ui.display, ui.xwindow, net_wm_window_type, XA_ATOM, 32,
4753 PropModeReplace, (unsigned char *)&dialog, 1);
4854
49- XStoreName(ui.display, ui.window, disname);
55+ XStoreName(ui.display, ui.xwindow, disname);
5056 Atom net_wm_name = XInternAtom(ui.display, "_NET_WM_NAME", False);
5157 char displayname[16];
5258 snprintf(displayname, 16, "%s %s", disname, version);
53- XChangeProperty(ui.display, ui.window, net_wm_name,
59+ XChangeProperty(ui.display, ui.xwindow, net_wm_name,
5460 XInternAtom(ui.display, "UTF8_STRING", False), 8,
5561 PropModeReplace, (unsigned char *)displayname, strlen(displayname));
5662
@@ -58,13 +64,13 @@
5864 if (classHint) {
5965 classHint->res_name = "unixcalc";
6066 classHint->res_class = "UnixCalc";
61- XSetClassHint(ui.display, ui.window, classHint);
67+ XSetClassHint(ui.display, ui.xwindow, classHint);
6268 XFree(classHint);
6369 }
6470
65- XSetWindowBackground(ui.display, ui.window, BGCOL);
71+ XSetWindowBackground(ui.display, ui.xwindow, BGCOL);
6672
67- XSelectInput(ui.display, ui.window,
73+ XSelectInput(ui.display, ui.xwindow,
6874 ExposureMask
6975 | ButtonPressMask
7076 | ButtonReleaseMask
@@ -74,7 +80,7 @@
7480 // | StructureNotifyMask
7581 );
7682
77- ui.gc = XCreateGC(ui.display, ui.window, 0, &values);
83+ ui.gc = XCreateGC(ui.display, ui.xwindow, 0, &values);
7884 if (!ui.gc) {
7985 cleanup(&ui);
8086 fprintf(stderr, "GCを作成に失敗。\n");
@@ -83,31 +89,78 @@
8389
8490 ui.visual = *DefaultVisual(ui.display, screen);
8591
86- ui.colormap = XCreateColormap(ui.display, ui.window, &ui.visual, AllocNone);
92+ ui.colormap = XCreateColormap(ui.display, ui.xwindow, &ui.visual, AllocNone);
8793 if (ui.colormap == None) {
8894 cleanup(&ui);
8995 fprintf(stderr, "カラーマップを作成に失敗。\n");
9096 exit(1);
9197 }
9298
93- ui.font = XftFontOpenName(ui.display, screen, "Noto Sans CJK-12");
94- if (!ui.font) {
95- cleanup(&ui);
96- fprintf(stderr, "フォントの読み込みに失敗。\n");
97- exit(1);
99+ XWindowAttributes attr;
100+ XGetWindowAttributes(ui.display, ui.xwindow, &attr);
101+ int w = attr.width;
102+
103+ {
104+ ui.resLabel.font = XftFontOpenName(ui.display, screen, "Noto Sans CJK-72");
105+ if (!ui.resLabel.font) {
106+ cleanup(&ui);
107+ fprintf(stderr, "解決フォントの読み込みに失敗。\n");
108+ exit(1);
109+ }
110+
111+ ui.resLabel.text[0] = '0';
112+ ui.resLabel.text[1] = '\0';
113+
114+ const FcChar8 *text = (const FcChar8 *)ui.resLabel.text;
115+ int len = strlen((const char *)text);
116+
117+ XGlyphInfo extents;
118+ XftTextExtentsUtf8(ui.display, ui.resLabel.font, text, len, &extents);
119+
120+ ui.resLabel.x = w - 20 - extents.xOff;
121+ ui.resLabel.y = 180;
122+ if (!XftColorAllocName(ui.display,
123+ DefaultVisual(ui.display, DefaultScreen(ui.display)),
124+ DefaultColormap(ui.display, DefaultScreen(ui.display)),
125+ "#ee4030", &ui.resLabel.fg_color)) {
126+ cleanup(&ui);
127+ fprintf(stderr, "色の役割に失敗。\n");
128+ exit(1);
129+ }
98130 }
99131
100- ui.prbfont = XftFontOpenName(ui.display, screen, "Noto Sans CJK-24");
101- if (!ui.prbfont) {
102- cleanup(&ui);
103- fprintf(stderr, "問題フォントの読み込みに失敗。\n");
104- exit(1);
132+ {
133+ ui.problemLabel.font = XftFontOpenName(ui.display, screen, "Noto Sans CJK-24");
134+ if (!ui.problemLabel.font) {
135+ cleanup(&ui);
136+ fprintf(stderr, "問題フォントの読み込みに失敗。\n");
137+ exit(1);
138+ }
139+
140+ ui.problemLabel.text[0] = '\0';
141+
142+ const FcChar8 *text = (const FcChar8 *)ui.problemLabel.text;
143+ int len = strlen((const char *)text);
144+
145+ XGlyphInfo extents;
146+ XftTextExtentsUtf8(ui.display, ui.problemLabel.font, text, len, &extents);
147+
148+ ui.problemLabel.x = w - 20 - extents.xOff;
149+ ui.problemLabel.y = 80;
150+ if (!XftColorAllocName(ui.display,
151+ DefaultVisual(ui.display, DefaultScreen(ui.display)),
152+ DefaultColormap(ui.display, DefaultScreen(ui.display)),
153+ "#b61729", &ui.problemLabel.fg_color)) {
154+ cleanup(&ui);
155+ fprintf(stderr, "色の役割に失敗。\n");
156+ exit(1);
157+ }
105158 }
106159
107- ui.disfont = XftFontOpenName(ui.display, screen, "Noto Sans CJK-72");
108- if (!ui.disfont) {
160+ ui.font = XftFontOpenName(ui.display, screen, "Noto Sans CJK-12");
161+ if (!ui.font) {
109162 cleanup(&ui);
110- fprintf(stderr, "解決フォントの読み込みに失敗。\n");
163+ fprintf(stderr, "フォントの読み込みに失敗。\n");
111164 exit(1);
112165 }
113166
@@ -117,24 +170,24 @@
117170 exit(1);
118171 }
119172
120- XMapWindow(ui.display, ui.window);
173+ XMapWindow(ui.display, ui.xwindow);
121174 {
122175 XWindowAttributes attr;
123- XGetWindowAttributes(ui.display, ui.window, &attr);
176+ XGetWindowAttributes(ui.display, ui.xwindow, &attr);
124177 XEvent fake = { .type = Expose };
125- fake.xexpose.window = ui.window;
178+ fake.xexpose.window = ui.xwindow;
126179 fake.xexpose.width = attr.width;
127180 fake.xexpose.height = attr.height;
128181 control_expose(&ui, &event);
129182 }
130183
131- while (isrunning) {
184+ while (ui.isrunning) {
132185 XNextEvent(ui.display, &event);
133186
134187 switch (event.type) {
135188 case Expose:
136189 case ConfigureNotify:
137- XClearWindow(ui.display, ui.window);
190+ XClearWindow(ui.display, ui.xwindow);
138191 control_expose(&ui, &event);
139192 break;
140193 case ButtonPress:
変更 src/control.c
@@ -4,8 +4,12 @@
44
55 #include "control.h"
66 #include "display.h"
7-#include "program.h"
87
8+char curinput[64] = {0};
9+int input_pos = 0;
10+int initialized = 0;
11+XftColor buttonColor;
12+
913 static const char *btn_labels[][10] = {
1014 { NULL }, // 数字表示
1115 { "C", "±", "%", "÷" },
@@ -16,7 +20,7 @@
1620 };
1721
1822 #define NUM_ROWS 6
19-#define NUM_COLS 6
23+#define NUM_COLS 4
2024
2125 static SuwaButton *find_button_at(int mx, int my) {
2226 static SuwaButton found = {0};
@@ -52,35 +56,35 @@
5256 found.y = 224 + (row - 1) * (btn_h + padding);
5357 found.width = btn_w;
5458 found.height = btn_h;
55- found.label = btn_labels[row][col];
59+ found.text = btn_labels[row][col];
5660 found.pressed = 1;
5761
5862 return &found;
5963 }
6064
61-void append_to_input(char c) {
65+void append_to_input(UiSystem *ui, char c) {
6266 if ((unsigned long)input_pos >= sizeof(curinput) - 2) return;
63- strcpy(displayprb, "");
67+ strcpy(ui->problemLabel.text, "");
6468 curinput[input_pos++] = c;
6569 curinput[input_pos] = '\0';
6670
67- strncpy(displaytxt, curinput, sizeof(displaytxt) - 1);
68- displaytxt[sizeof(displaytxt) - 1] = '\0';
71+ strncpy(ui->resLabel.text, curinput, sizeof(ui->resLabel.text) - 1);
72+ ui->resLabel.text[sizeof(ui->resLabel.text) - 1] = '\0';
6973 }
7074
71-void clear_calculator(void) {
72- strcpy(displayprb, "");
75+void clear_calculator(UiSystem *ui) {
76+ strcpy(ui->problemLabel.text, "");
7377 curinput[0] = '\0';
7478 input_pos = 0;
75- strcpy(displaytxt, "0");
79+ strcpy(ui->resLabel.text, "0");
7680 }
7781
78-double evaluate_simple(const char *expr) {
82+double evaluate_simple(UiSystem *ui, const char *expr) {
7983 double res = 0.0;
8084 double cur = 0.0;
8185 char op = '+';
8286 int i = 0;
83- snprintf(displayprb, sizeof(displayprb), "%s=", expr);
87+ snprintf(ui->problemLabel.text, sizeof(ui->problemLabel.text), "%s=", expr);
8488
8589 while (expr[i]) {
8690 if (expr[i] == ' ') { i++; continue; }
@@ -124,7 +128,7 @@
124128 if (e->type != Expose && e->type != ConfigureNotify) return;
125129
126130 XWindowAttributes attr;
127- XGetWindowAttributes(ui->display, ui->window, &attr);
131+ XGetWindowAttributes(ui->display, ui->xwindow, &attr);
128132 int w = attr.width;
129133 int h = attr.height;
130134
@@ -133,12 +137,12 @@
133137 ui->backbuf = None;
134138 }
135139
136- ui->backbuf = XCreatePixmap(ui->display, ui->window, w, h,
140+ ui->backbuf = XCreatePixmap(ui->display, ui->xwindow, w, h,
137141 DefaultDepth(ui->display, DefaultScreen(ui->display)));
138142
139143 if (ui->backbuf == None) {
140144 fprintf(stderr, "バックバッファ作成失敗!\n");
141- ui->backbuf = ui->window;
145+ ui->backbuf = ui->xwindow;
142146 }
143147
144148 ui->target = ui->backbuf;
@@ -157,80 +161,73 @@
157161 XFillRectangle(ui->display, ui->backbuf, ui->gc, 0, 0, w, h);
158162
159163 // 出力
160- if (displaytxt[0] != '\0' && ui->disfont) {
161- const FcChar8 *text = (const FcChar8 *)displaytxt;
162- int len = strlen((const char *)text);
163-
164+ {
164165 XGlyphInfo extents;
165- XftTextExtentsUtf8(ui->display, ui->disfont, text, len, &extents);
166+ XftTextExtentsUtf8(ui->display, ui->resLabel.font,
167+ (const FcChar8 *)ui->resLabel.text,
168+ strlen(ui->resLabel.text), &extents);
166169
167170 int tx = w - 20 - extents.xOff;
168- int ty = 160;
169171
170- XftColor discol;
171- XftColorAllocName(ui->display, DefaultVisual(ui->display, DefaultScreen(ui->display)),
172- DefaultColormap(ui->display, DefaultScreen(ui->display)),
173- "#ee4030", &discol);
174-
175- XftDrawStringUtf8(backdraw, &discol, ui->disfont, tx, ty, text, len);
176-
177- XftColorFree(ui->display, DefaultVisual(ui->display, DefaultScreen(ui->display)),
178- DefaultColormap(ui->display, DefaultScreen(ui->display)), &discol);
172+ XftDrawStringUtf8(
173+ backdraw, &ui->resLabel.fg_color, ui->resLabel.font,
174+ tx, ui->resLabel.y,
175+ (const FcChar8 *)ui->resLabel.text, 32);
179176 }
180177
181- if (displayprb[0] != '\0' && ui->prbfont) {
182- const FcChar8 *text = (const FcChar8 *)displayprb;
183- int len = strlen((const char *)text);
184-
178+ {
185179 XGlyphInfo extents;
186- XftTextExtentsUtf8(ui->display, ui->prbfont, text, len, &extents);
180+ XftTextExtentsUtf8(ui->display, ui->problemLabel.font,
181+ (const FcChar8 *)ui->problemLabel.text,
182+ strlen(ui->problemLabel.text), &extents);
187183
188184 int tx = w - 20 - extents.xOff;
189- int ty = 80;
190185
191- XftColor discol;
192- XftColorAllocName(ui->display,
193- DefaultVisual(ui->display, DefaultScreen(ui->display)),
194- DefaultColormap(ui->display, DefaultScreen(ui->display)),
195- "#b61729", &discol);
196-
197- XftDrawStringUtf8(backdraw, &discol, ui->prbfont, tx, ty, text, len);
198-
199- XftColorFree(ui->display,
200- DefaultVisual(ui->display, DefaultScreen(ui->display)),
201- DefaultColormap(ui->display, DefaultScreen(ui->display)), &discol);
186+ XftDrawStringUtf8(
187+ backdraw, &ui->problemLabel.fg_color,
188+ ui->problemLabel.font, tx, ui->problemLabel.y,
189+ (const FcChar8 *)ui->problemLabel.text, 32);
202190 }
203191
192+ for (int i = 0; i < 64; ++i) ui->buttons[i] = NULL;
204193 int width = 93;
205194 int height = 60;
206195 int padding = 2;
207196 printf("ウィンドウ: (%dx%d)\n", attr.width, attr.height);
208197
209- for (int row = 0; row < 6; ++row) {
198+ if (initialized == 0) {
199+ XftColorAllocName(ui->display,
200+ DefaultVisual(ui->display, DefaultScreen(ui->display)),
201+ DefaultColormap(ui->display, DefaultScreen(ui->display)),
202+ "#232020", &buttonColor);
203+ }
204+ initialized = 1;
205+
206+ for (int row = 1; row < NUM_ROWS; ++row) {
210207 int y = 162 + row * (height + padding);
211- if (y + height > h) break;
212208
213- int cols = 4;
214-
215- for (int col = 0; col < cols; ++col) {
209+ for (int col = 0; col < NUM_COLS; ++col) {
216210 const char *label = btn_labels[row][col];
217211 if (!label) continue;
218212
219213 int x = padding + col * (width + padding);
220214
221- SuwaButton btn;
222- btn.x = x;
223- btn.y = y;
224- btn.width = width;
225- btn.height = height;
226- btn.label = label;
227- btn.pressed = 0;
215+ SuwaButton *btn = malloc(sizeof(SuwaButton));
216+ btn->x = x;
217+ btn->y = y;
218+ btn->width = width;
219+ btn->height = height;
220+ btn->text = label;
221+ btn->bg_color = BTCOL;
222+ btn->fg_color = buttonColor;
223+ btn->pressed = 0;
224+ // ui->buttons[0] = btn;
228225
229- drawbuttons(ui, &btn, backdraw);
226+ drawbuttons(ui, btn, backdraw);
230227 }
231228 }
232229
233- XCopyArea(ui->display, ui->backbuf, ui->window, ui->gc, 0, 0, w, h, 0, 0);
230+ XCopyArea(ui->display, ui->backbuf, ui->xwindow, ui->gc, 0, 0, w, h, 0, 0);
234231 XftDrawDestroy(backdraw);
235232 XFlush(ui->display);
236233 }
@@ -275,23 +272,26 @@
275272 XftDrawDestroy(backdraw);
276273 XFlush(ui->display);
277274
278- const char *label = btn->label;
275+ const char *label = btn->text;
279276 if (strcmp(label, "C") == 0) {
280- clear_calculator();
277+ clear_calculator(ui);
281278 } else if (strcmp(label, "<") == 0) {
282279 curinput[--input_pos] = '\0';
283- strcpy(displaytxt, curinput[0] ? curinput : "0");
280+ strcpy(ui->resLabel.text, curinput[0] ? curinput : "0");
284281 } else if (strcmp(label, "=") == 0) {
285- double res = evaluate_simple(curinput);
282+ double res = evaluate_simple(ui, curinput);
286283 if (isnan(res)) {
287- strncpy(displaytxt, "Error", 5);
284+ strncpy(ui->resLabel.text, "Error", 5);
288285 } else {
289- snprintf(displaytxt, sizeof(displaytxt), "%.8g", res);
290- strncpy(curinput, displaytxt, strlen(displaytxt));
286+ snprintf(ui->resLabel.text, sizeof(ui->resLabel.text), "%.8g", res);
287+ strncpy(curinput, ui->resLabel.text, strlen(ui->resLabel.text));
291288 input_pos = strlen(curinput);
289+ strcpy(curinput, "");
290+ curinput[0] = '\0';
291+ input_pos = 0;
292292 }
293293 } else if (strlen(label) == 1) {
294- append_to_input(label[0]);
294+ append_to_input(ui, label[0]);
295295 }
296296
297297 control_expose(ui, &(XEvent){.type = Expose});
@@ -307,7 +307,7 @@
307307 len = XLookupString(&event->xkey, buf, sizeof(buf), &keysym, NULL);
308308
309309 if (keysym == XK_Q) {
310- isrunning = 0;
310+ ui->isrunning = 0;
311311 return;
312312 }
313313
@@ -341,16 +341,16 @@
341341 }
342342
343343 if (keysym == XK_C) {
344- clear_calculator();
344+ clear_calculator(ui);
345345 goto redraw;
346346 }
347347
348348 if (keysym == XK_Return || keysym == XK_KP_Enter) {
349- double res = evaluate_simple(curinput);
349+ double res = evaluate_simple(ui, curinput);
350350 if (isnan(res)) {
351- strncpy(displaytxt, "Error", 5);
351+ strncpy(ui->resLabel.text, "Error", 5);
352352 } else {
353- snprintf(displaytxt, sizeof(displaytxt), "%.8g", res);
353+ snprintf(ui->resLabel.text, sizeof(ui->resLabel.text), "%.8g", res);
354354 }
355355 goto redraw;
356356 }
@@ -359,7 +359,7 @@
359359 || (keysym == XK_Delete && input_pos > 0)
360360 || (keysym == XK_X && input_pos > 0)) {
361361 curinput[--input_pos] = '\0';
362- strcpy(displaytxt, curinput[0] ? curinput : "0");
362+ strcpy(ui->resLabel.text, curinput[0] ? curinput : "0");
363363 goto redraw;
364364 }
365365
@@ -373,7 +373,7 @@
373373 else if (keysym == XK_KP_Decimal || keysym == XK_period) c = '.';
374374
375375 if (c) {
376- append_to_input(c);
376+ append_to_input(ui, c);
377377 goto redraw;
378378 }
379379
変更 src/display.c
@@ -1,17 +1,16 @@
11 #include "display.h"
2-#include "program.h"
32
43 #include <string.h>
54
65 void drawbuttons(UiSystem *ui, SuwaButton *btn, XftDraw *xftdraw) {
7- unsigned long curbg = btn->pressed ? BTSEL : BTCOL;
6+ unsigned long curbg = btn->pressed ? BTSEL : btn->bg_color;
87 XSetForeground(ui->display, ui->gc, curbg);
98 XFillRectangle(ui->display, ui->target, ui->gc, btn->x, btn->y,
109 btn->width, btn->height);
1110
1211 // 文字の中央に
13- if (btn->label && ui->font && xftdraw) {
14- const FcChar8 *str = (const FcChar8 *)btn->label;
12+ if (btn->text && ui->font && xftdraw) {
13+ const FcChar8 *str = (const FcChar8 *)btn->text;
1514 int len = strlen((const char *)str);
1615
1716 XGlyphInfo extents;
@@ -23,9 +22,7 @@
2322 int tx = btn->x + (btn->width - text_w) / 2;
2423 int ty = btn->y + (btn->height - text_h) / 2 + ui->font->ascent;
2524
26- // tx -= extents.x;
27-
2825 XftDrawStringUtf8(xftdraw, &ui->color, ui->font, tx, ty,
29- (FcChar8 *)btn->label, len);
26+ (FcChar8 *)btn->text, len);
3027 }
3128 }
変更 src/display.h
@@ -1,6 +1,5 @@
11 #pragma once
22
3-#include "program.h"
43 #include "ui.h"
54
65 void drawbuttons(UiSystem *ui, SuwaButton *btn, XftDraw *xftdraw);
削除 src/program.c
@@ -1,12 +0,0 @@
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
@@ -1,19 +0,0 @@
1-#pragma once
2-
3-#include "ui.h"
4-
5-#define FGCOL 0xfcfcfc
6-#define BGCOL 0x232020
7-#define BTSEL 0xb61729
8-#define BTCOL 0xee4030
9-#define BTHVR 0xf35869
10-
11-extern int window_width;
12-extern int window_height;
13-extern int isrunning;
14-extern char displayprb[128];
15-extern char curinput[256];
16-extern char displaytxt[64];
17-extern int input_pos;
18-extern SuwaButton *hovered_btn;
19-extern SuwaButton *pressed_btn;
変更 src/ui.h
@@ -3,21 +3,45 @@
33 #include <X11/Xlib.h>
44 #include <X11/Xft/Xft.h>
55
6+#define FGCOL 0xfcfcfc
7+#define BGCOL 0x232020
8+#define BTSEL 0xb61729
9+#define BTCOL 0xee4030
10+#define BTHVR 0xf35869
11+
612 typedef struct {
13+ int x, y, width, height;
14+} SuwaWindow;
15+
16+typedef struct {
17+ int x, y, width, height;
18+ const char *text;
19+ XftFont *font;
20+ int bg_color;
21+ XftColor fg_color;
22+ int pressed; // 0 = 普通、1 = 押している
23+} SuwaButton;
24+
25+typedef struct {
26+ int x, y, width, height;
27+ char text[32];
28+ XftFont *font;
29+ XftColor fg_color;
30+} SuwaLabel;
31+
32+typedef struct {
33+ int isrunning;
734 Display *display;
8- Window window;
35+ Window xwindow;
936 Drawable target;
1037 GC gc;
1138 Visual visual;
12- XftDraw *xftdraw;
13- XftColor color, btncolor, textcolor;
14- XftFont *font, *prbfont, *disfont;
39+ XftColor color;
40+ XftFont *font;
1541 Colormap colormap;
1642 Pixmap backbuf;
43+ SuwaWindow window;
44+ SuwaLabel resLabel;
45+ SuwaLabel problemLabel;
46+ SuwaButton *buttons[32];
1747 } UiSystem;
18-
19-typedef struct {
20- int x, y, width, height;
21- const char *label;
22- int pressed; // 0 = 普通、1 = 押している
23-} SuwaButton;
変更 src/utils.c
@@ -1,20 +1,17 @@
11 #include "utils.h"
22
33 void cleanup(UiSystem *ui) {
4- if (ui->textcolor.pixel != 0)
5- XftColorFree(ui->display, &ui->visual, ui->colormap, &ui->textcolor);
6- if (ui->btncolor.pixel != 0)
7- XftColorFree(ui->display, &ui->visual, ui->colormap, &ui->btncolor);
8- if (ui->color.pixel != 0)
9- XftColorFree(ui->display, &ui->visual, ui->colormap, &ui->color);
10- if (ui->disfont) XftFontClose(ui->display, ui->disfont);
11- if (ui->prbfont) XftFontClose(ui->display, ui->prbfont);
4+ // フォント
5+ if (ui->resLabel.font) XftFontClose(ui->display, ui->resLabel.font);
6+ if (ui->problemLabel.font) XftFontClose(ui->display, ui->problemLabel.font);
127 if (ui->font) XftFontClose(ui->display, ui->font);
8+
9+ // その他
1310 if (ui->gc) XFreeGC(ui->display, ui->gc);
1411 if (ui->backbuf) {
1512 XFreePixmap(ui->display, ui->backbuf);
1613 ui->backbuf = None;
1714 }
18- if (ui->window) XDestroyWindow(ui->display, ui->window);
15+ if (ui->xwindow) XDestroyWindow(ui->display, ui->xwindow);
1916 if (ui->display) XCloseDisplay(ui->display);
2017 }
変更 src/utils.h
@@ -1,5 +1,5 @@
11 #pragma once
22
3-#include "program.h"
3+#include "ui.h"
44
55 void cleanup(UiSystem *ui);