From 4d7538b1800b88f1d56e6c80d94b46ab2013f3ed Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Sun, 28 Jul 2002 19:26:21 +0000 Subject: [PATCH] Moved some reusable code from BApplication to this shiny new place. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@510 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/app/AppMisc.h | 40 +++++++++++++ src/kits/app/AppMisc.cpp | 110 ++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 headers/private/app/AppMisc.h create mode 100644 src/kits/app/AppMisc.cpp diff --git a/headers/private/app/AppMisc.h b/headers/private/app/AppMisc.h new file mode 100644 index 0000000000..250c9e31b3 --- /dev/null +++ b/headers/private/app/AppMisc.h @@ -0,0 +1,40 @@ +//------------------------------------------------------------------------------ +// Copyright (c) 2001-2002, OpenBeOS +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// +// File Name: AppMisc.h +// Author: Ingo Weinhold (bonefish@users.sf.net) +// Description: Miscellaneous private functionality. +//------------------------------------------------------------------------------ + +#ifndef _APP_MISC_H +#define _APP_MISC_H + +#include +#include + +struct entry_ref; + +status_t get_app_path(char *buffer); +status_t get_app_ref(entry_ref *ref, bool traverse = true); + +thread_id main_thread_for(team_id team); + +#endif // _APP_MISC_H diff --git a/src/kits/app/AppMisc.cpp b/src/kits/app/AppMisc.cpp new file mode 100644 index 0000000000..17daadcd37 --- /dev/null +++ b/src/kits/app/AppMisc.cpp @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +// Copyright (c) 2001-2002, OpenBeOS +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// +// File Name: AppMisc.cpp +// Author: Ingo Weinhold (bonefish@users.sf.net) +// Description: Miscellaneous private functionality. +//------------------------------------------------------------------------------ + +#include + +#include +#include +#include +#include + +// get_app_path +/*! \brief Returns the path to the application's executable. + \param buffer A pointer to a pre-allocated character array of at least + size B_PATH_NAME_LENGTH + 1 to be filled in by this function. + \return + - \c B_OK: Everything went fine. + - \c B_BAD_VALUE: \c NULL \a buffer. + - another error code +*/ +status_t +get_app_path(char *buffer) +{ + status_t error = (buffer ? B_OK : B_BAD_VALUE); + image_info info; + int32 cookie = 0; + bool found = false; + if (error == B_OK) { + while (!found && get_next_image_info(0, &cookie, &info) == B_OK) { + if (info.type == B_APP_IMAGE) { + strncpy(buffer, info.name, B_PATH_NAME_LENGTH); + buffer[B_PATH_NAME_LENGTH] = 0; + found = true; + } + } + } + if (error == B_OK && !found) + error = B_ENTRY_NOT_FOUND; + return error; +} + +// get_app_ref +/*! \brief Returns an entry_ref referring to the application's exectable. + \param ref A pointer to a pre-allocated entry_ref to be initialized + to an entry_ref referring to the application's executable. + \param traverse If \c true, the function traverses symbolic links. + \return + - \c B_OK: Everything went fine. + - \c B_BAD_VALUE: \c NULL \a ref. + - another error code +*/ +status_t +get_app_ref(entry_ref *ref, bool traverse) +{ + status_t error = (ref ? B_OK : B_BAD_VALUE); + char appFilePath[B_PATH_NAME_LENGTH + 1]; + if (error == B_OK) + error = get_app_path(appFilePath); + if (error == B_OK) { + BEntry entry(appFilePath, traverse); + error = entry.GetRef(ref); + } + return error; +} + +// main_thread_for +/*! Returns the ID of the supplied team's main thread. + \param team The team. + \return + - The thread ID of the supplied team's main thread + - \c B_BAD_TEAM_ID: The supplied team ID does not identify a running team. + - another error code +*/ +thread_id +main_thread_for(team_id team) +{ + // For I can't find any trace of how to explicitly get the main thread, + // I assume the main thread is the one with the least thread ID. + thread_id thread = -1; + int32 cookie = 0; + thread_info info; + while (get_next_thread_info(team, &cookie, &info) == B_OK) { + if (thread < 0 || info.thread < thread) + thread = info.thread; + } + return thread; +} +