File: patternm_asc.c

package info (click to toggle)
faumachine 20100527-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 53,836 kB
  • ctags: 20,552
  • sloc: ansic: 179,550; asm: 3,645; makefile: 3,611; perl: 2,103; sh: 1,529; python: 600; xml: 563; lex: 210; vhdl: 204
file content (489 lines) | stat: -rw-r--r-- 11,534 bytes parent folder | download | duplicates (2)
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
/*
 * $Id: patternm_asc.c,v 1.32 2009-03-11 07:34:32 vrsieh Exp $ 
 *
 * Copyright (C) 2007-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

/* ********************* exported definitions ************************** */

#ifdef DEFINITIONS

#define	TERM_MAX_WIDTH	256
#define TERM_MAX_HEIGHT	128

#define FONT_WIDTH	8
#define FONT_HEIGHT	16

#define MAX_PATTERN_LEN 128

struct pattern {
	bool active;
	bool last_event;
	char pattern[MAX_PATTERN_LEN];
	void *_cpssp;
	struct sig_boolean* result;
};

#undef MAX_PATTERN_LEN
#endif /* DEFINITIONS */

/* ***************************** STATE ********************************* */

#ifdef STATE

#define MAX_PATTERN_NUM 4

struct {
	/* state */

	/* pattern slots */
	struct pattern patterns[MAX_PATTERN_NUM];

	/* pixel screen buffer */
	uint32_t pixel_buffer[MAX_HEIGHT][MAX_WIDTH];
	/* terminal width/heigth */
	unsigned int width;
	unsigned int height;

	/* ascii screen buffer */
	unsigned char screen8[MAX_HEIGHT / 8][MAX_WIDTH / 8 + 1];
	unsigned char dirty8[MAX_HEIGHT / 8][MAX_WIDTH / 8 + 1];

	unsigned char screen14[MAX_HEIGHT / 14][MAX_WIDTH / 8 + 1];
	unsigned char dirty14[MAX_HEIGHT / 14][MAX_WIDTH / 8 + 1];

	unsigned char screen16[MAX_HEIGHT / 16][MAX_WIDTH / 8 + 1];
	unsigned char dirty16[MAX_HEIGHT / 16][MAX_WIDTH / 8 + 1];

	unsigned int sync_count;
	bool check_necessary;
	bool match_necessary;
} NAME;

#undef MAX_PATTERN_NUM
#endif /* STATE */

/* ************************** BEHAVIOR ********************************* */

#ifdef BEHAVIOR

/* determine number of elements in an array. */
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))

/* what to store if no character could be detected? */
#define CHAR_UNKNOWN	255

static const
#include "glyphs_8x8.c"
static const
#include "glyphs_8x14.c"
static const
#include "glyphs_8x16.c"


static inline int
NAME_(check4_one_pattern)(
	const struct cpssp * cpssp,
	const struct pattern *p
)
{
	unsigned int y;

	if (! p->active) {
		return 0;
	}

	for (y = 0; y < cpssp->NAME.height / 16; y++) {
		if (strstr(cpssp->NAME.screen16[y], p->pattern) != NULL) {
			return 1;
		}
	}
	for (y = 0; y < cpssp->NAME.height / 14; y++) {
		if (strstr(cpssp->NAME.screen14[y], p->pattern) != NULL) {
			return 1;
		}
	}
	for (y = 0; y < cpssp->NAME.height / 8; y++) {
		if (strstr(cpssp->NAME.screen8[y], p->pattern) != NULL) {
			return 1;
		}
	}
	return 0;
}

static inline void
NAME_(check4pattern)(struct cpssp * cpssp)
{
	int i;
	struct pattern *p;
	int match;

	for (i = 0; i < ARRAY_SIZE(cpssp->NAME.patterns); i++) {
		p = &cpssp->NAME.patterns[i];
		match = NAME_(check4_one_pattern)(cpssp, p);

		if (match != p->last_event) {
			/*
			 * Match changed.
			 */

			/* Remember last match value. */
			p->last_event = match;

			sig_boolean_set(p->result, p, match);
		}
	}
}

static inline bool
NAME_(check_tile)(
	struct cpssp * cpssp,
	unsigned int x,
	unsigned int y,
	unsigned int height
)
{
	unsigned char bitmap[16], rbitmap[16];
	uint32_t fg, bg;
	int found;
	unsigned int x1;
	unsigned int y1;
	unsigned int i;

	memset(bitmap, 0, sizeof(bitmap));
	memset(rbitmap, 0, sizeof(bitmap));

	/* find out foreground/background color */

	bg = cpssp->NAME.pixel_buffer[y][x];
	for (y1 = 0; y1 < height; y1++) {
		for (x1 = 0; x1 < 8; x1++) {
			if (cpssp->NAME.pixel_buffer[y + y1][x + x1] != bg)
				goto found;
		}
	}
found:	;
	if (y1 != height) {
		fg = cpssp->NAME.pixel_buffer[y + y1][x + x1];
	} else {
		/* only one color */
		fg = ~bg;
	}

	/* setup black/white bitmap of character */

	for (y1 = 0; y1 < height; y1++) {
		for (x1 = 0; x1 < 8; x1++) {
			bitmap[y1] <<= 1;
			rbitmap[y1] <<= 1;
			if (cpssp->NAME.pixel_buffer[y + y1][x + x1] == fg) {
				bitmap[y1] |= 1;
			} else if (cpssp->NAME.pixel_buffer[y + y1][x + x1] == bg) {
				rbitmap[y1] |= 1;
			} else {
				/* more than 2 colors, probably in GFX mode */
				return false;
			}
		}
	}

	/* search for the character in font */
	switch (height) {
	case 8:
		for (i = 0; ; i++) {
			if (i == sizeof(glyph_8x8) / sizeof(glyph_8x8[0])) {
				found = CHAR_UNKNOWN;
				break;
			}
			if (memcmp(bitmap, glyph_8x8[i].pattern, 8) == 0
			 || memcmp(rbitmap, glyph_8x8[i].pattern, 8) == 0) {
				found = glyph_8x8[i].ord;
				break;
			}
		}

		cpssp->NAME.screen8[y / 8][x / 8] = found;
		break;
	case 14:
		for (i = 0; ; i++) {
			if (i == sizeof(glyph_8x14) / sizeof(glyph_8x14[0])) {
				found = CHAR_UNKNOWN;
				break;
			}
			if (memcmp(bitmap, glyph_8x14[i].pattern, 14) == 0
			 || memcmp(rbitmap, glyph_8x14[i].pattern, 14) == 0) {
				found = glyph_8x14[i].ord;
				break;
			}
		}

		cpssp->NAME.screen14[y / 14][x / 8] = found;
		break;
	case 16:
		for (i = 0; ; i++) {
			if (i == sizeof(glyph_8x16) / sizeof(glyph_8x16[0])) {
				found = CHAR_UNKNOWN;
				break;
			}
			if (memcmp(bitmap, glyph_8x16[i].pattern, 16) == 0
			 || memcmp(rbitmap, glyph_8x16[i].pattern, 16) == 0) {
				found = glyph_8x16[i].ord;
				break;
			}
		}

		cpssp->NAME.screen16[y / 16][x / 8] = found;
		break;
	default:
		assert(0); /* Cannot happen. */
	}
	return true;
}

static inline bool
NAME_(find_glyphs)(struct cpssp * cpssp)
{
	bool found;
	unsigned int x;
	unsigned int y;
	unsigned int xt;
	unsigned int yt;

	found = false;

	for (y = 0, yt = 0; y < cpssp->NAME.height; y += 16, yt++) {
		for (x = 0, xt = 0; x < cpssp->NAME.width; x += 8, xt++) {
			if (cpssp->NAME.dirty16[yt][xt]) {
				cpssp->NAME.dirty16[yt][xt] = 0;
				if (NAME_(check_tile)(cpssp, x, y, 16)) {
					found = true;
				}
			}
		}
	}

	for (y = 0, yt = 0; y < cpssp->NAME.height; y += 14, yt++) {
		for (x = 0, xt = 0; x < cpssp->NAME.width; x += 8, xt++) {
			if (cpssp->NAME.dirty14[yt][xt]) {
				cpssp->NAME.dirty14[yt][xt] = 0;
				if (NAME_(check_tile)(cpssp, x, y, 14)) {
					found = true;
				}
			}
		}
	}

	for (y = 0, yt = 0; y < cpssp->NAME.height; y += 8, yt++) {
		for (x = 0, xt = 0; x < cpssp->NAME.width; x += 8, xt++) {
			if (cpssp->NAME.dirty8[yt][xt]) {
				cpssp->NAME.dirty8[yt][xt] = 0;
				if (NAME_(check_tile)(cpssp, x, y, 8)) {
					found = true;
				}
			}
		}
	}

	return found;
}

static void
NAME_(size_set)(struct cpssp *cpssp, unsigned int w, unsigned int h)
{
	int y;

	assert(w <= MAX_WIDTH);
	assert(h <= MAX_HEIGHT);

	/*
	 * Clear old '\0' terminations.
	 */
	for (y = 0; y < cpssp->NAME.height / 16; y++) {
		cpssp->NAME.screen16[y][cpssp->NAME.width / FONT_WIDTH]
				= CHAR_UNKNOWN;
	}
	for (y = 0; y < cpssp->NAME.height / 14; y++) {
		cpssp->NAME.screen14[y][cpssp->NAME.width / FONT_WIDTH]
				= CHAR_UNKNOWN;
	}
	for (y = 0; y < cpssp->NAME.height / 8; y++) {
		cpssp->NAME.screen8[y][cpssp->NAME.width / FONT_WIDTH]
				= CHAR_UNKNOWN;
	}

	/*
	 * Set new size.
	 */
	cpssp->NAME.width = w;
	cpssp->NAME.height = h;

	/*
	 * Set new '\0' terminations.
	 */
	for (y = 0; y < cpssp->NAME.height / 16; y++) {
		cpssp->NAME.screen16[y][cpssp->NAME.width / FONT_WIDTH] = '\0';
	}
	for (y = 0; y < cpssp->NAME.height / 14; y++) {
		cpssp->NAME.screen14[y][cpssp->NAME.width / FONT_WIDTH] = '\0';
	}
	for (y = 0; y < cpssp->NAME.height / 8; y++) {
		cpssp->NAME.screen8[y][cpssp->NAME.width / FONT_WIDTH] = '\0';
	}

	/*
	 * Re-match everything...
	 */
	cpssp->NAME.match_necessary = true;
}

static void
NAME_(pixel_set)(
	struct cpssp *cpssp,
	unsigned int x, unsigned int y,
	uint8_t r, uint8_t g, uint8_t b
)
{
	assert(x < MAX_WIDTH);
	assert(y < MAX_HEIGHT);

	cpssp->NAME.pixel_buffer[y][x] = (r << 16) | (g << 8) | (b << 0);

	cpssp->NAME.dirty16[y / 16][x / 8] = 1;
	cpssp->NAME.dirty14[y / 14][x / 8] = 1;
	cpssp->NAME.dirty8[y /  8][x / 8] = 1;

	cpssp->NAME.match_necessary = true;
}

static void
NAME_(sync)(struct cpssp *cpssp)
{
	if (cpssp->NAME.match_necessary) {
		cpssp->NAME.match_necessary = false;
		if (NAME_(find_glyphs)(cpssp)) {
			cpssp->NAME.check_necessary = true;
		}
	}

	cpssp->NAME.sync_count++;

	if (2 <= cpssp->NAME.sync_count) {
		cpssp->NAME.sync_count = 0;
		if (cpssp->NAME.check_necessary) {
			cpssp->NAME.check_necessary = false;
			NAME_(check4pattern)(cpssp);
		}
	}
}

static void
NAME_(string_set)(void *s, const char* str)
{
	struct pattern* p = (struct pattern*)s;
	struct cpssp * cpssp = (struct cpssp *)p->_cpssp;
	int found;
	assert(str);

	if (*str) {
		if (sizeof(p->pattern) <= strlen(str)) {
			faum_log(FAUM_LOG_WARNING, "patternm", "asc",
				"watch string %s too long, ignoring\n", str);
		}

		strncpy(p->pattern, str, sizeof(p->pattern) - 1);

		p->pattern[sizeof(p->pattern) - 1] = '\0';
		p->active = true;

		found = NAME_(check4_one_pattern)(cpssp, p);
		p->last_event = found;
		sig_boolean_set(p->result, p, found);
	} else {
		p->pattern[0] = '\0';
		p->active = false;
		p->last_event = false;
		sig_boolean_set(p->result, p, 0);
	}
}

static void
NAME_(init)(
	struct sig_string *port_asc_text0,
	struct sig_boolean *port_asc_text0_state,
	struct sig_string *port_asc_text1,
	struct sig_boolean *port_asc_text1_state,
	struct sig_string *port_asc_text2,
	struct sig_boolean *port_asc_text2_state,
	struct sig_string *port_asc_text3,
	struct sig_boolean *port_asc_text3_state,
	struct cpssp * cpssp
)
{
	static const struct sig_string_funcs sf = {
		.set = NAME_(string_set)
	};
	int y;

	cpssp->NAME.width = 640;
	cpssp->NAME.height = 400;
	cpssp->NAME.check_necessary = false;
	cpssp->NAME.match_necessary = true;
	cpssp->NAME.sync_count = 0;

	/* initialize screen buffer */
	memset(cpssp->NAME.screen16, CHAR_UNKNOWN, sizeof(cpssp->NAME.screen16));
	memset(cpssp->NAME.screen14, CHAR_UNKNOWN, sizeof(cpssp->NAME.screen14));
	memset(cpssp->NAME.screen8, CHAR_UNKNOWN, sizeof(cpssp->NAME.screen8));

	/* initialize pixel buffer */
	memset(cpssp->NAME.pixel_buffer, 0, sizeof(cpssp->NAME.pixel_buffer));

	/* initialize dirty tile arrays
	 * (We have to look at each tile at least once
	 * to get a proper initial state of the screen.) */
	memset(cpssp->NAME.dirty16, 1, sizeof(cpssp->NAME.dirty16));
	memset(cpssp->NAME.dirty14, 1, sizeof(cpssp->NAME.dirty14));
	memset(cpssp->NAME.dirty8, 1, sizeof(cpssp->NAME.dirty8));

	/* add terminating '\0' to end of line (screen has one extra space in 
	 * x direction for this) */
	for (y = 0; y < cpssp->NAME.height / 16; y++) {
		cpssp->NAME.screen16[y][cpssp->NAME.width / 8] = '\0';
	}
	for (y = 0; y < cpssp->NAME.height / 14; y++) {
		cpssp->NAME.screen14[y][cpssp->NAME.width / 8] = '\0';
	}
	for (y = 0; y < cpssp->NAME.height / 8; y++) {
		cpssp->NAME.screen8[y][cpssp->NAME.width / 8] = '\0';
	}

	/* initialize members */
	for (y = 0; y < ARRAY_SIZE(cpssp->NAME.patterns); y++) {
		cpssp->NAME.patterns[y].active = false;
		cpssp->NAME.patterns[y].last_event = false;
		cpssp->NAME.patterns[y].pattern[0] = '\0';
		cpssp->NAME.patterns[y]._cpssp = cpssp;
	}

	cpssp->NAME.patterns[0].result = port_asc_text0_state;
	cpssp->NAME.patterns[1].result = port_asc_text1_state;
	cpssp->NAME.patterns[2].result = port_asc_text2_state;
	cpssp->NAME.patterns[3].result = port_asc_text3_state;

	sig_string_connect(port_asc_text0, &cpssp->NAME.patterns[0], &sf);
	sig_string_connect(port_asc_text1, &cpssp->NAME.patterns[1], &sf);
	sig_string_connect(port_asc_text2, &cpssp->NAME.patterns[2], &sf);
	sig_string_connect(port_asc_text3, &cpssp->NAME.patterns[3], &sf);
}

#undef ARRAY_SIZE
#undef CHAR_UNKNOWN
#undef TERM_MAX_WIDTH
#undef TERM_MAX_HEIGHT
#undef FONT_WIDTH
#undef FONT_HEIGHT

#endif /* BEHAVIOR */