綺麗に
諏訪子 <suwako@076.moe>、2026-01-17 18:35
コミット fa83f224e2ec2541e45cf9ab2afc5a2b3b5f06cb
・親 b20fa6c
・ファイルを見る
9個のファイル、+164 -151
変更
main.c
| @@ -4,73 +4,67 @@ | ||
| 4 | 4 | #include "src/utils.h" |
| 5 | 5 | #include "src/control.h" |
| 6 | 6 | |
| 7 | -XftColor color, btncolor; | |
| 8 | -Colormap colormap; | |
| 9 | -XftFont *font; | |
| 10 | -XftFont *prbfont; | |
| 11 | -XftFont *disfont; | |
| 12 | -Visual *visual; | |
| 13 | - | |
| 14 | 7 | const char *sofname = "ucalc"; |
| 15 | 8 | const char *version = "0.0.0"; |
| 16 | 9 | const char *disname = "Unix Calc"; |
| 17 | 10 | |
| 18 | 11 | int main() { |
| 19 | - Display *display; | |
| 20 | - Window window; | |
| 12 | + UiSystem ui; | |
| 21 | 13 | XEvent event; |
| 22 | 14 | int screen; |
| 23 | - GC gc = NULL; | |
| 24 | 15 | XGCValues values; |
| 25 | 16 | |
| 26 | - display = XOpenDisplay(NULL); | |
| 27 | - if (display == NULL) { | |
| 17 | + ui.display = XOpenDisplay(NULL); | |
| 18 | + if (ui.display == NULL) { | |
| 28 | 19 | fprintf(stderr, "画面を開けられません。\n"); |
| 29 | 20 | exit(1); |
| 30 | 21 | } |
| 31 | 22 | |
| 32 | - screen = DefaultScreen(display); | |
| 23 | + screen = DefaultScreen(ui.display); | |
| 33 | 24 | |
| 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); | |
| 36 | 27 | int window_x = (sw - window_width) / 3; |
| 37 | 28 | int window_y = (sh - window_height) / 2; |
| 38 | 29 | |
| 39 | - window = XCreateSimpleWindow(display, RootWindow(display, screen), | |
| 30 | + ui.window = XCreateSimpleWindow(ui.display, | |
| 31 | + RootWindow(ui.display, screen), | |
| 40 | 32 | 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); | |
| 43 | 35 | fprintf(stderr, "ウィンドウを作成に失敗。\n"); |
| 44 | 36 | exit(1); |
| 45 | 37 | } |
| 46 | 38 | |
| 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; | |
| 48 | 42 | |
| 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); | |
| 51 | 45 | |
| 52 | - XChangeProperty(display, window, net_wm_window_type, XA_ATOM, 32, | |
| 46 | + XChangeProperty(ui.display, ui.window, net_wm_window_type, XA_ATOM, 32, | |
| 53 | 47 | PropModeReplace, (unsigned char *)&dialog, 1); |
| 54 | 48 | |
| 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); | |
| 57 | 51 | char displayname[16]; |
| 58 | 52 | 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, | |
| 61 | 55 | PropModeReplace, (unsigned char *)displayname, strlen(displayname)); |
| 62 | 56 | |
| 63 | 57 | XClassHint *classHint = XAllocClassHint(); |
| 64 | 58 | if (classHint) { |
| 65 | 59 | classHint->res_name = "unixcalc"; |
| 66 | 60 | classHint->res_class = "UnixCalc"; |
| 67 | - XSetClassHint(display, window, classHint); | |
| 61 | + XSetClassHint(ui.display, ui.window, classHint); | |
| 68 | 62 | XFree(classHint); |
| 69 | 63 | } |
| 70 | 64 | |
| 71 | - XSetWindowBackground(display, window, BGCOL); | |
| 65 | + XSetWindowBackground(ui.display, ui.window, BGCOL); | |
| 72 | 66 | |
| 73 | - XSelectInput(display, window, | |
| 67 | + XSelectInput(ui.display, ui.window, | |
| 74 | 68 | ExposureMask |
| 75 | 69 | | ButtonPressMask |
| 76 | 70 | | ButtonReleaseMask |
| @@ -80,84 +74,84 @@ | ||
| 80 | 74 | // | StructureNotifyMask |
| 81 | 75 | ); |
| 82 | 76 | |
| 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); | |
| 86 | 80 | fprintf(stderr, "GCを作成に失敗。\n"); |
| 87 | 81 | exit(1); |
| 88 | 82 | } |
| 89 | 83 | |
| 90 | - visual = DefaultVisual(display, screen); | |
| 84 | + ui.visual = *DefaultVisual(ui.display, screen); | |
| 91 | 85 | |
| 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); | |
| 95 | 89 | fprintf(stderr, "カラーマップを作成に失敗。\n"); |
| 96 | 90 | exit(1); |
| 97 | 91 | } |
| 98 | 92 | |
| 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); | |
| 102 | 96 | fprintf(stderr, "フォントの読み込みに失敗。\n"); |
| 103 | 97 | exit(1); |
| 104 | 98 | } |
| 105 | 99 | |
| 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); | |
| 109 | 103 | fprintf(stderr, "問題フォントの読み込みに失敗。\n"); |
| 110 | 104 | exit(1); |
| 111 | 105 | } |
| 112 | 106 | |
| 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); | |
| 116 | 110 | fprintf(stderr, "解決フォントの読み込みに失敗。\n"); |
| 117 | 111 | exit(1); |
| 118 | 112 | } |
| 119 | 113 | |
| 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); | |
| 122 | 116 | fprintf(stderr, "色の役割に失敗。\n"); |
| 123 | 117 | exit(1); |
| 124 | 118 | } |
| 125 | 119 | |
| 126 | - XMapWindow(display, window); | |
| 120 | + XMapWindow(ui.display, ui.window); | |
| 127 | 121 | { |
| 128 | 122 | XWindowAttributes attr; |
| 129 | - XGetWindowAttributes(display, window, &attr); | |
| 123 | + XGetWindowAttributes(ui.display, ui.window, &attr); | |
| 130 | 124 | XEvent fake = { .type = Expose }; |
| 131 | - fake.xexpose.window = window; | |
| 125 | + fake.xexpose.window = ui.window; | |
| 132 | 126 | fake.xexpose.width = attr.width; |
| 133 | 127 | fake.xexpose.height = attr.height; |
| 134 | - control_expose(display, window, gc, &event, &color, font, disfont, prbfont); | |
| 128 | + control_expose(&ui, &event); | |
| 135 | 129 | } |
| 136 | 130 | |
| 137 | 131 | while (isrunning) { |
| 138 | - XNextEvent(display, &event); | |
| 132 | + XNextEvent(ui.display, &event); | |
| 139 | 133 | |
| 140 | 134 | switch (event.type) { |
| 141 | 135 | case Expose: |
| 142 | 136 | 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); | |
| 145 | 139 | break; |
| 146 | 140 | case ButtonPress: |
| 147 | 141 | 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); | |
| 149 | 143 | break; |
| 150 | 144 | } |
| 151 | 145 | case ButtonRelease: |
| 152 | 146 | 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); | |
| 154 | 148 | break; |
| 155 | 149 | } |
| 156 | 150 | case KeyPress: |
| 157 | - handle_key_press(display, window, gc, &event, &color, font, disfont, prbfont); | |
| 151 | + handle_key_press(&ui, &event); | |
| 158 | 152 | break; |
| 159 | 153 | // case MotionNotify: |
| 160 | - // handle_mouse_hover(display, window, gc, &event, &color, font); | |
| 154 | + // handle_mouse_hover(&ui, &event); | |
| 161 | 155 | // break; |
| 162 | 156 | case ClientMessage: |
| 163 | 157 | // WM_DELETE_WINDOW |
| @@ -165,7 +159,7 @@ | ||
| 165 | 159 | } |
| 166 | 160 | } |
| 167 | 161 | |
| 168 | - cleanup(display, window, gc, &color, &btncolor, font, disfont, prbfont, colormap, visual, backbuf); | |
| 162 | + cleanup(&ui); | |
| 169 | 163 | |
| 170 | 164 | return 0; |
| 171 | 165 | } |
| @@ -120,78 +120,85 @@ | ||
| 120 | 120 | return res; |
| 121 | 121 | } |
| 122 | 122 | |
| 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) { | |
| 125 | 124 | if (e->type != Expose && e->type != ConfigureNotify) return; |
| 126 | 125 | |
| 127 | 126 | XWindowAttributes attr; |
| 128 | - XGetWindowAttributes(dpy, wnd, &attr); | |
| 127 | + XGetWindowAttributes(ui->display, ui->window, &attr); | |
| 129 | 128 | int w = attr.width; |
| 130 | 129 | int h = attr.height; |
| 131 | 130 | |
| 132 | - if (backbuf != None) { | |
| 133 | - XFreePixmap(dpy, backbuf); | |
| 131 | + if (ui->backbuf != None) { | |
| 132 | + XFreePixmap(ui->display, ui->backbuf); | |
| 133 | + ui->backbuf = None; | |
| 134 | 134 | } |
| 135 | 135 | |
| 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))); | |
| 137 | 138 | |
| 138 | - if (backbuf == None) { | |
| 139 | - backbuf = wnd; | |
| 139 | + if (ui->backbuf == None) { | |
| 140 | + fprintf(stderr, "バックバッファ作成失敗!\n"); | |
| 141 | + ui->backbuf = ui->window; | |
| 140 | 142 | } |
| 141 | 143 | |
| 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))); | |
| 144 | 149 | if (!backdraw) { |
| 145 | 150 | fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n"); |
| 146 | - XFreePixmap(dpy, backbuf); | |
| 147 | - backbuf = None; | |
| 151 | + XFreePixmap(ui->display, ui->backbuf); | |
| 152 | + ui->backbuf = None; | |
| 148 | 153 | return; |
| 149 | 154 | } |
| 150 | 155 | |
| 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); | |
| 153 | 158 | |
| 154 | 159 | // 出力 |
| 155 | - if (displaytxt[0] != '\0' && df) { | |
| 160 | + if (displaytxt[0] != '\0' && ui->disfont) { | |
| 156 | 161 | const FcChar8 *text = (const FcChar8 *)displaytxt; |
| 157 | 162 | int len = strlen((const char *)text); |
| 158 | 163 | |
| 159 | 164 | XGlyphInfo extents; |
| 160 | - XftTextExtentsUtf8(dpy, df, text, len, &extents); | |
| 165 | + XftTextExtentsUtf8(ui->display, ui->disfont, text, len, &extents); | |
| 161 | 166 | |
| 162 | 167 | int tx = w - 20 - extents.xOff; |
| 163 | 168 | int ty = 160; |
| 164 | 169 | |
| 165 | 170 | 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)), | |
| 168 | 173 | "#ee4030", &discol); |
| 169 | 174 | |
| 170 | - XftDrawStringUtf8(backdraw, &discol, df, tx, ty, text, len); | |
| 175 | + XftDrawStringUtf8(backdraw, &discol, ui->disfont, tx, ty, text, len); | |
| 171 | 176 | |
| 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); | |
| 174 | 179 | } |
| 175 | 180 | |
| 176 | - if (displayprb[0] != '\0' && pf) { | |
| 181 | + if (displayprb[0] != '\0' && ui->prbfont) { | |
| 177 | 182 | const FcChar8 *text = (const FcChar8 *)displayprb; |
| 178 | 183 | int len = strlen((const char *)text); |
| 179 | 184 | |
| 180 | 185 | XGlyphInfo extents; |
| 181 | - XftTextExtentsUtf8(dpy, pf, text, len, &extents); | |
| 186 | + XftTextExtentsUtf8(ui->display, ui->prbfont, text, len, &extents); | |
| 182 | 187 | |
| 183 | 188 | int tx = w - 20 - extents.xOff; |
| 184 | 189 | int ty = 80; |
| 185 | 190 | |
| 186 | 191 | 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); | |
| 190 | 196 | |
| 191 | - XftDrawStringUtf8(backdraw, &discol, pf, tx, ty, text, len); | |
| 197 | + XftDrawStringUtf8(backdraw, &discol, ui->prbfont, tx, ty, text, len); | |
| 192 | 198 | |
| 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); | |
| 195 | 202 | } |
| 196 | 203 | |
| 197 | 204 | int width = 93; |
| @@ -219,50 +226,54 @@ | ||
| 219 | 226 | btn.label = label; |
| 220 | 227 | btn.pressed = 0; |
| 221 | 228 | |
| 222 | - drawbuttons(dpy, backbuf, gc, &btn, backdraw, color, f); | |
| 229 | + drawbuttons(ui, &btn, backdraw); | |
| 223 | 230 | } |
| 224 | 231 | } |
| 225 | 232 | |
| 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); | |
| 227 | 234 | XftDrawDestroy(backdraw); |
| 228 | - XFlush(dpy); | |
| 235 | + XFlush(ui->display); | |
| 229 | 236 | } |
| 230 | 237 | |
| 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) { | |
| 232 | 239 | SuwaButton *btn = find_button_at(mx, my); |
| 233 | 240 | if (!btn) return; |
| 234 | 241 | |
| 235 | 242 | 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))); | |
| 238 | 246 | if (!backdraw) { |
| 239 | 247 | fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n"); |
| 240 | - XFreePixmap(dpy, backbuf); | |
| 241 | - backbuf = None; | |
| 248 | + XFreePixmap(ui->display, ui->backbuf); | |
| 249 | + ui->backbuf = None; | |
| 242 | 250 | return; |
| 243 | 251 | } |
| 244 | 252 | |
| 245 | - drawbuttons(dpy, backbuf, gc, btn, backdraw, col, f); | |
| 246 | - XFlush(dpy); | |
| 253 | + drawbuttons(ui, btn, backdraw); | |
| 254 | + XFlush(ui->display); | |
| 247 | 255 | } |
| 248 | 256 | |
| 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) { | |
| 250 | 258 | SuwaButton *btn = find_button_at(mx, my); |
| 251 | 259 | if (!btn) return; |
| 252 | 260 | |
| 253 | 261 | btn->pressed = 0; |
| 254 | 262 | |
| 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))); | |
| 257 | 266 | if (!backdraw) { |
| 258 | 267 | fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n"); |
| 259 | - XFreePixmap(dpy, backbuf); | |
| 260 | - backbuf = None; | |
| 268 | + XFreePixmap(ui->display, ui->backbuf); | |
| 269 | + ui->backbuf = None; | |
| 261 | 270 | return; |
| 262 | 271 | } |
| 263 | - drawbuttons(dpy, backbuf, gc, btn, backdraw, col, f); | |
| 272 | + | |
| 273 | + ui->target = ui->backbuf; | |
| 274 | + drawbuttons(ui, btn, backdraw); | |
| 264 | 275 | XftDrawDestroy(backdraw); |
| 265 | - XFlush(dpy); | |
| 276 | + XFlush(ui->display); | |
| 266 | 277 | |
| 267 | 278 | const char *label = btn->label; |
| 268 | 279 | if (strcmp(label, "C") == 0) { |
| @@ -283,10 +294,10 @@ | ||
| 283 | 294 | append_to_input(label[0]); |
| 284 | 295 | } |
| 285 | 296 | |
| 286 | - control_expose(dpy, wnd, gc, &(XEvent){.type = Expose}, col, f, df, pf); | |
| 297 | + control_expose(ui, &(XEvent){.type = Expose}); | |
| 287 | 298 | } |
| 288 | 299 | |
| 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) { | |
| 290 | 301 | KeySym keysym; |
| 291 | 302 | char buf[32]; |
| 292 | 303 | int len; |
| @@ -369,21 +380,21 @@ | ||
| 369 | 380 | return; |
| 370 | 381 | |
| 371 | 382 | redraw: |
| 372 | - control_expose(dpy, wnd, gc, &(XEvent){.type = Expose}, col, f, df, pf); | |
| 383 | + control_expose(ui, &(XEvent){.type = Expose}); | |
| 373 | 384 | } |
| 374 | 385 | |
| 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) { | |
| 376 | 387 | // SuwaButton *hover = find_button_at(event->xmotion.x, event->xmotion.y); |
| 377 | 388 | |
| 378 | 389 | // if (hover != hovered_btn) { |
| 379 | 390 | // if (hovered_btn) { |
| 380 | 391 | // hovered_btn->pressed = 0; |
| 381 | -// redraw_single_button(dpy, wnd, gc, col, f, hovered_btn); | |
| 392 | +// redraw_single_button(&ui, hovered_btn); | |
| 382 | 393 | // } |
| 383 | 394 | // hovered_btn = hover; |
| 384 | 395 | |
| 385 | 396 | // if (hovered_btn) { |
| 386 | -// redraw_single_button(dpy, wnd, gc, col, f, hovered_btn); | |
| 397 | +// redraw_single_button(&ui, hovered_btn); | |
| 387 | 398 | // } |
| 388 | 399 | // } |
| 389 | 400 | // } |
| @@ -1,10 +1,9 @@ | ||
| 1 | 1 | #pragma once |
| 2 | 2 | |
| 3 | -#include <X11/Xlib.h> | |
| 4 | -#include <X11/Xft/Xft.h> | |
| 3 | +#include "ui.h" | |
| 5 | 4 | |
| 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); |
| @@ -3,28 +3,28 @@ | ||
| 3 | 3 | |
| 4 | 4 | #include <string.h> |
| 5 | 5 | |
| 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) { | |
| 8 | 7 | 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); | |
| 11 | 10 | |
| 12 | 11 | // 文字の中央に |
| 13 | - if (btn->label && font && xftdraw) { | |
| 12 | + if (btn->label && ui->font && xftdraw) { | |
| 14 | 13 | const FcChar8 *str = (const FcChar8 *)btn->label; |
| 15 | 14 | int len = strlen((const char *)str); |
| 16 | 15 | |
| 17 | 16 | XGlyphInfo extents; |
| 18 | - XftTextExtentsUtf8(dpy, font, str, len, &extents); | |
| 17 | + XftTextExtentsUtf8(ui->display, ui->font, str, len, &extents); | |
| 19 | 18 | |
| 20 | 19 | int text_w = extents.xOff; |
| 21 | - int text_h = font->ascent + font->descent; | |
| 20 | + int text_h = ui->font->ascent + ui->font->descent; | |
| 22 | 21 | |
| 23 | 22 | 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; | |
| 25 | 24 | |
| 26 | 25 | // tx -= extents.x; |
| 27 | 26 | |
| 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); | |
| 29 | 29 | } |
| 30 | 30 | } |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | #pragma once |
| 2 | 2 | |
| 3 | 3 | #include "program.h" |
| 4 | +#include "ui.h" | |
| 4 | 5 | |
| 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); |
| @@ -1,7 +1,5 @@ | ||
| 1 | 1 | #pragma once |
| 2 | 2 | |
| 3 | -#include <X11/Xlib.h> | |
| 4 | -#include <X11/Xft/Xft.h> | |
| 5 | 3 | #include "ui.h" |
| 6 | 4 | |
| 7 | 5 | #define FGCOL 0xfcfcfc |
| @@ -17,6 +15,5 @@ | ||
| 17 | 15 | extern char curinput[256]; |
| 18 | 16 | extern char displaytxt[64]; |
| 19 | 17 | extern int input_pos; |
| 20 | -extern Pixmap backbuf; | |
| 21 | 18 | extern SuwaButton *hovered_btn; |
| 22 | 19 | extern SuwaButton *pressed_btn; |
変更
src/ui.h
| @@ -1,5 +1,21 @@ | ||
| 1 | 1 | #pragma once |
| 2 | 2 | |
| 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 | + | |
| 3 | 19 | typedef struct { |
| 4 | 20 | int x, y, width, height; |
| 5 | 21 | const char *label; |
変更
src/utils.c
| @@ -1,19 +1,18 @@ | ||
| 1 | 1 | #include "utils.h" |
| 2 | 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; | |
| 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; | |
| 16 | 15 | } |
| 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); | |
| 19 | 18 | } |
変更
src/utils.h
| @@ -2,7 +2,4 @@ | ||
| 2 | 2 | |
| 3 | 3 | #include "program.h" |
| 4 | 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); | |
| 5 | +void cleanup(UiSystem *ui); |