KDECore
kkernel_mac.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021 #include <QtCore/qglobal.h>
00022
00023 #include "kkernel_mac.h"
00024
00025 #ifdef Q_OS_MACX
00026
00027 #include <unistd.h>
00028 #include <string.h>
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <sys/param.h>
00032 #include <crt_externs.h>
00033 #include <mach-o/dyld.h>
00034
00035 #include <CoreFoundation/CFBundle.h>
00036 #include <CoreFoundation/CFString.h>
00037 #include <CoreFoundation/CFURL.h>
00038 #include <QtCore/QFile>
00039 #include <QtCore/QProcess>
00040 #include <QtCore/QStringList>
00041 #include <QtCore/qvarlengtharray.h>
00042 #include <kstandarddirs.h>
00043 #include <ksharedconfig.h>
00044 #include <kconfig.h>
00045 #include <kdebug.h>
00046
00047 int timeout = 3000;
00048
00049 bool dbus_initialized = false;
00050
00055 static QString convert_CFString_to_QString(CFStringRef str) {
00056 CFIndex length = CFStringGetLength(str);
00057 const UniChar *chars = CFStringGetCharactersPtr(str);
00058 if (chars)
00059 return QString(reinterpret_cast<const QChar *>(chars), length);
00060
00061 QVarLengthArray<UniChar> buffer(length);
00062 CFStringGetCharacters(str, CFRangeMake(0, length), buffer.data());
00063 return QString(reinterpret_cast<const QChar *>(buffer.constData()), length);
00064 }
00065
00075 void
00076 mac_fork_and_reexec_self()
00077 {
00078 int argc = *_NSGetArgc();
00079 char ** argv = *_NSGetArgv();
00080 char * newargv[argc+2];
00081 char progname[PATH_MAX];
00082 uint32_t buflen = PATH_MAX;
00083 _NSGetExecutablePath(progname, &buflen);
00084 bool found_psn = false;
00085
00086 for (int i = 0; i < argc; i++) {
00087 newargv[i] = argv[i];
00088 }
00089
00090 newargv[argc] = "--nofork";
00091 newargv[argc+1] = NULL;
00092
00093 int x_fork_result = fork();
00094 switch(x_fork_result) {
00095
00096 case -1:
00097 #ifndef NDEBUG
00098 fprintf(stderr, "Mac OS X workaround fork() failed!\n");
00099 #endif
00100 ::_exit(255);
00101 break;
00102
00103 case 0:
00104
00105 execvp(progname, newargv);
00106 break;
00107
00108 default:
00109
00110 _exit(0);
00111 break;
00112
00113 }
00114 }
00115
00120 bool mac_set_dbus_address(QString value)
00121 {
00122 if (!value.isEmpty() && QFile::exists(value) && (QFile::permissions(value) & QFile::WriteUser)) {
00123 value = "unix:path=" + value;
00124 ::setenv("DBUS_SESSION_BUS_ADDRESS", value.toLocal8Bit(), 1);
00125 kDebug() << "set session bus address to" << value;
00126 return true;
00127 }
00128 return false;
00129 }
00130
00135 void mac_initialize_dbus()
00136 {
00137 if (dbus_initialized)
00138 return;
00139
00140 QString dbusVar = qgetenv("DBUS_SESSION_BUS_ADDRESS");
00141 if (!dbusVar.isEmpty()) {
00142 dbus_initialized = true;
00143 return;
00144 }
00145
00146 dbusVar = QFile::decodeName(qgetenv("DBUS_LAUNCHD_SESSION_BUS_SOCKET"));
00147 if (mac_set_dbus_address(dbusVar)) {
00148 dbus_initialized = true;
00149 return;
00150 }
00151
00152 QString externalProc;
00153 QStringList path = QFile::decodeName(qgetenv("KDEDIRS")).split(':').replaceInStrings(QRegExp("$"), "/bin");
00154 path << QFile::decodeName(qgetenv("PATH")).split(':') << "/usr/local/bin";
00155
00156 for (int i = 0; i < path.size(); ++i) {
00157 QString testLaunchctl = QString(path.at(i)).append("/launchctl");
00158 if (QFile(testLaunchctl).exists()) {
00159 externalProc = testLaunchctl;
00160 break;
00161 }
00162 }
00163
00164 QString line;
00165 QProcess qp;
00166 qp.setTextModeEnabled(true);
00167
00168 if (!externalProc.isEmpty()) {
00169 qp.start(externalProc, QStringList() << "getenv" << "DBUS_LAUNCHD_SESSION_BUS_SOCKET");
00170 if (!qp.waitForStarted(timeout)) {
00171 kDebug() << externalProc << "never started";
00172 } else {
00173 if (!qp.waitForReadyRead(timeout)) {
00174 kDebug() << externalProc << "never wrote anything";
00175 } else {
00176 while (qp.atEnd() == false) {
00177 line = qp.readLine().trimmed();
00178 if (mac_set_dbus_address(line)) {
00179 dbus_initialized = true;
00180 }
00181 }
00182 }
00183 qp.waitForFinished(timeout);
00184 }
00185 }
00186
00187 if (dbus_initialized == false) {
00188 kDebug() << "warning: unable to initialize D-Bus environment!";
00189 }
00190
00191 }
00192
00193 QString mac_app_filename() {
00194 static QString appFileName;
00195 if (appFileName.isEmpty()) {
00196 CFURLRef bundleURL = NULL;
00197 CFBundleRef bundle = NULL;
00198 CFStringRef bundlePath = NULL;
00199
00200 bundle = CFBundleGetMainBundle();
00201 if (bundle) {
00202 bundleURL = CFBundleCopyBundleURL(bundle);
00203 bundlePath = CFURLCopyFileSystemPath(bundleURL, kCFURLPOSIXPathStyle);
00204
00205 if (bundleURL) {
00206 CFRelease(bundleURL);
00207 }
00208
00209 if (bundlePath) {
00210 appFileName = convert_CFString_to_QString(bundlePath);
00211 CFRelease(bundlePath);
00212 }
00213 }
00214 }
00215 return appFileName;
00216 }
00217
00218 #endif