BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / apps / remotedesktop / RemoteView.cpp
blob595a7f2da0faec5f39cf15fb12161e76820c4185
1 /*
2 * Copyright 2009-2014, Haiku, Inc.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Michael Lotz <mmlr@mlotz.ch>
7 */
9 #include "NetReceiver.h"
10 #include "NetSender.h"
11 #include "RemoteMessage.h"
12 #include "RemoteView.h"
13 #include "StreamingRingBuffer.h"
15 #include <Application.h>
16 #include <Autolock.h>
17 #include <Bitmap.h>
18 #include <Message.h>
19 #include <NetEndpoint.h>
20 #include <Region.h>
21 #include <Shape.h>
22 #include <Window.h>
23 #include <utf8_functions.h>
25 #include <new>
26 #include <stdio.h>
29 static const uint8 kCursorData[] = { 16 /* size, 16x16 */,
30 1 /* depth, 1 bit per pixel */, 0, 0, /* hot spot at 0, 0 */
31 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
33 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
36 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
37 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
42 #define TRACE(x...) /*printf("RemoteView: " x)*/
43 #define TRACE_ALWAYS(x...) printf("RemoteView: " x)
44 #define TRACE_ERROR(x...) printf("RemoteView: " x)
47 typedef struct engine_state {
48 uint32 token;
49 BView * view;
50 ::pattern pattern;
51 BRegion clipping_region;
52 float pen_size;
53 bool sync_drawing;
54 } engine_state;
57 RemoteView::RemoteView(BRect frame, const char *remoteHost, uint16 remotePort)
59 BView(frame, "RemoteView", B_FOLLOW_NONE, B_WILL_DRAW),
60 fInitStatus(B_NO_INIT),
61 fIsConnected(false),
62 fReceiveBuffer(NULL),
63 fSendBuffer(NULL),
64 fEndpoint(NULL),
65 fReceiver(NULL),
66 fSender(NULL),
67 fStopThread(false),
68 fOffscreenBitmap(NULL),
69 fOffscreen(NULL),
70 fViewCursor(kCursorData),
71 fCursorBitmap(NULL),
72 fCursorVisible(false)
74 fReceiveBuffer = new(std::nothrow) StreamingRingBuffer(16 * 1024);
75 if (fReceiveBuffer == NULL) {
76 fInitStatus = B_NO_MEMORY;
77 TRACE_ERROR("no memory available\n");
78 return;
81 fInitStatus = fReceiveBuffer->InitCheck();
82 if (fInitStatus != B_OK)
83 return;
85 fSendBuffer = new(std::nothrow) StreamingRingBuffer(16 * 1024);
86 if (fSendBuffer == NULL) {
87 fInitStatus = B_NO_MEMORY;
88 TRACE_ERROR("no memory available\n");
89 return;
92 fInitStatus = fSendBuffer->InitCheck();
93 if (fInitStatus != B_OK)
94 return;
96 fEndpoint = new(std::nothrow) BNetEndpoint();
97 if (fEndpoint == NULL) {
98 fInitStatus = B_NO_MEMORY;
99 TRACE_ERROR("no memory available\n");
100 return;
103 fInitStatus = fEndpoint->Connect(remoteHost, remotePort);
104 if (fInitStatus != B_OK) {
105 TRACE_ERROR("failed to connect to %s:%" B_PRIu16 "\n",
106 remoteHost, remotePort);
107 return;
110 fSender = new(std::nothrow) NetSender(fEndpoint, fSendBuffer);
111 if (fSender == NULL) {
112 fInitStatus = B_NO_MEMORY;
113 TRACE_ERROR("no memory available\n");
114 return;
117 fReceiver = new(std::nothrow) NetReceiver(fEndpoint, fReceiveBuffer);
118 if (fReceiver == NULL) {
119 fInitStatus = B_NO_MEMORY;
120 TRACE_ERROR("no memory available\n");
121 return;
124 BRect bounds = frame.OffsetToCopy(0, 0);
125 fOffscreenBitmap = new(std::nothrow) BBitmap(bounds, B_BITMAP_ACCEPTS_VIEWS,
126 B_RGB32);
127 if (fOffscreenBitmap == NULL) {
128 fInitStatus = B_NO_MEMORY;
129 TRACE_ERROR("no memory available\n");
130 return;
133 fOffscreen = new(std::nothrow) BView(bounds, "offscreen remote view",
134 B_FOLLOW_NONE, B_WILL_DRAW);
135 if (fOffscreen == NULL) {
136 fInitStatus = B_NO_MEMORY;
137 TRACE_ERROR("no memory available\n");
138 return;
141 fOffscreenBitmap->AddChild(fOffscreen);
142 fOffscreen->SetDrawingMode(B_OP_COPY);
144 fDrawThread = spawn_thread(&_DrawEntry, "draw thread", B_NORMAL_PRIORITY,
145 this);
146 if (fDrawThread < 0) {
147 fInitStatus = fDrawThread;
149 TRACE_ERROR("failed to start _DrawThread()\n");
150 TRACE_ERROR("status = %" B_PRIx32 "\n", fInitStatus);
152 return;
155 resume_thread(fDrawThread);
159 RemoteView::~RemoteView()
161 fStopThread = true;
163 delete fReceiver;
164 delete fReceiveBuffer;
166 delete fSendBuffer;
167 delete fSender;
169 delete fEndpoint;
171 delete fOffscreenBitmap;
172 delete fCursorBitmap;
174 int32 result;
175 wait_for_thread(fDrawThread, &result);
179 status_t
180 RemoteView::InitCheck()
182 return fInitStatus;
186 void
187 RemoteView::AttachedToWindow()
189 SetViewColor(B_TRANSPARENT_COLOR);
190 SetViewCursor(&fViewCursor);
194 void
195 RemoteView::Draw(BRect updateRect)
197 SetDrawingMode(B_OP_COPY);
198 fOffscreenBitmap->Lock();
199 fOffscreen->Sync();
201 DrawBitmap(fOffscreenBitmap, updateRect, updateRect);
203 if (fCursorVisible && fCursorBitmap != NULL
204 && fCursorFrame.Intersects(updateRect)) {
205 DrawBitmap(fOffscreenBitmap, fCursorFrame, fCursorFrame);
206 SetDrawingMode(B_OP_ALPHA);
207 DrawBitmap(fCursorBitmap, fCursorFrame.LeftTop());
210 fOffscreenBitmap->Unlock();
214 void
215 RemoteView::MouseMoved(BPoint where, uint32 code, const BMessage *dragMessage)
217 if (!fIsConnected)
218 return;
220 _SendMouseMessage(RP_MOUSE_MOVED, where);
224 void
225 RemoteView::MouseDown(BPoint where)
227 if (!fIsConnected)
228 return;
230 _SendMouseMessage(RP_MOUSE_DOWN, where);
234 void
235 RemoteView::MouseUp(BPoint where)
237 if (!fIsConnected)
238 return;
240 _SendMouseMessage(RP_MOUSE_UP, where);
244 void
245 RemoteView::KeyDown(const char *bytes, int32 numBytes)
247 if (!fIsConnected)
248 return;
250 _SendKeyMessage(RP_KEY_DOWN, bytes, numBytes);
254 void
255 RemoteView::KeyUp(const char *bytes, int32 numBytes)
257 if (!fIsConnected)
258 return;
260 _SendKeyMessage(RP_KEY_UP, bytes, numBytes);
264 void
265 RemoteView::MessageReceived(BMessage *message)
267 if (!fIsConnected) {
268 BView::MessageReceived(message);
269 return;
272 switch (message->what) {
273 case B_UNMAPPED_KEY_DOWN:
274 case B_UNMAPPED_KEY_UP:
275 // these are easily repeated and then cause a flood of messages
276 // so we might not want them.
277 break;
279 case B_MODIFIERS_CHANGED:
281 uint32 modifiers = 0;
282 message->FindInt32("modifiers", (int32 *)&modifiers);
283 RemoteMessage message(NULL, fSendBuffer);
284 message.Start(RP_MODIFIERS_CHANGED);
285 message.Add(modifiers);
286 break;
289 case B_MOUSE_WHEEL_CHANGED:
291 float xDelta, yDelta;
292 if (message->FindFloat("be:wheel_delta_x", &xDelta) != B_OK)
293 xDelta = 0;
294 if (message->FindFloat("be:wheel_delta_y", &yDelta) != B_OK)
295 yDelta = 0;
297 RemoteMessage message(NULL, fSendBuffer);
298 message.Start(RP_MOUSE_WHEEL_CHANGED);
299 message.Add(xDelta);
300 message.Add(yDelta);
301 break;
305 BView::MessageReceived(message);
309 void
310 RemoteView::_SendMouseMessage(uint16 code, BPoint where)
312 RemoteMessage message(NULL, fSendBuffer);
313 message.Start(code);
314 message.Add(where);
316 if (code == RP_MOUSE_MOVED)
317 return;
319 BMessage *event = Window()->CurrentMessage();
321 int32 buttons = 0;
322 event->FindInt32("buttons", &buttons);
323 message.Add(buttons);
325 if (code == RP_MOUSE_DOWN)
326 return;
328 int32 clicks;
329 event->FindInt32("clicks", &clicks);
330 message.Add(clicks);
334 void
335 RemoteView::_SendKeyMessage(uint16 code, const char *bytes, int32 numBytes)
337 RemoteMessage message(NULL, fSendBuffer);
338 message.Start(code);
339 message.Add(numBytes);
340 message.AddList(bytes, numBytes);
342 BMessage *event = Window()->CurrentMessage();
344 int32 rawChar, key;
345 event->FindInt32("raw_char", &rawChar);
346 event->FindInt32("key", &key);
348 message.Add(rawChar);
349 message.Add(key);
354 RemoteView::_StateCompareByKey(const uint32 *key, const engine_state *state)
356 if (state->token == *key)
357 return 0;
359 if (state->token < *key)
360 return -1;
362 return 1;
366 engine_state *
367 RemoteView::_CreateState(uint32 token)
369 int32 index = fStates.BinarySearchIndexByKey(token, &_StateCompareByKey);
370 if (index >= 0) {
371 TRACE_ERROR("state for token %" B_PRIu32 " already in list\n", token);
372 return NULL;
375 engine_state *state = new(std::nothrow) engine_state;
376 if (state == NULL) {
377 TRACE_ERROR("failed to allocate engine state\n");
378 return NULL;
381 fOffscreenBitmap->Lock();
382 BView *offscreen = new(std::nothrow) BView(fOffscreenBitmap->Bounds(),
383 "offscreen remote view", B_FOLLOW_NONE, B_WILL_DRAW);
384 if (offscreen == NULL) {
385 TRACE_ERROR("failed to allocate offscreen view\n");
386 fOffscreenBitmap->Unlock();
387 delete state;
388 return NULL;
391 fOffscreenBitmap->AddChild(offscreen);
392 fOffscreenBitmap->Unlock();
394 state->token = token;
395 state->view = offscreen;
396 state->pattern = B_SOLID_HIGH;
397 state->clipping_region.MakeEmpty();
398 state->pen_size = 0;
399 state->sync_drawing = true;
401 fStates.AddItem(state, -index - 1);
402 return state;
406 void
407 RemoteView::_DeleteState(uint32 token)
409 int32 index = fStates.BinarySearchIndexByKey(token, &_StateCompareByKey);
410 if (index < 0)
411 return;
413 engine_state *state = fStates.RemoveItemAt(index);
415 fOffscreenBitmap->RemoveChild(state->view);
416 delete state->view;
417 delete state;
421 engine_state *
422 RemoteView::_FindState(uint32 token)
424 return fStates.BinarySearchByKey(token, &_StateCompareByKey);
428 int32
429 RemoteView::_DrawEntry(void *data)
431 ((RemoteView *)data)->_DrawThread();
432 return 0;
436 void
437 RemoteView::_DrawThread()
439 RemoteMessage reply(NULL, fSendBuffer);
440 RemoteMessage message(fReceiveBuffer, NULL);
442 // cursor
443 BPoint cursorHotSpot(0, 0);
445 reply.Start(RP_INIT_CONNECTION);
446 reply.Flush();
448 while (!fStopThread) {
449 uint16 code;
450 status_t status = message.NextMessage(code);
452 if (status != B_OK) {
453 if (status == B_TIMED_OUT || status == -1) {
454 TRACE_ERROR("could not connect to device\n");
455 } else {
456 TRACE_ERROR("failed to read message from receiver\n");
457 break;
461 TRACE("code %u with %ld bytes data\n", code, message.DataLeft());
463 BAutolock locker(this->Looper());
464 if (!locker.IsLocked())
465 break;
467 // handle stuff that doesn't go to a specific engine
468 switch (code) {
469 case RP_INIT_CONNECTION:
471 BRect bounds = fOffscreenBitmap->Bounds();
472 reply.Start(RP_UPDATE_DISPLAY_MODE);
473 reply.Add(bounds.IntegerWidth() + 1);
474 reply.Add(bounds.IntegerHeight() + 1);
475 if (reply.Flush() == B_OK)
476 fIsConnected = true;
478 continue;
481 case RP_CLOSE_CONNECTION:
483 be_app->PostMessage(B_QUIT_REQUESTED);
484 continue;
487 case RP_CREATE_STATE:
488 case RP_DELETE_STATE:
490 uint32 token;
491 message.Read(token);
493 if (code == RP_CREATE_STATE)
494 _CreateState(token);
495 else
496 _DeleteState(token);
498 continue;
501 case RP_SET_CURSOR:
503 BBitmap *bitmap;
504 BPoint oldHotSpot = cursorHotSpot;
505 message.Read(cursorHotSpot);
506 if (message.ReadBitmap(&bitmap) != B_OK)
507 continue;
509 delete fCursorBitmap;
510 fCursorBitmap = bitmap;
512 Invalidate(fCursorFrame);
514 BRect bounds = fCursorBitmap->Bounds();
515 fCursorFrame.right = fCursorFrame.left
516 + bounds.IntegerWidth() + 1;
517 fCursorFrame.bottom = fCursorFrame.bottom
518 + bounds.IntegerHeight() + 1;
520 fCursorFrame.OffsetBy(oldHotSpot - cursorHotSpot);
522 Invalidate(fCursorFrame);
523 continue;
526 case RP_SET_CURSOR_VISIBLE:
528 bool wasVisible = fCursorVisible;
529 message.Read(fCursorVisible);
530 if (wasVisible != fCursorVisible)
531 Invalidate(fCursorFrame);
532 continue;
535 case RP_MOVE_CURSOR_TO:
537 BPoint position;
538 message.Read(position);
540 if (fCursorVisible)
541 Invalidate(fCursorFrame);
543 fCursorFrame.OffsetTo(position - cursorHotSpot);
545 Invalidate(fCursorFrame);
546 continue;
549 case RP_INVALIDATE_RECT:
551 BRect rect;
552 if (message.Read(rect) != B_OK)
553 continue;
555 Invalidate(rect);
556 continue;
559 case RP_INVALIDATE_REGION:
561 BRegion region;
562 if (message.ReadRegion(region) != B_OK)
563 continue;
565 Invalidate(&region);
566 continue;
569 case RP_FILL_REGION_COLOR_NO_CLIPPING:
571 BRegion region;
572 rgb_color color;
574 message.ReadRegion(region);
575 if (message.Read(color) != B_OK)
576 continue;
578 fOffscreen->LockLooper();
579 fOffscreen->SetHighColor(color);
580 fOffscreen->FillRegion(&region);
581 fOffscreen->UnlockLooper();
582 Invalidate(&region);
583 continue;
586 case RP_COPY_RECT_NO_CLIPPING:
588 int32 xOffset, yOffset;
589 BRect rect;
591 message.Read(xOffset);
592 message.Read(yOffset);
593 if (message.Read(rect) != B_OK)
594 continue;
596 BRect dest = rect.OffsetByCopy(xOffset, yOffset);
597 fOffscreen->LockLooper();
598 fOffscreen->CopyBits(rect, dest);
599 fOffscreen->UnlockLooper();
600 continue;
604 uint32 token;
605 message.Read(token);
607 engine_state *state = _FindState(token);
608 if (state == NULL) {
609 TRACE_ERROR("didn't find state for token %" B_PRIu32 "\n", token);
610 state = _CreateState(token);
611 if (state == NULL) {
612 TRACE_ERROR("failed to create state for unknown token\n");
613 continue;
617 BView *offscreen = state->view;
618 ::pattern &pattern = state->pattern;
619 BRegion &clippingRegion = state->clipping_region;
620 float &penSize = state->pen_size;
621 bool &syncDrawing = state->sync_drawing;
622 BRegion invalidRegion;
624 BAutolock offscreenLocker(offscreen->Looper());
625 if (!offscreenLocker.IsLocked())
626 break;
628 switch (code) {
629 case RP_ENABLE_SYNC_DRAWING:
630 syncDrawing = true;
631 continue;
633 case RP_DISABLE_SYNC_DRAWING:
634 syncDrawing = false;
635 continue;
637 case RP_SET_OFFSETS:
639 int32 xOffset, yOffset;
640 message.Read(xOffset);
641 if (message.Read(yOffset) != B_OK)
642 continue;
644 offscreen->MovePenTo(xOffset, yOffset);
645 break;
648 case RP_SET_HIGH_COLOR:
649 case RP_SET_LOW_COLOR:
651 rgb_color color;
652 if (message.Read(color) != B_OK)
653 continue;
655 if (code == RP_SET_HIGH_COLOR)
656 offscreen->SetHighColor(color);
657 else
658 offscreen->SetLowColor(color);
660 break;
663 case RP_SET_PEN_SIZE:
665 float newPenSize;
666 if (message.Read(newPenSize) != B_OK)
667 continue;
669 offscreen->SetPenSize(newPenSize);
670 penSize = newPenSize / 2;
671 break;
674 case RP_SET_STROKE_MODE:
676 cap_mode capMode;
677 join_mode joinMode;
678 float miterLimit;
680 message.Read(capMode);
681 message.Read(joinMode);
682 if (message.Read(miterLimit) != B_OK)
683 continue;
685 offscreen->SetLineMode(capMode, joinMode, miterLimit);
686 break;
689 case RP_SET_BLENDING_MODE:
691 source_alpha sourceAlpha;
692 alpha_function alphaFunction;
694 message.Read(sourceAlpha);
695 if (message.Read(alphaFunction) != B_OK)
696 continue;
698 offscreen->SetBlendingMode(sourceAlpha, alphaFunction);
699 break;
702 case RP_SET_TRANSFORM:
704 BAffineTransform transform;
705 if (message.ReadTransform(transform) != B_OK)
706 continue;
708 offscreen->SetTransform(transform);
709 break;
712 case RP_SET_PATTERN:
714 if (message.Read(pattern) != B_OK)
715 continue;
716 break;
719 case RP_SET_DRAWING_MODE:
721 drawing_mode drawingMode;
722 if (message.Read(drawingMode) != B_OK)
723 continue;
725 offscreen->SetDrawingMode(drawingMode);
726 break;
729 case RP_SET_FONT:
731 BFont font;
732 if (message.ReadFontState(font) != B_OK)
733 continue;
735 offscreen->SetFont(&font);
736 break;
739 case RP_CONSTRAIN_CLIPPING_REGION:
741 if (message.ReadRegion(clippingRegion) != B_OK)
742 continue;
744 offscreen->ConstrainClippingRegion(&clippingRegion);
745 break;
748 case RP_INVERT_RECT:
750 BRect rect;
751 if (message.Read(rect) != B_OK)
752 continue;
754 offscreen->InvertRect(rect);
755 invalidRegion.Include(rect);
756 break;
759 case RP_DRAW_BITMAP:
761 BBitmap *bitmap;
762 BRect bitmapRect, viewRect;
763 uint32 options;
765 message.Read(bitmapRect);
766 message.Read(viewRect);
767 message.Read(options);
768 if (message.ReadBitmap(&bitmap) != B_OK || bitmap == NULL)
769 continue;
771 offscreen->DrawBitmap(bitmap, bitmapRect, viewRect, options);
772 invalidRegion.Include(viewRect);
773 delete bitmap;
774 break;
777 case RP_DRAW_BITMAP_RECTS:
779 color_space colorSpace;
780 int32 rectCount;
781 uint32 flags, options;
783 message.Read(options);
784 message.Read(colorSpace);
785 message.Read(flags);
786 message.Read(rectCount);
787 for (int32 i = 0; i < rectCount; i++) {
788 BBitmap *bitmap;
789 BRect viewRect;
791 message.Read(viewRect);
792 if (message.ReadBitmap(&bitmap, true, colorSpace,
793 flags) != B_OK || bitmap == NULL) {
794 continue;
797 offscreen->DrawBitmap(bitmap, bitmap->Bounds(), viewRect,
798 options);
799 invalidRegion.Include(viewRect);
800 delete bitmap;
803 break;
806 case RP_STROKE_ARC:
807 case RP_FILL_ARC:
808 case RP_FILL_ARC_GRADIENT:
810 BRect rect;
811 float angle, span;
813 message.Read(rect);
814 message.Read(angle);
815 if (message.Read(span) != B_OK)
816 continue;
818 if (code == RP_STROKE_ARC) {
819 offscreen->StrokeArc(rect, angle, span, pattern);
820 rect.InsetBy(-penSize, -penSize);
821 } else if (code == RP_FILL_ARC)
822 offscreen->FillArc(rect, angle, span, pattern);
823 else {
824 BGradient *gradient;
825 if (message.ReadGradient(&gradient) != B_OK)
826 continue;
828 offscreen->FillArc(rect, angle, span, *gradient);
829 delete gradient;
832 invalidRegion.Include(rect);
833 break;
836 case RP_STROKE_BEZIER:
837 case RP_FILL_BEZIER:
838 case RP_FILL_BEZIER_GRADIENT:
840 BPoint points[4];
841 if (message.ReadList(points, 4) != B_OK)
842 continue;
844 BRect bounds = _BuildInvalidateRect(points, 4);
845 if (code == RP_STROKE_BEZIER) {
846 offscreen->StrokeBezier(points, pattern);
847 bounds.InsetBy(-penSize, -penSize);
848 } else if (code == RP_FILL_BEZIER)
849 offscreen->FillBezier(points, pattern);
850 else {
851 BGradient *gradient;
852 if (message.ReadGradient(&gradient) != B_OK)
853 continue;
855 offscreen->FillBezier(points, *gradient);
856 delete gradient;
859 invalidRegion.Include(bounds);
860 break;
863 case RP_STROKE_ELLIPSE:
864 case RP_FILL_ELLIPSE:
865 case RP_FILL_ELLIPSE_GRADIENT:
867 BRect rect;
868 if (message.Read(rect) != B_OK)
869 continue;
871 if (code == RP_STROKE_ELLIPSE) {
872 offscreen->StrokeEllipse(rect, pattern);
873 rect.InsetBy(-penSize, -penSize);
874 } else if (code == RP_FILL_ELLIPSE)
875 offscreen->FillEllipse(rect, pattern);
876 else {
877 BGradient *gradient;
878 if (message.ReadGradient(&gradient) != B_OK)
879 continue;
881 offscreen->FillEllipse(rect, *gradient);
882 delete gradient;
885 invalidRegion.Include(rect);
886 break;
889 case RP_STROKE_POLYGON:
890 case RP_FILL_POLYGON:
891 case RP_FILL_POLYGON_GRADIENT:
893 BRect bounds;
894 bool closed;
895 int32 numPoints;
897 message.Read(bounds);
898 message.Read(closed);
899 if (message.Read(numPoints) != B_OK)
900 continue;
902 BPoint points[numPoints];
903 for (int32 i = 0; i < numPoints; i++)
904 message.Read(points[i]);
906 if (code == RP_STROKE_POLYGON) {
907 offscreen->StrokePolygon(points, numPoints, bounds, closed,
908 pattern);
909 bounds.InsetBy(-penSize, -penSize);
910 } else if (code == RP_FILL_POLYGON)
911 offscreen->FillPolygon(points, numPoints, bounds, pattern);
912 else {
913 BGradient *gradient;
914 if (message.ReadGradient(&gradient) != B_OK)
915 continue;
917 offscreen->FillPolygon(points, numPoints, bounds,
918 *gradient);
919 delete gradient;
922 invalidRegion.Include(bounds);
923 break;
926 case RP_STROKE_RECT:
927 case RP_FILL_RECT:
928 case RP_FILL_RECT_GRADIENT:
930 BRect rect;
931 if (message.Read(rect) != B_OK)
932 continue;
934 if (code == RP_STROKE_RECT) {
935 offscreen->StrokeRect(rect, pattern);
936 rect.InsetBy(-penSize, -penSize);
937 } else if (code == RP_FILL_RECT)
938 offscreen->FillRect(rect, pattern);
939 else {
940 BGradient *gradient;
941 if (message.ReadGradient(&gradient) != B_OK)
942 continue;
944 offscreen->FillRect(rect, *gradient);
945 delete gradient;
948 invalidRegion.Include(rect);
949 break;
952 case RP_STROKE_ROUND_RECT:
953 case RP_FILL_ROUND_RECT:
954 case RP_FILL_ROUND_RECT_GRADIENT:
956 BRect rect;
957 float xRadius, yRadius;
959 message.Read(rect);
960 message.Read(xRadius);
961 if (message.Read(yRadius) != B_OK)
962 continue;
964 if (code == RP_STROKE_ROUND_RECT) {
965 offscreen->StrokeRoundRect(rect, xRadius, yRadius,
966 pattern);
967 rect.InsetBy(-penSize, -penSize);
968 } else if (code == RP_FILL_ROUND_RECT)
969 offscreen->FillRoundRect(rect, xRadius, yRadius, pattern);
970 else {
971 BGradient *gradient;
972 if (message.ReadGradient(&gradient) != B_OK)
973 continue;
975 offscreen->FillRoundRect(rect, xRadius, yRadius,
976 *gradient);
977 delete gradient;
980 invalidRegion.Include(rect);
981 break;
984 case RP_STROKE_SHAPE:
985 case RP_FILL_SHAPE:
986 case RP_FILL_SHAPE_GRADIENT:
988 BRect bounds;
989 int32 opCount, pointCount;
991 message.Read(bounds);
992 if (message.Read(opCount) != B_OK)
993 continue;
995 BMessage archive;
996 for (int32 i = 0; i < opCount; i++) {
997 int32 op;
998 message.Read(op);
999 archive.AddInt32("ops", op);
1002 if (message.Read(pointCount) != B_OK)
1003 continue;
1005 for (int32 i = 0; i < pointCount; i++) {
1006 BPoint point;
1007 message.Read(point);
1008 archive.AddPoint("pts", point);
1011 BPoint offset;
1012 message.Read(offset);
1014 float scale;
1015 if (message.Read(scale) != B_OK)
1016 continue;
1018 offscreen->PushState();
1019 offscreen->MovePenTo(offset);
1020 offscreen->SetScale(scale);
1022 BShape shape(&archive);
1023 if (code == RP_STROKE_SHAPE) {
1024 offscreen->StrokeShape(&shape, pattern);
1025 bounds.InsetBy(-penSize, -penSize);
1026 } else if (code == RP_FILL_SHAPE)
1027 offscreen->FillShape(&shape, pattern);
1028 else {
1029 BGradient *gradient;
1030 if (message.ReadGradient(&gradient) != B_OK) {
1031 offscreen->PopState();
1032 continue;
1035 offscreen->FillShape(&shape, *gradient);
1036 delete gradient;
1039 offscreen->PopState();
1040 invalidRegion.Include(bounds);
1041 break;
1044 case RP_STROKE_TRIANGLE:
1045 case RP_FILL_TRIANGLE:
1046 case RP_FILL_TRIANGLE_GRADIENT:
1048 BRect bounds;
1049 BPoint points[3];
1051 message.ReadList(points, 3);
1052 if (message.Read(bounds) != B_OK)
1053 continue;
1055 if (code == RP_STROKE_TRIANGLE) {
1056 offscreen->StrokeTriangle(points[0], points[1], points[2],
1057 bounds, pattern);
1058 bounds.InsetBy(-penSize, -penSize);
1059 } else if (code == RP_FILL_TRIANGLE) {
1060 offscreen->FillTriangle(points[0], points[1], points[2],
1061 bounds, pattern);
1062 } else {
1063 BGradient *gradient;
1064 if (message.ReadGradient(&gradient) != B_OK)
1065 continue;
1067 offscreen->FillTriangle(points[0], points[1], points[2],
1068 bounds, *gradient);
1069 delete gradient;
1072 invalidRegion.Include(bounds);
1073 break;
1076 case RP_STROKE_LINE:
1078 BPoint points[2];
1079 if (message.ReadList(points, 2) != B_OK)
1080 continue;
1082 offscreen->StrokeLine(points[0], points[1], pattern);
1084 BRect bounds = _BuildInvalidateRect(points, 2);
1085 invalidRegion.Include(bounds.InsetBySelf(-penSize, -penSize));
1086 break;
1089 case RP_STROKE_LINE_ARRAY:
1091 int32 numLines;
1092 if (message.Read(numLines) != B_OK)
1093 continue;
1095 BRect bounds;
1096 offscreen->BeginLineArray(numLines);
1097 for (int32 i = 0; i < numLines; i++) {
1098 rgb_color color;
1099 BPoint start, end;
1100 message.ReadArrayLine(start, end, color);
1101 offscreen->AddLine(start, end, color);
1103 bounds.left = min_c(bounds.left, min_c(start.x, end.x));
1104 bounds.top = min_c(bounds.top, min_c(start.y, end.y));
1105 bounds.right = max_c(bounds.right, max_c(start.x, end.x));
1106 bounds.bottom = max_c(bounds.bottom, max_c(start.y, end.y));
1109 offscreen->EndLineArray();
1110 invalidRegion.Include(bounds);
1111 break;
1114 case RP_FILL_REGION:
1115 case RP_FILL_REGION_GRADIENT:
1117 BRegion region;
1118 if (message.ReadRegion(region) != B_OK)
1119 continue;
1121 if (code == RP_FILL_REGION)
1122 offscreen->FillRegion(&region, pattern);
1123 else {
1124 BGradient *gradient;
1125 if (message.ReadGradient(&gradient) != B_OK)
1126 continue;
1128 offscreen->FillRegion(&region, *gradient);
1129 delete gradient;
1132 invalidRegion.Include(&region);
1133 break;
1136 case RP_STROKE_POINT_COLOR:
1138 BPoint point;
1139 rgb_color color;
1141 message.Read(point);
1142 if (message.Read(color) != B_OK)
1143 continue;
1145 rgb_color oldColor = offscreen->HighColor();
1146 offscreen->SetHighColor(color);
1147 offscreen->StrokeLine(point, point);
1148 offscreen->SetHighColor(oldColor);
1150 invalidRegion.Include(
1151 BRect(point, point).InsetBySelf(-penSize, -penSize));
1152 break;
1155 case RP_STROKE_LINE_1PX_COLOR:
1157 BPoint points[2];
1158 rgb_color color;
1160 message.ReadList(points, 2);
1161 if (message.Read(color) != B_OK)
1162 continue;
1164 float oldSize = offscreen->PenSize();
1165 rgb_color oldColor = offscreen->HighColor();
1166 drawing_mode oldMode = offscreen->DrawingMode();
1167 offscreen->SetPenSize(1);
1168 offscreen->SetHighColor(color);
1169 offscreen->SetDrawingMode(B_OP_OVER);
1171 offscreen->StrokeLine(points[0], points[1]);
1173 offscreen->SetDrawingMode(oldMode);
1174 offscreen->SetHighColor(oldColor);
1175 offscreen->SetPenSize(oldSize);
1177 invalidRegion.Include(_BuildInvalidateRect(points, 2));
1178 break;
1181 case RP_STROKE_RECT_1PX_COLOR:
1182 case RP_FILL_RECT_COLOR:
1184 BRect rect;
1185 rgb_color color;
1187 message.Read(rect);
1188 if (message.Read(color) != B_OK)
1189 continue;
1191 rgb_color oldColor = offscreen->HighColor();
1192 offscreen->SetHighColor(color);
1194 if (code == RP_STROKE_RECT_1PX_COLOR) {
1195 float oldSize = PenSize();
1196 offscreen->SetPenSize(1);
1197 offscreen->StrokeRect(rect);
1198 offscreen->SetPenSize(oldSize);
1199 } else
1200 offscreen->FillRect(rect);
1202 offscreen->SetHighColor(oldColor);
1203 invalidRegion.Include(rect);
1204 break;
1207 case RP_DRAW_STRING:
1209 BPoint point;
1210 size_t length;
1211 char *string;
1212 bool hasDelta;
1214 message.Read(point);
1215 message.ReadString(&string, length);
1216 if (message.Read(hasDelta) != B_OK) {
1217 free(string);
1218 continue;
1221 if (hasDelta) {
1222 escapement_delta delta[length];
1223 message.ReadList(delta, length);
1224 offscreen->DrawString(string, point, delta);
1225 } else
1226 offscreen->DrawString(string, point);
1228 free(string);
1229 reply.Start(RP_DRAW_STRING_RESULT);
1230 reply.Add(token);
1231 reply.Add(offscreen->PenLocation());
1232 reply.Flush();
1234 font_height height;
1235 offscreen->GetFontHeight(&height);
1237 BRect bounds(point, offscreen->PenLocation());
1238 bounds.top -= height.ascent;
1239 bounds.bottom += height.descent;
1240 invalidRegion.Include(bounds);
1241 break;
1244 case RP_DRAW_STRING_WITH_OFFSETS:
1246 size_t length;
1247 char *string;
1248 message.ReadString(&string, length);
1249 int32 count = UTF8CountChars(string, length);
1251 BPoint offsets[count];
1252 if (message.ReadList(offsets, count) != B_OK) {
1253 free(string);
1254 continue;
1257 offscreen->DrawString(string, offsets, count);
1259 free(string);
1260 reply.Start(RP_DRAW_STRING_RESULT);
1261 reply.Add(token);
1262 reply.Add(offscreen->PenLocation());
1263 reply.Flush();
1265 BFont font;
1266 offscreen->GetFont(&font);
1268 BRect boxes[count];
1269 font.GetBoundingBoxesAsGlyphs(string, count, B_SCREEN_METRIC,
1270 boxes);
1272 font_height height;
1273 offscreen->GetFontHeight(&height);
1275 for (int32 i = 0; i < count; i++) {
1276 // TODO: validate
1277 boxes[i].OffsetBy(offsets[i] + BPoint(0, -height.ascent));
1278 invalidRegion.Include(boxes[i]);
1281 break;
1284 case RP_READ_BITMAP:
1286 BRect bounds;
1287 bool drawCursor;
1289 message.Read(bounds);
1290 if (message.Read(drawCursor) != B_OK)
1291 continue;
1293 // TODO: support the drawCursor flag
1294 BBitmap bitmap(bounds, B_BITMAP_NO_SERVER_LINK, B_RGB32);
1295 bitmap.ImportBits(fOffscreenBitmap, bounds.LeftTop(),
1296 BPoint(0, 0), bounds.IntegerWidth() + 1,
1297 bounds.IntegerHeight() + 1);
1299 reply.Start(RP_READ_BITMAP_RESULT);
1300 reply.Add(token);
1301 reply.AddBitmap(&bitmap);
1302 reply.Flush();
1303 break;
1306 default:
1307 TRACE_ERROR("unknown protocol code: %u\n", code);
1308 break;
1311 if (syncDrawing) {
1312 offscreen->Sync();
1313 Invalidate(&invalidRegion);
1319 BRect
1320 RemoteView::_BuildInvalidateRect(BPoint *points, int32 pointCount)
1322 BRect bounds(1000000, 1000000, 0, 0);
1323 for (int32 i = 0; i < pointCount; i++) {
1324 bounds.left = min_c(bounds.left, points[i].x);
1325 bounds.top = min_c(bounds.top, points[i].y);
1326 bounds.right = max_c(bounds.right, points[i].x);
1327 bounds.bottom = max_c(bounds.bottom, points[i].y);
1330 return bounds;