-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdewm.c
More file actions
232 lines (221 loc) · 9.1 KB
/
sdewm.c
File metadata and controls
232 lines (221 loc) · 9.1 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <X11/Xlib.h>
#include <X11/cursorfont.h>
#include <X11/extensions/Xcomposite.h>
#include <stdio.h>
#include <stdlib.h>
#include <intdef.h>
#include "core.h"
#include "client.h"
#include "window.h"
static bool wm_detecetd = false;
int wm_error_handler(Display* dpy, XErrorEvent* ev)
{
if (ev->error_code == BadAccess)
{
wm_detecetd = true;
return 0;
}
char err[256];
XGetErrorText(dpy, ev->error_code, err, sizeof(err));
elogf("[X11 error %d] %s", ev->error_code, err);
return 0;
}
int main(void)
{
Display* display = XOpenDisplay(NULL);
if (!display)
{
fprintf(stderr, "error: cannot open display\n");
return EXIT_FAILURE;
}
Window root = DefaultRootWindow(display);
Cursor default_cursor = XCreateFontCursor(display, XC_left_ptr);
XDefineCursor(display, root, default_cursor);
XSetErrorHandler(wm_error_handler);
XSelectInput(display, root, SubstructureRedirectMask | SubstructureNotifyMask);
XSync(display, false);
if (wm_detecetd)
{
fprintf(stderr, "error: another window manager is already running\n");
return EXIT_FAILURE;
}
printf("sdewm: running\ndisplay_width = %d\ndisplay_height = %d\n", XDisplayWidth(display, DefaultScreen(display)), XDisplayHeight(display, DefaultScreen(display)));
if (!core__init_log_stream())
{
fprintf(stderr, "error: could not initialize log file, quitting\n");
XCloseDisplay(display);
exit(EXIT_FAILURE);
}
client__initialize_map();
XEvent ev;
client_t* current_window = NULL;
bool is_resizing = false;
while (true)
{
XNextEvent(display, &ev);
//logf("[EVENT] Type: %d", ev.type);
switch (ev.type)
{
case MapRequest:
{
client__create(ev.xmaprequest.window, display, root);
break;
}
case Expose:
{
logf("Got Expose event");
if (!is_resizing)
client__redraw_all_decorations(display);
break;
}
case ConfigureRequest:
{
XConfigureRequestEvent *e = &ev.xconfigurerequest;
XWindowChanges changes = {
.x = e->x,
.y = e->y,
.width = e->width,
.height = e->height,
.border_width = e->border_width,
.sibling = e->above,
.stack_mode = e->detail
};
XConfigureWindow(display, e->window, e->value_mask, &changes);
break;
}
case ButtonPress:
{
if (ev.xbutton.button == Button2 && ev.xbutton.state & Mod1Mask)
goto end;
if (ev.xbutton.window != None && ev.xbutton.subwindow == None && ev.xbutton.button == Button1 /*&& (ev.xbutton.state & Mod1Mask)*/)
{
logf("Retrieving at click");
current_window = client__retrieve_from(ev.xbutton.window, true);
if (current_window != NULL)
{
XRaiseWindow(display, current_window->frame);
XSetInputFocus(display, current_window->client, RevertToPointerRoot, CurrentTime);
if (!client__can_close(&ev, current_window, display) && !client__can_resize(&ev, current_window, display, &is_resizing))
{
//printf("win: %ld, frame: %ld\nevw: %ld, evsw: %ld, root: %ld\n", current_window->client, current_window->frame, ev.xbutton.window, ev.xbutton.subwindow, ev.xbutton.root);
//XWindowAttributes attr;
//XGetWindowAttributes(display, current_window->frame, &attr);
//current_window->frame_x = attr.x;
//current_window->frame_y = attr.y;
current_window->drag_x = ev.xbutton.x_root;
current_window->drag_y = ev.xbutton.y_root;
current_window->state |= MOVE_MASK;
client__redraw_all_decorations(display);
}
}
}
else if (ev.xbutton.window != None && ev.xbutton.subwindow != None && ev.xbutton.button == Button1)
{
logf("Retrieving at click on child");
current_window = client__retrieve_from(ev.xbutton.window, true);
if (current_window != NULL)
{
XRaiseWindow(display, current_window->frame);
XSetInputFocus(display, current_window->client, RevertToPointerRoot, CurrentTime);
}
}
break;
}
case MotionNotify:
{
if (current_window != NULL && (current_window->state & MOVE_MASK))
{
//XMoveWindow(display, current_window->client, win_x, win_y);
int dx = ev.xmotion.x_root - current_window->drag_x;
int dy = ev.xmotion.y_root - current_window->drag_y;
XMoveWindow(display, current_window->frame, current_window->frame_x + dx, current_window->frame_y + dy);
}
else if (current_window != NULL && is_resizing)
{
window__handle_resize_event(&ev, current_window, display);
}
break;
}
case UnmapNotify:
{
Window client = ev.xunmap.window;
logf("Retrieving at unmapping");
client_t* _client = client__retrieve_from(client, false);
if (_client)
{
XDestroyWindow(display, _client->frame);
client__forget(_client);
logf("Valid client for unmapping");
}
break;
}
case DestroyNotify:
{
Window client = ev.xdestroywindow.window;
logf("Retrieving at destruction");
client_t* _client = client__retrieve_from(client, false);
if (_client)
{
XDestroyWindow(display, _client->frame);
client__forget(_client);
logf("Valid client for destruction");
}
break;
}
case ButtonRelease:
{
if (current_window && (current_window->state & MOVE_MASK) && ev.xbutton.button == Button1)
{
XWindowAttributes attr;
XGetWindowAttributes(display, current_window->frame, &attr);
current_window->state &= ~MOVE_MASK;
current_window->drag_x = 0;
current_window->drag_y = 0;
current_window->frame_x = attr.x;
current_window->frame_y = attr.y;
current_window = NULL;
}
else if (current_window && is_resizing && ev.xbutton.button == Button1)
{
XWindowAttributes attr;
XGetWindowAttributes(display, current_window->frame, &attr);
current_window->state &= ~RESIZE_REGION_MASK;
current_window->drag_x = 0;
current_window->drag_y = 0;
current_window->frame_x = attr.x;
current_window->frame_y = attr.y;
current_window->frame_w = attr.width;
current_window->frame_h = attr.height;
XGetWindowAttributes(display, current_window->client, &attr);
current_window->client_w = attr.width;
current_window->client_h = attr.height;
/*cairo_surface_destroy(current_window->surface);
cairo_destroy(current_window->cr);
current_window->surface = client__get_cairo_surface(current_window->frame, display, current_window->frame_w, current_window->frame_h, NULL);
current_window->cr = cairo_create(current_window->surface);*/
window__draw_decorations(current_window, display, 0, 0, true);
is_resizing = false;
current_window = NULL;
}
else
{
/*if (ev.xbutton.subwindow == None)
{
client_t* client = client__retrieve_from(ev.xbutton.window, true);
client__can_close(&ev, client, display);
}*/
}
break;
}
default:
break;
}
}
end:
printf("quitting sdewm...");
client__free_map();
core__close_log_stream();
XCloseDisplay(display);
printf("done\n");
return 0;
}