KDECore
kkernel_mac.cpp
Go to the documentation of this file.
00001 /* 00002 This file is part of the KDE libraries 00003 Copyright (C) 2008 Benjamin Reed <rangerrick@befunk.com> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License version 2 as published by the Free Software Foundation. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "kkernel_mac.h" 00021 00022 #include <config.h> 00023 00024 #ifdef Q_OS_MACX 00025 00026 #include <unistd.h> 00027 #include <string.h> 00028 #include <stdio.h> 00029 #include <stdlib.h> 00030 #include <sys/param.h> 00031 #include <crt_externs.h> 00032 #include <mach-o/dyld.h> 00033 00034 #include <CoreFoundation/CFBundle.h> 00035 #include <CoreFoundation/CFString.h> 00036 #include <CoreFoundation/CFURL.h> 00037 #include <QtCore/QFile> 00038 #include <QtCore/QProcess> 00039 #include <QtCore/QStringList> 00040 #include <QtCore/qvarlengtharray.h> 00041 #include <kstandarddirs.h> 00042 #include <ksharedconfig.h> 00043 #include <kconfig.h> 00044 #include <kdebug.h> 00045 00046 int timeout = 3000; // msec 00047 00048 bool dbus_initialized = false; 00049 00054 QString convert_CFString_to_QString(CFStringRef str) { 00055 CFIndex length = CFStringGetLength(str); 00056 const UniChar *chars = CFStringGetCharactersPtr(str); 00057 if (chars) 00058 return QString(reinterpret_cast<const QChar *>(chars), length); 00059 00060 QVarLengthArray<UniChar> buffer(length); 00061 CFStringGetCharacters(str, CFRangeMake(0, length), buffer.data()); 00062 return QString(reinterpret_cast<const QChar *>(buffer.constData()), length); 00063 } 00064 00074 void 00075 mac_fork_and_reexec_self() 00076 { 00077 int argc = *_NSGetArgc(); 00078 char ** argv = *_NSGetArgv(); 00079 char * newargv[argc+2]; 00080 char progname[PATH_MAX]; 00081 uint32_t buflen = PATH_MAX; 00082 _NSGetExecutablePath(progname, &buflen); 00083 bool found_psn = false; 00084 00085 for (int i = 0; i < argc; i++) { 00086 newargv[i] = argv[i]; 00087 } 00088 00089 newargv[argc] = "--nofork"; 00090 newargv[argc+1] = NULL; 00091 00092 int x_fork_result = fork(); 00093 switch(x_fork_result) { 00094 00095 case -1: 00096 #ifndef NDEBUG 00097 fprintf(stderr, "Mac OS X workaround fork() failed!\n"); 00098 #endif 00099 ::_exit(255); 00100 break; 00101 00102 case 0: 00103 // Child 00104 execvp(progname, newargv); 00105 break; 00106 00107 default: 00108 // Parent 00109 _exit(0); 00110 break; 00111 00112 } 00113 } 00114 00119 bool mac_set_dbus_address(QString value) 00120 { 00121 if (!value.isEmpty() && QFile::exists(value) && (QFile::permissions(value) & QFile::WriteUser)) { 00122 value = QLatin1String("unix:path=") + value; 00123 ::setenv("DBUS_SESSION_BUS_ADDRESS", value.toLocal8Bit(), 1); 00124 kDebug() << "set session bus address to" << value; 00125 return true; 00126 } 00127 return false; 00128 } 00129 00134 void mac_initialize_dbus() 00135 { 00136 if (dbus_initialized) 00137 return; 00138 00139 QString dbusVar = QString::fromLocal8Bit(qgetenv("DBUS_SESSION_BUS_ADDRESS")); 00140 if (!dbusVar.isEmpty()) { 00141 dbus_initialized = true; 00142 return; 00143 } 00144 00145 dbusVar = QFile::decodeName(qgetenv("DBUS_LAUNCHD_SESSION_BUS_SOCKET")); 00146 if (mac_set_dbus_address(dbusVar)) { 00147 dbus_initialized = true; 00148 return; 00149 } 00150 00151 QString externalProc; 00152 QStringList path = QFile::decodeName(qgetenv("KDEDIRS")).split(QLatin1Char(':')).replaceInStrings(QRegExp(QLatin1String("$")), QLatin1String("/bin")); 00153 path << QFile::decodeName(qgetenv("PATH")).split(QLatin1Char(':')) << QLatin1String("/usr/local/bin"); 00154 00155 for (int i = 0; i < path.size(); ++i) { 00156 QString testLaunchctl = QString(path.at(i)).append(QLatin1String("/launchctl")); 00157 if (QFile(testLaunchctl).exists()) { 00158 externalProc = testLaunchctl; 00159 break; 00160 } 00161 } 00162 00163 if (!externalProc.isEmpty()) { 00164 QProcess qp; 00165 qp.setTextModeEnabled(true); 00166 00167 qp.start(externalProc, QStringList() << QLatin1String("getenv") << QLatin1String("DBUS_LAUNCHD_SESSION_BUS_SOCKET")); 00168 if (!qp.waitForFinished(timeout)) { 00169 kDebug() << "error running" << externalProc << qp.errorString(); 00170 return; 00171 } 00172 if (qp.exitCode() != 0) { 00173 kDebug() << externalProc << "unsuccessful:" << qp.readAllStandardError(); 00174 return; 00175 } 00176 00177 QString line = QString::fromAscii(qp.readLine()).trimmed(); // read the first line 00178 if (mac_set_dbus_address(line)) 00179 dbus_initialized = true; // hooray 00180 } 00181 00182 if (dbus_initialized == false) { 00183 kDebug() << "warning: unable to initialize D-Bus environment!"; 00184 } 00185 00186 } 00187 00188 QString mac_app_filename() { 00189 static QString appFileName; 00190 if (appFileName.isEmpty()) { 00191 CFURLRef bundleURL = NULL; 00192 CFBundleRef bundle = NULL; 00193 CFStringRef bundlePath = NULL; 00194 00195 bundle = CFBundleGetMainBundle(); 00196 if (bundle) { 00197 bundleURL = CFBundleCopyBundleURL(bundle); 00198 bundlePath = CFURLCopyFileSystemPath(bundleURL, kCFURLPOSIXPathStyle); 00199 00200 if (bundleURL) { 00201 CFRelease(bundleURL); 00202 } 00203 00204 if (bundlePath) { 00205 appFileName = convert_CFString_to_QString(bundlePath); 00206 CFRelease(bundlePath); 00207 } 00208 } 00209 } 00210 return appFileName; 00211 } 00212 00213 #endif
KDE 4.6 API Reference