xref: /haiku/src/apps/processcontroller/Utilities.cpp (revision c05253c64ed5afe485062830e21ff448a48bc741)
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 		}
71 	} else
72 		infoPack.team_icon = NULL;
73 
74 	return true;
75 }
76 
77 
78 bool
79 launch(const char* signature, const char* path)
80 {
81 	status_t status = be_roster->Launch(signature);
82 	if (status != B_OK && path) {
83 		entry_ref ref;
84 		if (get_ref_for_path(path, &ref) == B_OK)
85 			status = be_roster->Launch(&ref);
86 	}
87 	return status == B_OK;
88 }
89 
90 
91 void
92 mix_colors(rgb_color &target, rgb_color & first, rgb_color & second, float mix)
93 {
94 	target.red = (uint8)(second.red * mix + (1. - mix) * first.red);
95 	target.green = (uint8)(second.green * mix + (1. - mix) * first.green);
96 	target.blue = (uint8)(second.blue * mix + (1. - mix) * first.blue);
97 	target.alpha = (uint8)(second.alpha * mix + (1. - mix) * first.alpha);
98 }
99 
100 
101 void
102 find_self(entry_ref& ref)
103 {
104 	int32 cookie = 0;
105 	image_info info;
106 	while (get_next_image_info (0, &cookie, &info) == B_OK) {
107 		if (((addr_t)info.text <= (addr_t)move_to_deskbar
108 			&& (addr_t)info.text + (size_t)info.text_size > (addr_t)move_to_deskbar)
109 			|| ((addr_t)info.data <= (addr_t)move_to_deskbar
110 			&& (addr_t)info.data + (size_t)info.data_size > (addr_t)move_to_deskbar)) {
111 			if (get_ref_for_path (info.name, &ref) == B_OK)
112 				return;
113 		}
114 	}
115 
116 	// This works, but not always... :(
117 	app_info appInfo;
118 	be_roster->GetAppInfo(kSignature, &appInfo);
119 	ref = appInfo.ref;
120 }
121 
122 
123 void
124 move_to_deskbar(BDeskbar& deskbar)
125 {
126 	entry_ref ref;
127 	find_self(ref);
128 
129 	deskbar.AddItem(&ref);
130 }
131 
132 
133 void
134 make_window_visible(BWindow* window, bool mayResize)
135 {
136 	uint32 flags = window->Flags();
137 	BRect screen = BScreen(window).Frame();
138 	BRect frame = window->Frame();
139 	screen.InsetBy(4, 8);
140 	screen.OffsetBy(0, 8);
141 
142 	if (mayResize) {
143 		float width = frame.Width();
144 		float height = frame.Height();
145 		if (screen.Width() < width && !(flags & B_NOT_H_RESIZABLE))
146 			width = screen.Width();
147 		if (screen.Height() < height && !(flags & B_NOT_V_RESIZABLE))
148 			height = screen.Height();
149 		if (width != frame.Width() || height != frame.Height()) {
150 			window->ResizeTo(width, height);
151 			frame.right = frame.left + width;
152 			frame.bottom = frame.top + height;
153 		}
154 	}
155 	if (frame.right > screen.right)
156 		window->MoveBy(screen.right-frame.right, 0);
157 	if (frame.bottom > screen.bottom)
158 		window->MoveBy(0, screen.bottom-frame.bottom);
159 	if (frame.left < screen.left)
160 		window->MoveTo(screen.left, frame.top);
161 	if (frame.top < screen.top)
162 		window->MoveBy(0, screen.top-frame.top);
163 }
164 
165 
166 BRect
167 bar_rect(BRect& frame, BFont* font)
168 {
169 	BRect rect(frame);
170 	font_height metrics;
171 	font->GetHeight(&metrics);
172 	float barHeight = metrics.ascent;
173 	rect.top = frame.top + (frame.Height() - barHeight) / 2;
174 	rect.bottom = frame.top + (frame.Height() + barHeight) / 2;
175 
176 	rect.left = frame.right - kMargin - kBarWidth;
177 	rect.right = frame.right - kMargin;
178 
179 	return rect;
180 }
181 
182