KDEUI
kapplication_win.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 <QtGui/QApplication>
00021 #include <kstandarddirs.h>
00022 #include <klocale.h>
00023
00024 #include <QTranslator>
00025 #include <QLocale>
00026 #include <QLibraryInfo>
00027 #include <QLibrary>
00028
00038 void KApplication_init_windows()
00039 {
00040
00041
00042
00043 QString qt_transl_file = QString("qt_") + QLocale::system().name();
00044 qt_transl_file.truncate(5);
00045 QTranslator *qt_transl = new QTranslator();
00046 if (qt_transl->load( qt_transl_file,
00047 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
00048 qApp->installTranslator( qt_transl );
00049 else
00050 delete qt_transl;
00051 }
00052
00053
00054
00055 #include <windows.h>
00056 #include <winperf.h>
00057 #include <psapi.h>
00058 #include <signal.h>
00059 #include <unistd.h>
00060
00061 #include <QtCore/QList>
00062 #include <QtCore/QtDebug>
00063
00064 static PPERF_OBJECT_TYPE FirstObject( PPERF_DATA_BLOCK PerfData )
00065 {
00066 return (PPERF_OBJECT_TYPE)((PBYTE)PerfData + PerfData->HeaderLength);
00067 }
00068
00069 static PPERF_INSTANCE_DEFINITION FirstInstance( PPERF_OBJECT_TYPE PerfObj )
00070 {
00071 return (PPERF_INSTANCE_DEFINITION)((PBYTE)PerfObj + PerfObj->DefinitionLength);
00072 }
00073
00074 static PPERF_OBJECT_TYPE NextObject( PPERF_OBJECT_TYPE PerfObj )
00075 {
00076 return (PPERF_OBJECT_TYPE)((PBYTE)PerfObj + PerfObj->TotalByteLength);
00077 }
00078
00079 static PPERF_COUNTER_DEFINITION FirstCounter( PPERF_OBJECT_TYPE PerfObj )
00080 {
00081 return (PPERF_COUNTER_DEFINITION) ((PBYTE)PerfObj + PerfObj->HeaderLength);
00082 }
00083
00084 static PPERF_INSTANCE_DEFINITION NextInstance( PPERF_INSTANCE_DEFINITION PerfInst )
00085 {
00086 PPERF_COUNTER_BLOCK PerfCntrBlk
00087 = (PPERF_COUNTER_BLOCK)((PBYTE)PerfInst + PerfInst->ByteLength);
00088 return (PPERF_INSTANCE_DEFINITION)((PBYTE)PerfCntrBlk + PerfCntrBlk->ByteLength);
00089 }
00090
00091 static PPERF_COUNTER_DEFINITION NextCounter( PPERF_COUNTER_DEFINITION PerfCntr )
00092 {
00093 return (PPERF_COUNTER_DEFINITION)((PBYTE)PerfCntr + PerfCntr->ByteLength);
00094 }
00095
00096 static PPERF_COUNTER_BLOCK CounterBlock(PPERF_INSTANCE_DEFINITION PerfInst)
00097 {
00098 return (PPERF_COUNTER_BLOCK) ((LPBYTE) PerfInst + PerfInst->ByteLength);
00099 }
00100
00101 #define GETPID_TOTAL 64 * 1024
00102 #define GETPID_BYTEINCREMENT 1024
00103 #define GETPID_PROCESS_OBJECT_INDEX 230
00104 #define GETPID_PROC_ID_COUNTER 784
00105
00106 QString fromWChar(const wchar_t *string, int size = -1)
00107 {
00108 return (sizeof(wchar_t) == sizeof(QChar)) ? QString::fromUtf16((ushort *)string, size)
00109 : QString::fromUcs4((uint *)string, size);
00110 }
00111
00112 void KApplication_getProcessesIdForName( const QString& processName, QList<int>& pids )
00113 {
00114 qDebug() << "KApplication_getProcessesIdForName" << processName;
00115 PPERF_OBJECT_TYPE perfObject;
00116 PPERF_INSTANCE_DEFINITION perfInstance;
00117 PPERF_COUNTER_DEFINITION perfCounter, curCounter;
00118 PPERF_COUNTER_BLOCK counterPtr;
00119 DWORD bufSize = GETPID_TOTAL;
00120 PPERF_DATA_BLOCK perfData = (PPERF_DATA_BLOCK) malloc( bufSize );
00121
00122 char key[64];
00123 sprintf(key,"%d %d", GETPID_PROCESS_OBJECT_INDEX, GETPID_PROC_ID_COUNTER);
00124 LONG lRes;
00125 while( (lRes = RegQueryValueExA( HKEY_PERFORMANCE_DATA,
00126 key,
00127 NULL,
00128 NULL,
00129 (LPBYTE) perfData,
00130 &bufSize )) == ERROR_MORE_DATA )
00131 {
00132
00133 bufSize += GETPID_BYTEINCREMENT;
00134 perfData = (PPERF_DATA_BLOCK) realloc( perfData, bufSize );
00135 }
00136
00137
00138 perfObject = FirstObject( perfData );
00139
00140
00141 for( uint i = 0; i < perfData->NumObjectTypes; i++ ) {
00142 if (perfObject->ObjectNameTitleIndex != GETPID_PROCESS_OBJECT_INDEX) {
00143 perfObject = NextObject( perfObject );
00144 continue;
00145 }
00146 pids.clear();
00147 perfCounter = FirstCounter( perfObject );
00148 perfInstance = FirstInstance( perfObject );
00149
00150 qDebug() << "INSTANCES: " << perfObject->NumInstances;
00151 for( int instance = 0; instance < perfObject->NumInstances; instance++ ) {
00152 const QString foundProcessName(
00153 fromWChar( (wchar_t *)( (PBYTE)perfInstance + perfInstance->NameOffset ) ) );
00154 qDebug() << "foundProcessName: " << foundProcessName;
00155 if ( foundProcessName == processName ) {
00156
00157 for( uint counter = 0; counter < perfObject->NumCounters; counter++ ) {
00158 if (curCounter->CounterNameTitleIndex == GETPID_PROC_ID_COUNTER) {
00159 counterPtr = CounterBlock(perfInstance);
00160 DWORD *value = (DWORD*)((LPBYTE) counterPtr + curCounter->CounterOffset);
00161 pids.append( int( *value ) );
00162 qDebug() << "found PID: " << int( *value );
00163 break;
00164 }
00165 curCounter = NextCounter( curCounter );
00166 }
00167 }
00168 perfInstance = NextInstance( perfInstance );
00169 }
00170 }
00171 free(perfData);
00172 RegCloseKey(HKEY_PERFORMANCE_DATA);
00173 }
00174
00175 bool KApplication_otherProcessesExist( const QString& processName )
00176 {
00177 QList<int> pids;
00178 KApplication_getProcessesIdForName( processName, pids );
00179 int myPid = getpid();
00180 foreach ( int pid, pids ) {
00181 if (myPid != pid) {
00182
00183 return true;
00184 }
00185 }
00186 return false;
00187 }
00188
00189 bool KApplication_killProcesses( const QString& processName )
00190 {
00191 QList<int> pids;
00192 KApplication_getProcessesIdForName( processName, pids );
00193 if ( pids.empty() )
00194 return true;
00195 qWarning() << "Killing process \"" << processName << " (pid=" << pids[0] << ")..";
00196 int overallResult = 0;
00197 foreach( int pid, pids ) {
00198 int result = kill( pid, SIGTERM );
00199 if ( result == 0 )
00200 continue;
00201 result = kill( pid, SIGKILL );
00202 if ( result != 0 )
00203 overallResult = result;
00204 }
00205 return overallResult == 0;
00206 }
00207
00208 struct EnumWindowsStruct
00209 {
00210 EnumWindowsStruct() : windowId( 0 ) {}
00211 int pid;
00212 HWND windowId;
00213 };
00214
00215 BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam )
00216 {
00217 if ( GetWindowLong( hwnd, GWL_STYLE ) & WS_VISIBLE ) {
00218 DWORD pidwin;
00219 GetWindowThreadProcessId(hwnd, &pidwin);
00220 if ( pidwin == ((EnumWindowsStruct*)lParam)->pid ) {
00221 ((EnumWindowsStruct*)lParam)->windowId = hwnd;
00222 return FALSE;
00223 }
00224 }
00225 return TRUE;
00226 }
00227
00228 void KApplication_activateWindowForProcess( const QString& executableName )
00229 {
00230 QList<int> pids;
00231 KApplication_getProcessesIdForName( executableName, pids );
00232 int myPid = getpid();
00233 int foundPid = 0;
00234 foreach ( int pid, pids ) {
00235 if (myPid != pid) {
00236 qDebug() << "activateWindowForProcess(): PID to activate:" << pid;
00237 foundPid = pid;
00238 break;
00239 }
00240 }
00241 if ( foundPid == 0 )
00242 return;
00243 EnumWindowsStruct winStruct;
00244 winStruct.pid = foundPid;
00245 EnumWindows( EnumWindowsProc, (LPARAM)&winStruct );
00246 if ( winStruct.windowId == NULL )
00247 return;
00248 SetForegroundWindow( winStruct.windowId );
00249 }
00250
00251
00252
00253
00254 bool KApplication_dbusIsPatched()
00255 {
00256 # ifdef __GNUC__
00257 # define DBUSLIB_PREFIX "lib"
00258 # else
00259 # define DBUSLIB_PREFIX ""
00260 # endif
00261 # ifdef _DEBUG
00262 # define DBUSLIB_SUFFIX "d"
00263 # else
00264 # define DBUSLIB_SUFFIX ""
00265 # endif
00266 QLibrary myLib(DBUSLIB_PREFIX "dbus-1" DBUSLIB_SUFFIX);
00267 return myLib.resolve("dbus_kde_patch");
00268 }