最適化
This commit is contained in:
119
main.c
119
main.c
@@ -10,6 +10,9 @@ const char *disname = "Unix Calc";
|
||||
|
||||
int main() {
|
||||
UiSystem ui;
|
||||
ui.isrunning = 1;
|
||||
ui.window.width = 382;
|
||||
ui.window.height = 534;
|
||||
XEvent event;
|
||||
int screen;
|
||||
XGCValues values;
|
||||
@@ -24,33 +27,36 @@ int main() {
|
||||
|
||||
int sw = DisplayWidth(ui.display, screen);
|
||||
int sh = DisplayHeight(ui.display, screen);
|
||||
int window_x = (sw - window_width) / 3;
|
||||
int window_y = (sh - window_height) / 2;
|
||||
ui.window.x = (sw - ui.window.width) / 3;
|
||||
ui.window.y = (sh - ui.window.height) / 2;
|
||||
|
||||
ui.window = XCreateSimpleWindow(ui.display,
|
||||
ui.xwindow = XCreateSimpleWindow(ui.display,
|
||||
RootWindow(ui.display, screen),
|
||||
window_x, window_y, window_width, window_height, 1, BTCOL, BGCOL);
|
||||
if (!ui.window) {
|
||||
ui.window.x, ui.window.y,
|
||||
ui.window.width, ui.window.height,
|
||||
1, BTCOL, BGCOL);
|
||||
if (!ui.xwindow) {
|
||||
cleanup(&ui);
|
||||
fprintf(stderr, "ウィンドウを作成に失敗。\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ui.backbuf = XCreatePixmap(ui.display, ui.window, window_width, window_height,
|
||||
ui.backbuf = XCreatePixmap(ui.display, ui.xwindow,
|
||||
ui.window.width, ui.window.height,
|
||||
DefaultDepth(ui.display, screen));
|
||||
ui.target = ui.backbuf;
|
||||
|
||||
Atom net_wm_window_type = XInternAtom(ui.display, "_NET_WM_WINDOW_TYPE", False);
|
||||
Atom dialog = XInternAtom(ui.display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
|
||||
|
||||
XChangeProperty(ui.display, ui.window, net_wm_window_type, XA_ATOM, 32,
|
||||
XChangeProperty(ui.display, ui.xwindow, net_wm_window_type, XA_ATOM, 32,
|
||||
PropModeReplace, (unsigned char *)&dialog, 1);
|
||||
|
||||
XStoreName(ui.display, ui.window, disname);
|
||||
XStoreName(ui.display, ui.xwindow, disname);
|
||||
Atom net_wm_name = XInternAtom(ui.display, "_NET_WM_NAME", False);
|
||||
char displayname[16];
|
||||
snprintf(displayname, 16, "%s %s", disname, version);
|
||||
XChangeProperty(ui.display, ui.window, net_wm_name,
|
||||
XChangeProperty(ui.display, ui.xwindow, net_wm_name,
|
||||
XInternAtom(ui.display, "UTF8_STRING", False), 8,
|
||||
PropModeReplace, (unsigned char *)displayname, strlen(displayname));
|
||||
|
||||
@@ -58,13 +64,13 @@ int main() {
|
||||
if (classHint) {
|
||||
classHint->res_name = "unixcalc";
|
||||
classHint->res_class = "UnixCalc";
|
||||
XSetClassHint(ui.display, ui.window, classHint);
|
||||
XSetClassHint(ui.display, ui.xwindow, classHint);
|
||||
XFree(classHint);
|
||||
}
|
||||
|
||||
XSetWindowBackground(ui.display, ui.window, BGCOL);
|
||||
XSetWindowBackground(ui.display, ui.xwindow, BGCOL);
|
||||
|
||||
XSelectInput(ui.display, ui.window,
|
||||
XSelectInput(ui.display, ui.xwindow,
|
||||
ExposureMask
|
||||
| ButtonPressMask
|
||||
| ButtonReleaseMask
|
||||
@@ -74,7 +80,7 @@ int main() {
|
||||
// | StructureNotifyMask
|
||||
);
|
||||
|
||||
ui.gc = XCreateGC(ui.display, ui.window, 0, &values);
|
||||
ui.gc = XCreateGC(ui.display, ui.xwindow, 0, &values);
|
||||
if (!ui.gc) {
|
||||
cleanup(&ui);
|
||||
fprintf(stderr, "GCを作成に失敗。\n");
|
||||
@@ -83,13 +89,74 @@ int main() {
|
||||
|
||||
ui.visual = *DefaultVisual(ui.display, screen);
|
||||
|
||||
ui.colormap = XCreateColormap(ui.display, ui.window, &ui.visual, AllocNone);
|
||||
ui.colormap = XCreateColormap(ui.display, ui.xwindow, &ui.visual, AllocNone);
|
||||
if (ui.colormap == None) {
|
||||
cleanup(&ui);
|
||||
fprintf(stderr, "カラーマップを作成に失敗。\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
XWindowAttributes attr;
|
||||
XGetWindowAttributes(ui.display, ui.xwindow, &attr);
|
||||
int w = attr.width;
|
||||
|
||||
{
|
||||
ui.resLabel.font = XftFontOpenName(ui.display, screen, "Noto Sans CJK-72");
|
||||
if (!ui.resLabel.font) {
|
||||
cleanup(&ui);
|
||||
fprintf(stderr, "解決フォントの読み込みに失敗。\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ui.resLabel.text[0] = '0';
|
||||
ui.resLabel.text[1] = '\0';
|
||||
|
||||
const FcChar8 *text = (const FcChar8 *)ui.resLabel.text;
|
||||
int len = strlen((const char *)text);
|
||||
|
||||
XGlyphInfo extents;
|
||||
XftTextExtentsUtf8(ui.display, ui.resLabel.font, text, len, &extents);
|
||||
|
||||
ui.resLabel.x = w - 20 - extents.xOff;
|
||||
ui.resLabel.y = 180;
|
||||
if (!XftColorAllocName(ui.display,
|
||||
DefaultVisual(ui.display, DefaultScreen(ui.display)),
|
||||
DefaultColormap(ui.display, DefaultScreen(ui.display)),
|
||||
"#ee4030", &ui.resLabel.fg_color)) {
|
||||
cleanup(&ui);
|
||||
fprintf(stderr, "色の役割に失敗。\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
ui.problemLabel.font = XftFontOpenName(ui.display, screen, "Noto Sans CJK-24");
|
||||
if (!ui.problemLabel.font) {
|
||||
cleanup(&ui);
|
||||
fprintf(stderr, "問題フォントの読み込みに失敗。\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ui.problemLabel.text[0] = '\0';
|
||||
|
||||
const FcChar8 *text = (const FcChar8 *)ui.problemLabel.text;
|
||||
int len = strlen((const char *)text);
|
||||
|
||||
XGlyphInfo extents;
|
||||
XftTextExtentsUtf8(ui.display, ui.problemLabel.font, text, len, &extents);
|
||||
|
||||
ui.problemLabel.x = w - 20 - extents.xOff;
|
||||
ui.problemLabel.y = 80;
|
||||
if (!XftColorAllocName(ui.display,
|
||||
DefaultVisual(ui.display, DefaultScreen(ui.display)),
|
||||
DefaultColormap(ui.display, DefaultScreen(ui.display)),
|
||||
"#b61729", &ui.problemLabel.fg_color)) {
|
||||
cleanup(&ui);
|
||||
fprintf(stderr, "色の役割に失敗。\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
ui.font = XftFontOpenName(ui.display, screen, "Noto Sans CJK-12");
|
||||
if (!ui.font) {
|
||||
cleanup(&ui);
|
||||
@@ -97,44 +164,30 @@ int main() {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ui.prbfont = XftFontOpenName(ui.display, screen, "Noto Sans CJK-24");
|
||||
if (!ui.prbfont) {
|
||||
cleanup(&ui);
|
||||
fprintf(stderr, "問題フォントの読み込みに失敗。\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ui.disfont = XftFontOpenName(ui.display, screen, "Noto Sans CJK-72");
|
||||
if (!ui.disfont) {
|
||||
cleanup(&ui);
|
||||
fprintf(stderr, "解決フォントの読み込みに失敗。\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!XftColorAllocName(ui.display, &ui.visual, ui.colormap, "#232020", &ui.color)) {
|
||||
cleanup(&ui);
|
||||
fprintf(stderr, "色の役割に失敗。\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
XMapWindow(ui.display, ui.window);
|
||||
XMapWindow(ui.display, ui.xwindow);
|
||||
{
|
||||
XWindowAttributes attr;
|
||||
XGetWindowAttributes(ui.display, ui.window, &attr);
|
||||
XGetWindowAttributes(ui.display, ui.xwindow, &attr);
|
||||
XEvent fake = { .type = Expose };
|
||||
fake.xexpose.window = ui.window;
|
||||
fake.xexpose.window = ui.xwindow;
|
||||
fake.xexpose.width = attr.width;
|
||||
fake.xexpose.height = attr.height;
|
||||
control_expose(&ui, &event);
|
||||
}
|
||||
|
||||
while (isrunning) {
|
||||
while (ui.isrunning) {
|
||||
XNextEvent(ui.display, &event);
|
||||
|
||||
switch (event.type) {
|
||||
case Expose:
|
||||
case ConfigureNotify:
|
||||
XClearWindow(ui.display, ui.window);
|
||||
XClearWindow(ui.display, ui.xwindow);
|
||||
control_expose(&ui, &event);
|
||||
break;
|
||||
case ButtonPress:
|
||||
|
||||
Reference in New Issue
Block a user