最適化
諏訪子 <suwako@076.moe>、2026-01-17 23:52
コミット 5f5f8248d8002419fe5fd934b46269966091323c
・親 c6f2982
・ファイルを見る
11個のファイル、+213 -172
変更
Makefile
| @@ -14,9 +14,11 @@ | ||
| 14 | 14 | |
| 15 | 15 | PREFIX = /usr/local |
| 16 | 16 | |
| 17 | +# cc | clang | gcc | |
| 17 | 18 | CC = cc |
| 19 | +# lldb | gdb | |
| 20 | +DEBUGGER = lldb | |
| 18 | 21 | FILES = main.c src/*.c |
| 19 | -# FILES = main.c | |
| 20 | 22 | |
| 21 | 23 | CFLAGS = -Wall -Wextra \ |
| 22 | 24 | -I./dep/include -I/usr/include -I/usr/local/include -I/usr/X11R6/include \ |
| @@ -31,7 +33,7 @@ | ||
| 31 | 33 | |
| 32 | 34 | debug: |
| 33 | 35 | ${CC} -O0 -g ${CFLAGS} -o ${NAME} ${FILES} ${LDFLAGS} |
| 34 | - lldb -o run ${NAME} | |
| 36 | + ${DEBUGGER} -o run ${NAME} | |
| 35 | 37 | |
| 36 | 38 | develop: |
| 37 | 39 | ${CC} -O3 -g ${CFLAGS} -o ${NAME} ${FILES} ${LDFLAGS} ${SLIB} |
変更
README.md
| @@ -1,19 +1,19 @@ | ||
| 1 | 1 | # Unix Calc |
| 2 | 2 | |
| 3 | -XLibre向け電卓。 | |
| 3 | +XLibre向け電卓。\ | |
| 4 | 4 | Xorg以外、従属ライブラリを使いません。 |
| 5 | 5 | |
| 6 | 6 | ## インストールする方法 | Installation |
| 7 | 7 | ### BSD |
| 8 | 8 | ```sh |
| 9 | 9 | make |
| 10 | -doas make install | |
| 10 | +doas make install release | |
| 11 | 11 | ``` |
| 12 | 12 | |
| 13 | 13 | ### Linux |
| 14 | 14 | ```sh |
| 15 | 15 | make |
| 16 | -doas bmake install | |
| 16 | +doas bmake install release | |
| 17 | 17 | ``` |
| 18 | 18 | |
| 19 | 19 | \ |
変更
main.c
| @@ -10,6 +10,9 @@ | ||
| 10 | 10 | |
| 11 | 11 | int main() { |
| 12 | 12 | UiSystem ui; |
| 13 | + ui.isrunning = 1; | |
| 14 | + ui.window.width = 382; | |
| 15 | + ui.window.height = 534; | |
| 13 | 16 | XEvent event; |
| 14 | 17 | int screen; |
| 15 | 18 | XGCValues values; |
| @@ -24,33 +27,36 @@ | ||
| 24 | 27 | |
| 25 | 28 | int sw = DisplayWidth(ui.display, screen); |
| 26 | 29 | 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; | |
| 29 | 32 | |
| 30 | - ui.window = XCreateSimpleWindow(ui.display, | |
| 33 | + ui.xwindow = XCreateSimpleWindow(ui.display, | |
| 31 | 34 | 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) { | |
| 34 | 39 | cleanup(&ui); |
| 35 | 40 | fprintf(stderr, "ウィンドウを作成に失敗。\n"); |
| 36 | 41 | exit(1); |
| 37 | 42 | } |
| 38 | 43 | |
| 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, | |
| 40 | 46 | DefaultDepth(ui.display, screen)); |
| 41 | 47 | ui.target = ui.backbuf; |
| 42 | 48 | |
| 43 | 49 | Atom net_wm_window_type = XInternAtom(ui.display, "_NET_WM_WINDOW_TYPE", False); |
| 44 | 50 | Atom dialog = XInternAtom(ui.display, "_NET_WM_WINDOW_TYPE_DIALOG", False); |
| 45 | 51 | |
| 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, | |
| 47 | 53 | PropModeReplace, (unsigned char *)&dialog, 1); |
| 48 | 54 | |
| 49 | - XStoreName(ui.display, ui.window, disname); | |
| 55 | + XStoreName(ui.display, ui.xwindow, disname); | |
| 50 | 56 | Atom net_wm_name = XInternAtom(ui.display, "_NET_WM_NAME", False); |
| 51 | 57 | char displayname[16]; |
| 52 | 58 | 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, | |
| 54 | 60 | XInternAtom(ui.display, "UTF8_STRING", False), 8, |
| 55 | 61 | PropModeReplace, (unsigned char *)displayname, strlen(displayname)); |
| 56 | 62 | |
| @@ -58,13 +64,13 @@ | ||
| 58 | 64 | if (classHint) { |
| 59 | 65 | classHint->res_name = "unixcalc"; |
| 60 | 66 | classHint->res_class = "UnixCalc"; |
| 61 | - XSetClassHint(ui.display, ui.window, classHint); | |
| 67 | + XSetClassHint(ui.display, ui.xwindow, classHint); | |
| 62 | 68 | XFree(classHint); |
| 63 | 69 | } |
| 64 | 70 | |
| 65 | - XSetWindowBackground(ui.display, ui.window, BGCOL); | |
| 71 | + XSetWindowBackground(ui.display, ui.xwindow, BGCOL); | |
| 66 | 72 | |
| 67 | - XSelectInput(ui.display, ui.window, | |
| 73 | + XSelectInput(ui.display, ui.xwindow, | |
| 68 | 74 | ExposureMask |
| 69 | 75 | | ButtonPressMask |
| 70 | 76 | | ButtonReleaseMask |
| @@ -74,7 +80,7 @@ | ||
| 74 | 80 | // | StructureNotifyMask |
| 75 | 81 | ); |
| 76 | 82 | |
| 77 | - ui.gc = XCreateGC(ui.display, ui.window, 0, &values); | |
| 83 | + ui.gc = XCreateGC(ui.display, ui.xwindow, 0, &values); | |
| 78 | 84 | if (!ui.gc) { |
| 79 | 85 | cleanup(&ui); |
| 80 | 86 | fprintf(stderr, "GCを作成に失敗。\n"); |
| @@ -83,31 +89,78 @@ | ||
| 83 | 89 | |
| 84 | 90 | ui.visual = *DefaultVisual(ui.display, screen); |
| 85 | 91 | |
| 86 | - ui.colormap = XCreateColormap(ui.display, ui.window, &ui.visual, AllocNone); | |
| 92 | + ui.colormap = XCreateColormap(ui.display, ui.xwindow, &ui.visual, AllocNone); | |
| 87 | 93 | if (ui.colormap == None) { |
| 88 | 94 | cleanup(&ui); |
| 89 | 95 | fprintf(stderr, "カラーマップを作成に失敗。\n"); |
| 90 | 96 | exit(1); |
| 91 | 97 | } |
| 92 | 98 | |
| 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 | + } | |
| 98 | 130 | } |
| 99 | 131 | |
| 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 | + } | |
| 105 | 158 | } |
| 106 | 159 | |
| 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) { | |
| 109 | 162 | cleanup(&ui); |
| 110 | - fprintf(stderr, "解決フォントの読み込みに失敗。\n"); | |
| 163 | + fprintf(stderr, "フォントの読み込みに失敗。\n"); | |
| 111 | 164 | exit(1); |
| 112 | 165 | } |
| 113 | 166 | |
| @@ -117,24 +170,24 @@ | ||
| 117 | 170 | exit(1); |
| 118 | 171 | } |
| 119 | 172 | |
| 120 | - XMapWindow(ui.display, ui.window); | |
| 173 | + XMapWindow(ui.display, ui.xwindow); | |
| 121 | 174 | { |
| 122 | 175 | XWindowAttributes attr; |
| 123 | - XGetWindowAttributes(ui.display, ui.window, &attr); | |
| 176 | + XGetWindowAttributes(ui.display, ui.xwindow, &attr); | |
| 124 | 177 | XEvent fake = { .type = Expose }; |
| 125 | - fake.xexpose.window = ui.window; | |
| 178 | + fake.xexpose.window = ui.xwindow; | |
| 126 | 179 | fake.xexpose.width = attr.width; |
| 127 | 180 | fake.xexpose.height = attr.height; |
| 128 | 181 | control_expose(&ui, &event); |
| 129 | 182 | } |
| 130 | 183 | |
| 131 | - while (isrunning) { | |
| 184 | + while (ui.isrunning) { | |
| 132 | 185 | XNextEvent(ui.display, &event); |
| 133 | 186 | |
| 134 | 187 | switch (event.type) { |
| 135 | 188 | case Expose: |
| 136 | 189 | case ConfigureNotify: |
| 137 | - XClearWindow(ui.display, ui.window); | |
| 190 | + XClearWindow(ui.display, ui.xwindow); | |
| 138 | 191 | control_expose(&ui, &event); |
| 139 | 192 | break; |
| 140 | 193 | case ButtonPress: |
| @@ -4,8 +4,12 @@ | ||
| 4 | 4 | |
| 5 | 5 | #include "control.h" |
| 6 | 6 | #include "display.h" |
| 7 | -#include "program.h" | |
| 8 | 7 | |
| 8 | +char curinput[64] = {0}; | |
| 9 | +int input_pos = 0; | |
| 10 | +int initialized = 0; | |
| 11 | +XftColor buttonColor; | |
| 12 | + | |
| 9 | 13 | static const char *btn_labels[][10] = { |
| 10 | 14 | { NULL }, // 数字表示 |
| 11 | 15 | { "C", "±", "%", "÷" }, |
| @@ -16,7 +20,7 @@ | ||
| 16 | 20 | }; |
| 17 | 21 | |
| 18 | 22 | #define NUM_ROWS 6 |
| 19 | -#define NUM_COLS 6 | |
| 23 | +#define NUM_COLS 4 | |
| 20 | 24 | |
| 21 | 25 | static SuwaButton *find_button_at(int mx, int my) { |
| 22 | 26 | static SuwaButton found = {0}; |
| @@ -52,35 +56,35 @@ | ||
| 52 | 56 | found.y = 224 + (row - 1) * (btn_h + padding); |
| 53 | 57 | found.width = btn_w; |
| 54 | 58 | found.height = btn_h; |
| 55 | - found.label = btn_labels[row][col]; | |
| 59 | + found.text = btn_labels[row][col]; | |
| 56 | 60 | found.pressed = 1; |
| 57 | 61 | |
| 58 | 62 | return &found; |
| 59 | 63 | } |
| 60 | 64 | |
| 61 | -void append_to_input(char c) { | |
| 65 | +void append_to_input(UiSystem *ui, char c) { | |
| 62 | 66 | if ((unsigned long)input_pos >= sizeof(curinput) - 2) return; |
| 63 | - strcpy(displayprb, ""); | |
| 67 | + strcpy(ui->problemLabel.text, ""); | |
| 64 | 68 | curinput[input_pos++] = c; |
| 65 | 69 | curinput[input_pos] = '\0'; |
| 66 | 70 | |
| 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'; | |
| 69 | 73 | } |
| 70 | 74 | |
| 71 | -void clear_calculator(void) { | |
| 72 | - strcpy(displayprb, ""); | |
| 75 | +void clear_calculator(UiSystem *ui) { | |
| 76 | + strcpy(ui->problemLabel.text, ""); | |
| 73 | 77 | curinput[0] = '\0'; |
| 74 | 78 | input_pos = 0; |
| 75 | - strcpy(displaytxt, "0"); | |
| 79 | + strcpy(ui->resLabel.text, "0"); | |
| 76 | 80 | } |
| 77 | 81 | |
| 78 | -double evaluate_simple(const char *expr) { | |
| 82 | +double evaluate_simple(UiSystem *ui, const char *expr) { | |
| 79 | 83 | double res = 0.0; |
| 80 | 84 | double cur = 0.0; |
| 81 | 85 | char op = '+'; |
| 82 | 86 | int i = 0; |
| 83 | - snprintf(displayprb, sizeof(displayprb), "%s=", expr); | |
| 87 | + snprintf(ui->problemLabel.text, sizeof(ui->problemLabel.text), "%s=", expr); | |
| 84 | 88 | |
| 85 | 89 | while (expr[i]) { |
| 86 | 90 | if (expr[i] == ' ') { i++; continue; } |
| @@ -124,7 +128,7 @@ | ||
| 124 | 128 | if (e->type != Expose && e->type != ConfigureNotify) return; |
| 125 | 129 | |
| 126 | 130 | XWindowAttributes attr; |
| 127 | - XGetWindowAttributes(ui->display, ui->window, &attr); | |
| 131 | + XGetWindowAttributes(ui->display, ui->xwindow, &attr); | |
| 128 | 132 | int w = attr.width; |
| 129 | 133 | int h = attr.height; |
| 130 | 134 | |
| @@ -133,12 +137,12 @@ | ||
| 133 | 137 | ui->backbuf = None; |
| 134 | 138 | } |
| 135 | 139 | |
| 136 | - ui->backbuf = XCreatePixmap(ui->display, ui->window, w, h, | |
| 140 | + ui->backbuf = XCreatePixmap(ui->display, ui->xwindow, w, h, | |
| 137 | 141 | DefaultDepth(ui->display, DefaultScreen(ui->display))); |
| 138 | 142 | |
| 139 | 143 | if (ui->backbuf == None) { |
| 140 | 144 | fprintf(stderr, "バックバッファ作成失敗!\n"); |
| 141 | - ui->backbuf = ui->window; | |
| 145 | + ui->backbuf = ui->xwindow; | |
| 142 | 146 | } |
| 143 | 147 | |
| 144 | 148 | ui->target = ui->backbuf; |
| @@ -157,80 +161,73 @@ | ||
| 157 | 161 | XFillRectangle(ui->display, ui->backbuf, ui->gc, 0, 0, w, h); |
| 158 | 162 | |
| 159 | 163 | // 出力 |
| 160 | - if (displaytxt[0] != '\0' && ui->disfont) { | |
| 161 | - const FcChar8 *text = (const FcChar8 *)displaytxt; | |
| 162 | - int len = strlen((const char *)text); | |
| 163 | - | |
| 164 | + { | |
| 164 | 165 | 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); | |
| 166 | 169 | |
| 167 | 170 | int tx = w - 20 - extents.xOff; |
| 168 | - int ty = 160; | |
| 169 | 171 | |
| 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); | |
| 179 | 176 | } |
| 180 | 177 | |
| 181 | - if (displayprb[0] != '\0' && ui->prbfont) { | |
| 182 | - const FcChar8 *text = (const FcChar8 *)displayprb; | |
| 183 | - int len = strlen((const char *)text); | |
| 184 | - | |
| 178 | + { | |
| 185 | 179 | 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); | |
| 187 | 183 | |
| 188 | 184 | int tx = w - 20 - extents.xOff; |
| 189 | - int ty = 80; | |
| 190 | 185 | |
| 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); | |
| 202 | 190 | } |
| 203 | 191 | |
| 192 | + for (int i = 0; i < 64; ++i) ui->buttons[i] = NULL; | |
| 204 | 193 | int width = 93; |
| 205 | 194 | int height = 60; |
| 206 | 195 | int padding = 2; |
| 207 | 196 | printf("ウィンドウ: (%dx%d)\n", attr.width, attr.height); |
| 208 | 197 | |
| 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) { | |
| 210 | 207 | int y = 162 + row * (height + padding); |
| 211 | - if (y + height > h) break; | |
| 212 | 208 | |
| 213 | - int cols = 4; | |
| 214 | - | |
| 215 | - for (int col = 0; col < cols; ++col) { | |
| 209 | + for (int col = 0; col < NUM_COLS; ++col) { | |
| 216 | 210 | const char *label = btn_labels[row][col]; |
| 217 | 211 | if (!label) continue; |
| 218 | 212 | |
| 219 | 213 | int x = padding + col * (width + padding); |
| 220 | 214 | |
| 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; | |
| 228 | 225 | |
| 229 | - drawbuttons(ui, &btn, backdraw); | |
| 226 | + drawbuttons(ui, btn, backdraw); | |
| 230 | 227 | } |
| 231 | 228 | } |
| 232 | 229 | |
| 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); | |
| 234 | 231 | XftDrawDestroy(backdraw); |
| 235 | 232 | XFlush(ui->display); |
| 236 | 233 | } |
| @@ -275,23 +272,26 @@ | ||
| 275 | 272 | XftDrawDestroy(backdraw); |
| 276 | 273 | XFlush(ui->display); |
| 277 | 274 | |
| 278 | - const char *label = btn->label; | |
| 275 | + const char *label = btn->text; | |
| 279 | 276 | if (strcmp(label, "C") == 0) { |
| 280 | - clear_calculator(); | |
| 277 | + clear_calculator(ui); | |
| 281 | 278 | } else if (strcmp(label, "<") == 0) { |
| 282 | 279 | curinput[--input_pos] = '\0'; |
| 283 | - strcpy(displaytxt, curinput[0] ? curinput : "0"); | |
| 280 | + strcpy(ui->resLabel.text, curinput[0] ? curinput : "0"); | |
| 284 | 281 | } else if (strcmp(label, "=") == 0) { |
| 285 | - double res = evaluate_simple(curinput); | |
| 282 | + double res = evaluate_simple(ui, curinput); | |
| 286 | 283 | if (isnan(res)) { |
| 287 | - strncpy(displaytxt, "Error", 5); | |
| 284 | + strncpy(ui->resLabel.text, "Error", 5); | |
| 288 | 285 | } 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)); | |
| 291 | 288 | input_pos = strlen(curinput); |
| 289 | + strcpy(curinput, ""); | |
| 290 | + curinput[0] = '\0'; | |
| 291 | + input_pos = 0; | |
| 292 | 292 | } |
| 293 | 293 | } else if (strlen(label) == 1) { |
| 294 | - append_to_input(label[0]); | |
| 294 | + append_to_input(ui, label[0]); | |
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | control_expose(ui, &(XEvent){.type = Expose}); |
| @@ -307,7 +307,7 @@ | ||
| 307 | 307 | len = XLookupString(&event->xkey, buf, sizeof(buf), &keysym, NULL); |
| 308 | 308 | |
| 309 | 309 | if (keysym == XK_Q) { |
| 310 | - isrunning = 0; | |
| 310 | + ui->isrunning = 0; | |
| 311 | 311 | return; |
| 312 | 312 | } |
| 313 | 313 | |
| @@ -341,16 +341,16 @@ | ||
| 341 | 341 | } |
| 342 | 342 | |
| 343 | 343 | if (keysym == XK_C) { |
| 344 | - clear_calculator(); | |
| 344 | + clear_calculator(ui); | |
| 345 | 345 | goto redraw; |
| 346 | 346 | } |
| 347 | 347 | |
| 348 | 348 | if (keysym == XK_Return || keysym == XK_KP_Enter) { |
| 349 | - double res = evaluate_simple(curinput); | |
| 349 | + double res = evaluate_simple(ui, curinput); | |
| 350 | 350 | if (isnan(res)) { |
| 351 | - strncpy(displaytxt, "Error", 5); | |
| 351 | + strncpy(ui->resLabel.text, "Error", 5); | |
| 352 | 352 | } else { |
| 353 | - snprintf(displaytxt, sizeof(displaytxt), "%.8g", res); | |
| 353 | + snprintf(ui->resLabel.text, sizeof(ui->resLabel.text), "%.8g", res); | |
| 354 | 354 | } |
| 355 | 355 | goto redraw; |
| 356 | 356 | } |
| @@ -359,7 +359,7 @@ | ||
| 359 | 359 | || (keysym == XK_Delete && input_pos > 0) |
| 360 | 360 | || (keysym == XK_X && input_pos > 0)) { |
| 361 | 361 | curinput[--input_pos] = '\0'; |
| 362 | - strcpy(displaytxt, curinput[0] ? curinput : "0"); | |
| 362 | + strcpy(ui->resLabel.text, curinput[0] ? curinput : "0"); | |
| 363 | 363 | goto redraw; |
| 364 | 364 | } |
| 365 | 365 | |
| @@ -373,7 +373,7 @@ | ||
| 373 | 373 | else if (keysym == XK_KP_Decimal || keysym == XK_period) c = '.'; |
| 374 | 374 | |
| 375 | 375 | if (c) { |
| 376 | - append_to_input(c); | |
| 376 | + append_to_input(ui, c); | |
| 377 | 377 | goto redraw; |
| 378 | 378 | } |
| 379 | 379 |
| @@ -1,17 +1,16 @@ | ||
| 1 | 1 | #include "display.h" |
| 2 | -#include "program.h" | |
| 3 | 2 | |
| 4 | 3 | #include <string.h> |
| 5 | 4 | |
| 6 | 5 | 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; | |
| 8 | 7 | XSetForeground(ui->display, ui->gc, curbg); |
| 9 | 8 | XFillRectangle(ui->display, ui->target, ui->gc, btn->x, btn->y, |
| 10 | 9 | btn->width, btn->height); |
| 11 | 10 | |
| 12 | 11 | // 文字の中央に |
| 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; | |
| 15 | 14 | int len = strlen((const char *)str); |
| 16 | 15 | |
| 17 | 16 | XGlyphInfo extents; |
| @@ -23,9 +22,7 @@ | ||
| 23 | 22 | int tx = btn->x + (btn->width - text_w) / 2; |
| 24 | 23 | int ty = btn->y + (btn->height - text_h) / 2 + ui->font->ascent; |
| 25 | 24 | |
| 26 | - // tx -= extents.x; | |
| 27 | - | |
| 28 | 25 | XftDrawStringUtf8(xftdraw, &ui->color, ui->font, tx, ty, |
| 29 | - (FcChar8 *)btn->label, len); | |
| 26 | + (FcChar8 *)btn->text, len); | |
| 30 | 27 | } |
| 31 | 28 | } |
| @@ -1,6 +1,5 @@ | ||
| 1 | 1 | #pragma once |
| 2 | 2 | |
| 3 | -#include "program.h" | |
| 4 | 3 | #include "ui.h" |
| 5 | 4 | |
| 6 | 5 | 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 @@ | ||
| 3 | 3 | #include <X11/Xlib.h> |
| 4 | 4 | #include <X11/Xft/Xft.h> |
| 5 | 5 | |
| 6 | +#define FGCOL 0xfcfcfc | |
| 7 | +#define BGCOL 0x232020 | |
| 8 | +#define BTSEL 0xb61729 | |
| 9 | +#define BTCOL 0xee4030 | |
| 10 | +#define BTHVR 0xf35869 | |
| 11 | + | |
| 6 | 12 | 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; | |
| 7 | 34 | Display *display; |
| 8 | - Window window; | |
| 35 | + Window xwindow; | |
| 9 | 36 | Drawable target; |
| 10 | 37 | GC gc; |
| 11 | 38 | Visual visual; |
| 12 | - XftDraw *xftdraw; | |
| 13 | - XftColor color, btncolor, textcolor; | |
| 14 | - XftFont *font, *prbfont, *disfont; | |
| 39 | + XftColor color; | |
| 40 | + XftFont *font; | |
| 15 | 41 | Colormap colormap; |
| 16 | 42 | Pixmap backbuf; |
| 43 | + SuwaWindow window; | |
| 44 | + SuwaLabel resLabel; | |
| 45 | + SuwaLabel problemLabel; | |
| 46 | + SuwaButton *buttons[32]; | |
| 17 | 47 | } 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 @@ | ||
| 1 | 1 | #include "utils.h" |
| 2 | 2 | |
| 3 | 3 | 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); | |
| 12 | 7 | if (ui->font) XftFontClose(ui->display, ui->font); |
| 8 | + | |
| 9 | + // その他 | |
| 13 | 10 | if (ui->gc) XFreeGC(ui->display, ui->gc); |
| 14 | 11 | if (ui->backbuf) { |
| 15 | 12 | XFreePixmap(ui->display, ui->backbuf); |
| 16 | 13 | ui->backbuf = None; |
| 17 | 14 | } |
| 18 | - if (ui->window) XDestroyWindow(ui->display, ui->window); | |
| 15 | + if (ui->xwindow) XDestroyWindow(ui->display, ui->xwindow); | |
| 19 | 16 | if (ui->display) XCloseDisplay(ui->display); |
| 20 | 17 | } |
変更
src/utils.h
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | #pragma once |
| 2 | 2 | |
| 3 | -#include "program.h" | |
| 3 | +#include "ui.h" | |
| 4 | 4 | |
| 5 | 5 | void cleanup(UiSystem *ui); |