xref: /haiku/src/tests/servers/app/drawing_debugger/DrawingDebugger.cpp (revision 3c905d812f6f5fc96e4ae8d25d5f464376b7ce52)
1*3c905d81SMichael Lotz #include <Application.h>
2*3c905d81SMichael Lotz #include <Bitmap.h>
3*3c905d81SMichael Lotz #include <View.h>
4*3c905d81SMichael Lotz #include <Window.h>
5*3c905d81SMichael Lotz #include <malloc.h>
6*3c905d81SMichael Lotz #include <stdio.h>
7*3c905d81SMichael Lotz 
8*3c905d81SMichael Lotz 
9*3c905d81SMichael Lotz #if 0
10*3c905d81SMichael Lotz {
11*3c905d81SMichael Lotz 	// usage example for simple rects
12*3c905d81SMichael Lotz 	port_id port = find_port("drawing_debugger_port");
13*3c905d81SMichael Lotz 	if (port >= 0) {
14*3c905d81SMichael Lotz 		write_port_etc(port, 'RECT', &interestingRect, sizeof(BRect),
15*3c905d81SMichael Lotz 			B_RELATIVE_TIMEOUT, 0);
16*3c905d81SMichael Lotz 	}
17*3c905d81SMichael Lotz 
18*3c905d81SMichael Lotz 
19*3c905d81SMichael Lotz 	// usage example for regions
20*3c905d81SMichael Lotz 	port_id port = find_port("drawing_debugger_port");
21*3c905d81SMichael Lotz 	if (port >= 0) {
22*3c905d81SMichael Lotz 		int32 rectCount = region->CountRects();
23*3c905d81SMichael Lotz 		for (int32 i = 0; i < rectCount; i++) {
24*3c905d81SMichael Lotz 			BRect interestingRect = region->RectAt(i);
25*3c905d81SMichael Lotz 			write_port_etc(port, 'RECT', &interestingRect, sizeof(BRect),
26*3c905d81SMichael Lotz 				B_RELATIVE_TIMEOUT, 0);
27*3c905d81SMichael Lotz 		}
28*3c905d81SMichael Lotz 	}
29*3c905d81SMichael Lotz }
30*3c905d81SMichael Lotz #endif
31*3c905d81SMichael Lotz 
32*3c905d81SMichael Lotz 
33*3c905d81SMichael Lotz class DrawingDebuggerView : public BView {
34*3c905d81SMichael Lotz public:
35*3c905d81SMichael Lotz 								DrawingDebuggerView(BRect frame);
36*3c905d81SMichael Lotz virtual							~DrawingDebuggerView();
37*3c905d81SMichael Lotz 
38*3c905d81SMichael Lotz virtual	void					Draw(BRect updateRect);
39*3c905d81SMichael Lotz 
40*3c905d81SMichael Lotz private:
41*3c905d81SMichael Lotz static	int32					_PortListener(void *data);
42*3c905d81SMichael Lotz 
43*3c905d81SMichael Lotz 		BBitmap *				fBitmap;
44*3c905d81SMichael Lotz 		BView *					fOffscreenView;
45*3c905d81SMichael Lotz 		thread_id				fListenerThread;
46*3c905d81SMichael Lotz 		bool					fStopListener;
47*3c905d81SMichael Lotz };
48*3c905d81SMichael Lotz 
49*3c905d81SMichael Lotz 
50*3c905d81SMichael Lotz class DrawingDebuggerWindow : public BWindow {
51*3c905d81SMichael Lotz public:
52*3c905d81SMichael Lotz 								DrawingDebuggerWindow(BRect frame);
53*3c905d81SMichael Lotz 
54*3c905d81SMichael Lotz private:
55*3c905d81SMichael Lotz 		DrawingDebuggerView *	fView;
56*3c905d81SMichael Lotz };
57*3c905d81SMichael Lotz 
58*3c905d81SMichael Lotz 
59*3c905d81SMichael Lotz class DrawingDebuggerApp : public BApplication {
60*3c905d81SMichael Lotz public:
61*3c905d81SMichael Lotz 								DrawingDebuggerApp();
62*3c905d81SMichael Lotz 
63*3c905d81SMichael Lotz private:
64*3c905d81SMichael Lotz 		DrawingDebuggerWindow *	fWindow;
65*3c905d81SMichael Lotz };
66*3c905d81SMichael Lotz 
67*3c905d81SMichael Lotz 
68*3c905d81SMichael Lotz rgb_color
random_color()69*3c905d81SMichael Lotz random_color()
70*3c905d81SMichael Lotz {
71*3c905d81SMichael Lotz 	rgb_color result;
72*3c905d81SMichael Lotz 	result.red = system_time() % 256;
73*3c905d81SMichael Lotz 	result.green = (system_time() >> 8) % 256;
74*3c905d81SMichael Lotz 	result.blue = (system_time() >> 16) % 256;
75*3c905d81SMichael Lotz 	return result;
76*3c905d81SMichael Lotz }
77*3c905d81SMichael Lotz 
78*3c905d81SMichael Lotz 
DrawingDebuggerApp()79*3c905d81SMichael Lotz DrawingDebuggerApp::DrawingDebuggerApp()
80*3c905d81SMichael Lotz 	:	BApplication("application/x.vnd-Haiku.DrawingDebugger")
81*3c905d81SMichael Lotz {
82*3c905d81SMichael Lotz 	fWindow = new DrawingDebuggerWindow(BRect(200, 200, 999, 799));
83*3c905d81SMichael Lotz 	fWindow->Show();
84*3c905d81SMichael Lotz }
85*3c905d81SMichael Lotz 
86*3c905d81SMichael Lotz 
DrawingDebuggerWindow(BRect frame)87*3c905d81SMichael Lotz DrawingDebuggerWindow::DrawingDebuggerWindow(BRect frame)
88*3c905d81SMichael Lotz 	:	BWindow(frame, "DrawingDebugger", B_TITLED_WINDOW,
89*3c905d81SMichael Lotz 			B_QUIT_ON_WINDOW_CLOSE)
90*3c905d81SMichael Lotz {
91*3c905d81SMichael Lotz 	fView = new DrawingDebuggerView(frame.OffsetToSelf(0, 0));
92*3c905d81SMichael Lotz 	AddChild(fView);
93*3c905d81SMichael Lotz }
94*3c905d81SMichael Lotz 
95*3c905d81SMichael Lotz 
DrawingDebuggerView(BRect frame)96*3c905d81SMichael Lotz DrawingDebuggerView::DrawingDebuggerView(BRect frame)
97*3c905d81SMichael Lotz 	:	BView(frame, "DrawingDebuggerView", B_FOLLOW_ALL, B_WILL_DRAW)
98*3c905d81SMichael Lotz {
99*3c905d81SMichael Lotz 	fBitmap = new BBitmap(frame, B_RGB32, true);
100*3c905d81SMichael Lotz 	fOffscreenView= new BView(frame, "OffscreenView", B_FOLLOW_NONE, 0);
101*3c905d81SMichael Lotz 	fOffscreenView->SetViewColor(B_TRANSPARENT_32_BIT);
102*3c905d81SMichael Lotz 	fBitmap->AddChild(fOffscreenView);
103*3c905d81SMichael Lotz 
104*3c905d81SMichael Lotz 	fStopListener = false;
105*3c905d81SMichael Lotz 	fListenerThread = spawn_thread(_PortListener, "port_listener",
106*3c905d81SMichael Lotz 		B_NORMAL_PRIORITY, this);
107*3c905d81SMichael Lotz 	resume_thread(fListenerThread);
108*3c905d81SMichael Lotz }
109*3c905d81SMichael Lotz 
110*3c905d81SMichael Lotz 
~DrawingDebuggerView()111*3c905d81SMichael Lotz DrawingDebuggerView::~DrawingDebuggerView()
112*3c905d81SMichael Lotz {
113*3c905d81SMichael Lotz 	fStopListener = true;
114*3c905d81SMichael Lotz 	int32 returnCode = B_OK;
115*3c905d81SMichael Lotz 	wait_for_thread(fListenerThread, &returnCode);
116*3c905d81SMichael Lotz 	delete fBitmap;
117*3c905d81SMichael Lotz }
118*3c905d81SMichael Lotz 
119*3c905d81SMichael Lotz 
120*3c905d81SMichael Lotz void
Draw(BRect updateRect)121*3c905d81SMichael Lotz DrawingDebuggerView::Draw(BRect updateRect)
122*3c905d81SMichael Lotz {
123*3c905d81SMichael Lotz 	DrawBitmap(fBitmap, updateRect, updateRect);
124*3c905d81SMichael Lotz }
125*3c905d81SMichael Lotz 
126*3c905d81SMichael Lotz 
127*3c905d81SMichael Lotz int32
_PortListener(void * data)128*3c905d81SMichael Lotz DrawingDebuggerView::_PortListener(void *data)
129*3c905d81SMichael Lotz {
130*3c905d81SMichael Lotz 	DrawingDebuggerView *view = (DrawingDebuggerView *)data;
131*3c905d81SMichael Lotz 	port_id port = create_port(1000, "drawing_debugger_port");
132*3c905d81SMichael Lotz 	if (port < 0) {
133*3c905d81SMichael Lotz 		printf("failed to create port\n");
134*3c905d81SMichael Lotz 		return port;
135*3c905d81SMichael Lotz 	}
136*3c905d81SMichael Lotz 
137*3c905d81SMichael Lotz 	size_t bufferSize = 1024;
138*3c905d81SMichael Lotz 	uint8 *buffer = (uint8 *)malloc(bufferSize);
139*3c905d81SMichael Lotz 	if (buffer == NULL)
140*3c905d81SMichael Lotz 		return B_NO_MEMORY;
141*3c905d81SMichael Lotz 
142*3c905d81SMichael Lotz 	while (!view->fStopListener) {
143*3c905d81SMichael Lotz 		int32 code = 0;
144*3c905d81SMichael Lotz 		status_t result = read_port_etc(port, &code, buffer, bufferSize,
145*3c905d81SMichael Lotz 			B_RELATIVE_TIMEOUT, 100000);
146*3c905d81SMichael Lotz 		if (result == B_INTERRUPTED || result == B_TIMED_OUT)
147*3c905d81SMichael Lotz 			continue;
148*3c905d81SMichael Lotz 		if (result < B_OK) {
149*3c905d81SMichael Lotz 			printf("failed to read from port\n");
150*3c905d81SMichael Lotz 			return result;
151*3c905d81SMichael Lotz 		}
152*3c905d81SMichael Lotz 
153*3c905d81SMichael Lotz 		switch (code) {
154*3c905d81SMichael Lotz 			case 'RECT':
155*3c905d81SMichael Lotz 			{
156*3c905d81SMichael Lotz 				BRect *rect = (BRect *)buffer;
157*3c905d81SMichael Lotz 				view->fBitmap->Lock();
158*3c905d81SMichael Lotz 				view->fOffscreenView->SetHighColor(random_color());
159*3c905d81SMichael Lotz 				view->fOffscreenView->FillRect(*rect, B_SOLID_HIGH);
160*3c905d81SMichael Lotz 				view->fOffscreenView->Sync();
161*3c905d81SMichael Lotz 				view->fBitmap->Unlock();
162*3c905d81SMichael Lotz 
163*3c905d81SMichael Lotz 				view->LockLooper();
164*3c905d81SMichael Lotz 				view->Invalidate(*rect);
165*3c905d81SMichael Lotz 				view->UnlockLooper();
166*3c905d81SMichael Lotz 				break;
167*3c905d81SMichael Lotz 			}
168*3c905d81SMichael Lotz 
169*3c905d81SMichael Lotz 			default:
170*3c905d81SMichael Lotz 				printf("unsupported code '%.4s'\n", (char *)&code);
171*3c905d81SMichael Lotz 				break;
172*3c905d81SMichael Lotz 		}
173*3c905d81SMichael Lotz 	}
174*3c905d81SMichael Lotz 
175*3c905d81SMichael Lotz 	free(buffer);
176*3c905d81SMichael Lotz 	delete_port(port);
177*3c905d81SMichael Lotz 	return 0;
178*3c905d81SMichael Lotz }
179*3c905d81SMichael Lotz 
180*3c905d81SMichael Lotz 
181*3c905d81SMichael Lotz int
main(int argc,const char * argv[])182*3c905d81SMichael Lotz main(int argc, const char *argv[])
183*3c905d81SMichael Lotz {
184*3c905d81SMichael Lotz 	DrawingDebuggerApp *app = new DrawingDebuggerApp();
185*3c905d81SMichael Lotz 	app->Run();
186*3c905d81SMichael Lotz 	delete app;
187*3c905d81SMichael Lotz 	return 0;
188*3c905d81SMichael Lotz }
189