suwako / UnixCalc アーカイブ

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

綺麗に

諏訪子 <suwako@076.moe>、2026-01-17 18:35
コミット fa83f224e2ec2541e45cf9ab2afc5a2b3b5f06cb ・親 b20fa6c ・ファイルを見る

9個のファイル、+164 -151

変更 main.c
@@ -4,73 +4,67 @@
44 #include "src/utils.h"
55 #include "src/control.h"
66
7-XftColor color, btncolor;
8-Colormap colormap;
9-XftFont *font;
10-XftFont *prbfont;
11-XftFont *disfont;
12-Visual *visual;
13-
147 const char *sofname = "ucalc";
158 const char *version = "0.0.0";
169 const char *disname = "Unix Calc";
1710
1811 int main() {
19- Display *display;
20- Window window;
12+ UiSystem ui;
2113 XEvent event;
2214 int screen;
23- GC gc = NULL;
2415 XGCValues values;
2516
26- display = XOpenDisplay(NULL);
27- if (display == NULL) {
17+ ui.display = XOpenDisplay(NULL);
18+ if (ui.display == NULL) {
2819 fprintf(stderr, "画面を開けられません。\n");
2920 exit(1);
3021 }
3122
32- screen = DefaultScreen(display);
23+ screen = DefaultScreen(ui.display);
3324
34- int sw = DisplayWidth(display, screen);
35- int sh = DisplayHeight(display, screen);
25+ int sw = DisplayWidth(ui.display, screen);
26+ int sh = DisplayHeight(ui.display, screen);
3627 int window_x = (sw - window_width) / 3;
3728 int window_y = (sh - window_height) / 2;
3829
39- window = XCreateSimpleWindow(display, RootWindow(display, screen),
30+ ui.window = XCreateSimpleWindow(ui.display,
31+ RootWindow(ui.display, screen),
4032 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);
33+ if (!ui.window) {
34+ cleanup(&ui);
4335 fprintf(stderr, "ウィンドウを作成に失敗。\n");
4436 exit(1);
4537 }
4638
47- backbuf = XCreatePixmap(display, window, window_width, window_height, DefaultDepth(display, screen));
39+ ui.backbuf = XCreatePixmap(ui.display, ui.window, window_width, window_height,
40+ DefaultDepth(ui.display, screen));
41+ ui.target = ui.backbuf;
4842
49- Atom net_wm_window_type = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
50- Atom dialog = XInternAtom(display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
43+ Atom net_wm_window_type = XInternAtom(ui.display, "_NET_WM_WINDOW_TYPE", False);
44+ Atom dialog = XInternAtom(ui.display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
5145
52- XChangeProperty(display, window, net_wm_window_type, XA_ATOM, 32,
46+ XChangeProperty(ui.display, ui.window, net_wm_window_type, XA_ATOM, 32,
5347 PropModeReplace, (unsigned char *)&dialog, 1);
5448
55- XStoreName(display, window, disname);
56- Atom net_wm_name = XInternAtom(display, "_NET_WM_NAME", False);
49+ XStoreName(ui.display, ui.window, disname);
50+ Atom net_wm_name = XInternAtom(ui.display, "_NET_WM_NAME", False);
5751 char displayname[16];
5852 snprintf(displayname, 16, "%s %s", disname, version);
59- XChangeProperty(display, window, net_wm_name,
60- XInternAtom(display, "UTF8_STRING", False), 8,
53+ XChangeProperty(ui.display, ui.window, net_wm_name,
54+ XInternAtom(ui.display, "UTF8_STRING", False), 8,
6155 PropModeReplace, (unsigned char *)displayname, strlen(displayname));
6256
6357 XClassHint *classHint = XAllocClassHint();
6458 if (classHint) {
6559 classHint->res_name = "unixcalc";
6660 classHint->res_class = "UnixCalc";
67- XSetClassHint(display, window, classHint);
61+ XSetClassHint(ui.display, ui.window, classHint);
6862 XFree(classHint);
6963 }
7064
71- XSetWindowBackground(display, window, BGCOL);
65+ XSetWindowBackground(ui.display, ui.window, BGCOL);
7266
73- XSelectInput(display, window,
67+ XSelectInput(ui.display, ui.window,
7468 ExposureMask
7569 | ButtonPressMask
7670 | ButtonReleaseMask
@@ -80,84 +74,84 @@
8074 // | StructureNotifyMask
8175 );
8276
83- gc = XCreateGC(display, window, 0, &values);
84- if (!gc) {
85- cleanup(display, window, gc, &color, &btncolor, font, disfont, prbfont, colormap, visual, backbuf);
77+ ui.gc = XCreateGC(ui.display, ui.window, 0, &values);
78+ if (!ui.gc) {
79+ cleanup(&ui);
8680 fprintf(stderr, "GCを作成に失敗。\n");
8781 exit(1);
8882 }
8983
90- visual = DefaultVisual(display, screen);
84+ ui.visual = *DefaultVisual(ui.display, screen);
9185
92- colormap = XCreateColormap(display, window, visual, AllocNone);
93- if (colormap == None) {
94- cleanup(display, window, gc, &color, &btncolor, font, disfont, prbfont, colormap, visual, backbuf);
86+ ui.colormap = XCreateColormap(ui.display, ui.window, &ui.visual, AllocNone);
87+ if (ui.colormap == None) {
88+ cleanup(&ui);
9589 fprintf(stderr, "カラーマップを作成に失敗。\n");
9690 exit(1);
9791 }
9892
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);
93+ ui.font = XftFontOpenName(ui.display, screen, "Noto Sans CJK-12");
94+ if (!ui.font) {
95+ cleanup(&ui);
10296 fprintf(stderr, "フォントの読み込みに失敗。\n");
10397 exit(1);
10498 }
10599
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);
100+ ui.prbfont = XftFontOpenName(ui.display, screen, "Noto Sans CJK-24");
101+ if (!ui.prbfont) {
102+ cleanup(&ui);
109103 fprintf(stderr, "問題フォントの読み込みに失敗。\n");
110104 exit(1);
111105 }
112106
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);
107+ ui.disfont = XftFontOpenName(ui.display, screen, "Noto Sans CJK-72");
108+ if (!ui.disfont) {
109+ cleanup(&ui);
116110 fprintf(stderr, "解決フォントの読み込みに失敗。\n");
117111 exit(1);
118112 }
119113
120- if (!XftColorAllocName(display, visual, colormap, "#232020", &color)) {
121- cleanup(display, window, gc, &color, &btncolor, font, disfont, prbfont, colormap, visual, backbuf);
114+ if (!XftColorAllocName(ui.display, &ui.visual, ui.colormap, "#232020", &ui.color)) {
115+ cleanup(&ui);
122116 fprintf(stderr, "色の役割に失敗。\n");
123117 exit(1);
124118 }
125119
126- XMapWindow(display, window);
120+ XMapWindow(ui.display, ui.window);
127121 {
128122 XWindowAttributes attr;
129- XGetWindowAttributes(display, window, &attr);
123+ XGetWindowAttributes(ui.display, ui.window, &attr);
130124 XEvent fake = { .type = Expose };
131- fake.xexpose.window = window;
125+ fake.xexpose.window = ui.window;
132126 fake.xexpose.width = attr.width;
133127 fake.xexpose.height = attr.height;
134- control_expose(display, window, gc, &event, &color, font, disfont, prbfont);
128+ control_expose(&ui, &event);
135129 }
136130
137131 while (isrunning) {
138- XNextEvent(display, &event);
132+ XNextEvent(ui.display, &event);
139133
140134 switch (event.type) {
141135 case Expose:
142136 case ConfigureNotify:
143- XClearWindow(display, window);
144- control_expose(display, window, gc, &event, &color, font, disfont, prbfont);
137+ XClearWindow(ui.display, ui.window);
138+ control_expose(&ui, &event);
145139 break;
146140 case ButtonPress:
147141 if (event.xbutton.button == Button1) {
148- handle_button_press(display, gc, event.xbutton.x, event.xbutton.y, &color, font);
142+ handle_button_press(&ui, event.xbutton.x, event.xbutton.y);
149143 break;
150144 }
151145 case ButtonRelease:
152146 if (event.xbutton.button == Button1) {
153- handle_button_release(display, window, gc, event.xbutton.x, event.xbutton.y, &color, font, disfont, prbfont);
147+ handle_button_release(&ui, event.xbutton.x, event.xbutton.y);
154148 break;
155149 }
156150 case KeyPress:
157- handle_key_press(display, window, gc, &event, &color, font, disfont, prbfont);
151+ handle_key_press(&ui, &event);
158152 break;
159153 // case MotionNotify:
160- // handle_mouse_hover(display, window, gc, &event, &color, font);
154+ // handle_mouse_hover(&ui, &event);
161155 // break;
162156 case ClientMessage:
163157 // WM_DELETE_WINDOW
@@ -165,7 +159,7 @@
165159 }
166160 }
167161
168- cleanup(display, window, gc, &color, &btncolor, font, disfont, prbfont, colormap, visual, backbuf);
162+ cleanup(&ui);
169163
170164 return 0;
171165 }
変更 src/control.c
@@ -120,78 +120,85 @@
120120 return res;
121121 }
122122
123-void control_expose(Display *dpy, Window wnd, GC gc, XEvent *e, XftColor *color, XftFont *f, XftFont *df, XftFont *pf) {
124- (void)color;
123+void control_expose(UiSystem *ui, XEvent *e) {
125124 if (e->type != Expose && e->type != ConfigureNotify) return;
126125
127126 XWindowAttributes attr;
128- XGetWindowAttributes(dpy, wnd, &attr);
127+ XGetWindowAttributes(ui->display, ui->window, &attr);
129128 int w = attr.width;
130129 int h = attr.height;
131130
132- if (backbuf != None) {
133- XFreePixmap(dpy, backbuf);
131+ if (ui->backbuf != None) {
132+ XFreePixmap(ui->display, ui->backbuf);
133+ ui->backbuf = None;
134134 }
135135
136- backbuf = XCreatePixmap(dpy, wnd, w, h, DefaultDepth(dpy, DefaultScreen(dpy)));
136+ ui->backbuf = XCreatePixmap(ui->display, ui->window, w, h,
137+ DefaultDepth(ui->display, DefaultScreen(ui->display)));
137138
138- if (backbuf == None) {
139- backbuf = wnd;
139+ if (ui->backbuf == None) {
140+ fprintf(stderr, "バックバッファ作成失敗!\n");
141+ ui->backbuf = ui->window;
140142 }
141143
142- XftDraw *backdraw = XftDrawCreate(dpy, backbuf,
143- DefaultVisual(dpy, DefaultScreen(dpy)), DefaultColormap(dpy, DefaultScreen(dpy)));
144+ ui->target = ui->backbuf;
145+
146+ XftDraw *backdraw = XftDrawCreate(ui->display, ui->backbuf,
147+ DefaultVisual(ui->display, DefaultScreen(ui->display)),
148+ DefaultColormap(ui->display, DefaultScreen(ui->display)));
144149 if (!backdraw) {
145150 fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n");
146- XFreePixmap(dpy, backbuf);
147- backbuf = None;
151+ XFreePixmap(ui->display, ui->backbuf);
152+ ui->backbuf = None;
148153 return;
149154 }
150155
151- XSetForeground(dpy, gc, BGCOL);
152- XFillRectangle(dpy, backbuf, gc, 0, 0, w, h);
156+ XSetForeground(ui->display, ui->gc, BGCOL);
157+ XFillRectangle(ui->display, ui->backbuf, ui->gc, 0, 0, w, h);
153158
154159 // 出力
155- if (displaytxt[0] != '\0' && df) {
160+ if (displaytxt[0] != '\0' && ui->disfont) {
156161 const FcChar8 *text = (const FcChar8 *)displaytxt;
157162 int len = strlen((const char *)text);
158163
159164 XGlyphInfo extents;
160- XftTextExtentsUtf8(dpy, df, text, len, &extents);
165+ XftTextExtentsUtf8(ui->display, ui->disfont, text, len, &extents);
161166
162167 int tx = w - 20 - extents.xOff;
163168 int ty = 160;
164169
165170 XftColor discol;
166- XftColorAllocName(dpy, DefaultVisual(dpy, DefaultScreen(dpy)),
167- DefaultColormap(dpy, DefaultScreen(dpy)),
171+ XftColorAllocName(ui->display, DefaultVisual(ui->display, DefaultScreen(ui->display)),
172+ DefaultColormap(ui->display, DefaultScreen(ui->display)),
168173 "#ee4030", &discol);
169174
170- XftDrawStringUtf8(backdraw, &discol, df, tx, ty, text, len);
175+ XftDrawStringUtf8(backdraw, &discol, ui->disfont, tx, ty, text, len);
171176
172- XftColorFree(dpy, DefaultVisual(dpy, DefaultScreen(dpy)),
173- DefaultColormap(dpy, DefaultScreen(dpy)), &discol);
177+ XftColorFree(ui->display, DefaultVisual(ui->display, DefaultScreen(ui->display)),
178+ DefaultColormap(ui->display, DefaultScreen(ui->display)), &discol);
174179 }
175180
176- if (displayprb[0] != '\0' && pf) {
181+ if (displayprb[0] != '\0' && ui->prbfont) {
177182 const FcChar8 *text = (const FcChar8 *)displayprb;
178183 int len = strlen((const char *)text);
179184
180185 XGlyphInfo extents;
181- XftTextExtentsUtf8(dpy, pf, text, len, &extents);
186+ XftTextExtentsUtf8(ui->display, ui->prbfont, text, len, &extents);
182187
183188 int tx = w - 20 - extents.xOff;
184189 int ty = 80;
185190
186191 XftColor discol;
187- XftColorAllocName(dpy, DefaultVisual(dpy, DefaultScreen(dpy)),
188- DefaultColormap(dpy, DefaultScreen(dpy)),
189- "#b61729", &discol);
192+ XftColorAllocName(ui->display,
193+ DefaultVisual(ui->display, DefaultScreen(ui->display)),
194+ DefaultColormap(ui->display, DefaultScreen(ui->display)),
195+ "#b61729", &discol);
190196
191- XftDrawStringUtf8(backdraw, &discol, pf, tx, ty, text, len);
197+ XftDrawStringUtf8(backdraw, &discol, ui->prbfont, tx, ty, text, len);
192198
193- XftColorFree(dpy, DefaultVisual(dpy, DefaultScreen(dpy)),
194- DefaultColormap(dpy, DefaultScreen(dpy)), &discol);
199+ XftColorFree(ui->display,
200+ DefaultVisual(ui->display, DefaultScreen(ui->display)),
201+ DefaultColormap(ui->display, DefaultScreen(ui->display)), &discol);
195202 }
196203
197204 int width = 93;
@@ -219,50 +226,54 @@
219226 btn.label = label;
220227 btn.pressed = 0;
221228
222- drawbuttons(dpy, backbuf, gc, &btn, backdraw, color, f);
229+ drawbuttons(ui, &btn, backdraw);
223230 }
224231 }
225232
226- XCopyArea(dpy, backbuf, wnd, gc, 0, 0, w, h, 0, 0);
233+ XCopyArea(ui->display, ui->backbuf, ui->window, ui->gc, 0, 0, w, h, 0, 0);
227234 XftDrawDestroy(backdraw);
228- XFlush(dpy);
235+ XFlush(ui->display);
229236 }
230237
231-void handle_button_press(Display *dpy, GC gc, int mx, int my, XftColor *col, XftFont *f) {
238+void handle_button_press(UiSystem *ui, int mx, int my) {
232239 SuwaButton *btn = find_button_at(mx, my);
233240 if (!btn) return;
234241
235242 btn->pressed = 1;
236- XftDraw *backdraw = XftDrawCreate(dpy, backbuf,
237- DefaultVisual(dpy, DefaultScreen(dpy)), DefaultColormap(dpy, DefaultScreen(dpy)));
243+ XftDraw *backdraw = XftDrawCreate(ui->display, ui->backbuf,
244+ DefaultVisual(ui->display, DefaultScreen(ui->display)),
245+ DefaultColormap(ui->display, DefaultScreen(ui->display)));
238246 if (!backdraw) {
239247 fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n");
240- XFreePixmap(dpy, backbuf);
241- backbuf = None;
248+ XFreePixmap(ui->display, ui->backbuf);
249+ ui->backbuf = None;
242250 return;
243251 }
244252
245- drawbuttons(dpy, backbuf, gc, btn, backdraw, col, f);
246- XFlush(dpy);
253+ drawbuttons(ui, btn, backdraw);
254+ XFlush(ui->display);
247255 }
248256
249-void handle_button_release(Display *dpy, Window wnd, GC gc, int mx, int my, XftColor *col, XftFont *f, XftFont *df, XftFont *pf) {
257+void handle_button_release(UiSystem *ui, int mx, int my) {
250258 SuwaButton *btn = find_button_at(mx, my);
251259 if (!btn) return;
252260
253261 btn->pressed = 0;
254262
255- XftDraw *backdraw = XftDrawCreate(dpy, backbuf,
256- DefaultVisual(dpy, DefaultScreen(dpy)), DefaultColormap(dpy, DefaultScreen(dpy)));
263+ XftDraw *backdraw = XftDrawCreate(ui->display, ui->backbuf,
264+ DefaultVisual(ui->display, DefaultScreen(ui->display)),
265+ DefaultColormap(ui->display, DefaultScreen(ui->display)));
257266 if (!backdraw) {
258267 fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n");
259- XFreePixmap(dpy, backbuf);
260- backbuf = None;
268+ XFreePixmap(ui->display, ui->backbuf);
269+ ui->backbuf = None;
261270 return;
262271 }
263- drawbuttons(dpy, backbuf, gc, btn, backdraw, col, f);
272+
273+ ui->target = ui->backbuf;
274+ drawbuttons(ui, btn, backdraw);
264275 XftDrawDestroy(backdraw);
265- XFlush(dpy);
276+ XFlush(ui->display);
266277
267278 const char *label = btn->label;
268279 if (strcmp(label, "C") == 0) {
@@ -283,10 +294,10 @@
283294 append_to_input(label[0]);
284295 }
285296
286- control_expose(dpy, wnd, gc, &(XEvent){.type = Expose}, col, f, df, pf);
297+ control_expose(ui, &(XEvent){.type = Expose});
287298 }
288299
289-void handle_key_press(Display *dpy, Window wnd, GC gc, XEvent *event, XftColor *col, XftFont *f, XftFont *df, XftFont *pf) {
300+void handle_key_press(UiSystem *ui, XEvent *event) {
290301 KeySym keysym;
291302 char buf[32];
292303 int len;
@@ -369,21 +380,21 @@
369380 return;
370381
371382 redraw:
372- control_expose(dpy, wnd, gc, &(XEvent){.type = Expose}, col, f, df, pf);
383+ control_expose(ui, &(XEvent){.type = Expose});
373384 }
374385
375-// void handle_mouse_hover(Display *dpy, Window wnd, GC gc, XEvent *event, XftColor *col, XftFont *f) {
386+// void handle_mouse_hover(UiSystem *ui, XEvent *event) {
376387 // SuwaButton *hover = find_button_at(event->xmotion.x, event->xmotion.y);
377388
378389 // if (hover != hovered_btn) {
379390 // if (hovered_btn) {
380391 // hovered_btn->pressed = 0;
381-// redraw_single_button(dpy, wnd, gc, col, f, hovered_btn);
392+// redraw_single_button(&ui, hovered_btn);
382393 // }
383394 // hovered_btn = hover;
384395
385396 // if (hovered_btn) {
386-// redraw_single_button(dpy, wnd, gc, col, f, hovered_btn);
397+// redraw_single_button(&ui, hovered_btn);
387398 // }
388399 // }
389400 // }
変更 src/control.h
@@ -1,10 +1,9 @@
11 #pragma once
22
3-#include <X11/Xlib.h>
4-#include <X11/Xft/Xft.h>
3+#include "ui.h"
54
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);
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
@@ -3,28 +3,28 @@
33
44 #include <string.h>
55
6-void drawbuttons(Display *dpy, Drawable target, GC gc, SuwaButton *btn,
7- XftDraw *xftdraw, XftColor *textcolor, XftFont *font) {
6+void drawbuttons(UiSystem *ui, SuwaButton *btn, XftDraw *xftdraw) {
87 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);
8+ XSetForeground(ui->display, ui->gc, curbg);
9+ XFillRectangle(ui->display, ui->target, ui->gc, btn->x, btn->y, btn->width, btn->height);
1110
1211 // 文字の中央に
13- if (btn->label && font && xftdraw) {
12+ if (btn->label && ui->font && xftdraw) {
1413 const FcChar8 *str = (const FcChar8 *)btn->label;
1514 int len = strlen((const char *)str);
1615
1716 XGlyphInfo extents;
18- XftTextExtentsUtf8(dpy, font, str, len, &extents);
17+ XftTextExtentsUtf8(ui->display, ui->font, str, len, &extents);
1918
2019 int text_w = extents.xOff;
21- int text_h = font->ascent + font->descent;
20+ int text_h = ui->font->ascent + ui->font->descent;
2221
2322 int tx = btn->x + (btn->width - text_w) / 2;
24- int ty = btn->y + (btn->height - text_h) / 2 + font->ascent;
23+ int ty = btn->y + (btn->height - text_h) / 2 + ui->font->ascent;
2524
2625 // tx -= extents.x;
2726
28- XftDrawStringUtf8(xftdraw, textcolor, font, tx, ty, (FcChar8 *)btn->label, len);
27+ XftDrawStringUtf8(xftdraw, &ui->textcolor, ui->font, tx, ty,
28+ (FcChar8 *)btn->label, len);
2929 }
3030 }
変更 src/display.h
@@ -1,6 +1,6 @@
11 #pragma once
22
33 #include "program.h"
4+#include "ui.h"
45
5-void drawbuttons(Display *dpy, Drawable target, GC gc, SuwaButton *btn,
6- XftDraw *xftdraw, XftColor *textcolor, XftFont *font);
6+void drawbuttons(UiSystem *ui, SuwaButton *btn, XftDraw *xftdraw);
変更 src/program.h
@@ -1,7 +1,5 @@
11 #pragma once
22
3-#include <X11/Xlib.h>
4-#include <X11/Xft/Xft.h>
53 #include "ui.h"
64
75 #define FGCOL 0xfcfcfc
@@ -17,6 +15,5 @@
1715 extern char curinput[256];
1816 extern char displaytxt[64];
1917 extern int input_pos;
20-extern Pixmap backbuf;
2118 extern SuwaButton *hovered_btn;
2219 extern SuwaButton *pressed_btn;
変更 src/ui.h
@@ -1,5 +1,21 @@
11 #pragma once
22
3+#include <X11/Xlib.h>
4+#include <X11/Xft/Xft.h>
5+
6+typedef struct {
7+ Display *display;
8+ Window window;
9+ Drawable target;
10+ GC gc;
11+ Visual visual;
12+ XftDraw *xftdraw;
13+ XftColor color, btncolor, textcolor;
14+ XftFont *font, *prbfont, *disfont;
15+ Colormap colormap;
16+ Pixmap backbuf;
17+} UiSystem;
18+
319 typedef struct {
420 int x, y, width, height;
521 const char *label;
変更 src/utils.c
@@ -1,19 +1,18 @@
11 #include "utils.h"
22
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;
3+void cleanup(UiSystem *ui) {
4+ if (ui->btncolor.pixel != 0)
5+ XftColorFree(ui->display, &ui->visual, ui->colormap, &ui->btncolor);
6+ if (ui->color.pixel != 0)
7+ XftColorFree(ui->display, &ui->visual, ui->colormap, &ui->color);
8+ if (ui->disfont) XftFontClose(ui->display, ui->disfont);
9+ if (ui->prbfont) XftFontClose(ui->display, ui->prbfont);
10+ if (ui->font) XftFontClose(ui->display, ui->font);
11+ if (ui->gc) XFreeGC(ui->display, ui->gc);
12+ if (ui->backbuf) {
13+ XFreePixmap(ui->display, ui->backbuf);
14+ ui->backbuf = None;
1615 }
17- if (window) XDestroyWindow(display, window);
18- if (display) XCloseDisplay(display);
16+ if (ui->window) XDestroyWindow(ui->display, ui->window);
17+ if (ui->display) XCloseDisplay(ui->display);
1918 }
変更 src/utils.h
@@ -2,7 +2,4 @@
22
33 #include "program.h"
44
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);
5+void cleanup(UiSystem *ui);