summaryrefslogtreecommitdiff
path: root/twin_screen.c
blob: 8de800791799c90b2304e0c2e490e33113f0a147 (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
/*
 * Twin - A Tiny Window System
 * Copyright © 2004 Keith Packard <keithp@keithp.com>
 * All rights reserved.
 *
 * This Library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This Library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with the Twin Library; see the file COPYING.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "twinint.h"

twin_screen_t *
twin_screen_create (twin_coord_t	width,
		    twin_coord_t	height, 
		    twin_put_begin_t	put_begin,
		    twin_put_span_t	put_span,
		    void		*closure)
{
    twin_screen_t   *screen = calloc (1, sizeof (twin_screen_t));
    if (!screen)
	return 0;
    screen->top = 0;
    screen->bottom = 0;
    screen->width = width;
    screen->height = height;
    screen->damage.left = screen->damage.right = 0;
    screen->damage.top = screen->damage.bottom = 0;
    screen->damaged = NULL;
    screen->damaged_closure = NULL;
    screen->disable = 0;
    screen->background = 0;
    screen->put_begin = put_begin;
    screen->put_span = put_span;
    screen->closure = closure;

    screen->button_x = screen->button_y = -1;
    return screen;
}

void
twin_screen_destroy (twin_screen_t *screen)
{
    while (screen->bottom)
	twin_pixmap_hide (screen->bottom);
    free (screen);
}

void
twin_screen_register_damaged (twin_screen_t *screen, 
			      void (*damaged) (void *),
			      void *closure)
{
    screen->damaged = damaged;
    screen->damaged_closure = closure;
}

void
twin_screen_enable_update (twin_screen_t *screen)
{
    if (--screen->disable == 0)
    {
	if (screen->damage.left < screen->damage.right &&
	    screen->damage.top < screen->damage.bottom)
	{
	    if (screen->damaged)
		(*screen->damaged) (screen->damaged_closure);
	}
    }
}

void
twin_screen_disable_update (twin_screen_t *screen)
{
    screen->disable++;
}

#include <stdio.h>

void
twin_screen_damage (twin_screen_t *screen,
		    twin_coord_t left, twin_coord_t top,
		    twin_coord_t right, twin_coord_t bottom)
{
    if (left < 0)
	left = 0;
    if (top < 0)
	top = 0;
    if (right > screen->width)
	right = screen->width;
    if (bottom > screen->height)
	bottom = screen->height;

    if (screen->damage.left == screen->damage.right)
    {
	screen->damage.left = left;
	screen->damage.right = right;
	screen->damage.top = top;
	screen->damage.bottom = bottom;
    }
    else
    {
	if (left < screen->damage.left)
	    screen->damage.left = left;
	if (top < screen->damage.top)
	    screen->damage.top = top;
	if (screen->damage.right < right)
	    screen->damage.right = right;
	if (screen->damage.bottom < bottom)
	    screen->damage.bottom = bottom;
    }
    if (screen->damaged && !screen->disable)
	(*screen->damaged) (screen->damaged_closure);
}

void
twin_screen_resize (twin_screen_t *screen, 
		    twin_coord_t width, twin_coord_t height)
{
    screen->width = width;
    screen->height = height;
    twin_screen_damage (screen, 0, 0, screen->width, screen->height);
}

twin_bool_t
twin_screen_damaged (twin_screen_t *screen)
{
    return (screen->damage.left < screen->damage.right &&
	    screen->damage.top < screen->damage.bottom);
}

static void
twin_screen_span_pixmap(twin_screen_t *screen, twin_argb32_t *span,
			twin_pixmap_t *p, twin_coord_t y,
			twin_coord_t left, twin_coord_t right)
{
    twin_pointer_t  dst;
    twin_source_u	src;
    twin_coord_t	p_left, p_right;
		
    /* bounds check in y */
    if (y < p->y)
	return;
    if (p->y + p->height <= y)
	return;
    /* bounds check in x*/
    p_left = left;
    if (p_left < p->x)
	p_left = p->x;
    p_right = right;
    if (p_right > p->x + p->width)
	p_right = p->x + p->width;
    if (p_left >= p_right)
	return;
    dst.argb32 = span + (p_left - left);
    src.p = twin_pixmap_pointer (p, p_left - p->x, y - p->y);
    if (p->format == TWIN_RGB16)
	_twin_rgb16_source_argb32 (dst, src, p_right - p_left);
    else
	_twin_argb32_over_argb32 (dst, src, p_right - p_left);
}

void
twin_screen_update (twin_screen_t *screen)
{
    twin_coord_t	left = screen->damage.left;
    twin_coord_t	top = screen->damage.top;
    twin_coord_t	right = screen->damage.right;
    twin_coord_t	bottom = screen->damage.bottom;
    
    if (right > screen->width)
	right = screen->width;
    if (bottom > screen->height)
	bottom = screen->height;

    if (!screen->disable && left < right && top < bottom)
    {
	twin_argb32_t	*span;
        twin_pixmap_t	*p;
	twin_coord_t	y;
	twin_coord_t	width = right - left;

	screen->damage.left = screen->damage.right = 0;
	screen->damage.top = screen->damage.bottom = 0;
	/* XXX what is the maximum number of lines? */
	span = malloc (width * sizeof (twin_argb32_t));
	if (!span)
	    return;
	
	if (screen->put_begin)
	    (*screen->put_begin) (left, top, right, bottom, screen->closure);
	for (y = top; y < bottom; y++)
	{
	    if (screen->background)
	    {
		twin_pointer_t  dst;
		twin_source_u	src;
		twin_coord_t	p_left;
		twin_coord_t	m_left;
		twin_coord_t	p_this;
		twin_coord_t	p_width = screen->background->width;
		twin_coord_t	p_y = y % screen->background->height;
		
		for (p_left = left; p_left < right; p_left += p_this)
		{
		    dst.argb32 = span + (p_left - left);
		    m_left = p_left % p_width;
		    p_this = p_width - m_left;
		    if (p_left + p_this > right)
			p_this = right - p_left;
		    src.p = twin_pixmap_pointer (screen->background,
						 m_left, p_y);
		    _twin_argb32_source_argb32 (dst, src, p_this);
		}
	    }
	    else
		memset (span, 0xff, width * sizeof (twin_argb32_t));

	    for (p = screen->bottom; p; p = p->up)
		twin_screen_span_pixmap(screen, span, p, y, left, right);

	    if (screen->cursor)
		twin_screen_span_pixmap(screen, span, screen->cursor,
					y, left, right);

	    (*screen->put_span) (left, y, right, span, screen->closure);
	}
	free (span);
    }
}

void
twin_screen_set_active (twin_screen_t *screen, twin_pixmap_t *pixmap)
{
    twin_event_t    ev;
    twin_pixmap_t   *old = screen->active;
    screen->active = pixmap;
    if (old)
    {
	ev.kind = TwinEventDeactivate;
	twin_pixmap_dispatch (old, &ev);
    }
    if (pixmap)
    {
	ev.kind = TwinEventActivate;
	twin_pixmap_dispatch (pixmap, &ev);
    }
}

twin_pixmap_t *
twin_screen_get_active (twin_screen_t *screen)
{
    return screen->active;
}

void
twin_screen_set_background (twin_screen_t *screen, twin_pixmap_t *pixmap)
{
    if (screen->background)
	twin_pixmap_destroy (screen->background);
    screen->background = pixmap;
    twin_screen_damage (screen, 0, 0, screen->width, screen->height);
}

twin_pixmap_t *
twin_screen_get_background (twin_screen_t *screen)
{
    return screen->background;
}

static void
twin_screen_damage_cursor(twin_screen_t *screen)
{
    twin_screen_damage (screen,
			screen->cursor->x,
			screen->cursor->y,
			screen->cursor->x + screen->cursor->width,
			screen->cursor->y + screen->cursor->height);
}

void
twin_screen_set_cursor (twin_screen_t *screen, twin_pixmap_t *pixmap,
			twin_fixed_t hotspot_x, twin_fixed_t hotspot_y)
{
    if (screen->cursor) {
	twin_screen_damage_cursor(screen);
	twin_pixmap_destroy(screen->cursor);
    }
    screen->cursor = pixmap;
    screen->curs_hx = hotspot_x;
    screen->curs_hy = hotspot_y;
    pixmap->x = screen->curs_x - hotspot_x;
    pixmap->y = screen->curs_y - hotspot_y;
    if (pixmap)
	twin_screen_damage_cursor(screen);
}

static void
twin_screen_update_cursor(twin_screen_t *screen,
			  twin_coord_t x, twin_coord_t y)
{
    if (screen->cursor)
	twin_screen_damage_cursor(screen);

    screen->curs_x = x;
    screen->curs_y = y;

    if (screen->cursor) {
	screen->cursor->x = screen->curs_x - screen->curs_hx;
	screen->cursor->y = screen->curs_y - screen->curs_hy;
	twin_screen_damage_cursor(screen);
    }
}

twin_bool_t
twin_screen_dispatch (twin_screen_t *screen,
		      twin_event_t  *event)
{
    twin_pixmap_t   *pixmap;
    
    switch (event->kind) {
    case TwinEventMotion:
    case TwinEventButtonDown:
    case TwinEventButtonUp:
        twin_screen_update_cursor(screen, event->u.pointer.screen_x,
				  event->u.pointer.screen_y);
	pixmap = screen->pointer;
	if (!pixmap)
	{
	    for (pixmap = screen->top; pixmap; pixmap = pixmap->down)
		if (!twin_pixmap_transparent (pixmap,
					      event->u.pointer.screen_x,
					      event->u.pointer.screen_y))
		{
		    break;
		}
	    if (event->kind == TwinEventButtonDown)
		screen->pointer = pixmap;
	}
	if (event->kind == TwinEventButtonUp)
	    screen->pointer = NULL;
	if (pixmap)
	{
	    event->u.pointer.x = event->u.pointer.screen_x - pixmap->x;
	    event->u.pointer.y = event->u.pointer.screen_y - pixmap->y;
	}
	break;
    case TwinEventKeyDown:
    case TwinEventKeyUp:
    case TwinEventUcs4:
	pixmap = screen->active;
	break;
    default:
	pixmap = NULL;
	break;
    }
    if (pixmap)
	return twin_pixmap_dispatch (pixmap, event);
    return TWIN_FALSE;
}