BPicture: Fix archive constructor.
[haiku.git] / src / kits / app / AppMisc.cpp
blob1a607f785f67e5a8639c20a77cbb53ceb79a8f91
1 /*
2 * Copyright 2001-2015, Haiku, Inc.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Axel Dörfler, axeld@pinc-software.de
7 * Ingo Weinhold, bonefish@@users.sf.net
8 */
11 #include <AppMisc.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/utsname.h>
16 #include <unistd.h>
18 #include <Entry.h>
19 #include <image.h>
20 #include <Messenger.h>
21 #include <OS.h>
23 #include <ServerLink.h>
24 #include <ServerProtocol.h>
27 namespace BPrivate {
30 static team_id sCurrentTeam = -1;
33 /*! \brief Returns the path to an application's executable.
34 \param team The application's team ID.
35 \param buffer A pointer to a pre-allocated character array of at least
36 size B_PATH_NAME_LENGTH to be filled in by this function.
37 \return
38 - \c B_OK: Everything went fine.
39 - \c B_BAD_VALUE: \c NULL \a buffer.
40 - another error code
42 status_t
43 get_app_path(team_id team, char *buffer)
45 // The only way to get the path to the application's executable seems to
46 // be to get an image_info of its image, which also contains a path.
47 // Several images may belong to the team (libraries, add-ons), but only
48 // the one in question should be typed B_APP_IMAGE.
49 if (!buffer)
50 return B_BAD_VALUE;
52 image_info info;
53 int32 cookie = 0;
55 while (get_next_image_info(team, &cookie, &info) == B_OK) {
56 if (info.type == B_APP_IMAGE) {
57 strlcpy(buffer, info.name, B_PATH_NAME_LENGTH - 1);
58 return B_OK;
62 return B_ENTRY_NOT_FOUND;
66 /*! \brief Returns the path to the application's executable.
67 \param buffer A pointer to a pre-allocated character array of at least
68 size B_PATH_NAME_LENGTH to be filled in by this function.
69 \return
70 - \c B_OK: Everything went fine.
71 - \c B_BAD_VALUE: \c NULL \a buffer.
72 - another error code
74 status_t
75 get_app_path(char *buffer)
77 return get_app_path(B_CURRENT_TEAM, buffer);
81 /*! \brief Returns an entry_ref referring to an application's executable.
82 \param team The application's team ID.
83 \param ref A pointer to a pre-allocated entry_ref to be initialized
84 to an entry_ref referring to the application's executable.
85 \param traverse If \c true, the function traverses symbolic links.
86 \return
87 - \c B_OK: Everything went fine.
88 - \c B_BAD_VALUE: \c NULL \a ref.
89 - another error code
91 status_t
92 get_app_ref(team_id team, entry_ref *ref, bool traverse)
94 status_t error = (ref ? B_OK : B_BAD_VALUE);
95 char appFilePath[B_PATH_NAME_LENGTH];
97 if (error == B_OK)
98 error = get_app_path(team, appFilePath);
100 if (error == B_OK) {
101 BEntry entry(appFilePath, traverse);
102 error = entry.GetRef(ref);
105 return error;
109 /*! \brief Returns an entry_ref referring to the application's executable.
110 \param ref A pointer to a pre-allocated entry_ref to be initialized
111 to an entry_ref referring to the application's executable.
112 \param traverse If \c true, the function traverses symbolic links.
113 \return
114 - \c B_OK: Everything went fine.
115 - \c B_BAD_VALUE: \c NULL \a ref.
116 - another error code
118 status_t
119 get_app_ref(entry_ref *ref, bool traverse)
121 return get_app_ref(B_CURRENT_TEAM, ref, traverse);
125 /*! \brief Returns the ID of the current team.
126 \return The ID of the current team.
128 team_id
129 current_team()
131 if (sCurrentTeam < 0) {
132 thread_info info;
133 if (get_thread_info(find_thread(NULL), &info) == B_OK)
134 sCurrentTeam = info.team;
136 return sCurrentTeam;
140 void
141 init_team_after_fork()
143 sCurrentTeam = -1;
147 /*! Returns the ID of the supplied team's main thread.
148 \param team The team.
149 \return
150 - The thread ID of the supplied team's main thread
151 - \c B_BAD_TEAM_ID: The supplied team ID does not identify a running team.
152 - another error code
154 thread_id
155 main_thread_for(team_id team)
157 // Under Haiku the team ID is equal to it's main thread ID. We just get
158 // a team info to verify the existence of the team.
159 team_info info;
160 status_t error = get_team_info(team, &info);
161 return error == B_OK ? team : error;
165 /*! \brief Returns whether the application identified by the supplied
166 \c team_id is currently showing a modal window.
167 \param team the ID of the application in question.
168 \return \c true, if the application is showing a modal window, \c false
169 otherwise.
171 bool
172 is_app_showing_modal_window(team_id team)
174 // TODO: Implement!
175 return true;
179 /*! Creates a connection with the desktop.
181 status_t
182 create_desktop_connection(ServerLink* link, const char* name, int32 capacity)
184 // Create the port so that the app_server knows where to send messages
185 port_id clientPort = create_port(capacity, name);
186 if (clientPort < 0)
187 return clientPort;
189 link->SetReceiverPort(clientPort);
191 BMessage request(AS_GET_DESKTOP);
192 request.AddInt32("user", getuid());
193 request.AddInt32("version", AS_PROTOCOL_VERSION);
194 request.AddString("target", getenv("TARGET_SCREEN"));
196 BMessenger server("application/x-vnd.Haiku-app_server");
197 BMessage reply;
198 status_t status = server.SendMessage(&request, &reply);
199 if (status != B_OK)
200 return status;
202 port_id desktopPort = reply.GetInt32("port", B_ERROR);
203 if (desktopPort < 0)
204 return desktopPort;
206 link->SetSenderPort(desktopPort);
207 return B_OK;
211 } // namespace BPrivate