xref: /haiku/src/apps/processcontroller/Utilities.cpp (revision 97dfeb96704e5dbc5bec32ad7b21379d0125e031)
1 /*
2 	ProcessController © 2000, Georges-Edouard Berenger, All Rights Reserved.
3 	Copyright (C) 2004 beunited.org
4 
5 	This library is free software; you can redistribute it and/or
6 	modify it under the terms of the GNU Lesser General Public
7 	License as published by the Free Software Foundation; either
8 	version 2.1 of the License, or (at your option) any later version.
9 
10 	This library is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 	Lesser General Public License for more details.
14 
15 	You should have received a copy of the GNU Lesser General Public
16 	License along with this library; if not, write to the Free Software
17 	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19 
20 
21 #include "Utilities.h"
22 #include "PCWorld.h"
23 #include "ProcessController.h"
24 #include "icons.h"
25 
26 #include <AppMisc.h>
27 #include <Alert.h>
28 #include <Bitmap.h>
29 #include <Deskbar.h>
30 #include <FindDirectory.h>
31 #include <NodeInfo.h>
32 #include <Path.h>
33 #include <Roster.h>
34 #include <Screen.h>
35 #include <Window.h>
36 
37 #include <stdio.h>
38 #include <string.h>
39 
40 
41 bool
42 get_team_name_and_icon(info_pack& infoPack, bool icon)
43 {
44 	BPath systemPath;
45 	find_directory(B_BEOS_SYSTEM_DIRECTORY, &systemPath);
46 
47 	bool nameFromArgs = false;
48 	bool tryTrackerIcon = true;
49 
50 	for (int len = strlen(infoPack.team_info.args) - 1;
51 			len >= 0 && infoPack.team_info.args[len] == ' '; len--) {
52 		infoPack.team_info.args[len] = 0;
53 	}
54 
55 	app_info info;
56 	status_t status = be_roster->GetRunningAppInfo(infoPack.team_info.team, &info);
57 	if (status != B_OK) {
58 		if (infoPack.team_info.team == B_SYSTEM_TEAM) {
59 			// Get icon and name from kernel image
60 			system_info	systemInfo;
61 			get_system_info(&systemInfo);
62 
63 			BPath kernelPath(systemPath);
64 			kernelPath.Append(systemInfo.kernel_name);
65 			get_ref_for_path(kernelPath.Path(), &info.ref);
66 			nameFromArgs = true;
67 		} else {
68 			status = BPrivate::get_app_ref(infoPack.team_info.team, &info.ref);
69 			nameFromArgs = true;
70 			tryTrackerIcon = (status == B_OK);
71 		}
72 	}
73 
74 	strncpy(infoPack.team_name, nameFromArgs ? infoPack.team_info.args : info.ref.name,
75 		B_PATH_NAME_LENGTH - 1);
76 
77 	if (icon) {
78 		infoPack.team_icon = new BBitmap(BRect(0, 0, 15, 15), B_RGBA32);
79 		if (!tryTrackerIcon
80 			|| BNodeInfo::GetTrackerIcon(&info.ref, infoPack.team_icon,
81 				B_MINI_ICON) != B_OK) {
82 			BMimeType genericAppType(B_APP_MIME_TYPE);
83 			status = genericAppType.GetIcon(infoPack.team_icon, B_MINI_ICON);
84 		}
85 	} else
86 		infoPack.team_icon = NULL;
87 
88 	return true;
89 }
90 
91 
92 bool
93 launch(const char* signature, const char* path)
94 {
95 	status_t status = be_roster->Launch(signature);
96 	if (status != B_OK && path) {
97 		entry_ref ref;
98 		if (get_ref_for_path(path, &ref) == B_OK)
99 			status = be_roster->Launch(&ref);
100 	}
101 	return status == B_OK;
102 }
103 
104 
105 void
106 mix_colors(rgb_color &target, rgb_color & first, rgb_color & second, float mix)
107 {
108 	target.red = (uint8)(second.red * mix + (1. - mix) * first.red);
109 	target.green = (uint8)(second.green * mix + (1. - mix) * first.green);
110 	target.blue = (uint8)(second.blue * mix + (1. - mix) * first.blue);
111 	target.alpha = (uint8)(second.alpha * mix + (1. - mix) * first.alpha);
112 }
113 
114 
115 void
116 find_self(entry_ref& ref)
117 {
118 	int32 cookie = 0;
119 	image_info info;
120 	while (get_next_image_info (0, &cookie, &info) == B_OK) {
121 		if (((addr_t)info.text <= (addr_t)move_to_deskbar
122 			&& (addr_t)info.text + (size_t)info.text_size > (addr_t)move_to_deskbar)
123 			|| ((addr_t)info.data <= (addr_t)move_to_deskbar
124 			&& (addr_t)info.data + (size_t)info.data_size > (addr_t)move_to_deskbar)) {
125 			if (get_ref_for_path (info.name, &ref) == B_OK)
126 				return;
127 		}
128 	}
129 
130 	// This works, but not always... :(
131 	app_info appInfo;
132 	be_roster->GetAppInfo(kSignature, &appInfo);
133 	ref = appInfo.ref;
134 }
135 
136 
137 void
138 move_to_deskbar(BDeskbar& deskbar)
139 {
140 	entry_ref ref;
141 	find_self(ref);
142 
143 	deskbar.AddItem(&ref);
144 }
145 
146 
147 void
148 make_window_visible(BWindow* window, bool mayResize)
149 {
150 	uint32 flags = window->Flags();
151 	BRect screen = BScreen(window).Frame();
152 	BRect frame = window->Frame();
153 	screen.InsetBy(4, 8);
154 	screen.OffsetBy(0, 8);
155 
156 	if (mayResize) {
157 		float width = frame.Width();
158 		float height = frame.Height();
159 		if (screen.Width() < width && !(flags & B_NOT_H_RESIZABLE))
160 			width = screen.Width();
161 		if (screen.Height() < height && !(flags & B_NOT_V_RESIZABLE))
162 			height = screen.Height();
163 		if (width != frame.Width() || height != frame.Height()) {
164 			window->ResizeTo(width, height);
165 			frame.right = frame.left + width;
166 			frame.bottom = frame.top + height;
167 		}
168 	}
169 	if (frame.right > screen.right)
170 		window->MoveBy(screen.right-frame.right, 0);
171 	if (frame.bottom > screen.bottom)
172 		window->MoveBy(0, screen.bottom-frame.bottom);
173 	if (frame.left < screen.left)
174 		window->MoveTo(screen.left, frame.top);
175 	if (frame.top < screen.top)
176 		window->MoveBy(0, screen.top-frame.top);
177 }
178 
179