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