Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

bitmap_con.c 45.1 KB
Newer Older
1
/* $Id: bitmap_con.c,v 1.148 2020/06/27 00:04:44 deuce Exp $ */
deuce's avatar
deuce committed
2 3 4 5 6 7 8 9 10 11 12 13

#include <stdarg.h>
#include <stdio.h>		/* NULL */
#include <stdlib.h>
#include <string.h>

#include "threadwrap.h"
#include "semwrap.h"
#include "gen_defs.h"
#include "genwrap.h"
#include "dirwrap.h"
#include "xpbeep.h"
Deucе's avatar
Deucе committed
14
#include "scale.h"
deuce's avatar
deuce committed
15 16 17 18 19 20 21 22 23 24

#if (defined CIOLIB_IMPORTS)
 #undef CIOLIB_IMPORTS
#endif
#if (defined CIOLIB_EXPORTS)
 #undef CIOLIB_EXPORTS
#endif

#include "ciolib.h"
#include "vidmodes.h"
deuce's avatar
deuce committed
25
#include "bitmap_con.h"
deuce's avatar
deuce committed
26

27
static uint32_t palette[65536];
28

29 30
#if 0

31
static int dbg_pthread_mutex_lock(pthread_mutex_t *lptr, unsigned line)
32 33 34 35 36 37 38 39
{
	int ret = pthread_mutex_lock(lptr);

	if (ret)
		fprintf(stderr, "pthread_mutex_lock() returned %d at %u\n", ret, line);
	return ret;
}

40
static int dbg_pthread_mutex_unlock(pthread_mutex_t *lptr, unsigned line)
41 42 43 44 45 46 47 48
{
	int ret = pthread_mutex_unlock(lptr);

	if (ret)
		fprintf(stderr, "pthread_mutex_lock() returned %d at %u\n", ret, line);
	return ret;
}

49
static int dbg_pthread_mutex_trylock(pthread_mutex_t *lptr, unsigned line)
50 51 52 53 54 55 56 57 58 59 60 61 62 63
{
	int ret = pthread_mutex_trylock(lptr);

	if (ret)
		fprintf(stderr, "pthread_mutex_lock() returned %d at %u\n", ret, line);
	return ret;
}

#define pthread_mutex_lock(a)		dbg_pthread_mutex_lock(a, __LINE__)
#define pthread_mutex_unlock(a)		dbg_pthread_mutex_unlock(a, __LINE__)
#define pthread_mutex_trylock(a)	dbg_pthread_trymutex_lock(a, __LINE__)

#endif

64 65 66
/* Structs */

struct bitmap_screen {
67 68 69
	uint32_t *screen;
	int		screenwidth;
	int		screenheight;
70
	pthread_mutex_t	screenlock;
71
	int		update_pixels;
Deucе's avatar
Deucе committed
72
	struct rectlist *rect;
73
};
deuce's avatar
deuce committed
74 75

struct bitmap_callbacks {
76
	void	(*drawrect)		(struct rectlist *data);
77
	void	(*flush)		(void);
deuce's avatar
deuce committed
78
	pthread_mutex_t lock;
79
	unsigned rects;
deuce's avatar
deuce committed
80 81
};

82
/* Static globals */
83

84 85 86
static int default_font=-99;
static int current_font[4]={-99, -99, -99, -99};
static int bitmap_initialized=0;
87 88
static struct bitmap_screen screena;
static struct bitmap_screen screenb;
89 90 91 92
struct video_stats vstat;
static struct bitmap_callbacks callbacks;
static unsigned char *font[4];
static int force_redraws=0;
Deucе's avatar
Deucе committed
93
static int force_cursor=0;
94
struct rectlist *free_rects;
95
pthread_mutex_t free_rect_lock;
deuce's avatar
deuce committed
96

97
/* The read lock must be held here. */
98
#define PIXEL_OFFSET(screen, x, y)	( (y)*(screen).screenwidth+(x) )
deuce's avatar
deuce committed
99

100
/* Exported globals */
101

102
pthread_mutex_t		vstatlock;
103
pthread_mutex_t blinker_lock;
deuce's avatar
deuce committed
104

105
/* Forward declarations */
106

107
static int bitmap_loadfont_locked(char *filename);
108
static void set_vmem_cell(struct vstat_vmem *vmem_ptr, size_t pos, uint16_t cell, uint32_t fg, uint32_t bg);
109
static int bitmap_attr2palette_locked(uint8_t attr, uint32_t *fgp, uint32_t *bgp);
110
static void	cb_drawrect(struct rectlist *data);
111 112 113
static void request_redraw_locked(void);
static void request_redraw(void);
static void memset_u32(void *buf, uint32_t u, size_t len);
deuce's avatar
deuce committed
114
static int bitmap_draw_one_char(unsigned int xpos, unsigned int ypos);
115 116 117
static void cb_flush(void);
static int check_redraw(void);
static void blinker_thread(void *data);
Deucе's avatar
Deucе committed
118
static __inline void both_screens(struct bitmap_screen** current, struct bitmap_screen** noncurrent);
deuce's avatar
deuce committed
119
static int update_from_vmem(int force);
120
static int bitmap_vmem_puttext_locked(int sx, int sy, int ex, int ey, struct vmem_cell *fill);
121
static uint32_t color_value(uint32_t col);
Deucе's avatar
Deucе committed
122
void bitmap_drv_free_rect(struct rectlist *rect);
123

124 125 126
/**************************************************************/
/* These functions get called from the driver and ciolib only */
/**************************************************************/
127

128
static int bitmap_loadfont_locked(char *filename)
129
{
130 131
	static char current_filename[MAX_PATH];
	unsigned int fontsize;
Deucе's avatar
Deucе committed
132
	int fdw;
133 134 135 136
	int fw;
	int fh;
	int i;
	FILE	*fontfile=NULL;
137

138
	if(!bitmap_initialized)
139
		return(0);
140 141 142 143 144 145 146 147 148 149 150 151 152
	if(current_font[0]==-99 || current_font[0]>(sizeof(conio_fontdata)/sizeof(struct conio_font_data_struct)-2)) {
		for(i=0; conio_fontdata[i].desc != NULL; i++) {
			if(!strcmp(conio_fontdata[i].desc, "Codepage 437 English")) {
				current_font[0]=i;
				break;
			}
		}
		if(conio_fontdata[i].desc==NULL)
			current_font[0]=0;
	}
	if(current_font[0]==-1)
		filename=current_filename;
	else if(conio_fontdata[current_font[0]].desc==NULL)
153
		return(0);
154

155 156 157 158 159 160 161 162
	for (i=1; i<sizeof(current_font)/sizeof(current_font[0]); i++) {
		if(current_font[i] == -1)
			;
		else if (current_font[i] < 0)
			current_font[i]=current_font[0];
		else if(conio_fontdata[current_font[i]].desc==NULL)
			current_font[i]=current_font[0];
	}
163

164
	fh=vstat.charheight;
Deucе's avatar
Deucе committed
165 166
	fdw = vstat.charwidth - (vstat.flags & VIDMODES_FLAG_EXPAND) ? 1 : 0;
	fw = fdw / 8 + (fdw % 8 ? 1 : 0);
167

168 169 170 171 172 173 174
	fontsize=fw*fh*256*sizeof(unsigned char);

	for (i=0; i<sizeof(font)/sizeof(font[0]); i++) {
		if(font[i])
			FREE_AND_NULL(font[i]);
		if((font[i]=(unsigned char *)malloc(fontsize))==NULL)
			goto error_return;
175
	}
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

	if(filename != NULL) {
		if(flength(filename)!=fontsize)
			goto error_return;
		if((fontfile=fopen(filename,"rb"))==NULL)
			goto error_return;
		if(fread(font[0], 1, fontsize, fontfile)!=fontsize)
			goto error_return;
		fclose(fontfile);
		fontfile=NULL;
		current_font[0]=-1;
		if(filename != current_filename)
			SAFECOPY(current_filename,filename);
		for (i=1; i<sizeof(font)/sizeof(font[0]); i++) {
			if (current_font[i]==-1)
				memcpy(font[i], font[0], fontsize);
		}
	}
	for (i=0; i<sizeof(font)/sizeof(font[0]); i++) {
		if (current_font[i] < 0)
			continue;
Deucе's avatar
Deucе committed
197
		switch(fdw) {
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
			case 8:
				switch(vstat.charheight) {
					case 8:
						if(conio_fontdata[current_font[i]].eight_by_eight==NULL) {
							if (i==0)
								goto error_return;
							else
								FREE_AND_NULL(font[i]);
						}
						else
							memcpy(font[i], conio_fontdata[current_font[i]].eight_by_eight, fontsize);
						break;
					case 14:
						if(conio_fontdata[current_font[i]].eight_by_fourteen==NULL) {
							if (i==0)
								goto error_return;
							else
								FREE_AND_NULL(font[i]);
						}
						else
							memcpy(font[i], conio_fontdata[current_font[i]].eight_by_fourteen, fontsize);
						break;
					case 16:
						if(conio_fontdata[current_font[i]].eight_by_sixteen==NULL) {
							if (i==0)
								goto error_return;
							else
								FREE_AND_NULL(font[i]);
						}
						else
							memcpy(font[i], conio_fontdata[current_font[i]].eight_by_sixteen, fontsize);
						break;
					default:
						goto error_return;
				}
				break;
			default:
				goto error_return;
		}
237 238
	}

239
    return(1);
240 241 242 243 244 245

error_return:
	for (i=0; i<sizeof(font)/sizeof(font[0]); i++)
		FREE_AND_NULL(font[i]);
	if(fontfile)
		fclose(fontfile);
246
	return(0);
247 248 249 250 251 252 253 254 255 256
}

/***************************************************/
/* These functions get called from the driver only */
/***************************************************/

/***********************************************/
/* These functions get called from ciolib only */
/***********************************************/

257
static int bitmap_vmem_puttext_locked(int sx, int sy, int ex, int ey, struct vmem_cell *fill)
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
{
	int x,y;
	struct vstat_vmem *vmem_ptr;

	if(!bitmap_initialized)
		return(0);

	if(		   sx < 1
			|| sy < 1
			|| ex < 1
			|| ey < 1
			|| sx > cio_textinfo.screenwidth
			|| sy > cio_textinfo.screenheight
			|| sx > ex
			|| sy > ey
			|| ex > cio_textinfo.screenwidth
			|| ey > cio_textinfo.screenheight
			|| fill==NULL)
		return(0);

	pthread_mutex_lock(&vstatlock);
	vmem_ptr = get_vmem(&vstat);
	for(y=sy-1;y<ey;y++) {
		for(x=sx-1;x<ex;x++) {
282
			memcpy(&vmem_ptr->vmem[y*cio_textinfo.screenwidth+x], fill++, sizeof(*fill));
283
			bitmap_draw_one_char(x+1, y+1);
284 285 286 287 288 289 290
		}
	}
	release_vmem(vmem_ptr);
	pthread_mutex_unlock(&vstatlock);
	return(1);
}

Deucе's avatar
Deucе committed
291 292
static void
set_vmem_cell(struct vstat_vmem *vmem_ptr, size_t pos, uint16_t cell, uint32_t fg, uint32_t bg)
293
{
294 295
	int		altfont;
	int		font;
296

297
	bitmap_attr2palette_locked(cell>>8, fg == 0xffffff ? &fg : NULL, bg == 0xffffff ? &bg : NULL);
298

299 300 301 302 303 304
	altfont = (cell>>11 & 0x01) | ((cell>>14) & 0x02);
	if (!vstat.bright_altcharset)
		altfont &= ~0x01;
	if (!vstat.blink_altcharset)
		altfont &= ~0x02;
	font=current_font[altfont];
305 306
	if (font == -99)
		font = default_font;
307 308 309 310 311 312 313 314
	if (font < 0 || font > 255)
		font = 0;

	vmem_ptr->vmem[pos].legacy_attr = cell >> 8;
	vmem_ptr->vmem[pos].ch = cell & 0xff;
	vmem_ptr->vmem[pos].fg = fg;
	vmem_ptr->vmem[pos].bg = bg;
	vmem_ptr->vmem[pos].font = font;
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
}

static int bitmap_attr2palette_locked(uint8_t attr, uint32_t *fgp, uint32_t *bgp)
{
	uint32_t fg = attr & 0x0f;
	uint32_t bg = (attr >> 4) & 0x0f;

	if(!vstat.bright_background)
		bg &= 0x07;
	if(vstat.no_bright)
		fg &= 0x07;

	if (fgp)
		*fgp = vstat.palette[fg];
	if (bgp)
		*bgp = vstat.palette[bg];

332
	return 1;
333 334 335 336 337 338
}

/**********************************************************************/
/* These functions get called from ciolib and the blinker thread only */
/**********************************************************************/

339 340
static int
cursor_visible_locked(void)
341
{
342 343 344 345 346 347 348 349
	if (!vstat.curs_visible)
		return 0;
	if (vstat.curs_start > vstat.curs_end)
		return 0;
	if (vstat.curs_blinks) {
		if (vstat.curs_blink)
			return 1;
		return 0;
350
	}
351
	return 1;
352 353
}

354
static void	cb_drawrect(struct rectlist *data)
355
{
356 357 358 359
	int x, y;
	uint32_t *pixel;
	uint32_t cv;

360 361
	if (data == NULL)
		return;
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
	/*
	 * Draw the cursor if it's visible
	 * 1) It's located at vstat.curs_col/vstat.curs_row.
	 * 2) The size is defined by vstat.curs_start and vstat.curs_end...
	 *    the are both rows from the top of the cell.
	 *    If vstat.curs_start > vstat.curs_end, the cursor is not shown.
	 * 3) If vstat.curs_visible is false, the cursor is not shown.
	 * 4) If vstat.curs_blinks is false, the cursor does not blink.
	 * 5) When blinking, the cursor is shown when vstat.blink is true.
	 */
	pthread_mutex_lock(&vstatlock);
	if (cursor_visible_locked()) {
		cv = color_value(ciolib_fg);
		for (y = vstat.curs_start; y <= vstat.curs_end; y++) {
			pixel = &data->data[((vstat.curs_row - 1) * vstat.charheight + y) * data->rect.width + (vstat.curs_col - 1) * vstat.charwidth];
			for (x = 0; x < vstat.charwidth; x++) {
				*(pixel++) = cv;
			}
		}
	}
	pthread_mutex_unlock(&vstatlock);
383
	pthread_mutex_lock(&callbacks.lock);
384
	callbacks.drawrect(data);
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
	callbacks.rects++;
	pthread_mutex_unlock(&callbacks.lock);
}

static void request_redraw_locked(void)
{
	force_redraws = 1;
}

static void request_redraw(void)
{
	pthread_mutex_lock(&vstatlock);
	request_redraw_locked();
	pthread_mutex_unlock(&vstatlock);
}
400

401 402 403
/*
 * Called with the screen lock held
 */
404
static struct rectlist *alloc_full_rect(struct bitmap_screen *screen)
405 406 407
{
	struct rectlist * ret;

408
	pthread_mutex_lock(&free_rect_lock);
409
	while (free_rects) {
410
		if (free_rects->rect.width == screen->screenwidth && free_rects->rect.height == screen->screenheight) {
411 412
			ret = free_rects;
			free_rects = free_rects->next;
413 414
			ret->next = NULL;
			ret->rect.x = ret->rect.y = 0;
415
			pthread_mutex_unlock(&free_rect_lock);
416 417 418 419 420 421 422 423 424
			return ret;
		}
		else {
			free(free_rects->data);
			ret = free_rects->next;
			free(free_rects);
			free_rects = ret;
		}
	}
425
	pthread_mutex_unlock(&free_rect_lock);
426 427

	ret = malloc(sizeof(struct rectlist));
428
	ret->next = NULL;
429 430
	ret->rect.x = 0;
	ret->rect.y = 0;
431 432
	ret->rect.width = screen->screenwidth;
	ret->rect.height = screen->screenheight;
433 434 435 436 437 438
	ret->data = malloc(ret->rect.width * ret->rect.height * sizeof(ret->data[0]));
	if (ret->data == NULL)
		FREE_AND_NULL(ret);
	return ret;
}

439 440 441
static uint32_t color_value(uint32_t col)
{
	if (col & 0x80000000)
Deucе's avatar
Deucе committed
442
		return col & 0xffffff;
Deucе's avatar
Deucе committed
443
	if ((col & 0xffffff) < sizeof(palette) / sizeof(palette[0]))
Deucе's avatar
Deucе committed
444
		return palette[col & 0xffffff] & 0xffffff;
Deucе's avatar
Deucе committed
445
	fprintf(stderr, "Invalid colour value: %08x\n", col);
Deucе's avatar
Deucе committed
446
	return 0;
447 448
}

449
static struct rectlist *get_full_rectangle_locked(struct bitmap_screen *screen)
450
{
deuce's avatar
deuce committed
451
	struct rectlist *rect;
452

Deucе's avatar
Deucе committed
453
	// TODO: Some sort of caching here would make things faster...?
454
	if(callbacks.drawrect) {
455
		rect = alloc_full_rect(screen);
456 457
		if (!rect)
			return rect;
Deucе's avatar
Deucе committed
458
		memcpy(rect->data, screen->rect->data, sizeof(*rect->data) * screen->screenwidth * screen->screenheight);
459 460 461
		return rect;
	}
	return NULL;
462 463
}

464 465 466 467
static void memset_u32(void *buf, uint32_t u, size_t len)
{
	size_t i;
	char *cbuf = buf;
468

469 470 471
	for (i = 0; i < len; i++) {
		memcpy(cbuf, &u, sizeof(uint32_t));
		cbuf += sizeof(uint32_t);
472 473 474
	}
}

475
/*
deuce's avatar
deuce committed
476
 * vstatlock needs to be held.
477
 */
deuce's avatar
deuce committed
478
static int bitmap_draw_one_char(unsigned int xpos, unsigned int ypos)
deuce's avatar
deuce committed
479
{
480 481
	uint32_t fg;
	uint32_t bg;
Deucе's avatar
Deucе committed
482
	int fdw;
deuce's avatar
deuce committed
483 484
	int		xoffset=(xpos-1)*vstat.charwidth;
	int		yoffset=(ypos-1)*vstat.charheight;
485
	int		x;
Deucе's avatar
Deucе committed
486 487
	int		fdx;
	uint8_t fb = 0;
488 489
	int		y;
	int		fontoffset;
Deucе's avatar
Deucе committed
490
	int		pixeloffset;
491
	unsigned char *this_font;
deuce's avatar
deuce committed
492
	WORD	sch;
deuce's avatar
deuce committed
493
	struct vstat_vmem *vmem_ptr;
494
	BOOL	draw_fg = TRUE;
deuce's avatar
deuce committed
495

496 497
	if(!bitmap_initialized) {
		return(-1);
deuce's avatar
deuce committed
498 499
	}

deuce's avatar
deuce committed
500
	vmem_ptr = vstat.vmem;
501

502 503
	if(!vmem_ptr) {
		return(-1);
504 505
	}

506 507 508
	sch=vmem_ptr->vmem[(ypos-1)*cio_textinfo.screenwidth+(xpos-1)].legacy_attr << 8 | vmem_ptr->vmem[(ypos-1)*cio_textinfo.screenwidth+(xpos-1)].ch;
	fg = vmem_ptr->vmem[(ypos-1)*cio_textinfo.screenwidth+(xpos-1)].fg;
	bg = vmem_ptr->vmem[(ypos-1)*cio_textinfo.screenwidth+(xpos-1)].bg;
509

510 511 512
	if (vstat.forced_font) {
		this_font = vstat.forced_font;
	}
deuce's avatar
deuce committed
513
	else {
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
		if (current_font[0] == -1)
			this_font = font[0];
		else {
			switch (vstat.charheight) {
				case 8:
					this_font = (unsigned char *)conio_fontdata[vmem_ptr->vmem[(ypos-1)*cio_textinfo.screenwidth+(xpos-1)].font].eight_by_eight;
					break;
				case 14:
					this_font = (unsigned char *)conio_fontdata[vmem_ptr->vmem[(ypos-1)*cio_textinfo.screenwidth+(xpos-1)].font].eight_by_fourteen;
					break;
				case 16:
					this_font = (unsigned char *)conio_fontdata[vmem_ptr->vmem[(ypos-1)*cio_textinfo.screenwidth+(xpos-1)].font].eight_by_sixteen;
					break;
				default:
					return(-1);
			}
deuce's avatar
deuce committed
530
		}
531
	}
532 533
	if (this_font == NULL)
		this_font = font[0];
Deucе's avatar
Deucе committed
534 535
	fdw = vstat.charwidth - (vstat.flags & VIDMODES_FLAG_EXPAND) ? 1 : 0;
	fontoffset=(sch & 0xff) * (vstat.charheight * ((fdw + 7) / 8));
536

Deucе's avatar
Deucе committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
	pthread_mutex_lock(&screena.screenlock);
	pthread_mutex_lock(&screenb.screenlock);

	if ((xoffset + vstat.charwidth > screena.screenwidth) || (yoffset + vstat.charheight > screena.screenheight) ||
	    (xoffset + vstat.charwidth > screenb.screenwidth) || (yoffset + vstat.charheight > screenb.screenheight)) {
		pthread_mutex_unlock(&screenb.screenlock);
		pthread_mutex_unlock(&screena.screenlock);
		return(-1);
	}

	if((!screena.screen) || (!screenb.screen)) {
		pthread_mutex_unlock(&screenb.screenlock);
		pthread_mutex_unlock(&screena.screenlock);
		return(-1);
	}

553
	draw_fg = ((!(sch & 0x8000)) || vstat.no_blink);
Deucе's avatar
Deucе committed
554
	pixeloffset = PIXEL_OFFSET(screena, xoffset, yoffset);
deuce's avatar
deuce committed
555
	for(y=0; y<vstat.charheight; y++) {
556
		for(x=0; x<vstat.charwidth; x++) {
Deucе's avatar
Deucе committed
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
			fdx = x;
			fb = this_font[fontoffset];
			if ((x & 0x07) == 7)
				fontoffset++;
			if (vstat.flags & VIDMODES_FLAG_EXPAND) {
				if (x == vstat.charwidth - 1) {
					fontoffset--;
					fdx--;
					if (!(vstat.flags & VIDMODES_FLAG_LINE_GRAPHICS_EXPAND)) {
						fb = 0;
					}
					else if ((sch & 0xff) >= 0xC0 && (sch & 0xff) <= 0xDF) {
						fb = this_font[fontoffset];
					}
					else
						fb = 0;
					
				}
			}

			if(fb & (0x80 >> (fdx & 7)) && draw_fg) {
Deucе's avatar
Deucе committed
578
				if (screena.screen[pixeloffset] != fg) {
579
					screena.update_pixels = 1;
Deucе's avatar
Deucе committed
580 581
					screena.screen[pixeloffset] = fg;
					screena.rect->data[pixeloffset] = color_value(fg);
582
				}
583
			}
584
			else {
Deucе's avatar
Deucе committed
585
				if (screena.screen[pixeloffset] != bg) {
586
					screena.update_pixels = 1;
Deucе's avatar
Deucе committed
587 588
					screena.screen[pixeloffset] = bg;
					screena.rect->data[pixeloffset] = color_value(bg);
589
				}
590
			}
Deucе's avatar
Deucе committed
591 592

			if(fb & (0x80 >> (fdx & 7))) {
Deucе's avatar
Deucе committed
593
				if (screenb.screen[pixeloffset] != fg) {
594
					screenb.update_pixels = 1;
Deucе's avatar
Deucе committed
595 596
					screenb.screen[pixeloffset] = fg;
					screenb.rect->data[pixeloffset] = color_value(fg);
597 598 599
				}
			}
			else {
Deucе's avatar
Deucе committed
600
				if (screenb.screen[pixeloffset] != bg) {
601
					screenb.update_pixels = 1;
Deucе's avatar
Deucе committed
602 603
					screenb.screen[pixeloffset]=bg;
					screenb.rect->data[pixeloffset] = color_value(bg);
604 605
				}
			}
Deucе's avatar
Deucе committed
606
			pixeloffset++;
deuce's avatar
deuce committed
607
		}
608 609
		if (x & 0x07)
			fontoffset++;
Deucе's avatar
Deucе committed
610
		pixeloffset += screena.screenwidth - vstat.charwidth;
deuce's avatar
deuce committed
611
	}
612 613
	pthread_mutex_unlock(&screenb.screenlock);
	pthread_mutex_unlock(&screena.screenlock);
deuce's avatar
deuce committed
614

615
	return(0);
deuce's avatar
deuce committed
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 647
/***********************************************************/
/* These functions get called from the blinker thread only */
/***********************************************************/

static void cb_flush(void)
{
	pthread_mutex_lock(&callbacks.lock);
	if (callbacks.rects) {
		callbacks.flush();
		callbacks.rects = 0;
	}
	pthread_mutex_unlock(&callbacks.lock);
}

static int check_redraw(void)
{
	int ret;

	pthread_mutex_lock(&vstatlock);
	ret = force_redraws;
	force_redraws = 0;
	pthread_mutex_unlock(&vstatlock);
	return ret;
}

/* Blinker Thread */
static void blinker_thread(void *data)
{
	void *rect;
	int count=0;
648 649 650
	int curs_changed;
	int blink_changed;
	struct bitmap_screen *screen;
Deucе's avatar
Deucе committed
651
	struct bitmap_screen *ncscreen;
652 653 654

	SetThreadName("Blinker");
	while(1) {
655 656
		curs_changed = 0;
		blink_changed = 0;
657 658
		do {
			SLEEP(10);
Deucе's avatar
Deucе committed
659
			both_screens(&screen, &ncscreen);
660
		} while (screen->screen == NULL);
661
		count++;
662 663
		if (count==25) {
			pthread_mutex_lock(&vstatlock);
664
			curs_changed = cursor_visible_locked();
665 666 667 668
			if(vstat.curs_blink)
				vstat.curs_blink=FALSE;
			else
				vstat.curs_blink=TRUE;
669
			curs_changed = (curs_changed != cursor_visible_locked());
670 671
			pthread_mutex_unlock(&vstatlock);
		}
672 673 674 675 676 677
		if(count==50) {
			pthread_mutex_lock(&vstatlock);
			if(vstat.blink)
				vstat.blink=FALSE;
			else
				vstat.blink=TRUE;
678 679
			blink_changed = 1;
			curs_changed = cursor_visible_locked();
680 681 682 683
			if(vstat.curs_blink)
				vstat.curs_blink=FALSE;
			else
				vstat.curs_blink=TRUE;
684
			curs_changed = (curs_changed != cursor_visible_locked());
685 686 687 688 689 690
			count=0;
			pthread_mutex_unlock(&vstatlock);
		}
		/* Lock out ciolib while we handle shit */
		pthread_mutex_lock(&blinker_lock);
		if (check_redraw()) {
deuce's avatar
deuce committed
691
			if (update_from_vmem(TRUE))
692 693 694
				request_redraw();
		}
		else {
695
			if (count==0)
deuce's avatar
deuce committed
696 697
				if (update_from_vmem(FALSE))
					request_redraw();
698
		}
699
		pthread_mutex_lock(&screen->screenlock);
Deucе's avatar
Deucе committed
700
		// TODO: Maybe we can optimize the blink_changed forced update?
701
		if (screen->update_pixels || curs_changed || blink_changed) {
Deucе's avatar
Deucе committed
702 703 704 705 706
			// If the other screen is update_pixels == 2, clear it.
			pthread_mutex_lock(&ncscreen->screenlock);
			if (ncscreen->update_pixels == 2)
				ncscreen->update_pixels = 0;
			pthread_mutex_unlock(&ncscreen->screenlock);
707 708 709
			rect = get_full_rectangle_locked(screen);
			screen->update_pixels = 0;
			pthread_mutex_unlock(&screen->screenlock);
710
			cb_drawrect(rect);
711
		}
Deucе's avatar
Deucе committed
712 713 714 715
		else {
			if (force_cursor) {
				rect = get_full_rectangle_locked(screen);
			}
716
			pthread_mutex_unlock(&screen->screenlock);
Deucе's avatar
Deucе committed
717 718 719 720 721
			if (force_cursor) {
				cb_drawrect(rect);
				force_cursor = 0;
			}
		}
722 723 724 725 726
		cb_flush();
		pthread_mutex_unlock(&blinker_lock);
	}
}

Deucе's avatar
Deucе committed
727 728 729 730 731 732 733
static __inline struct bitmap_screen *noncurrent_screen_locked(void)
{
	if (vstat.blink)
		return &screenb;
	return &screena;
}

734 735 736 737 738 739 740
static __inline struct bitmap_screen *current_screen_locked(void)
{
	if (vstat.blink)
		return &screena;
	return(&screenb);
}

Deucе's avatar
Deucе committed
741
static __inline void both_screens(struct bitmap_screen** current, struct bitmap_screen** noncurrent)
742
{
743
	pthread_mutex_lock(&vstatlock);
Deucе's avatar
Deucе committed
744 745
	*current = current_screen_locked();
	*noncurrent = noncurrent_screen_locked();
746
	pthread_mutex_unlock(&vstatlock);
747 748
}

749
/*
750 751 752
 * Updates any changed cells... blinking, modified flags, and the cursor
 * Is also used (with force = TRUE) to completely redraw the screen from
 * vmem (such as in the case of a font load).
753
 */
deuce's avatar
deuce committed
754
static int update_from_vmem(int force)
deuce's avatar
deuce committed
755
{
deuce's avatar
deuce committed
756 757 758
	static struct video_stats vs;
	struct vstat_vmem *vmem_ptr;
	int x,y,width,height;
759
	unsigned int pos;
deuce's avatar
deuce committed
760

761 762
	int bright_attr_changed=0;
	int blink_attr_changed=0;
deuce's avatar
deuce committed
763

764
	if(!bitmap_initialized)
765
		return(-1);
deuce's avatar
deuce committed
766

deuce's avatar
deuce committed
767 768
	width=cio_textinfo.screenwidth;
	height=cio_textinfo.screenheight;
deuce's avatar
deuce committed
769

770
	pthread_mutex_lock(&vstatlock);
771 772

	if (vstat.vmem == NULL) {
773
		pthread_mutex_unlock(&vstatlock);
774
		return -1;
deuce's avatar
deuce committed
775
	}
776

777
	if(vstat.vmem->vmem == NULL) {
778
		pthread_mutex_unlock(&vstatlock);
779
		return -1;
780
	}
781

deuce's avatar
deuce committed
782
	/* If we change window size, redraw everything */
783
	if(vs.cols!=vstat.cols || vs.rows != vstat.rows) {
784 785 786 787
		/* Force a full redraw */
		width=vstat.cols;
		height=vstat.rows;
		force=1;
788
	}
789

790 791 792 793 794
	/* Did the meaning of the blink bit change? */
	if (vstat.bright_background != vs.bright_background ||
			vstat.no_blink != vs.no_blink ||
			vstat.blink_altcharset != vs.blink_altcharset)
	    blink_attr_changed = 1;
deuce's avatar
deuce committed
795

796 797 798 799 800
	/* Did the meaning of the bright bit change? */
	if (vstat.no_bright != vs.no_bright ||
			vstat.bright_altcharset != vs.bright_altcharset)
		bright_attr_changed = 1;

deuce's avatar
deuce committed
801
	/* Get vmem pointer */
802 803 804 805 806 807 808 809 810 811 812 813
	vmem_ptr = get_vmem(&vstat);

	/* 
	 * Now we go through each character seeing if it's changed (or force is set)
	 * We combine updates into rectangles by lines...
	 * 
	 * First, in the same line, we build this_rect.
	 * At the end of the line, if this_rect is the same width as the screen,
	 * we add it to last_rect.
	 */

	for(y=0;y<height;y++) {
deuce's avatar
deuce committed
814
		pos=y*vstat.cols;
815 816
		for(x=0;x<width;x++) {
			/* Last this char been updated? */
817 818 819 820 821
			if(force										/* Forced */
			    || ((vstat.vmem->vmem[pos].legacy_attr & 0x80) && blink_attr_changed)
			    || ((vstat.vmem->vmem[pos].legacy_attr & 0x08) && bright_attr_changed))	/* Bright char */
			    {
				bitmap_draw_one_char(x+1,y+1);
deuce's avatar
deuce committed
822
			}
823
			pos++;
824
		}
deuce's avatar
deuce committed
825
	}
826 827
	release_vmem(vmem_ptr);

828
	pthread_mutex_unlock(&vstatlock);
deuce's avatar
deuce committed
829

830 831 832
	vs = vstat;

	return(0);
deuce's avatar
deuce committed
833 834
}

835 836
/*************************************/

837 838 839 840
/**********************/
/* Called from ciolib */
/**********************/
int bitmap_puttext(int sx, int sy, int ex, int ey, void *fill)
841
{
842 843
	int x, y;
	int ret = 1;
844
	uint16_t *buf = fill;
845
	struct vstat_vmem *vmem_ptr;
846 847 848

	pthread_mutex_lock(&blinker_lock);

849 850 851 852 853
	pthread_mutex_lock(&vstatlock);
	vmem_ptr = get_vmem(&vstat);
	for(y=sy-1;y<ey;y++) {
		for(x=sx-1;x<ex;x++) {
			set_vmem_cell(vmem_ptr, y*cio_textinfo.screenwidth+x, *(buf++), 0x00ffffff, 0x00ffffff);
854
			bitmap_draw_one_char(x+1, y+1);
855 856 857 858
		}
	}
	release_vmem(vmem_ptr);
	pthread_mutex_unlock(&vstatlock);
859
	pthread_mutex_unlock(&blinker_lock);
860
	return ret;
861 862
}

863
int bitmap_vmem_puttext(int sx, int sy, int ex, int ey, struct vmem_cell *fill)
864
{
865
	int ret;
866 867 868

	if(!bitmap_initialized)
		return(0);
869 870

	pthread_mutex_lock(&blinker_lock);
871
	ret = bitmap_vmem_puttext_locked(sx, sy, ex, ey, fill);
872
	pthread_mutex_unlock(&blinker_lock);
873 874

	return ret;
875 876
}

877
int bitmap_vmem_gettext(int sx, int sy, int ex, int ey, struct vmem_cell *fill)
deuce's avatar
deuce committed
878
{
879 880
	int x,y;
	struct vstat_vmem *vmem_ptr;
deuce's avatar
deuce committed
881

882
	if(!bitmap_initialized)
883
		return(0);
deuce's avatar
deuce committed
884

885 886 887 888 889 890 891 892 893 894
	if(		   sx < 1
			|| sy < 1
			|| ex < 1
			|| ey < 1
			|| sx > ex
			|| sy > ey
			|| ex > cio_textinfo.screenwidth
			|| ey > cio_textinfo.screenheight
			|| fill==NULL) {
		return(0);
deuce's avatar
deuce committed
895 896
	}

897
	pthread_mutex_lock(&blinker_lock);
898
	pthread_mutex_lock(&vstatlock);
899 900
	vmem_ptr = get_vmem(&vstat);
	for(y=sy-1;y<ey;y++) {
901 902
		for(x=sx-1;x<ex;x++)
			memcpy(fill++, &vmem_ptr->vmem[y*cio_textinfo.screenwidth+x], sizeof(*fill));
deuce's avatar
deuce committed
903
	}
904
	release_vmem(vmem_ptr);
905
	pthread_mutex_unlock(&vstatlock);
906
	pthread_mutex_unlock(&blinker_lock);
907
	return(1);
deuce's avatar
deuce committed
908 909 910 911
}

void bitmap_gotoxy(int x, int y)
{
912 913
	if(!bitmap_initialized)
		return;
914
	/* Move cursor location */
915
	pthread_mutex_lock(&blinker_lock);
Deucе's avatar
Deucе committed
916 917 918 919 920 921
	pthread_mutex_lock(&vstatlock);
	if (vstat.curs_col != x + cio_textinfo.winleft - 1 || vstat.curs_row != y + cio_textinfo.wintop - 1) {
		cio_textinfo.curx=x;
		cio_textinfo.cury=y;
		vstat.curs_col = x + cio_textinfo.winleft - 1;
		vstat.curs_row = y + cio_textinfo.wintop - 1;
Deucе's avatar
Deucе committed
922 923
		if (cursor_visible_locked())
			force_cursor = 1;
Deucе's avatar
Deucе committed
924 925
	}
	pthread_mutex_unlock(&vstatlock);
926
	pthread_mutex_unlock(&blinker_lock);
deuce's avatar
deuce committed
927 928
}

929
void bitmap_setcursortype(int type)
deuce's avatar
deuce committed
930
{
931 932
	if(!bitmap_initialized)
		return;
933
	pthread_mutex_lock(&blinker_lock);
934
	pthread_mutex_lock(&vstatlock);
935 936 937 938 939 940 941 942
	switch(type) {
		case _NOCURSOR:
			vstat.curs_start=0xff;
			vstat.curs_end=0;
			break;
		case _SOLIDCURSOR:
			vstat.curs_start=0;
			vstat.curs_end=vstat.charheight-1;
Deucе's avatar
Deucе committed
943
			force_cursor = 1;