File: skyeye_lcd_beos.cpp

package info (click to toggle)
skyeye 1.2.5-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,236 kB
  • ctags: 19,345
  • sloc: ansic: 90,379; sh: 5,188; python: 707; cpp: 417; makefile: 322; exp: 38
file content (459 lines) | stat: -rw-r--r-- 10,578 bytes parent folder | download | duplicates (4)
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
/*
	skyeye_lcd_beos.cpp - LCD display emulation on BeOS' BWindow.
	Copyright (C) 2003 - 2007 Skyeye Develop Group
	for help please send mail to <skyeye-developer@lists.gro.clinux.org>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program 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 General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*
 * 01/31/2007   written by Anthony Lee
 */

#include <stdlib.h>
#include <kernel/OS.h>
#include <support/String.h>

#include "skyeye_lcd_beos.h"


extern "C" {

extern void beos_lcd_skPenEvent(unsigned int *buffer, int eventType, int stateType, int x, int y);

/* defined in skyeye_lcd.c, but we don't include the "skyeye_lcd.h" */
extern void skyeye_convert_color_from_lcd_dma(void *lcd_dev, int x, int y, int w, int h,
					      void (*func)(uint32_t color, void *user_data1, void *user_data2),
					      void *user_data, void *user_data2);

void* SkyEyeLCD_Be_new(int width, int virtual_width,
		       int height,
		       int depth,
		       const uint32_t *colormap,
		       const void *bufferFB,
		       unsigned int *bufferPen,
		       lcd_lookup_color_func func,
		       void *lcd_dev)
{
	SkyEyeLCD_Be *lcd = new SkyEyeLCD_Be(width, virtual_width,
					     height,
					     depth,
					     colormap,
					     bufferFB,
					     bufferPen,
					     func,
					     lcd_dev);

	if(lcd->InitCheck() == B_OK) return (void*)lcd;

	delete lcd;
	return NULL;
}


void SkyEyeLCD_Be_delete(void *_lcd)
{
	SkyEyeLCD_Be *lcd = (SkyEyeLCD_Be*)_lcd;
	if(lcd) delete lcd;
}


void SkyEyeLCD_Be_Update(void *_lcd)
{
	SkyEyeLCD_Be *lcd = (SkyEyeLCD_Be*)_lcd;
	if(lcd) lcd->Update();
}


void SkyEyeLCD_Be_DMAChanged(void *_lcd, int l, int t, int r, int b)
{
	SkyEyeLCD_Be *lcd = (SkyEyeLCD_Be*)_lcd;
	if(lcd) lcd->DMAChanged(l, t, r, b);
}

} // extern "C"


static void SkyEyeLCD_Be_At_Exit_Callback()
{
	while(be_app_messenger.SendMessage(B_QUIT_REQUESTED) == B_OK) snooze(500000);
}


static int32 SkyEyeLCD_Be_Thread_App(void *data)
{
	new BApplication("application/x-vnd.skyeye-lcd-app");

	atexit(SkyEyeLCD_Be_At_Exit_Callback);

	be_app->Run();

	delete be_app;

	return 0;
}


bool
SkyEyeLCD_Be::InitApplication()
{
	if(be_app_messenger.IsValid()) return true;

	thread_id tid = spawn_thread(SkyEyeLCD_Be_Thread_App, NULL, B_URGENT_DISPLAY_PRIORITY, NULL);
	if(tid < 0 || resume_thread(tid) != B_OK) return false;

	while(be_app_messenger.IsValid() == false) snooze(100000);

	return true;
}


bool
SkyEyeLCD_Be::IsDepthSupported(int depth) const
{
	switch(depth)
	{
		case 1:
		case 8:
		case 16:
		case 24:
		case 32:
			return true;

		case 2:
			return(fLookupColorFunc != NULL);

		case 4:
			return(fLookupColorFunc != NULL || fColormap != NULL);

		default:
			return false;
	}
}


status_t
SkyEyeLCD_Be::InitCheck() const
{
	if(fWidth <= 0 || fVirtualWidth < fWidth ||
	   fHeight <= 0 ||
	   !IsDepthSupported(fDepth) ||
	   fBufferFB == NULL ||
	   (fLookupColorFunc != NULL && fDevice == NULL)) return B_ERROR;

	if(!fMsgr.IsValid() || fBitmap == NULL) return B_NO_INIT;

	return B_OK;
}


SkyEyeLCD_Be::SkyEyeLCD_Be(int width, int virtual_width,
		           int height,
			   int depth,
			   const uint32_t *colormap,
			   const void *bufferFB,
			   unsigned int *bufferPen,
			   lcd_lookup_color_func func,
			   void *lcd_dev)
	: fWidth(width), fVirtualWidth(virtual_width),
	  fHeight(height), fDepth(depth), fColormap(colormap),
	  fUpdateAll(true),
	  fBufferFB(bufferFB), fBufferPen(bufferPen),
	  fLookupColorFunc(func), fDevice(lcd_dev),
	  fBitmap(NULL), fTimestamp(0)
{
	if(InitApplication() == false || InitCheck() == B_ERROR) return;

	fBufferPenTmp = (unsigned int*)malloc(sizeof(unsigned int) * 8);
	fBitmap = new BBitmap(BRect(0, 0, (fLookupColorFunc == NULL ? fVirtualWidth : fWidth) - 1, fHeight - 1),
			      (fDepth == 1 && fLookupColorFunc == NULL ? B_GRAY1 : B_RGB32), false);

	if(fBufferPenTmp == NULL || fBitmap->InitCheck() != B_OK)
	{
		if(fBufferPenTmp != NULL) free(fBufferPenTmp);
		if(fBitmap != NULL) delete fBitmap;
		fBufferPenTmp = NULL;
		fBitmap = NULL;
		return;
	}

	memcpy(fBufferPenTmp, fBufferPen, sizeof(unsigned int) * 8);

	BString title;
	title << width << "x" << height << "x" << depth << " SkyEye LCD & Touch Screen (BeOS)";

	BWindow *win = new BWindow(BRect(100, 100, 100 + fWidth - 1, 100 + fHeight - 1),
			           title.String(),
				   B_TITLED_WINDOW,
				   B_NOT_CLOSABLE | B_NOT_ZOOMABLE | B_NOT_RESIZABLE);
	BView *view = new SkyEyeLCD_Be_View(this);

	/* for B_GRAY1 */
	view->SetHighColor(255, 255, 255);
	view->SetLowColor(0, 0, 0);

	win->Lock();
	win->AddChild(view);
	win->Show();
	fMsgr = BMessenger(view, NULL);
	win->Unlock();
}


SkyEyeLCD_Be::~SkyEyeLCD_Be()
{
	if(fMsgr.IsValid())
	{
		while(fMsgr.LockTarget() == false) snooze(100000);

		BLooper *looper;
		BView *view = (BView*)fMsgr.Target(&looper);
		view->RemoveSelf();
		looper->PostMessage(B_QUIT_REQUESTED);
		looper->Unlock();

		delete view;
	}

	if(fBufferPenTmp != NULL) free(fBufferPenTmp);
	if(fBitmap != NULL) delete fBitmap;
}


void
SkyEyeLCD_Be::DMAChanged(int l, int t, int r, int b)
{
	while(fBitmap->LockBits() != B_OK);
	if (fUpdateRect.IsValid() == false)
		fUpdateRect = BRect(l, t, r, b);
	else
		fUpdateRect = (fUpdateRect | BRect(l, t, r, b));
	fUpdateAll = false;
	fBitmap->UnlockBits();
}


void
SkyEyeLCD_Be::DMAConvertCallback(uint32_t c, uint8 **buf_addr, void *noused)
{
	*(*buf_addr) = (c >> 16) & 0xff;
	*((*buf_addr) + 1)= (c >> 8) & 0xff;
	*((*buf_addr) + 2) = c & 0xff;

	*buf_addr = (*buf_addr) + 3;
}


void
SkyEyeLCD_Be::Update()
{
	while(fBitmap->LockBits() != B_OK);

	if(real_time_clock_usecs() - fTimestamp >= (bigtime_t)200000UL && (fUpdateAll || fUpdateRect.IsValid()))
	{
		void *data = NULL;
		int32 length, offset;
		color_space cs;
		BRect rect;

		if(fUpdateAll == false)
		{
			rect = fUpdateRect;
			rect.right += 1;
			rect.bottom += 1;
			fUpdateRect = BRect();
		}
		else
		{
			rect = BRect(0, 0, fWidth, fHeight);
		}

		if(fLookupColorFunc != NULL)
		{
				offset = fWidth * (int)rect.top * 4;
				length = fWidth * rect.IntegerHeight() * 3;
				cs = B_RGB32;
				if((data = malloc(length)) != NULL)
				{
					uint8 *tmp = (uint8*)data;
					skyeye_convert_color_from_lcd_dma(fDevice,
									  0, (int)rect.top, fWidth, rect.IntegerHeight(),
									  (void (*)(uint32_t, void*, void*))DMAConvertCallback,
									  &tmp, NULL);
				}
		}
		else switch(fDepth)
		{
			case 1:
				data = (void*)fBufferFB;
				offset = fVirtualWidth * (int)rect.top / 8;
				length = fVirtualWidth * rect.IntegerHeight() / 8;
				cs = B_GRAY1;
				break;

			case 24:
				data = (void*)fBufferFB;
				offset = fVirtualWidth * (int)rect.top * 4;
				length = fVirtualWidth * rect.IntegerHeight() * 3;
				cs = B_RGB32;
				break;

			case 4: /* index */
			case 8: /* 3-3-2 */
			case 16: /* 5-6-5 */
			case 32:
				offset = fVirtualWidth * (int)rect.top * 4;
				length = fVirtualWidth * rect.IntegerHeight() * 3;
				cs = B_RGB32;
				if((data = malloc(length)) != NULL)
				{
					uint8 *tmp = (uint8*)data;
					uint8 *buf = (uint8*)fBufferFB + fVirtualWidth * (int)rect.top * fDepth / 8;

					int32 count = fWidth * rect.IntegerHeight();
					int32 i = 0;

					while(count-- > 0)
					{
						if(fDepth == 4)
						{
							uint32 c = *(fColormap + (count % 2 == 0 ? (*buf & 0xf) : (*buf >> 4)));
							*tmp++ = (c >> 16) & 0xff; // red
							*tmp++ = (c >> 8) & 0xff; // green
							*tmp++ = c & 0xff; // blue
							if(count % 2 == 0) buf++;
						}
						if(fDepth == 8)
						{
							*tmp++ = (*buf & 0xe0) | 0x1f; // red
							*tmp++ = ((*buf & 0x1c) << 3) | 0x1f; // green
							*tmp++ = ((*buf++ & 0x03) << 6) | 0x3f; // blue
						}
						else if(fDepth == 16)
						{
							*tmp++ = ((*((uint16*)buf) & 0xf800) >> 8) | 0x0007; // red
							*tmp++ = ((*((uint16*)buf) & 0x07e0) >> 3) | 0x0003; // green
							*tmp++ = ((*((uint16*)buf) & 0x001f) << 3) | 0x0007; // blue
							buf += 2;
						}
						else
						{
							uint32 c = *((uint32*)buf);
							*tmp++ = (c >> 16) & 0xff; // red
							*tmp++ = (c >> 8) & 0xff; // green
							*tmp++ = c & 0xff; // blue
							buf += 4;
						}

						if(++i == fWidth)
						{
							tmp += (fVirtualWidth - fWidth) * 3;
							buf += (fVirtualWidth - fWidth) * fDepth / 8;
							i = 0;
						}
					}
				}
				break;

			default:
				break;
		}

		if(data != NULL)
		{
			BMessage aMsg('slcd');

			fBitmap->SetBits(data, length, offset, cs);
			if(data != fBufferFB) free(data);

			rect.right -= 1;
			rect.bottom -= 1;
			aMsg.AddRect("frame", rect);
			fMsgr.SendMessage(&aMsg);
		}

		fTimestamp = real_time_clock_usecs();
	}

	memcpy(fBufferPen, fBufferPenTmp, sizeof(unsigned int) * 8);

	fBitmap->UnlockBits();
}


SkyEyeLCD_Be_View::SkyEyeLCD_Be_View(SkyEyeLCD_Be *lcd)
	: BView(BRect(0, 0, lcd->fWidth - 1, lcd->fHeight - 1), NULL, B_FOLLOW_NONE, 0), fLCD(lcd)
{
}


SkyEyeLCD_Be_View::~SkyEyeLCD_Be_View()
{
}


void
SkyEyeLCD_Be_View::MessageReceived(BMessage *msg)
{
	BRect rect;

	switch(msg->what)
	{
		case 'slcd':
			if(msg->FindRect("frame", &rect) != B_OK) break;
			Draw(rect);
			break;

		default:
			BView::MessageReceived(msg);
	}
}


void
SkyEyeLCD_Be_View::Draw(BRect updateRect)
{
	while(fLCD->fBitmap->LockBits() != B_OK);
	DrawBitmap(fLCD->fBitmap, updateRect, updateRect);
	fLCD->fBitmap->UnlockBits();
}


void
SkyEyeLCD_Be_View::MouseDown(BPoint where)
{
	while(fLCD->fBitmap->LockBits() != B_OK);
	beos_lcd_skPenEvent(fLCD->fBufferPenTmp, 0, 1, (int)where.x, (int)where.y);
	fLCD->fBitmap->UnlockBits();
}


void
SkyEyeLCD_Be_View::MouseUp(BPoint where)
{
	while(fLCD->fBitmap->LockBits() != B_OK);
	beos_lcd_skPenEvent(fLCD->fBufferPenTmp, 1, 0, (int)where.x, (int)where.y);
	fLCD->fBitmap->UnlockBits();
}


void
SkyEyeLCD_Be_View::MouseMoved(BPoint where, uint32 code, const BMessage *a_message)
{
	while(fLCD->fBitmap->LockBits() != B_OK);
	if(*(fLCD->fBufferPenTmp + 5) == 1) beos_lcd_skPenEvent(fLCD->fBufferPenTmp, 2, 1, (int)where.x, (int)where.y);
	fLCD->fBitmap->UnlockBits();
}