xref: /haiku/src/apps/devices/ResourceUsageWindow.cpp (revision 268f99dd7dc4bd7474a8bd2742d3f1ec1de6752a)
1 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2 //
3 //	Copyright (c) 2003-2004, Haiku
4 //
5 //  This software is part of the Haiku distribution and is covered
6 //  by the MIT License.
7 //
8 //
9 //  File:        ResourceUsageWindow.cpp
10 //  Author:      Sikosis, Jérôme Duval
11 //  Description: Devices Preferences
12 //  Created :    July 19, 2003
13 //
14 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
15 
16 
17 // Includes ------------------------------------------------------------------
18 #include <Box.h>
19 #include <Catalog.h>
20 #include <Screen.h>
21 #include <ScrollView.h>
22 #include <stdio.h>
23 #include <strings.h>
24 #include <TabView.h>
25 
26 #include "DevicesInfo.h"
27 #include "DevicesWindows.h"
28 
29 #undef B_TRANSLATION_CONTEXT
30 #define B_TRANSLATION_CONTEXT "ResourceUsageWindow"
31 
32 class IRQDMAItem : public BListItem
33 {
34 	public:
35 		IRQDMAItem(int32 number, const char* name);
36 		~IRQDMAItem();
37 		virtual void DrawItem(BView *, BRect, bool = false);
38 	private:
39 		char* fName;
40 		int32 fNumber;
41 };
42 
43 
IRQDMAItem(int32 number,const char * name)44 IRQDMAItem::IRQDMAItem(int32 number, const char* name)
45 	: BListItem(),
46 	fNumber(number)
47 {
48 	fName = strdup(name);
49 }
50 
~IRQDMAItem()51 IRQDMAItem::~IRQDMAItem()
52 {
53 	free(fName);
54 }
55 
56 /***********************************************************
57  * DrawItem
58  ***********************************************************/
59 void
DrawItem(BView * owner,BRect itemRect,bool complete)60 IRQDMAItem::DrawItem(BView *owner, BRect itemRect, bool complete)
61 {
62 	rgb_color kBlack = { 0,0,0,0 };
63 	rgb_color kHighlight = { 156,154,156,0 };
64 
65 	if (IsSelected() || complete) {
66 		rgb_color color;
67 		if (IsSelected())
68 			color = kHighlight;
69 		else
70 			color = owner->ViewColor();
71 
72 		owner->SetHighColor(color);
73 		owner->SetLowColor(color);
74 		owner->FillRect(itemRect);
75 		owner->SetHighColor(kBlack);
76 
77 	} else {
78 		owner->SetLowColor(owner->ViewColor());
79 	}
80 
81 	BFont font = be_plain_font;
82 	font_height	finfo;
83 	font.GetHeight(&finfo);
84 
85 	BPoint point = BPoint(itemRect.left + 5, itemRect.bottom - finfo.descent + 1);
86 
87 	owner->SetHighColor(kBlack);
88 	owner->SetFont(be_plain_font);
89 	owner->MovePenTo(point);
90 	if (fNumber > -1) {
91 		char string[2];
92 		sprintf(string, "%ld", fNumber);
93 		owner->DrawString(string);
94 	}
95 	point += BPoint(28, 0);
96 	owner->MovePenTo(point);
97 	owner->DrawString(fName);
98 }
99 
100 class RangeItem : public BListItem
101 {
102 	public:
103 		RangeItem(uint32 lowAddress, uint32 highAddress, const char* name);
104 		~RangeItem();
105 		virtual void DrawItem(BView *, BRect, bool = false);
106 		static int Compare(const void *firstArg, const void *secondArg);
107 	private:
108 		char* fName;
109 		uint32 fLowAddress, fHighAddress;
110 };
111 
112 
RangeItem(uint32 lowAddress,uint32 highAddress,const char * name)113 RangeItem::RangeItem(uint32 lowAddress, uint32 highAddress, const char* name)
114 	: BListItem(),
115 	fLowAddress(lowAddress),
116 	fHighAddress(highAddress)
117 {
118 	fName = strdup(name);
119 }
120 
~RangeItem()121 RangeItem::~RangeItem()
122 {
123 	free(fName);
124 }
125 
126 /***********************************************************
127  * DrawItem
128  ***********************************************************/
129 void
DrawItem(BView * owner,BRect itemRect,bool complete)130 RangeItem::DrawItem(BView *owner, BRect itemRect, bool complete)
131 {
132 	rgb_color kBlack = { 0,0,0,0 };
133 	rgb_color kHighlight = { 156,154,156,0 };
134 
135 	if (IsSelected() || complete) {
136 		rgb_color color;
137 		if (IsSelected())
138 			color = kHighlight;
139 		else
140 			color = owner->ViewColor();
141 
142 		owner->SetHighColor(color);
143 		owner->SetLowColor(color);
144 		owner->FillRect(itemRect);
145 		owner->SetHighColor(kBlack);
146 
147 	} else {
148 		owner->SetLowColor(owner->ViewColor());
149 	}
150 
151 	BFont font = be_plain_font;
152 	font_height	finfo;
153 	font.GetHeight(&finfo);
154 
155 	BPoint point = BPoint(itemRect.left + 17, itemRect.bottom - finfo.descent + 1);
156 	owner->SetFont(be_fixed_font);
157 	owner->SetHighColor(kBlack);
158 	owner->MovePenTo(point);
159 
160 	if (fLowAddress >= 0) {
161 		char string[255];
162 		sprintf(string, "0x%04lx - 0x%04lx", fLowAddress, fHighAddress);
163 		owner->DrawString(string);
164 	}
165 	point += BPoint(174, 0);
166 	owner->SetFont(be_plain_font);
167 	owner->MovePenTo(point);
168 	owner->DrawString(fName);
169 }
170 
171 int
Compare(const void * firstArg,const void * secondArg)172 RangeItem::Compare(const void *firstArg, const void *secondArg)
173 {
174 	const RangeItem *item1 = *static_cast<const RangeItem * const *>(firstArg);
175 	const RangeItem *item2 = *static_cast<const RangeItem * const *>(secondArg);
176 
177 	if (item1->fLowAddress < item2->fLowAddress) {
178 		return -1;
179 	} else if (item1->fLowAddress > item2->fLowAddress) {
180 		return 1;
181 	} else
182 		return 0;
183 
184 }
185 
186 
187 // ----------------------------------------------------------------------------
188 
189 
190 // ResourceUsageWindow - Constructor
ResourceUsageWindow(BRect frame,BList & list)191 ResourceUsageWindow::ResourceUsageWindow(BRect frame, BList &list)
192 	: BWindow (frame, B_TRANSLATE("Resource Usage"), B_TITLED_WINDOW_LOOK,
193 		B_NORMAL_WINDOW_FEEL , B_NOT_ZOOMABLE|B_NOT_RESIZABLE)
194 {
195 	InitWindow(list);
196 	CenterOnScreen();
197 	Show();
198 }
199 // ----------------------------------------------------------------------------
200 
201 
202 // ResourceUsageWindow - Destructor
~ResourceUsageWindow()203 ResourceUsageWindow::~ResourceUsageWindow()
204 {
205 
206 }
207 // ----------------------------------------------------------------------------
208 
209 
210 // ResourceUsageWindow::InitWindow -- Initialization Commands here
InitWindow(BList & list)211 void ResourceUsageWindow::InitWindow(BList &list)
212 {
213 	BRect rtab = Bounds();
214 	BRect rlist = Bounds();
215 	rtab.top += 10;
216 	rlist.top += 10;
217 	rlist.left += 12;
218 	rlist.right -= 15 + B_V_SCROLL_BAR_WIDTH;
219 	rlist.bottom -= 47;
220 
221 	// Create the TabView and Tabs
222 	BTabView *tabView = new BTabView(rtab,"resource_usage_tabview");
223 	tabView->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
224 
225 	rtab = tabView->Bounds();
226 	rtab.InsetBy(5,5);
227 
228 	// Create the ListViews
229 	BListView *IRQListView = new BListView(rlist, "IRQListView",
230 		B_SINGLE_SELECTION_LIST, B_FOLLOW_LEFT | B_FOLLOW_TOP,
231 		B_WILL_DRAW | B_NAVIGABLE);
232 	BListView *DMAListView = new BListView(rlist, "DMAListView",
233 		B_SINGLE_SELECTION_LIST, B_FOLLOW_LEFT | B_FOLLOW_TOP,
234 		B_WILL_DRAW | B_NAVIGABLE);
235 	BListView *IORangeListView = new BListView(rlist, "IORangeListView",
236 		B_SINGLE_SELECTION_LIST, B_FOLLOW_LEFT | B_FOLLOW_TOP,
237 		B_WILL_DRAW | B_NAVIGABLE);
238 	BListView *memoryListView = new BListView(rlist, "memoryListView",
239 		B_SINGLE_SELECTION_LIST, B_FOLLOW_LEFT | B_FOLLOW_TOP,
240 		B_WILL_DRAW | B_NAVIGABLE);
241 
242 	BScrollView *IRQScrollView = new BScrollView("scroll_list1", IRQListView,
243 		B_FOLLOW_LEFT|B_FOLLOW_TOP, 0, false, true, B_FANCY_BORDER);
244 	BScrollView *DMAScrollView = new BScrollView("scroll_list2", DMAListView,
245 		B_FOLLOW_LEFT|B_FOLLOW_TOP, 0, false, true, B_FANCY_BORDER);
246 	BScrollView *IORangeScrollView = new BScrollView("scroll_list3",
247 		IORangeListView, B_FOLLOW_LEFT|B_FOLLOW_TOP, 0, false, true,
248 		B_FANCY_BORDER);
249 	BScrollView *memoryScrollView = new BScrollView("scroll_list4",
250 		memoryListView, B_FOLLOW_LEFT|B_FOLLOW_TOP, 0, false, true,
251 		B_FANCY_BORDER);
252 
253 	BTab *tab = new BTab();
254 	tabView->AddTab(IRQScrollView, tab);
255 	tab->SetLabel(B_TRANSLATE("IRQ"));
256 	tab = new BTab();
257 	tabView->AddTab(DMAScrollView, tab);
258 	tab->SetLabel(B_TRANSLATE("DMA"));
259 	tab = new BTab();
260 	tabView->AddTab(IORangeScrollView, tab);
261 	tab->SetLabel(B_TRANSLATE("IO Range"));
262 	tab = new BTab();
263 	tabView->AddTab(memoryScrollView, tab);
264 	tab->SetLabel(B_TRANSLATE("Memory Range"));
265 
266 	{
267 		uint32 mask = 1;
268 
269 		for (int i=0;i<16;mask<<=1,i++) {
270 			bool first = true;
271 
272 			for (int32 j=0; j<list.CountItems(); j++) {
273 				DevicesInfo *deviceInfo = (DevicesInfo *)list.ItemAt(j);
274 				struct device_configuration *current = deviceInfo->GetCurrent();
275 				resource_descriptor r;
276 
277 				int32 num = count_resource_descriptors_of_type(current,
278 					B_IRQ_RESOURCE);
279 
280 				for (int32 k=0;k<num;k++) {
281 					get_nth_resource_descriptor_of_type(current, k, B_IRQ_RESOURCE,
282 							&r, sizeof(resource_descriptor));
283 
284 					if (mask & r.d.m.mask) {
285 						IRQListView->AddItem(new IRQDMAItem(first ? i : -1,
286 							deviceInfo->GetCardName()));
287 						first = false;
288 					}
289 				}
290 			}
291 
292 			if (first) {
293 				IRQListView->AddItem(new IRQDMAItem(i, ""));
294 			}
295 		}
296 	}
297 
298 	{
299 		uint32 mask = 1;
300 
301 		for (int i=0;i<8;mask<<=1,i++) {
302 			bool first = true;
303 
304 			for (int32 j=0; j<list.CountItems(); j++) {
305 				DevicesInfo *deviceInfo = (DevicesInfo *)list.ItemAt(j);
306 				struct device_configuration *current = deviceInfo->GetCurrent();
307 				resource_descriptor r;
308 
309 				int32 num = count_resource_descriptors_of_type(current,
310 					B_DMA_RESOURCE);
311 
312 				for (int32 k=0;k<num;k++) {
313 					get_nth_resource_descriptor_of_type(current, k,
314 						B_DMA_RESOURCE,	&r, sizeof(resource_descriptor));
315 
316 					if (mask & r.d.m.mask) {
317 						DMAListView->AddItem(new IRQDMAItem(first ? i : -1,
318 							deviceInfo->GetCardName()));
319 						first = false;
320 					}
321 				}
322 			}
323 
324 			if (first) {
325 				DMAListView->AddItem(new IRQDMAItem(i, ""));
326 			}
327 		}
328 	}
329 
330 	{
331 		for (int32 j=0; j<list.CountItems(); j++) {
332 			DevicesInfo *deviceInfo = (DevicesInfo *)list.ItemAt(j);
333 			struct device_configuration *current = deviceInfo->GetCurrent();
334 			resource_descriptor r;
335 
336 			int32 num = count_resource_descriptors_of_type(current,
337 				B_IO_PORT_RESOURCE);
338 
339 			for (int32 k=0;k<num;k++) {
340 				get_nth_resource_descriptor_of_type(current, k,
341 					B_IO_PORT_RESOURCE,	&r, sizeof(resource_descriptor));
342 
343 				IORangeListView->AddItem(new RangeItem(r.d.r.minbase,
344 					r.d.r.minbase + r.d.r.len - 1, deviceInfo->GetCardName()));
345 			}
346 		}
347 
348 		IORangeListView->SortItems(&RangeItem::Compare);
349 	}
350 
351 	{
352 		for (int32 j=0; j<list.CountItems(); j++) {
353 			DevicesInfo *deviceInfo = (DevicesInfo *)list.ItemAt(j);
354 			struct device_configuration *current = deviceInfo->GetCurrent();
355 			resource_descriptor r;
356 
357 			int32 num = count_resource_descriptors_of_type(current,
358 				B_MEMORY_RESOURCE);
359 
360 			for (int32 k=0;k<num;k++) {
361 				get_nth_resource_descriptor_of_type(current, k, B_MEMORY_RESOURCE,
362 						&r, sizeof(resource_descriptor));
363 
364 				memoryListView->AddItem(new RangeItem(r.d.r.minbase,
365 					r.d.r.minbase + r.d.r.len - 1, deviceInfo->GetCardName()));
366 			}
367 		}
368 
369 		memoryListView->SortItems(&RangeItem::Compare);
370 	}
371 
372 
373 	BBox *background = new BBox(Bounds(), "background");
374 	background->SetBorder(B_NO_BORDER);
375 	AddChild(background);
376 	background->AddChild(tabView);
377 }
378 // ----------------------------------------------------------------------------
379 
380 
381 // ResourceUsageWindow::MessageReceived -- receives messages
382 void
MessageReceived(BMessage * message)383 ResourceUsageWindow::MessageReceived (BMessage *message)
384 {
385 	switch(message->what)
386 	{
387 		default:
388 			BWindow::MessageReceived(message);
389 			break;
390 	}
391 }
392 // ----------------------------------------------------------------------------
393