| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | #include <stdio.h> |
| 38 | |
| 39 | #include <X11/Xatom.h> |
| 40 | #include "control.h" |
| 41 | |
| 42 | const char *sofname = "ucalc"; |
| 43 | const char *version = "0.0.0"; |
| 44 | const char *disname = "Unix Calc"; |
| 45 | |
| 46 | static Time lasthovertime = 0; |
| 47 | |
| 48 | int 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 | |
| 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 | } |