suwako / UnixCalc アーカイブ

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

master
ブランチ master
UnixCalc / control.c
12.3 KB、405行 生 Blame 履歴
1/**************************************************************************************
2# 076 License
3
4Copyright (c) 2026 テクニカル諏訪子
5
6Permission is hereby granted to any person obtaining a copy of the software
7UnixCalc (the "Software") to use, modify, merge, copy, publish, distribute,
8sublicense, and/or sell copies of the Software, subject to the following conditions:
9
10 1. **Origin Attribution**:
11 - You must not misrepresent the origin of the Software; you must not claim
12 you created the original Software.
13 - If the Software is used in a product, you must either:
14 a. Provide clear attribution in the product's documentation, user interface,
15 or other visible areas, **OR**
16 b. Pay the original developers a fee they specify in writing.
17 2. **Usage Restriction**:
18 - The Software, or any derivative works, dependencies, or libraries
19 incorporating it, must not be used for censorship or to suppress freedom of
20 speech, expression, or creativity. Prohibited uses include, but are not
21 limited to:
22 - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
23 ideas, or objective reality.
24 - Tools or systems designed to restrict access to information or
25 artistic works.
26 3. **Notice Preservation**:
27 - This license and the above copyright notice must remain intact in all copies
28 of the source code.
29
30THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
31INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
32PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
33HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
34CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
35OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36**************************************************************************************/
37#include <X11/keysym.h>
38#include <math.h>
39#include <ctype.h>
40
41#include "control.h"
42
43#define NUM_ROWS 6
44#define NUM_COLS 4
45
46#define DESIGN_WIDTH 382
47#define DESIGN_HEIGHT 534
48
49#define DESIGN_BUTTON_AREA_Y 224
50#define DESIGN_BUTTON_WIDTH 93
51#define DESIGN_BUTTON_HEIGHT 60
52#define DESIGN_PADDING 2
53
54char curinput[64] = {0};
55int input_pos = 0;
56int initialized = 0;
57SuwaButton *lasthover = NULL;
58
59static const char *btn_labels[][10] = {
60 { NULL }, // 数字表示
61 { "C", "±", "%", "÷" },
62 { "7", "8", "9", "×" },
63 { "4", "5", "6", "-" },
64 { "1", "2", "3", "+" },
65 { "0", ".", "=", "<" },
66};
67
68static SuwaButton *find_button_at(SuwaWindow *window, int mx, int my) {
69 static SuwaButton found = {0};
70
71 float scale = get_scale(window);
72 if (scale <= 0) scale = 1.0f;
73
74 int btn_w = (int)(DESIGN_BUTTON_WIDTH * scale + .5f);
75 int btn_h = (int)(DESIGN_BUTTON_HEIGHT * scale + .5f);
76 int padding = (int)(DESIGN_PADDING * scale + .5f);;
77 int start_y = (int)(DESIGN_BUTTON_AREA_Y * scale + .5f);;
78
79 int row = -1;
80 int col = -1;
81
82 for (int r = 1; r < NUM_ROWS; ++r) {
83 int y_start = start_y + (r - 1) * btn_h + padding;
84 if (my >= y_start && my < y_start + btn_h) {
85 row = r;
86 break;
87 }
88 }
89
90 if (row < 0) return NULL;
91
92 for (int c = 0; c < NUM_COLS; ++c) {
93 int x_start = padding + c * (btn_w + padding);
94 if (mx >= x_start && mx < x_start + btn_w) {
95 col = c;
96 break;
97 }
98 }
99
100 if (col < 0) return NULL;
101
102 found.x = padding + col * (btn_w + padding);
103 found.y = start_y + (row - 1) * (btn_h + padding);
104 found.width = btn_w;
105 found.height = btn_h;
106 found.text = btn_labels[row][col];
107 found.row = row;
108 found.col = col;
109
110 return &found;
111}
112
113void append_to_input(CtrlLabels *labels, char c) {
114 if ((unsigned long)input_pos >= sizeof(curinput) - 2) return;
115 strcpy(labels->problem->text, "");
116 curinput[input_pos++] = c;
117 curinput[input_pos] = '\0';
118
119 strncpy(labels->res->text, curinput, sizeof(labels->res->text) - 1);
120 labels->res->text[sizeof(labels->res->text) - 1] = '\0';
121}
122
123void clear_calculator(CtrlLabels *labels) {
124 strcpy(labels->problem->text, "");
125 curinput[0] = '\0';
126 input_pos = 0;
127 strcpy(labels->res->text, "0");
128}
129
130double evaluate_simple(CtrlLabels *labels, const char *expr) {
131 double res = 0.0;
132 double cur = 0.0;
133 char op = '+';
134 int i = 0;
135 snprintf(labels->problem->text, sizeof(labels->problem->text), "%s=", expr);
136
137 while (expr[i]) {
138 if (expr[i] == ' ') { i++; continue; }
139
140 if (isdigit(expr[i]) || expr[i] == '.') {
141 char numbuf[32];
142 int j = 0;
143 while (isdigit(expr[i]) || expr[i] == '.') {
144 if (j < 31) numbuf[j++] = expr[i];
145 ++i;
146 }
147 numbuf[j] = '\0';
148 cur = atof(numbuf);
149 } else {
150 if (op == '+') res += cur;
151 else if (op == '-') res -= cur;
152 else if (op == '*') res *= cur;
153 else if (op == '/') {
154 if (cur == 0) return NAN;
155 res /= cur;
156 }
157
158 op = expr[i];
159 ++i;
160 cur = 0.0;
161 }
162 }
163
164 if (op == '+') res += cur;
165 else if (op == '-') res -= cur;
166 else if (op == '*') res *= cur;
167 else if (op == '/') {
168 if (cur == 0) return NAN;
169 res /= cur;
170 }
171
172 return res;
173}
174
175void initialize_basic_buttons(SuwaWindow *window, XftDraw *backdraw, float scale,
176 SuwaButton *but) {
177 int bw = (int)(DESIGN_BUTTON_WIDTH * scale + .5f);
178 int bh = (int)(DESIGN_BUTTON_HEIGHT * scale + .5f);
179 int bp = (int)(DESIGN_PADDING * scale + .5f);
180 int by = (int)(DESIGN_BUTTON_AREA_Y * scale + .5f);
181
182 XftColor buttonColor;
183 buttonColor = suwaui_set_button_fgcolor(window, COLOR_BS);
184
185 for (int row = 1; row < NUM_ROWS; ++row) {
186 for (int col = 0; col < NUM_COLS; ++col) {
187 SuwaButton btn = suwaui_add_button(bp + col * (bw + bp),
188 by + (row - 1) * (bh + bp),
189 bw, bh, btn_labels[row][col], window->font, COLOR_ND, buttonColor);
190
191 if (but != NULL && but->text != NULL) {
192 if (strcmp(but->text, btn.text) == 0) {
193 if (but->pressed == 1) btn.pressed = 1;
194 else if (but->hovered == 1) btn.hovered = 1;
195 }
196 }
197 suwaui_draw_button(window, &btn, backdraw);
198 }
199 }
200}
201
202void control_expose(SuwaWindow *window, CtrlLabels *labels, SuwaButton *button) {
203 if (window->event.type != Expose && window->event.type != ConfigureNotify) return;
204
205 XWindowAttributes attr;
206 XGetWindowAttributes(window->display, window->xwindow, &attr);
207 int w = attr.width;
208 int h = attr.height;
209
210 if (window->backbuf != None) XFreePixmap(window->display, window->backbuf);
211 window->backbuf = XCreatePixmap(window->display, window->xwindow, w, h,
212 DefaultDepth(window->display, DefaultScreen(window->display)));
213
214 if (window->backbuf == None) window->backbuf = window->xwindow;
215 window->target = window->backbuf;
216
217 XftDraw *backdraw = XftDrawCreate(window->display, window->backbuf,
218 DefaultVisual(window->display, DefaultScreen(window->display)),
219 DefaultColormap(window->display, DefaultScreen(window->display)));
220 if (!backdraw) {
221 fprintf(stderr, "Pixmap向けXftDrawの作成に失敗。\n");
222 XFreePixmap(window->display, window->backbuf);
223 window->backbuf = None;
224 return;
225 }
226
227 XSetForeground(window->display, window->gc, COLOR_BD);
228 XFillRectangle(window->display, window->backbuf, window->gc, 0, 0, w, h);
229
230 float scale = get_scale(window);
231 if (scale <= 0) scale = 1.f;
232
233 int label_padding = (int)(20 * scale + .5f);
234
235 // 出力
236 {
237 XGlyphInfo extents;
238 XftTextExtentsUtf8(window->display, labels->res->font,
239 (const FcChar8 *)labels->res->text,
240 strlen(labels->res->text), &extents);
241
242 int tx = w - label_padding - extents.xOff;
243 int ty = (int)(180 * scale + .5f);
244
245 XftDrawStringUtf8(
246 backdraw, &labels->res->fg_color, labels->res->font,
247 tx, ty, (const FcChar8 *)labels->res->text, 64);
248 }
249
250 {
251 XGlyphInfo extents;
252 XftTextExtentsUtf8(window->display, labels->problem->font,
253 (const FcChar8 *)labels->problem->text,
254 strlen(labels->problem->text), &extents);
255
256 int tx = w - label_padding - extents.xOff;
257 int ty = (int)(80 * scale + .5f);
258
259 XftDrawStringUtf8(
260 backdraw, &labels->problem->fg_color, labels->problem->font,
261 tx, ty, (const FcChar8 *)labels->problem->text, 64);
262 }
263
264 initialize_basic_buttons(window, backdraw, scale, button);
265
266 XCopyArea(window->display, window->backbuf, window->xwindow, window->gc,
267 0, 0, w, h, 0, 0);
268 XftDrawDestroy(backdraw);
269 XFlush(window->display);
270}
271
272void handle_button_press(SuwaWindow *window, CtrlLabels *labels, int mx, int my) {
273 SuwaButton *btn = find_button_at(window, mx, my);
274 if (!btn) return;
275
276 btn->pressed = 1;
277 window->event = (XEvent){.type = Expose};
278 control_expose(window, labels, btn);
279}
280
281void handle_button_release(SuwaWindow *window, CtrlLabels *labels, int mx, int my) {
282 SuwaButton *btn = find_button_at(window, mx, my);
283 if (!btn) return;
284
285 btn->pressed = 0;
286 const char *label = btn->text;
287 if (strcmp(label, "C") == 0) {
288 clear_calculator(labels);
289 } else if (strcmp(label, "×") == 0) {
290 append_to_input(labels, '*');
291 } else if (strcmp(label, "÷") == 0) {
292 append_to_input(labels, '/');
293 } else if (strcmp(label, "<") == 0) {
294 curinput[--input_pos] = '\0';
295 strcpy(labels->res->text, curinput[0] ? curinput : "0");
296 } else if (strcmp(label, "=") == 0) {
297 double res = evaluate_simple(labels, curinput);
298 if (isnan(res)) strncpy(labels->res->text, "Error", 5);
299 else snprintf(labels->res->text, sizeof(labels->res->text), "%.8g", res);
300 } else if (strlen(label) == 1) {
301 append_to_input(labels, label[0]);
302 }
303
304 window->event = (XEvent){.type = Expose};
305 control_expose(window, labels, btn);
306}
307
308void handle_key_press(SuwaWindow *window, CtrlLabels *labels) {
309 KeySym keysym;
310 char buf[32];
311 int len;
312 (void)len;
313
314 keysym = XLookupKeysym(&window->event.xkey, 0);
315 len = XLookupString(&window->event.xkey, buf, sizeof(buf), &keysym, NULL);
316
317 if (keysym == XK_Q) {
318 window->isrunning = 0;
319 return;
320 }
321
322 if (keysym == XK_B) {
323 puts("標準電卓画面");
324 }
325
326 if (keysym == XK_P) {
327 puts("プログラマー電卓画面");
328 }
329
330 if (keysym == XK_H) {
331 puts("履歴画面");
332 }
333
334 if (keysym == XK_h) {
335 puts("ヘルプ画面");
336 puts("-----");
337 puts("Q = 終了 Exit");
338 puts("X = 1文字の消し Remove 1 character");
339 puts("C = クリ了 Clear");
340 puts("h = ヘルプ画面 Help screen");
341 puts("H = 履歴画面 History screen");
342 puts("B = 標準電卓画面 Basic calculator screen");
343 puts("P = プログラマー電卓画面 Programmer calculator screen");
344 }
345
346 if (keysym == XK_C) {
347 clear_calculator(labels);
348 goto redraw;
349 }
350
351 if (keysym == XK_Return || keysym == XK_KP_Enter) {
352 double res = evaluate_simple(labels, curinput);
353 if (isnan(res)) strncpy(labels->res->text, "Error", 5);
354 else snprintf(labels->res->text, sizeof(labels->res->text), "%.8g", res);
355 goto redraw;
356 }
357
358 if ((keysym == XK_BackSpace && input_pos > 0)
359 || (keysym == XK_Delete && input_pos > 0)
360 || (keysym == XK_X && input_pos > 0)) {
361 curinput[--input_pos] = '\0';
362 strcpy(labels->res->text, curinput[0] ? curinput : "0");
363 goto redraw;
364 }
365
366 char c = 0;
367 if (keysym >= XK_0 && keysym <= XK_9) c = '0' + (keysym - XK_0);
368 else if (keysym >= XK_KP_0 && keysym <= XK_KP_9) c = '0' + (keysym - XK_0);
369 else if (keysym == XK_KP_Add || keysym == XK_plus) c = '+';
370 else if (keysym == XK_KP_Subtract || keysym == XK_minus) c = '-';
371 else if (keysym == XK_KP_Multiply || keysym == XK_asterisk) c = '*';
372 else if (keysym == XK_KP_Divide || keysym == XK_slash) c = '/';
373 else if (keysym == XK_KP_Decimal || keysym == XK_period) c = '.';
374
375 if (c) {
376 append_to_input(labels, c);
377 goto redraw;
378 }
379
380 return;
381
382redraw:
383 window->event = (XEvent){.type = Expose};
384 control_expose(window, labels, &(SuwaButton){0});
385}
386
387void reset_mouse_hover(SuwaWindow *window, CtrlLabels *labels) {
388 if (!lasthover) return;
389 lasthover->hovered = 0;
390 lasthover = NULL;
391 window->event = (XEvent){.type = Expose};
392 control_expose(window, labels, &(SuwaButton){0});
393}
394
395void handle_mouse_hover(SuwaWindow *window, CtrlLabels *labels, int mx, int my) {
396 SuwaButton *btn = find_button_at(window, mx, my);
397 if (!btn) return;
398
399 if (lasthover && lasthover != btn) lasthover->hovered = 0;
400 if (btn) btn->hovered = 1;
401 lasthover = btn;
402
403 window->event = (XEvent){.type = Expose};
404 control_expose(window, labels, btn ? btn : &(SuwaButton){0});
405}