BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / apps / devices / ResourceUsageWindow.cpp
blobd2747dcf64565deab522be585db67548172ec9ee
1 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2 //
3 // Copyright (c) 2003-2004, OpenBeOS
4 //
5 // This software is part of the OpenBeOS 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 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
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>
26 #include "DevicesInfo.h"
27 #include "DevicesWindows.h"
29 #undef B_TRANSLATION_CONTEXT
30 #define B_TRANSLATION_CONTEXT "ResourceUsageWindow"
32 class IRQDMAItem : public BListItem
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;
44 IRQDMAItem::IRQDMAItem(int32 number, const char* name)
45 : BListItem(),
46 fNumber(number)
48 fName = strdup(name);
51 IRQDMAItem::~IRQDMAItem()
53 free(fName);
56 /***********************************************************
57 * DrawItem
58 ***********************************************************/
59 void
60 IRQDMAItem::DrawItem(BView *owner, BRect itemRect, bool complete)
62 rgb_color kBlack = { 0,0,0,0 };
63 rgb_color kHighlight = { 156,154,156,0 };
65 if (IsSelected() || complete) {
66 rgb_color color;
67 if (IsSelected())
68 color = kHighlight;
69 else
70 color = owner->ViewColor();
72 owner->SetHighColor(color);
73 owner->SetLowColor(color);
74 owner->FillRect(itemRect);
75 owner->SetHighColor(kBlack);
77 } else {
78 owner->SetLowColor(owner->ViewColor());
81 BFont font = be_plain_font;
82 font_height finfo;
83 font.GetHeight(&finfo);
85 BPoint point = BPoint(itemRect.left + 5, itemRect.bottom - finfo.descent + 1);
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);
95 point += BPoint(28, 0);
96 owner->MovePenTo(point);
97 owner->DrawString(fName);
100 class RangeItem : public BListItem
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;
113 RangeItem::RangeItem(uint32 lowAddress, uint32 highAddress, const char* name)
114 : BListItem(),
115 fLowAddress(lowAddress),
116 fHighAddress(highAddress)
118 fName = strdup(name);
121 RangeItem::~RangeItem()
123 free(fName);
126 /***********************************************************
127 * DrawItem
128 ***********************************************************/
129 void
130 RangeItem::DrawItem(BView *owner, BRect itemRect, bool complete)
132 rgb_color kBlack = { 0,0,0,0 };
133 rgb_color kHighlight = { 156,154,156,0 };
135 if (IsSelected() || complete) {
136 rgb_color color;
137 if (IsSelected())
138 color = kHighlight;
139 else
140 color = owner->ViewColor();
142 owner->SetHighColor(color);
143 owner->SetLowColor(color);
144 owner->FillRect(itemRect);
145 owner->SetHighColor(kBlack);
147 } else {
148 owner->SetLowColor(owner->ViewColor());
151 BFont font = be_plain_font;
152 font_height finfo;
153 font.GetHeight(&finfo);
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);
160 if (fLowAddress >= 0) {
161 char string[255];
162 sprintf(string, "0x%04lx - 0x%04lx", fLowAddress, fHighAddress);
163 owner->DrawString(string);
165 point += BPoint(174, 0);
166 owner->SetFont(be_plain_font);
167 owner->MovePenTo(point);
168 owner->DrawString(fName);
171 int
172 RangeItem::Compare(const void *firstArg, const void *secondArg)
174 const RangeItem *item1 = *static_cast<const RangeItem * const *>(firstArg);
175 const RangeItem *item2 = *static_cast<const RangeItem * const *>(secondArg);
177 if (item1->fLowAddress < item2->fLowAddress) {
178 return -1;
179 } else if (item1->fLowAddress > item2->fLowAddress) {
180 return 1;
181 } else
182 return 0;
187 // ----------------------------------------------------------------------------
190 // ResourceUsageWindow - Constructor
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)
195 InitWindow(list);
196 CenterOnScreen();
197 Show();
199 // ----------------------------------------------------------------------------
202 // ResourceUsageWindow - Destructor
203 ResourceUsageWindow::~ResourceUsageWindow()
207 // ----------------------------------------------------------------------------
210 // ResourceUsageWindow::InitWindow -- Initialization Commands here
211 void ResourceUsageWindow::InitWindow(BList &list)
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;
221 // Create the TabView and Tabs
222 BTabView *tabView = new BTabView(rtab,"resource_usage_tabview");
223 tabView->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
225 rtab = tabView->Bounds();
226 rtab.InsetBy(5,5);
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);
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);
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"));
267 uint32 mask = 1;
269 for (int i=0;i<16;mask<<=1,i++) {
270 bool first = true;
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;
277 int32 num = count_resource_descriptors_of_type(current,
278 B_IRQ_RESOURCE);
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));
284 if (mask & r.d.m.mask) {
285 IRQListView->AddItem(new IRQDMAItem(first ? i : -1,
286 deviceInfo->GetCardName()));
287 first = false;
292 if (first) {
293 IRQListView->AddItem(new IRQDMAItem(i, ""));
299 uint32 mask = 1;
301 for (int i=0;i<8;mask<<=1,i++) {
302 bool first = true;
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;
309 int32 num = count_resource_descriptors_of_type(current,
310 B_DMA_RESOURCE);
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));
316 if (mask & r.d.m.mask) {
317 DMAListView->AddItem(new IRQDMAItem(first ? i : -1,
318 deviceInfo->GetCardName()));
319 first = false;
324 if (first) {
325 DMAListView->AddItem(new IRQDMAItem(i, ""));
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;
336 int32 num = count_resource_descriptors_of_type(current,
337 B_IO_PORT_RESOURCE);
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));
343 IORangeListView->AddItem(new RangeItem(r.d.r.minbase,
344 r.d.r.minbase + r.d.r.len - 1, deviceInfo->GetCardName()));
348 IORangeListView->SortItems(&RangeItem::Compare);
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;
357 int32 num = count_resource_descriptors_of_type(current,
358 B_MEMORY_RESOURCE);
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));
364 memoryListView->AddItem(new RangeItem(r.d.r.minbase,
365 r.d.r.minbase + r.d.r.len - 1, deviceInfo->GetCardName()));
369 memoryListView->SortItems(&RangeItem::Compare);
373 BBox *background = new BBox(Bounds(), "background");
374 background->SetBorder(B_NO_BORDER);
375 AddChild(background);
376 background->AddChild(tabView);
378 // ----------------------------------------------------------------------------
381 // ResourceUsageWindow::MessageReceived -- receives messages
382 void
383 ResourceUsageWindow::MessageReceived (BMessage *message)
385 switch(message->what)
387 default:
388 BWindow::MessageReceived(message);
389 break;
392 // ----------------------------------------------------------------------------