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