xref: /haiku/src/kits/app/AppMisc.cpp (revision d3d8b26997fac34a84981e6d2b649521de2cc45a)
1 /*
2  * Copyright 2001-2005, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Ingo Weinhold, bonefish@@users.sf.net
7  */
8 
9 
10 #include <string.h>
11 #include <sys/utsname.h>
12 
13 #include <AppMisc.h>
14 #include <Entry.h>
15 #include <image.h>
16 #include <OS.h>
17 
18 
19 namespace BPrivate {
20 
21 // get_app_path
22 /*!	\brief Returns the path to an application's executable.
23 	\param team The application's team ID.
24 	\param buffer A pointer to a pre-allocated character array of at least
25 		   size B_PATH_NAME_LENGTH to be filled in by this function.
26 	\return
27 	- \c B_OK: Everything went fine.
28 	- \c B_BAD_VALUE: \c NULL \a buffer.
29 	- another error code
30 */
31 status_t
32 get_app_path(team_id team, char *buffer)
33 {
34 	// The only way to get the path to the application's executable seems to
35 	// be to get an image_info of its image, which also contains a path.
36 	// Several images may belong to the team (libraries, add-ons), but only
37 	// the one in question should be typed B_APP_IMAGE.
38 	if (!buffer)
39 		return B_BAD_VALUE;
40 
41 	image_info info;
42 	int32 cookie = 0;
43 
44 	while (get_next_image_info(team, &cookie, &info) == B_OK) {
45 		if (info.type == B_APP_IMAGE) {
46 			strlcpy(buffer, info.name, B_PATH_NAME_LENGTH - 1);
47 			return B_OK;
48 		}
49 	}
50 
51 	return B_ENTRY_NOT_FOUND;
52 }
53 
54 // get_app_path
55 /*!	\brief Returns the path to the application's executable.
56 	\param buffer A pointer to a pre-allocated character array of at least
57 		   size B_PATH_NAME_LENGTH to be filled in by this function.
58 	\return
59 	- \c B_OK: Everything went fine.
60 	- \c B_BAD_VALUE: \c NULL \a buffer.
61 	- another error code
62 */
63 status_t
64 get_app_path(char *buffer)
65 {
66 	return get_app_path(B_CURRENT_TEAM, buffer);
67 }
68 
69 // get_app_ref
70 /*!	\brief Returns an entry_ref referring to an application's executable.
71 	\param team The application's team ID.
72 	\param ref A pointer to a pre-allocated entry_ref to be initialized
73 		   to an entry_ref referring to the application's executable.
74 	\param traverse If \c true, the function traverses symbolic links.
75 	\return
76 	- \c B_OK: Everything went fine.
77 	- \c B_BAD_VALUE: \c NULL \a ref.
78 	- another error code
79 */
80 status_t
81 get_app_ref(team_id team, entry_ref *ref, bool traverse)
82 {
83 	status_t error = (ref ? B_OK : B_BAD_VALUE);
84 	char appFilePath[B_PATH_NAME_LENGTH];
85 
86 	if (error == B_OK)
87 		error = get_app_path(team, appFilePath);
88 
89 	if (error == B_OK) {
90 		BEntry entry(appFilePath, traverse);
91 		error = entry.GetRef(ref);
92 	}
93 
94 	return error;
95 }
96 
97 // get_app_ref
98 /*!	\brief Returns an entry_ref referring to the application's executable.
99 	\param ref A pointer to a pre-allocated entry_ref to be initialized
100 		   to an entry_ref referring to the application's executable.
101 	\param traverse If \c true, the function traverses symbolic links.
102 	\return
103 	- \c B_OK: Everything went fine.
104 	- \c B_BAD_VALUE: \c NULL \a ref.
105 	- another error code
106 */
107 status_t
108 get_app_ref(entry_ref *ref, bool traverse)
109 {
110 	return get_app_ref(B_CURRENT_TEAM, ref, traverse);
111 }
112 
113 // current_team
114 /*!	\brief Returns the ID of the current team.
115 	\return The ID of the current team.
116 */
117 team_id
118 current_team()
119 {
120 	team_id team = -1;
121 	thread_info info;
122 	if (get_thread_info(find_thread(NULL), &info) == B_OK)
123 		team = info.team;
124 	return team;
125 }
126 
127 // main_thread_for
128 /*!	Returns the ID of the supplied team's main thread.
129 	\param team The team.
130 	\return
131 	- The thread ID of the supplied team's main thread
132 	- \c B_BAD_TEAM_ID: The supplied team ID does not identify a running team.
133 	- another error code
134 */
135 thread_id
136 main_thread_for(team_id team)
137 {
138 #ifdef __HAIKU__
139 	// Under Haiku the team ID is equal to it's main thread ID. We just get
140 	// a team info to verify the existence of the team.
141 	team_info info;
142 	status_t error = get_team_info(team, &info);
143 	return (error == B_OK ? team : error);
144 #else
145 	// For I can't find any trace of how to explicitly get the main thread,
146 	// I assume the main thread is the one with the least thread ID.
147 	thread_id thread = B_BAD_TEAM_ID;
148 	int32 cookie = 0;
149 	thread_info info;
150 	while (get_next_thread_info(team, &cookie, &info) == B_OK) {
151 		if (thread < 0 || info.thread < thread)
152 			thread = info.thread;
153 	}
154 	return thread;
155 #endif
156 }
157 
158 // is_running_on_haiku
159 /*!	Returns whether we're running under Haiku natively.
160 
161 	This is a runtime check for components compiled only once for both
162 	BeOS and Haiku and nevertheless need to behave differently on the two
163 	systems, like the registrar, which uses another MIME database directory
164 	under BeOS.
165 
166 	\return \c true, if we're running under Haiku, \c false otherwise.
167 */
168 bool
169 is_running_on_haiku()
170 {
171 	struct utsname info;
172 	return (uname(&info) == 0 && strcmp(info.sysname, "Haiku") == 0);
173 }
174 
175 // is_app_showing_modal_window
176 /*!	\brief Returns whether the application identified by the supplied
177 		   \c team_id is currently showing a modal window.
178 	\param team the ID of the application in question.
179 	\return \c true, if the application is showing a modal window, \c false
180 			otherwise.
181 */
182 bool
183 is_app_showing_modal_window(team_id team)
184 {
185 	// TODO: Implement!
186 	return true;
187 }
188 
189 } // namespace BPrivate
190 
191