suwako / UnixCalc アーカイブ

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

master
ブランチ master
UnixCalc / main.c
5.1 KB、146行 生 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 <stdio.h>
38
39#include <X11/Xatom.h>
40#include "control.h"
41
42const char *sofname = "ucalc";
43const char *version = "0.0.0";
44const char *disname = "Unix Calc";
45
46static Time lasthovertime = 0;
47
48int main() {
49#if defined(__OpenBSD__)
50 SuwaWindow window = suwaui_create_window(
51 500, 400, 382, 534,
52 disname, version, "unixcalc", "UnixCalc",
53 "Noto Sans CJK-8", COLOR_BS,
54 0, 1, 0);
55 SuwaLabel resLabel = suwaui_add_label(&window, 20, 180, 0, 0, "0",
56 "Noto Sans CJK-48", COLOR_NS, 1);
57 SuwaLabel problemLabel = suwaui_add_label(&window, 20, 80, 0, 0, "",
58 "Noto Sans CJK-12", COLOR_DS, 1);
59#elif defined(__FreeBSD__)
60 SuwaWindow window = suwaui_create_window(
61 20, 40, 382, 534,
62 disname, version, "unixcalc", "UnixCalc",
63 "Noto Sans CJK-12", COLOR_BS,
64 0, 1, 0);
65 SuwaLabel resLabel = suwaui_add_label(&window, 20, 180, 0, 0, "0",
66 "Noto Sans CJK-72", COLOR_NS, 1);
67 SuwaLabel problemLabel = suwaui_add_label(&window, 20, 80, 0, 0, "",
68 "Noto Sans CJK-24", COLOR_DS, 1);
69#endif
70
71 CtrlLabels lbl = {
72 .res = &resLabel,
73 .problem = &problemLabel
74 };
75
76 XMapWindow(window.display, window.xwindow);
77 {
78 XWindowAttributes attr;
79 XGetWindowAttributes(window.display, window.xwindow, &attr);
80 XEvent fake = { .type = Expose };
81 fake.xexpose.window = window.xwindow;
82 fake.xexpose.width = attr.width;
83 fake.xexpose.height = attr.height;
84 control_expose(&window, &lbl, &(SuwaButton){0});
85 }
86
87 while (window.isrunning) {
88 XNextEvent(window.display, &window.event);
89
90 switch (window.event.type) {
91 case Expose:
92 case ConfigureNotify:
93 XClearWindow(window.display, window.xwindow);
94 control_expose(&window, &lbl, &(SuwaButton){0});
95 break;
96 case ButtonPress:
97 if (window.event.xbutton.button == Button1) {
98 int x = window.event.xbutton.x;
99 int y = window.event.xbutton.y;
100 handle_button_press(&window, &lbl, x, y);
101 break;
102 }
103 case ButtonRelease:
104 if (window.event.xbutton.button == Button1) {
105 int x = window.event.xbutton.x;
106 int y = window.event.xbutton.y;
107 handle_button_release(&window, &lbl, x, y);
108 break;
109 }
110 case KeyPress:
111 handle_key_press(&window, &lbl);
112 break;
113 case MotionNotify:
114 Time now = window.event.xmotion.time;
115 if (now - lasthovertime < 40) break;
116 if (now - lasthovertime > 50) {
117 reset_mouse_hover(&window, &lbl);
118 }
119 lasthovertime = now;
120 Window root, child;
121 int root_x, root_y,
122 win_x, win_y;
123 unsigned int mask;
124
125 XQueryPointer(window.display, window.xwindow, &root, &child,
126 &root_x, &root_y, &win_x, &win_y, &mask);
127 if (window.event.xmotion.state == 0) {
128 break;
129 }
130 handle_mouse_hover(&window, &lbl, win_x, win_y);
131 break;
132 case LeaveNotify:
133 reset_mouse_hover(&window, &lbl);
134 break;
135 case ClientMessage:
136 // WM_DELETE_WINDOW
137 break;
138 }
139 }
140
141 suwaui_del_label(&window, &problemLabel);
142 suwaui_del_label(&window, &resLabel);
143 suwaui_destroy_window(&window);
144
145 return 0;
146}