-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx11.c
161 lines (131 loc) · 4.52 KB
/
x11.c
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
int x11_init(XRESOURCES* res, CONFIG* config){
Window root;
XSetWindowAttributes window_attributes;
Atom wm_state_fullscreen;
XTextProperty window_name;
pid_t pid = getpid();
unsigned u;
char* gobo_path = NULL;
//allocate some structures
XSizeHints* size_hints = XAllocSizeHints();
XWMHints* wm_hints = XAllocWMHints();
XClassHint* class_hints = XAllocClassHint();
if(!size_hints || !wm_hints || !class_hints){
fprintf(stderr, "Failed to allocate X data structures\n");
return -1;
}
//x data initialization
res->display = XOpenDisplay(NULL);
if(!(res->display)){
fprintf(stderr, "Failed to open display\n");
XFree(size_hints);
XFree(wm_hints);
XFree(class_hints);
return -1;
}
//XSynchronize(res->display, True);
res->screen = DefaultScreen(res->display);
root = RootWindow(res->display, res->screen);
if(!XMatchVisualInfo(res->display, res->screen, 32, TrueColor, &(res->visual_info))) {
fprintf(stderr, "Display does not support RGBA TrueColor visual\n");
return -1;
}
res->colormap = XCreateColormap(res->display, root, res->visual_info.visual, AllocNone);
//set up window params
window_attributes.background_pixel = XBlackPixel(res->display, res->screen);
window_attributes.border_pixel = XBlackPixel(res->display, res->screen);
window_attributes.colormap = res->colormap;
window_attributes.cursor = None;
window_attributes.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask;
res->window_width = DisplayWidth(res->display, res->screen);
res->window_height = DisplayHeight(res->display, res->screen);
if(config->windowed){
res->window_width = config->window_width;
res->window_height = config->window_height;
}
//create window
res->main = XCreateWindow(res->display,
root,
0,
0,
res->window_width,
res->window_height,
0,
32,
InputOutput,
res->visual_info.visual,
CWBackPixel | CWCursor | CWEventMask | CWBorderPixel | CWColormap,
&window_attributes);
//set window properties
if(XStringListToTextProperty(&(config->window_name), 1, &window_name) == 0){
fprintf(stderr, "Failed to create string list, aborting\n");
return -1;
}
wm_hints->flags = 0;
class_hints->res_name = "xlaser";
class_hints->res_class = "xlaser";
XSetWMProperties(res->display, res->main, &window_name, NULL, NULL, 0, NULL, wm_hints, class_hints);
XFree(window_name.value);
XFree(size_hints);
XFree(wm_hints);
XFree(class_hints);
//set fullscreen mode
if(!config->windowed){
wm_state_fullscreen = XInternAtom(res->display, "_NET_WM_STATE_FULLSCREEN", False);
XChangeProperty(res->display, res->main, XInternAtom(res->display, "_NET_WM_STATE", False), XA_ATOM, 32, PropModeReplace, (unsigned char*) &wm_state_fullscreen, 1);
}
XChangeProperty(res->display, res->main, XInternAtom(res->display, "_NET_WM_PID", False), XA_CARDINAL, 32, PropModeReplace, (unsigned char*)&pid, 1);
//register for WM_DELETE_WINDOW messages
res->wm_delete = XInternAtom(res->display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(res->display, res->main, &(res->wm_delete), 1);
//map window
XMapRaised(res->display, res->main);
//get x socket fds
if(!xfd_add(&(res->xfds), XConnectionNumber(res->display))){
fprintf(stderr, "Failed to allocate xfd memory\n");
return -1;
}
if(XAddConnectionWatch(res->display, xconn_watch, (void*)(&(res->xfds))) == 0){
fprintf(stderr, "Failed to register connection watch procedure\n");
return -1;
}
//prepare image data
//FIXME TODO this whole section needs error checks
gobo_path = calloc(strlen(config->gobo_prefix) + 9, sizeof(char));
if(!gobo_path){
fprintf(stderr, "Failed to allocate memory for gobo search path\n");
return -1;
}
strcpy(gobo_path, config->gobo_prefix);
for(u = 0; u < 256; u++){
res->gobo[u].height = 0;
res->gobo[u].width = 0;
snprintf(gobo_path + strlen(config->gobo_prefix), 8, "%d.png", u);
res->gobo[u].data = stbi_load(gobo_path, &(res->gobo[u].width), &(res->gobo[u].height), &(res->gobo[u].components), 4);
if(res->gobo[u].data){
fprintf(stderr, "Gobo %d: %s %dx%d @ %d\n", u, gobo_path, res->gobo[u].width, res->gobo[u].height, res->gobo[u].components);
}
}
free(gobo_path);
return backend_init(res, config);
}
void x11_cleanup(XRESOURCES* res){
unsigned u;
if(!res->display){
return;
}
backend_free(res);
for(u = 0; u < 256; u++){
if(res->gobo[u].data){
free(res->gobo[u].data);
}
}
if(res->main){
XDestroyWindow(res->display, res->main);
}
if(res->colormap){
XFreeColormap(res->display, res->colormap);
}
XCloseDisplay(res->display);
xfd_free(&(res->xfds));
}