summaryrefslogtreecommitdiff
path: root/src/window.c
blob: af7f024844695bce0f5313440b5cc84ed6e8a146 (plain)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "Xlib-wayland.h"

#include "private.h"

struct csx_window *
csx_window_create(struct csx_display *display, struct csx_window *parent)
{
	struct csx_window *window;

	window = malloc(sizeof *window);
	if (window == NULL)
		return 0; /* FIXME: BadAlloc */ 

	memset(window, 0, sizeof *window);
	window->id = csx_display_add_resource(display, window);
	window->parent = parent;

	window->display = display;
	wl_list_init(&window->child_list);
	if (parent) 
		wl_list_insert(&parent->child_list, &window->link);
	pixman_region32_init(&window->output_clip);

	return window;
}

struct csx_window *
csx_window_pick(struct csx_window *window,
		int x, int y, int *local_x, int *local_y)
{
	struct csx_window *w;

	wl_list_for_each(w, &window->child_list, link) {
		if (w->mapped && w->x <= x & w->y <= y &&
		    x < w->x + w->width && y < w->y + w->height)
			return csx_window_pick(w, x - w->x, y - w->y,
					       local_x, local_y);
	}

	*local_x = x;
	*local_y = y;

	return window;
}

void
csx_window_send_create_notify(struct csx_window *window)
{
	struct csx_display *display = window->display;
	struct csx_event *event;

	event = malloc(sizeof *event);

	event->xevent.xcreatewindow.type = CreateNotify;
	event->xevent.xcreatewindow.serial = display->serial;
	event->xevent.xcreatewindow.send_event = False;
	event->xevent.xcreatewindow.display = display->xdisplay;
	event->xevent.xcreatewindow.parent = window->parent->id;
	event->xevent.xcreatewindow.window = window->id;
	event->xevent.xcreatewindow.x = window->x;
	event->xevent.xcreatewindow.y = window->y;
	event->xevent.xcreatewindow.width = window->width;
	event->xevent.xcreatewindow.height = window->height;
	event->xevent.xcreatewindow.border_width = window->border_width;
	event->xevent.xcreatewindow.override_redirect = False;

	wl_list_insert(display->event_list.prev, &event->link);
}

WL_EXPORT Window
XCreateWindow(Display *xdisplay, Window xparent, int x, int y,
	      unsigned int width, unsigned int height,
	      unsigned int border_width, int depth,
	      unsigned int class, Visual *visual,
	      unsigned long valuemask,
	      XSetWindowAttributes *attributes)
{
	struct csx_display *display = csx_display(xdisplay);
	struct csx_window *window, *parent;
	int i;

	parent = csx_display_lookup_resource(display, xparent);
	if (parent == NULL)
		/* do something, really */;

	window = csx_window_create(display, parent);
	if (window == NULL) {
		csx_display_bad_alloc(display);
		return None;
	}

	window->x = x;
	window->y = y;
	window->width = width;
	window->height = height;
	window->border_width = border_width;
	window->depth = depth;
	window->class = class;
	window->visual = visual;

	for (i = 0; i < 15; i++) {
		switch (valuemask & (1 << i)) {
		case CWBackPixmap:
		case CWBackPixel:
		case CWBorderPixmap:
		case CWBorderPixel:
		case CWBitGravity:
		case CWWinGravity:
		case CWBackingStore:
		case CWBackingPlanes:
		case CWBackingPixel:
		case CWOverrideRedirect:
		case CWSaveUnder:
			break;
		case CWEventMask:
			window->event_mask = attributes->event_mask;
			break;
		case CWDontPropagate:
		case CWColormap:
		case CWCursor:
			break;
		}
	}

	if (parent->event_mask & SubstructureNotifyMask)
		csx_window_send_create_notify(window);

	return window->id;
}

WL_EXPORT Window
XCreateSimpleWindow(Display *xdisplay, Window parent, int x, int y,
		    unsigned int width, unsigned int height,
		    unsigned int border_width, unsigned long border,
		    unsigned long background)
{
	unsigned long valuemask;
	XSetWindowAttributes attributes;

	attributes.background_pixel = background;
	attributes.border_pixel = border;
	return XCreateWindow(xdisplay, parent, x, y, width, height,
			     border_width, 0, CopyFromParent, CopyFromParent,
			     CWBackPixel | CWBorderPixel, &attributes);
}

static void
handle_ping(void *data, struct wl_shell_surface *shell_surface,
	    uint32_t serial)
{
	wl_shell_surface_pong(shell_surface, serial);
}

static void
handle_configure(void *data, struct wl_shell_surface *shell_surface,
		 uint32_t edges, int32_t width, int32_t height)
{
}

static void
handle_popup_done(void *data, struct wl_shell_surface *shell_surface)
{
}

static const struct wl_shell_surface_listener shell_surface_listener = {
	handle_ping,
	handle_configure,
	handle_popup_done
};

void
csx_display_send_map_notify(struct csx_display *display,
			    struct csx_window *window)
{
	struct csx_event *event;

	if (!(window->event_mask & StructureNotifyMask))
		return;

	event = malloc(sizeof *event);
	event->xevent.type = MapNotify;

	event->xevent.xmap.type = MapNotify;
	event->xevent.xmap.serial = display->serial;
	event->xevent.xmap.send_event = False;
	event->xevent.xmap.display = display->xdisplay;
	event->xevent.xmap.event = window->id;
	event->xevent.xmap.window = window->id;
	event->xevent.xmap.override_redirect = False;

	wl_list_insert(display->event_list.prev, &event->link);
}

void
csx_window_map_tree(struct csx_window *window, struct csx_pixmap *pixmap)
{
	struct csx_window *child;

	wl_list_for_each(child, &window->child_list, link)
		csx_window_map_tree(child, pixmap);

	window->pixmap = pixmap;
	csx_display_send_map_notify(window->display, window);
}

void
csx_window_map_toplevel(struct csx_window *window)
{
	struct csx_display *display = window->display;
	struct csx_pixmap *pixmap;

	pixmap = csx_pixmap_create(window->display,
				   window->width, window->height, 32);
	csx_window_map_tree(window, pixmap);

	window->surface = wl_compositor_create_surface(display->compositor);
	wl_surface_set_user_data(window->surface, window);
	wl_surface_attach(window->surface, window->pixmap->buffer, 0, 0);
	window->shell_surface =
		wl_shell_get_shell_surface(display->shell, window->surface);
	wl_shell_surface_add_listener(window->shell_surface,
				      &shell_surface_listener, window);
	wl_shell_surface_set_toplevel(window->shell_surface);
	wl_surface_commit(window->surface);
}

WL_EXPORT int
XMapWindow(Display *xdisplay, Window xwindow)
{
	struct csx_display *display = csx_display(xdisplay);
	struct csx_window *window;
	struct csx_pixmap *pixmap;

	window = csx_display_lookup_resource(display, xwindow);
	if (window == NULL) {
		csx_display_bad_window(display);
		return 0; /* what's the return value? */
	}
	
	if (window->mapped)
		/* Already mapped */
		return 0;

	window->mapped = 1;
	if (window->parent == display->root) {
		csx_window_map_toplevel(window);
	} else if (window->parent->mapped) {
		window->pixmap = window->parent->pixmap;
		csx_display_send_map_notify(display, window);
	}

}

WL_EXPORT int
XMapSubwindows(Display *display, Window xwindow)
{
}

WL_EXPORT int
XChangeWindowAttributes(Display *xdisplay, Window xwindow,
			unsigned long mask, XSetWindowAttributes *attributes)
{
	struct csx_display *display = csx_display(xdisplay);
	struct csx_window *window;

	window = csx_display_lookup_resource(display, xwindow);
	if (window == NULL) {
		csx_display_bad_window(display);
		return 0; /* what's the return value? */
	}
}

WL_EXPORT int
XConfigureWindow(Display *xdisplay, Window xwindow,
		 unsigned int mask, XWindowChanges *values)
{
	struct csx_display *display = csx_display(xdisplay);
	struct csx_window *window;

	window = csx_display_lookup_resource(display, xwindow);
	if (window == NULL) {
		csx_display_bad_window(display);
		return 0; /* what's the return value? */
	}

	if (mask & CWX)
		window->x = values->x;
	if (mask & CWY)
		window->y = values->y;
	if (mask & CWWidth)
		window->width = values->width;
	if (mask & CWHeight)
		window->height = values->height;

	/* Send out events as necessary: exposures, configure notify etc */
}

WL_EXPORT int
XReparentWindow(Display *xdisplay, Window xwindow,
		Window xparent, int x, int y)
{
	struct csx_display *display = csx_display(xdisplay);
	struct csx_window *window, *parent;

	window = csx_display_lookup_resource(display, xwindow);
	if (window == NULL) {
		csx_display_bad_window(display);
		return 0; /* what's the return value? */
	}

	parent = csx_display_lookup_resource(display, xparent);
	if (parent == NULL) {
		csx_display_bad_window(display);
		return 0; /* what's the return value? */
	}

	/* FIXME: Lot more to do here... */
	wl_list_remove(&window->link);
	wl_list_insert(&parent->child_list, &window->link);
}

WL_EXPORT int
XResizeWindow(Display *xdisplay, Window xwindow,
	      unsigned int width, unsigned int height)
{
	XWindowChanges values;

	values.width = width;
	values.height = height;

	return XConfigureWindow(xdisplay, xwindow,
				CWWidth | CWHeight, &values);
}

WL_EXPORT int
XMoveResizeWindow(Display *xdisplay, Window xwindow,
		  int x, int y, unsigned int width, unsigned int height)
{
	XWindowChanges values;

	values.x = x;
	values.y = y;
	values.width = width;
	values.height = height;

	return XConfigureWindow(xdisplay, xwindow,
				CWX | CWY | CWWidth | CWHeight, &values);
}

WL_EXPORT int
XMoveWindow(Display *xdisplay, Window xwindow, int x, int y)
{
	XWindowChanges values;

	values.x = x;
	values.y = y;

	return XConfigureWindow(xdisplay, xwindow, CWX | CWY, &values);
}

WL_EXPORT int
XLowerWindow(Display *xdisplay, Window xwindow)
{
	struct csx_display *display = csx_display(xdisplay);
	struct csx_window *window;

	window = csx_display_lookup_resource(display, xwindow);
	if (window == NULL) {
		csx_display_bad_window(display);
		return 0; /* what's the return value? */
	}
}

WL_EXPORT int
XRaiseWindow(Display *display, Window xwindow)
{
}

WL_EXPORT int
XMapRaised(Display *xdisplay, Window xwindow)
{
	struct csx_display *display = csx_display(xdisplay);
	struct csx_window *window;

	window = csx_display_lookup_resource(display, xwindow);
	if (window == NULL) {
		csx_display_bad_window(display);
		return 0; /* what's the return value? */
	}
}

void
csx_window_destroy(struct csx_window *window)
{
	struct csx_window *child, *next;
	struct csx_event *event;

	wl_list_for_each_safe(child, next, &window->child_list, link)
		csx_window_destroy(child);

#if 0
	event = csx_display_create_event(display);
	event->xevent.type = DestroyNotify;
#endif

	csx_display_remove_resource(window->display, window->id);
	wl_list_remove(&window->link);
	free(window);
}

WL_EXPORT int
XDestroyWindow(Display *xdisplay, Window xwindow)
{
	struct csx_display *display = csx_display(xdisplay);
	struct csx_window *window;

	window = csx_display_lookup_resource(display, xwindow);
	if (window == NULL)
		/* BadWindow */;
	if (window == display->root)
		return;

	csx_window_destroy(window);

	return 0; /* return what? */
}

WL_EXPORT int
XDestroySubwindows(Display *xdisplay, Window xwindow)
{
	struct csx_display *display = csx_display(xdisplay);
	struct csx_window *window, *child, *next;

	window = csx_display_lookup_resource(display, xwindow);
	if (window == NULL)
		/* BadWindow */;

	wl_list_for_each_reverse_safe(child, next, &window->child_list, link)
		csx_window_destroy(child);
}