xref: /haiku/src/kits/app/AppMisc.cpp (revision 2ae568931fcac7deb9f1e6ff4e47213fbfe4029b)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2002, OpenBeOS
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		AppMisc.cpp
23 //	Author:			Ingo Weinhold (bonefish@users.sf.net)
24 //	Description:	Miscellaneous private functionality.
25 //------------------------------------------------------------------------------
26 
27 #include <string.h>
28 #include <sys/utsname.h>
29 
30 #include <AppMisc.h>
31 #include <Entry.h>
32 #include <image.h>
33 #include <OS.h>
34 
35 namespace BPrivate {
36 
37 // get_app_path
38 /*!	\brief Returns the path to an application's executable.
39 	\param team The application's team ID.
40 	\param buffer A pointer to a pre-allocated character array of at least
41 		   size B_PATH_NAME_LENGTH to be filled in by this function.
42 	\return
43 	- \c B_OK: Everything went fine.
44 	- \c B_BAD_VALUE: \c NULL \a buffer.
45 	- another error code
46 */
47 status_t
48 get_app_path(team_id team, char *buffer)
49 {
50 	// The only way to get the path to the application's executable seems to
51 	// be to get an image_info of its image, which also contains a path.
52 	// Several images may belong to the team (libraries, add-ons), but only
53 	// the one in question should be typed B_APP_IMAGE.
54 	if (!buffer)
55 		return B_BAD_VALUE;
56 
57 	image_info info;
58 	int32 cookie = 0;
59 
60 	while (get_next_image_info(team, &cookie, &info) == B_OK) {
61 		if (info.type == B_APP_IMAGE) {
62 			strlcpy(buffer, info.name, B_PATH_NAME_LENGTH - 1);
63 			return B_OK;
64 		}
65 	}
66 
67 	return B_ENTRY_NOT_FOUND;
68 }
69 
70 // get_app_path
71 /*!	\brief Returns the path to the application's executable.
72 	\param buffer A pointer to a pre-allocated character array of at least
73 		   size B_PATH_NAME_LENGTH to be filled in by this function.
74 	\return
75 	- \c B_OK: Everything went fine.
76 	- \c B_BAD_VALUE: \c NULL \a buffer.
77 	- another error code
78 */
79 status_t
80 get_app_path(char *buffer)
81 {
82 	return get_app_path(B_CURRENT_TEAM, buffer);
83 }
84 
85 // get_app_ref
86 /*!	\brief Returns an entry_ref referring to an application's executable.
87 	\param team The application's team ID.
88 	\param ref A pointer to a pre-allocated entry_ref to be initialized
89 		   to an entry_ref referring to the application's executable.
90 	\param traverse If \c true, the function traverses symbolic links.
91 	\return
92 	- \c B_OK: Everything went fine.
93 	- \c B_BAD_VALUE: \c NULL \a ref.
94 	- another error code
95 */
96 status_t
97 get_app_ref(team_id team, entry_ref *ref, bool traverse)
98 {
99 	status_t error = (ref ? B_OK : B_BAD_VALUE);
100 	char appFilePath[B_PATH_NAME_LENGTH];
101 
102 	if (error == B_OK)
103 		error = get_app_path(team, appFilePath);
104 
105 	if (error == B_OK) {
106 		BEntry entry(appFilePath, traverse);
107 		error = entry.GetRef(ref);
108 	}
109 
110 	return error;
111 }
112 
113 // get_app_ref
114 /*!	\brief Returns an entry_ref referring to the application's executable.
115 	\param ref A pointer to a pre-allocated entry_ref to be initialized
116 		   to an entry_ref referring to the application's executable.
117 	\param traverse If \c true, the function traverses symbolic links.
118 	\return
119 	- \c B_OK: Everything went fine.
120 	- \c B_BAD_VALUE: \c NULL \a ref.
121 	- another error code
122 */
123 status_t
124 get_app_ref(entry_ref *ref, bool traverse)
125 {
126 	return get_app_ref(B_CURRENT_TEAM, ref, traverse);
127 }
128 
129 // current_team
130 /*!	\brief Returns the ID of the current team.
131 	\return The ID of the current team.
132 */
133 team_id
134 current_team()
135 {
136 	team_id team = -1;
137 	thread_info info;
138 	if (get_thread_info(find_thread(NULL), &info) == B_OK)
139 		team = info.team;
140 	return team;
141 }
142 
143 // main_thread_for
144 /*!	Returns the ID of the supplied team's main thread.
145 	\param team The team.
146 	\return
147 	- The thread ID of the supplied team's main thread
148 	- \c B_BAD_TEAM_ID: The supplied team ID does not identify a running team.
149 	- another error code
150 */
151 thread_id
152 main_thread_for(team_id team)
153 {
154 #ifdef __HAIKU__
155 	// Under Haiku the team ID is equal to it's main thread ID. We just get
156 	// a team info to verify the existence of the team.
157 	team_info info;
158 	status_t error = get_team_info(team, &info);
159 	return (error == B_OK ? team : error);
160 #else
161 	// For I can't find any trace of how to explicitly get the main thread,
162 	// I assume the main thread is the one with the least thread ID.
163 	thread_id thread = B_BAD_TEAM_ID;
164 	int32 cookie = 0;
165 	thread_info info;
166 	while (get_next_thread_info(team, &cookie, &info) == B_OK) {
167 		if (thread < 0 || info.thread < thread)
168 			thread = info.thread;
169 	}
170 	return thread;
171 #endif
172 }
173 
174 // is_running_on_haiku
175 /*!	Returns whether we're running under Haiku natively.
176 
177 	This is a runtime check for components compiled only once for both
178 	BeOS and Haiku and nevertheless need to behave differently on the two
179 	systems, like the registrar, which uses another MIME database directory
180 	under BeOS.
181 
182 	\return \c true, if we're running under Haiku, \c false otherwise.
183 */
184 bool
185 is_running_on_haiku()
186 {
187 	struct utsname info;
188 	return (uname(&info) == 0 && strcmp(info.sysname, "Haiku") == 0);
189 }
190 
191 // is_app_showing_modal_window
192 /*!	\brief Returns whether the application identified by the supplied
193 		   \c team_id is currently showing a modal window.
194 	\param team the ID of the application in question.
195 	\return \c true, if the application is showing a modal window, \c false
196 			otherwise.
197 */
198 bool
199 is_app_showing_modal_window(team_id team)
200 {
201 	// TODO: Implement!
202 	return true;
203 }
204 
205 } // namespace BPrivate
206 
207