summaryrefslogtreecommitdiff
path: root/window.c
blob: ed4b850eff82dee6ace7c788925293d8d71cbc18 (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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <assert.h>
#include "window.h"
#include "list.h"

struct ws_t
{
    Display *			display;
    int				depth;
    Visual *			visual;
    pixman_format_code_t	format;
    pixman_bool_t		composited;
    pixman_bool_t		has_alpha;
    int				byte_order;
    int				fd;
    Colormap			colormap;

    list_t			windows;
};

struct window_t
{
    link_t		link;
    ws_t *		ws;
    XID			xid;
    int			ref_count;
    pixman_image_t *	backing_store;
    int			x, y;
    pixman_region32_t	repaint;
    list_t		event_queue;
};

static pixman_bool_t
is_composited (Display *dpy)
{
    Atom atom = XInternAtom (dpy, "_NET_WM_CM_S0", True);

    return atom && XGetSelectionOwner (dpy, atom) != None;
}

static int
popcount (uint32_t v)
{
    int n = 0;
    while (v)
    {
	if (v & 1)
	    n++;
	v >>= 1;
    }
    return n;
}

/* A mask consisting of N bits set to 1. */
#define MASK(N) ((1UL << (N))-1)

static void
format_to_masks (pixman_format_code_t format,
		 uint32_t *alpha,
		 uint32_t *red,
		 uint32_t *green,
		 uint32_t *blue)
{
    int a, r, g, b;

    a = PIXMAN_FORMAT_A (format);
    r = PIXMAN_FORMAT_R (format);
    g = PIXMAN_FORMAT_G (format);
    b = PIXMAN_FORMAT_B (format);

    if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ARGB)
    {
	*alpha = MASK (a) << (r + g + b);
	*red   = MASK (r) << (g + b);
	*green = MASK (g) << (g);
	*blue  = MASK (b);
    }
    else if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
    {
	*alpha = MASK (a) << (b + g + r);
	*red   = MASK (r) << (g + r);
	*green = MASK (g) << (r);
	*blue  = MASK (b);
    }
    else
    {
	assert (0);
    }
}

static pixman_bool_t
visual_to_format (Visual *visual, int depth, pixman_format_code_t *format_o)
{
    pixman_format_code_t format;
    int format_type, bpp;
    int a, r, g, b;

    /* Not bothering with anything but TrueColor */
    if (visual->class != TrueColor)
	return FALSE;

    r = popcount (visual->red_mask);
    g = popcount (visual->green_mask);
    b = popcount (visual->blue_mask);
    a = depth - (r + g + b);

    if (visual->red_mask > visual->blue_mask)
	format_type = PIXMAN_TYPE_ARGB;
    else
	format_type = PIXMAN_TYPE_ABGR;

    if (depth > 16)
	bpp = 32;
    else if (depth > 8)
	bpp = 16;
    else if (depth > 4)
	bpp = 8;
    else if (depth == 4)
	bpp = 4;
    else
	return FALSE;

    format = PIXMAN_FORMAT (bpp, format_type, a, r, g, b);

    if (!pixman_format_supported_destination (format))
	return FALSE;

    *format_o = format;
    return TRUE;
}

/* Window system */
ws_t *
ws_open (void)
{
    ws_t *ws;
    Display *display;
    int x;

    if (!(display = XOpenDisplay (NULL)))
	return NULL;

    ws = malloc (sizeof *ws);
    ws->display = display;
    ws->composited = is_composited (display);

    /* Use the default visual ... */
    ws->depth = DefaultDepth (ws->display, 0);
    ws->visual = DefaultVisual (ws->display, 0);
    ws->has_alpha = FALSE;

    /* ... unless there is an ARGB visual available */
    if (ws->composited)
    {
	XVisualInfo *visual_list;
	XVisualInfo visual_template;
	int n_visuals, i;

	n_visuals = 0;
	visual_template.screen = 0;
	visual_list = XGetVisualInfo (ws->display, VisualScreenMask, &visual_template, &n_visuals);

	for (i = 0; i < n_visuals; ++i)
	{
	    XVisualInfo *visinfo = &(visual_list[i]);

	    if (visinfo->class == TrueColor	&&
		visinfo->depth == 32		&&
		(visinfo->red_mask ==   0x00ff0000	&&
		 visinfo->green_mask == 0x0000ff00	&&
		 visinfo->blue_mask ==  0x000000ff))
	    {
		ws->depth = 32;
		ws->visual = visinfo->visual;
		ws->has_alpha = TRUE;
		break;
	    }
	}
    }

    ws->colormap = XCreateColormap (
	ws->display, RootWindow (ws->display, 0), ws->visual, AllocNone);

    if (!visual_to_format (ws->visual, ws->depth, &ws->format))
	return NULL;

    x = 1;

    if (*(char *)&x == 1)
	ws->byte_order = LSBFirst;
    else
	ws->byte_order = MSBFirst;

    list_init (&ws->windows);

    return ws;
}

int
ws_get_fd (ws_t *ws)
{
    return XConnectionNumber (ws->display);
}

static window_t *
find_window (ws_t *ws, XID xid)
{
    link_t *link;

    LIST_FOREACH (&ws->windows, link)
    {
	window_t *window = CONTAINER_OF (window_t, link, link);

	if (window->xid == xid)
	    return window;
    }

    return NULL;
}

static int
get_width (window_t *window)
{
    return pixman_image_get_width (window->backing_store);
}

static int
get_height (window_t *window)
{
    return pixman_image_get_height (window->backing_store);
}

int
ws_window_get_width (window_t *window)
{
    return get_width (window);
}

int
ws_window_get_height (window_t *window)
{
    return get_height (window);
}

static void
copy_backing_store_to_window (window_t *window, int x, int y, int width, int height)
{
    pixman_format_code_t format = window->ws->format;
    Display *dpy = window->ws->display;
    uint32_t a, r, g, b;
    XImage image;
    XGCValues values;
    GC gc;

    format_to_masks (format, &a, &r, &g, &b);

    image.width = get_width (window);
    image.height = get_height (window);
    image.xoffset = 0;
    image.format = ZPixmap;
    image.data = (char *)pixman_image_get_data (window->backing_store);
    image.byte_order = window->ws->byte_order;
    image.bitmap_unit = 32;
    image.bitmap_bit_order = window->ws->byte_order;
    image.bitmap_pad = 32;
    image.depth = PIXMAN_FORMAT_DEPTH (format);
    image.bytes_per_line = pixman_image_get_stride (window->backing_store);
    image.bits_per_pixel = PIXMAN_FORMAT_BPP (format);
    image.red_mask = r;
    image.green_mask = g;
    image.blue_mask = b;

    XInitImage (&image);

    gc = XCreateGC (dpy, window->xid, 0, &values);

    XPutImage (dpy, window->xid, gc, &image,
	       x, y, x, y, width, height);

    XFreeGC (dpy, gc);
}

#define MIN(a,b)   (((a) < (b))? (a) : (b))

static void
update_local_geometry (window_t *window, int x, int y, int width, int height)
{
    int old_width = get_width (window);
    int old_height = get_height (window);

    if (width != old_width || height != old_height)
    {
	pixman_image_t *new_backing =
	    pixman_image_create_bits (
		window->ws->format, width, height, NULL, -1);

	pixman_image_composite32 (
	    PIXMAN_OP_SRC,
	    window->backing_store, NULL, new_backing,
	    0, 0, 0, 0, 0, 0,
	    MIN (width, old_width),
	    MIN (height, old_height));

	pixman_image_unref (window->backing_store);
	window->backing_store = new_backing;
    }

    window->x = x;
    window->y = y;
}

static void
window_queue_event (window_t *window, const ws_event_t *event)
{
    ws_event_t *copy = malloc (sizeof *copy);

    *copy = *event;

    list_append (&window->event_queue, &copy->common.link);
}

static void
process_configure_notify (ws_t *ws, XEvent *event)
{
    window_t *window;
    int new_x, new_y;
    int new_width, new_height;
    ws_window_configure_type_t config_type;

    if (!(window = find_window (ws, event->xexpose.window)))
	return;

    new_x = window->x;
    new_y = window->y;

    if (!event->xconfigure.send_event && !event->xconfigure.override_redirect)
    {
	Window child_window;
	Display *dpy = window->ws->display;

	XTranslateCoordinates (
	    dpy, window->xid, RootWindow (dpy, 0), 0, 0, &new_x, &new_y,
	    &child_window);
    }
    else
    {
	new_x = event->xconfigure.x;
	new_y = event->xconfigure.y;
    }

    new_width = event->xconfigure.width;
    new_height = event->xconfigure.height;

    /* If we are configure to a new size, then notify the user */
    config_type = 0;

    if (new_width != get_width (window)		||
	new_height != get_height (window))
    {
	config_type |= WS_WINDOW_SIZE_CHANGED;
    }

    if (new_x != window->x || new_y != window->y)
	config_type |= WS_WINDOW_POSITION_CHANGED;

    if (config_type)
    {
	ws_event_t wsevent;

	wsevent.configure.common.type = WS_CONFIGURE;
	wsevent.configure.configure_type = config_type;
	wsevent.configure.window = window;

	window_queue_event (window, &wsevent);
    }

    update_local_geometry (window, new_x, new_y, new_width, new_height);
}

static void
process_expose (ws_t *ws, const XEvent *event)
{
    window_t *window;
	    
    if (!(window = find_window (ws, event->xexpose.window)))
	return;

    pixman_region32_union_rect (&window->repaint, &window->repaint, 
				event->xexpose.x, event->xexpose.y,
				event->xexpose.width, event->xexpose.height);
}

pixman_bool_t
ws_pending (ws_t *ws)
{
    return XPending (ws->display);
}

static void
emit_events (window_t *window)
{
    while (!list_is_empty (&window->event_queue))
    {
	link_t *link = window->event_queue.head;
	ws_event_t *event = CONTAINER_OF (ws_event_t, common.link, link);

	printf ("emit %d\n", event->common.type);
	
	list_unlink (link);
	free (event);
    }
}

void
ws_process (ws_t *ws)
{
    pixman_region32_t expose;
    link_t *link;

    pixman_region32_init (&expose);

    while (ws_pending (ws))
    {
	XEvent event;

	XNextEvent (ws->display, &event);

	switch (event.type)
	{
	case ConfigureNotify:
	    process_configure_notify (ws, &event);
	    break;
	case Expose:
	    process_expose (ws, &event);
	    break;
	case MotionNotify:
	case ButtonPress:
	case ButtonRelease:
	    ;
	}
    }

    LIST_FOREACH (&ws->windows, link)
    {
	window_t *window = CONTAINER_OF (window_t, link, link);
	pixman_box32_t *boxes;
	int n_boxes;

	/* FIXME: when resizing windows, the associated exposes end up
	 * causing undefined data to be copied to the window. Something
	 * should perhaps be done about that. For example by keeping
	 * track of windows bits that haven't been painted, and never
	 * painting those.
	 */
	boxes = pixman_region32_rectangles (&window->repaint, &n_boxes);
	while (n_boxes--)
	{
	    copy_backing_store_to_window (window,
					  boxes->x1, boxes->y1,
					  boxes->x2 - boxes->x1,
					  boxes->y2 - boxes->y1);

	    boxes++;
	}

	emit_events (window);
    }
}

/* Window */
window_t *
ws_create_window (ws_t           *ws,
		  int             x,
		  int             y,
		  int             width,
		  int             height)
{
    window_t *window = malloc (sizeof *window);
    Window root = RootWindow (ws->display, 0);
    XSetWindowAttributes attr;

    attr.background_pixmap = None;
    attr.colormap = ws->colormap;
    attr.background_pixel = 0;
    attr.border_pixel = 0;
    attr.bit_gravity = NorthWestGravity;

#if 0
    printf ("%d %x %x %x %x %x\n", ws->depth, ws->visual->red_mask, ws->visual->green_mask, ws->visual->blue_mask, ws->visual->visualid, ws->colormap);
#endif
    window->xid = XCreateWindow (
	ws->display, root, x, y, width, height, 0,
	ws->depth, InputOutput, ws->visual,
	CWBitGravity | CWBackPixel | CWBorderPixel | CWBackPixmap | CWColormap, &attr);

    window->ref_count = 1;
    window->backing_store =
	pixman_image_create_bits (ws->format, width, height, NULL, -1);
    window->ws = ws;
    window->x = x;
    window->y = y;

    list_prepend (&ws->windows, &window->link);

    XSelectInput (ws->display, window->xid, ExposureMask | StructureNotifyMask);

    pixman_region32_init (&window->repaint);
    list_init (&window->event_queue);
    
    return window;
}

void
ws_window_ref (window_t *window)
{
    window->ref_count++;
    return;
}

void
ws_window_unref (window_t *window)
{
    if (--window->ref_count == 0)
    {
	XDestroyWindow (window->ws->display, window->xid);
	free (window);
    }
}

void
ws_window_show (window_t *window)
{
    XMapWindow (window->ws->display, window->xid);
}

void
ws_window_hide (window_t *window)
{
    XUnmapWindow (window->ws->display, window->xid);
}

static void
ws_window_move_resize (window_t *window,
		       int       x,
		       int       y,
		       int	 width,
		       int	 height)
{
    update_local_geometry (window, x, y, width, height);

    XMoveResizeWindow (
	window->ws->display, window->xid, x, y, width, height);
}

void
ws_window_move (window_t       *window,
		int             x,
		int             y)
{
    ws_window_move_resize (
	window, x, y, get_width (window), get_height (window));
}

void
ws_window_resize (window_t       *window,
		  int             w,
		  int             h)
{
    ws_window_move_resize (
	window, window->x, window->y, w, h);
}

void
ws_window_copy_area (window_t       *window,
		     int             src_x,
		     int             src_y,
		     int             dst_x,
		     int             dst_y,
		     int             width,
		     int             height)
{
    pixman_format_code_t format =
	pixman_image_get_format (window->backing_store);
    pixman_image_t *tmp =
	pixman_image_create_bits (format, width, height, NULL, -1);

    pixman_image_composite32 (PIXMAN_OP_SRC,
			      window->backing_store, NULL, tmp,
			      src_x, src_y, 0, 0, 0, 0, width, height);
    pixman_image_composite32 (PIXMAN_OP_SRC,
			      tmp, NULL, window->backing_store,
			      0, 0, 0, 0, dst_x, dst_y, width, height);
}

void
ws_window_copy_from_image (window_t       *window,
			   pixman_image_t *image,
			   int             image_x,
			   int             image_y,
			   int             win_x,
			   int             win_y,
			   int             width,
			   int             height)
{
    /* copy from image to backing */
    pixman_image_composite32 (PIXMAN_OP_SRC,
			      image, NULL, window->backing_store,
			      image_x, image_y, 0, 0, win_x, win_y,
			      width, height);
}

void
ws_window_copy_to_image (window_t       *window,
			 pixman_image_t *image,
			 int	         image_x,
			 int	         image_y,
			 int	         win_x,
			 int	         win_y,
			 int	         width,
			 int	         height)
{
    /* copy from backing to image */
    pixman_image_composite32 (PIXMAN_OP_SRC,
			      window->backing_store, NULL, image,
			      image_x, image_y, 0, 0, win_x, win_y,
			      width, height);
}

void
ws_window_finish (window_t  **windows,
		  int	      n_windows)
{
    int i;

    for (i = 0; i < n_windows; ++i)
    {
	window_t *window = windows[i];

	copy_backing_store_to_window (
	    window, 0, 0, get_width (window), get_height (window));

	XFlush (window->ws->display);
    }
}