xref: /haiku/src/kits/app/AppMisc.cpp (revision 4b9cf09f14ca4d93f9063d4b824758ce1ea599c0)
1 /*
2  * Copyright 2001-2009, 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 <AppMisc.h>
11 #include <Entry.h>
12 #include <image.h>
13 #include <OS.h>
14 
15 #include <string.h>
16 #include <sys/utsname.h>
17 
18 
19 namespace BPrivate {
20 
21 BLocker gInitializationLock("global init lock");
22 
23 
24 /*!	\brief Returns the path to an application's executable.
25 	\param team The application's team ID.
26 	\param buffer A pointer to a pre-allocated character array of at least
27 		   size B_PATH_NAME_LENGTH to be filled in by this function.
28 	\return
29 	- \c B_OK: Everything went fine.
30 	- \c B_BAD_VALUE: \c NULL \a buffer.
31 	- another error code
32 */
33 status_t
34 get_app_path(team_id team, char *buffer)
35 {
36 	// The only way to get the path to the application's executable seems to
37 	// be to get an image_info of its image, which also contains a path.
38 	// Several images may belong to the team (libraries, add-ons), but only
39 	// the one in question should be typed B_APP_IMAGE.
40 	if (!buffer)
41 		return B_BAD_VALUE;
42 
43 	image_info info;
44 	int32 cookie = 0;
45 
46 	while (get_next_image_info(team, &cookie, &info) == B_OK) {
47 		if (info.type == B_APP_IMAGE) {
48 			strlcpy(buffer, info.name, B_PATH_NAME_LENGTH - 1);
49 			return B_OK;
50 		}
51 	}
52 
53 	return B_ENTRY_NOT_FOUND;
54 }
55 
56 
57 /*!	\brief Returns the path to the application's executable.
58 	\param buffer A pointer to a pre-allocated character array of at least
59 		   size B_PATH_NAME_LENGTH to be filled in by this function.
60 	\return
61 	- \c B_OK: Everything went fine.
62 	- \c B_BAD_VALUE: \c NULL \a buffer.
63 	- another error code
64 */
65 status_t
66 get_app_path(char *buffer)
67 {
68 	return get_app_path(B_CURRENT_TEAM, buffer);
69 }
70 
71 
72 /*!	\brief Returns an entry_ref referring to an application's executable.
73 	\param team The application's team ID.
74 	\param ref A pointer to a pre-allocated entry_ref to be initialized
75 		   to an entry_ref referring to the application's executable.
76 	\param traverse If \c true, the function traverses symbolic links.
77 	\return
78 	- \c B_OK: Everything went fine.
79 	- \c B_BAD_VALUE: \c NULL \a ref.
80 	- another error code
81 */
82 status_t
83 get_app_ref(team_id team, entry_ref *ref, bool traverse)
84 {
85 	status_t error = (ref ? B_OK : B_BAD_VALUE);
86 	char appFilePath[B_PATH_NAME_LENGTH];
87 
88 	if (error == B_OK)
89 		error = get_app_path(team, appFilePath);
90 
91 	if (error == B_OK) {
92 		BEntry entry(appFilePath, traverse);
93 		error = entry.GetRef(ref);
94 	}
95 
96 	return error;
97 }
98 
99 
100 /*!	\brief Returns an entry_ref referring to the application's executable.
101 	\param ref A pointer to a pre-allocated entry_ref to be initialized
102 		   to an entry_ref referring to the application's executable.
103 	\param traverse If \c true, the function traverses symbolic links.
104 	\return
105 	- \c B_OK: Everything went fine.
106 	- \c B_BAD_VALUE: \c NULL \a ref.
107 	- another error code
108 */
109 status_t
110 get_app_ref(entry_ref *ref, bool traverse)
111 {
112 	return get_app_ref(B_CURRENT_TEAM, ref, traverse);
113 }
114 
115 
116 /*!	\brief Returns the ID of the current team.
117 	\return The ID of the current team.
118 */
119 team_id
120 current_team()
121 {
122 	static team_id team = -1;
123 	if (team < 0) {
124 		thread_info info;
125 		if (get_thread_info(find_thread(NULL), &info) == B_OK)
126 			team = info.team;
127 	}
128 	return team;
129 }
130 
131 
132 /*!	Returns the ID of the supplied team's main thread.
133 	\param team The team.
134 	\return
135 	- The thread ID of the supplied team's main thread
136 	- \c B_BAD_TEAM_ID: The supplied team ID does not identify a running team.
137 	- another error code
138 */
139 thread_id
140 main_thread_for(team_id team)
141 {
142 	// Under Haiku the team ID is equal to it's main thread ID. We just get
143 	// a team info to verify the existence of the team.
144 	team_info info;
145 	status_t error = get_team_info(team, &info);
146 	return (error == B_OK ? team : error);
147 }
148 
149 
150 /*!	\brief Returns whether the application identified by the supplied
151 		   \c team_id is currently showing a modal window.
152 	\param team the ID of the application in question.
153 	\return \c true, if the application is showing a modal window, \c false
154 			otherwise.
155 */
156 bool
157 is_app_showing_modal_window(team_id team)
158 {
159 	// TODO: Implement!
160 	return true;
161 }
162 
163 } // namespace BPrivate
164