suwako / UnixMenu アーカイブ

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

master
ブランチ master
UnixMenu / main.c
4.5 KB、149行 生 Blame 履歴
1#include <stdio.h>
2#include <stdlib.h>
3
4#include <X11/Xatom.h>
5
6#include "src/control.h"
7#include "src/program.h"
8#include "src/display.h"
9#include "src/parser.h"
10#include "src/utils.h"
11
12XftColor color, selcolor;
13Colormap colormap;
14XftDraw *draw;
15XftFont *font;
16Visual *visual;
17
18const char *sofname = "um";
19const char *version = "0.2.1";
20const char *disname = "Unix Menu";
21
22int main() {
23 Display *display;
24 Window window;
25 XEvent event;
26 int screen;
27 GC gc = NULL;
28 XGCValues values;
29 char input[MAX_NAME_LEN] = {0};
30 int sel = 0;
31
32 fetch_programs();
33
34 display = XOpenDisplay(NULL);
35 if (display == NULL) {
36 fprintf(stderr, "画面を開けられません。\n");
37 exit(1);
38 }
39
40 screen = DefaultScreen(display);
41
42 int screen_width = DisplayWidth(display, screen);
43 int screen_height = DisplayHeight(display, screen);
44 window_width = screen_width / 4;
45 window_height = screen_height / 4;
46 int window_x = (screen_width - window_width) / 2;
47 int window_y = (screen_height - window_height) / 2;
48
49 window = XCreateSimpleWindow(display, RootWindow(display, screen),
50 window_x, window_y, window_width, window_height, 1, FGCOL, FGCOL);
51 if (!window) {
52 cleanup(display, window, gc, &color, &selcolor, draw, font, colormap, visual);
53 }
54
55 Atom net_wm_window_type = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
56 Atom dialog = XInternAtom(display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
57
58 XChangeProperty(display, window, net_wm_window_type, XA_ATOM, 32,
59 PropModeReplace, (unsigned char *)&dialog, 1);
60
61 XStoreName(display, window, disname);
62 Atom net_wm_name = XInternAtom(display, "_NET_WM_NAME", False);
63 XChangeProperty(display, window, net_wm_name,
64 XInternAtom(display, "UTF8_STRING", False), 8,
65 PropModeReplace, (unsigned char *)disname, strlen(disname));
66
67 XClassHint *classHint = XAllocClassHint();
68 if (classHint) {
69 classHint->res_name = "um";
70 classHint->res_class = "UnixMenu";
71 XSetClassHint(display, window, classHint);
72 XFree(classHint);
73 }
74
75 XSetWindowBackground(display, window, BGCOL);
76
77 XSelectInput(display, window,
78 ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask);
79
80 gc = XCreateGC(display, window, 0, &values);
81 if (!gc) {
82 cleanup(display, window, gc, &color, &selcolor, draw, font, colormap, visual);
83 fprintf(stderr, "GCを作れません。\n");
84 exit(1);
85 }
86
87 visual = DefaultVisual(display, screen);
88
89 colormap = XCreateColormap(display, window, visual, AllocNone);
90 if (colormap == None) {
91 cleanup(display, window, gc, &color, &selcolor, draw, font, colormap, visual);
92 fprintf(stderr, "カラーマップを作れません。\n");
93 exit(1);
94 }
95
96 draw = XftDrawCreate(display, window, visual, colormap);
97 if (!draw) {
98 cleanup(display, window, gc, &color, &selcolor, draw, font, colormap, visual);
99 fprintf(stderr, "ドローを作れません。\n");
100 exit(1);
101 }
102
103 font = XftFontOpenName(display, screen, "Noto Sans CJK-12");
104 if (!font) {
105 cleanup(display, window, gc, &color, &selcolor, draw, font, colormap, visual);
106 fprintf(stderr, "フォントの読み込みに失敗。\n");
107 exit(1);
108 }
109
110#if defined(__OpenBSD__)
111 if (!XftColorAllocName(display, visual, colormap, "#f1ed25", &color)) {
112 cleanup(display, window, gc, &color, &selcolor, draw, font, colormap, visual);
113 fprintf(stderr, "色の役割に失敗。\n");
114 }
115
116 if (!XftColorAllocName(display, visual, colormap, "#12120f", &selcolor)) {
117 cleanup(display, window, gc, &color, &selcolor, draw, font, colormap, visual);
118 fprintf(stderr, "選択色の役割に失敗。\n");
119 }
120#elif defined(__FreeBSD__)
121 if (!XftColorAllocName(display, visual, colormap, "#ee4030", &color)) {
122 cleanup(display, window, gc, &color, &selcolor, draw, font, colormap, visual);
123 fprintf(stderr, "色の役割に失敗。\n");
124 }
125
126 if (!XftColorAllocName(display, visual, colormap, "#120f0f", &selcolor)) {
127 cleanup(display, window, gc, &color, &selcolor, draw, font, colormap, visual);
128 fprintf(stderr, "選択色の役割に失敗。\n");
129 }
130#endif
131
132 XMapWindow(display, window);
133
134 while (1) {
135 XNextEvent(display, &event);
136
137 if (event.type == Expose || event.type == ConfigureNotify) {
138 control_expose(display, window, gc, &event, input, &sel);
139 } else if (event.type == KeyPress) {
140 control_keypress(display, window, gc, &event, input, &sel);
141 } else if (event.type == ButtonPress) {
142 control_buttonpress(&event, programs, item_height);
143 }
144 }
145
146 cleanup(display, window, gc, &color, &selcolor, draw, font, colormap, visual);
147
148 return 0;
149}