xref: /haiku/src/apps/processcontroller/Utilities.cpp (revision 4a55cc230cf7566cadcbb23b1928eefff8aea9a2)
1 /*
2  * Copyright 2000, Georges-Edouard Berenger. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "Utilities.h"
8 #include "PCWorld.h"
9 #include "ProcessController.h"
10 #include "icons.h"
11 
12 #include <AppMisc.h>
13 #include <Alert.h>
14 #include <Bitmap.h>
15 #include <ControlLook.h>
16 #include <Deskbar.h>
17 #include <FindDirectory.h>
18 #include <NodeInfo.h>
19 #include <Path.h>
20 #include <Roster.h>
21 #include <Screen.h>
22 #include <Window.h>
23 
24 #include <stdio.h>
25 #include <string.h>
26 
27 
28 bool
29 get_team_name_and_icon(info_pack& infoPack, bool icon)
30 {
31 	BPath systemPath;
32 	find_directory(B_BEOS_SYSTEM_DIRECTORY, &systemPath);
33 
34 	bool nameFromArgs = false;
35 	bool tryTrackerIcon = true;
36 
37 	for (int len = strlen(infoPack.team_info.args) - 1;
38 			len >= 0 && infoPack.team_info.args[len] == ' '; len--) {
39 		infoPack.team_info.args[len] = 0;
40 	}
41 
42 	app_info info;
43 	status_t status = be_roster->GetRunningAppInfo(infoPack.team_info.team, &info);
44 	if (status != B_OK) {
45 		if (infoPack.team_info.team == B_SYSTEM_TEAM) {
46 			// Get icon and name from kernel image
47 			system_info	systemInfo;
48 			get_system_info(&systemInfo);
49 
50 			BPath kernelPath(systemPath);
51 			kernelPath.Append(systemInfo.kernel_name);
52 			get_ref_for_path(kernelPath.Path(), &info.ref);
53 			nameFromArgs = true;
54 		} else {
55 			status = BPrivate::get_app_ref(infoPack.team_info.team, &info.ref);
56 			nameFromArgs = true;
57 			tryTrackerIcon = (status == B_OK);
58 		}
59 	}
60 
61 	strncpy(infoPack.team_name, nameFromArgs ? infoPack.team_info.args : info.ref.name,
62 		B_PATH_NAME_LENGTH - 1);
63 
64 	if (icon) {
65 		infoPack.team_icon = new BBitmap(BRect(BPoint(0, 0),
66 			be_control_look->ComposeIconSize(B_MINI_ICON)), B_RGBA32);
67 		if (!tryTrackerIcon
68 			|| BNodeInfo::GetTrackerIcon(&info.ref, infoPack.team_icon,
69 				(icon_size)-1) != B_OK) {
70 			BMimeType genericAppType(B_APP_MIME_TYPE);
71 			status = genericAppType.GetIcon(infoPack.team_icon,
72 				(icon_size)(infoPack.team_icon->Bounds().IntegerWidth() + 1));
73 			// failed to get icon
74 			if (status != B_OK) {
75 				delete infoPack.team_icon;
76 				infoPack.team_icon = NULL;
77 				return false;
78 			}
79 		}
80 	} else
81 		infoPack.team_icon = NULL;
82 
83 	return true;
84 }
85 
86 
87 bool
88 launch(const char* signature, const char* path)
89 {
90 	status_t status = be_roster->Launch(signature);
91 	if (status != B_OK && path) {
92 		entry_ref ref;
93 		if (get_ref_for_path(path, &ref) == B_OK)
94 			status = be_roster->Launch(&ref);
95 	}
96 	return status == B_OK;
97 }
98 
99 
100 void
101 mix_colors(rgb_color &target, rgb_color & first, rgb_color & second, float mix)
102 {
103 	target.red = (uint8)(second.red * mix + (1. - mix) * first.red);
104 	target.green = (uint8)(second.green * mix + (1. - mix) * first.green);
105 	target.blue = (uint8)(second.blue * mix + (1. - mix) * first.blue);
106 	target.alpha = (uint8)(second.alpha * mix + (1. - mix) * first.alpha);
107 }
108 
109 
110 void
111 find_self(entry_ref& ref)
112 {
113 	int32 cookie = 0;
114 	image_info info;
115 	while (get_next_image_info (0, &cookie, &info) == B_OK) {
116 		if (((addr_t)info.text <= (addr_t)move_to_deskbar
117 			&& (addr_t)info.text + (size_t)info.text_size > (addr_t)move_to_deskbar)
118 			|| ((addr_t)info.data <= (addr_t)move_to_deskbar
119 			&& (addr_t)info.data + (size_t)info.data_size > (addr_t)move_to_deskbar)) {
120 			if (get_ref_for_path (info.name, &ref) == B_OK)
121 				return;
122 		}
123 	}
124 
125 	// This works, but not always... :(
126 	app_info appInfo;
127 	be_roster->GetAppInfo(kSignature, &appInfo);
128 	ref = appInfo.ref;
129 }
130 
131 
132 void
133 move_to_deskbar(BDeskbar& deskbar)
134 {
135 	entry_ref ref;
136 	find_self(ref);
137 
138 	deskbar.AddItem(&ref);
139 }
140 
141 
142 void
143 make_window_visible(BWindow* window, bool mayResize)
144 {
145 	uint32 flags = window->Flags();
146 	BRect screen = BScreen(window).Frame();
147 	BRect frame = window->Frame();
148 	screen.InsetBy(4, 8);
149 	screen.OffsetBy(0, 8);
150 
151 	if (mayResize) {
152 		float width = frame.Width();
153 		float height = frame.Height();
154 		if (screen.Width() < width && !(flags & B_NOT_H_RESIZABLE))
155 			width = screen.Width();
156 		if (screen.Height() < height && !(flags & B_NOT_V_RESIZABLE))
157 			height = screen.Height();
158 		if (width != frame.Width() || height != frame.Height()) {
159 			window->ResizeTo(width, height);
160 			frame.right = frame.left + width;
161 			frame.bottom = frame.top + height;
162 		}
163 	}
164 	if (frame.right > screen.right)
165 		window->MoveBy(screen.right-frame.right, 0);
166 	if (frame.bottom > screen.bottom)
167 		window->MoveBy(0, screen.bottom-frame.bottom);
168 	if (frame.left < screen.left)
169 		window->MoveTo(screen.left, frame.top);
170 	if (frame.top < screen.top)
171 		window->MoveBy(0, screen.top-frame.top);
172 }
173 
174 
175 BRect
176 bar_rect(BRect& frame, BFont* font)
177 {
178 	BRect rect(frame);
179 	font_height metrics;
180 	font->GetHeight(&metrics);
181 	float barHeight = metrics.ascent;
182 	rect.top = frame.top + (frame.Height() - barHeight) / 2;
183 	rect.bottom = frame.top + (frame.Height() + barHeight) / 2;
184 
185 	rect.left = frame.right - kMargin - kBarWidth;
186 	rect.right = frame.right - kMargin;
187 
188 	return rect;
189 }
190 
191