xref: /haiku/src/apps/patchbay/EndpointInfo.cpp (revision a3e794ae459fec76826407f8ba8c94cd3535f128)
1 /* EndpointInfo.cpp
2  * ----------------
3  * Implements the EndpointInfo object.
4  *
5  * Copyright 2013, Haiku, Inc. All rights reserved.
6  * Distributed under the terms of the MIT License.
7  *
8  * Revisions by Pete Goodeve
9  *
10  * Copyright 1999, Be Incorporated.   All Rights Reserved.
11  * This file may be used under the terms of the Be Sample Code License.
12  */
13 #include "EndpointInfo.h"
14 
15 #include <Bitmap.h>
16 #include <Debug.h>
17 #include <IconUtils.h>
18 #include <Message.h>
19 #include <MidiRoster.h>
20 #include <MidiEndpoint.h>
21 
22 const char* LARGE_ICON_NAME = "be:large_icon";
23 const char* MINI_ICON_NAME = "be:mini_icon";
24 const char* VECTOR_ICON_NAME = "icon";
25 const uint32 LARGE_ICON_TYPE = 'ICON';
26 const uint32 MINI_ICON_TYPE = 'MICN';
27 const uint32 VECTOR_ICON_TYPE = 'VICN';
28 extern const uint8 LARGE_ICON_SIZE = 32;
29 extern const uint8 MINI_ICON_SIZE = 16;
30 extern const icon_size DISPLAY_ICON_SIZE = B_LARGE_ICON;
31 extern const color_space ICON_COLOR_SPACE = B_CMAP8;
32 
33 static BBitmap* CreateIcon(const BMessage* msg, icon_size which);
34 
35 
36 EndpointInfo::EndpointInfo()
37 	:
38 	fId(-1),
39 	fIcon(NULL)
40 {}
41 
42 
43 EndpointInfo::EndpointInfo(int32 id)
44 	:
45 	fId(id),
46 	fIcon(NULL)
47 {
48 	BMidiRoster* roster = BMidiRoster::MidiRoster();
49 	if (roster != NULL) {
50 		BMidiEndpoint* endpoint = roster->FindEndpoint(id);
51 		if (endpoint != NULL) {
52 			printf("endpoint %" B_PRId32 " = %p\n", id, endpoint);
53 			BMessage msg;
54 			if (endpoint->GetProperties(&msg) == B_OK) {
55 				fIcon = CreateIcon(&msg, DISPLAY_ICON_SIZE);
56 			}
57 			endpoint->Release();
58 		}
59 	}
60 }
61 
62 
63 EndpointInfo::EndpointInfo(const EndpointInfo& info)
64 	:
65 	fId(info.fId)
66 {
67 	fIcon = (info.fIcon) ? new BBitmap(info.fIcon) : NULL;
68 }
69 
70 
71 EndpointInfo&
72 EndpointInfo::operator=(const EndpointInfo& info)
73 {
74 	if (&info != this) {
75 		fId = info.fId;
76 		delete fIcon;
77 		fIcon = (info.fIcon) ? new BBitmap(info.fIcon) : NULL;
78 	}
79 	return *this;
80 }
81 
82 
83 EndpointInfo::~EndpointInfo()
84 {
85 	delete fIcon;
86 }
87 
88 
89 void
90 EndpointInfo::UpdateProperties(const BMessage* props)
91 {
92 	delete fIcon;
93 	fIcon = CreateIcon(props, DISPLAY_ICON_SIZE);
94 }
95 
96 
97 static BBitmap*
98 CreateIcon(const BMessage* msg, icon_size which)
99 {
100 
101 	const void* data;
102 	ssize_t size;
103 	BBitmap* bitmap = NULL;
104 
105 	// See if a Haiku Vector Icon available
106 	if (msg->FindData(VECTOR_ICON_NAME, VECTOR_ICON_TYPE, &data,
107 		&size) == B_OK)  {
108 		BRect r(0, 0, LARGE_ICON_SIZE - 1, LARGE_ICON_SIZE - 1);
109 		bitmap = new BBitmap(r, B_RGBA32);
110 		if (BIconUtils::GetVectorIcon((const uint8*)data, size,
111 			bitmap) == B_OK) {
112 			printf("Created vector icon bitmap\n");
113 			return bitmap;
114 		} else {
115 			delete bitmap;
116 			bitmap = NULL;
117 		}
118 	}
119 
120 	// If not, look for BeOS style icon
121 	float bmapSize;
122 	uint32 iconType;
123 	const char* iconName;
124 
125 	if (which == B_LARGE_ICON) {
126 		bmapSize = LARGE_ICON_SIZE - 1;
127 		iconType = LARGE_ICON_TYPE;
128 		iconName = LARGE_ICON_NAME;
129 	} else if (which == B_MINI_ICON) {
130 		bmapSize = MINI_ICON_SIZE - 1;
131 		iconType = MINI_ICON_TYPE;
132 		iconName = MINI_ICON_NAME;
133 	} else
134 		return NULL;
135 
136 	if (msg->FindData(iconName, iconType, &data, &size) == B_OK) {
137 		bitmap = new BBitmap(BRect(0, 0, bmapSize, bmapSize),
138 			ICON_COLOR_SPACE);
139 		ASSERT((bitmap->BitsLength() == size));
140 		memcpy(bitmap->Bits(), data, size);
141 	}
142 	return bitmap;
143 }
144