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 curCounter = perfCounter;
00153 const QString foundProcessName(
00154 fromWChar( (wchar_t *)( (PBYTE)perfInstance + perfInstance->NameOffset ) ) );
00155 qDebug() << "foundProcessName: " << foundProcessName;
00156 if ( foundProcessName == processName ) {
00157
00158 for( uint counter = 0; counter < perfObject->NumCounters; counter++ ) {
00159 if (curCounter->CounterNameTitleIndex == GETPID_PROC_ID_COUNTER) {
00160 counterPtr = CounterBlock(perfInstance);
00161 DWORD *value = (DWORD*)((LPBYTE) counterPtr + curCounter->CounterOffset);
00162 pids.append( int( *value ) );
00163 qDebug() << "found PID: " << int( *value );
00164 break;
00165 }
00166 curCounter = NextCounter( curCounter );
00167 }
00168 }
00169 perfInstance = NextInstance( perfInstance );
00170 }
00171 }
00172 free(perfData);
00173 RegCloseKey(HKEY_PERFORMANCE_DATA);
00174 }
00175
00176 bool KApplication_otherProcessesExist( const QString& processName )
00177 {
00178 QList<int> pids;
00179 KApplication_getProcessesIdForName( processName, pids );
00180 int myPid = getpid();
00181 foreach ( int pid, pids ) {
00182 if (myPid != pid) {
00183
00184 return true;
00185 }
00186 }
00187 return false;
00188 }
00189
00190 bool KApplication_killProcesses( const QString& processName )
00191 {
00192 QList<int> pids;
00193 KApplication_getProcessesIdForName( processName, pids );
00194 if ( pids.empty() )
00195 return true;
00196 qWarning() << "Killing process \"" << processName << " (pid=" << pids[0] << ")..";
00197 int overallResult = 0;
00198 foreach( int pid, pids ) {
00199 int result = kill( pid, SIGTERM );
00200 if ( result == 0 )
00201 continue;
00202 result = kill( pid, SIGKILL );
00203 if ( result != 0 )
00204 overallResult = result;
00205 }
00206 return overallResult == 0;
00207 }
00208
00209 struct EnumWindowsStruct
00210 {
00211 EnumWindowsStruct() : windowId( 0 ) {}
00212 int pid;
00213 HWND windowId;
00214 };
00215
00216 BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam )
00217 {
00218 if ( GetWindowLong( hwnd, GWL_STYLE ) & WS_VISIBLE ) {
00219 DWORD pidwin;
00220 GetWindowThreadProcessId(hwnd, &pidwin);
00221 if ( pidwin == ((EnumWindowsStruct*)lParam)->pid ) {
00222 ((EnumWindowsStruct*)lParam)->windowId = hwnd;
00223 return false;
00224 }
00225 }
00226 return true;
00227 }
00228
00229 void KApplication_activateWindowForProcess( const QString& executableName )
00230 {
00231 QList<int> pids;
00232 KApplication_getProcessesIdForName( executableName, pids );
00233 int myPid = getpid();
00234 int foundPid = 0;
00235 foreach ( int pid, pids ) {
00236 if (myPid != pid) {
00237 qDebug() << "activateWindowForProcess(): PID to activate:" << pid;
00238 foundPid = pid;
00239 break;
00240 }
00241 }
00242 if ( foundPid == 0 )
00243 return;
00244 EnumWindowsStruct winStruct;
00245 winStruct.pid = foundPid;
00246 EnumWindows( EnumWindowsProc, (LPARAM)&winStruct );
00247 if ( winStruct.windowId == NULL )
00248 return;
00249 SetForegroundWindow( winStruct.windowId );
00250 }
00251
00252
00253
00254
00255 bool KApplication_dbusIsPatched()
00256 {
00257 # ifdef __GNUC__
00258 # define DBUSLIB_PREFIX "lib"
00259 # else
00260 # define DBUSLIB_PREFIX ""
00261 # endif
00262 # ifdef _DEBUG
00263 # define DBUSLIB_SUFFIX "d"
00264 # else
00265 # define DBUSLIB_SUFFIX ""
00266 # endif
00267 QLibrary myLib(DBUSLIB_PREFIX "dbus-1" DBUSLIB_SUFFIX);
00268 return myLib.resolve("dbus_kde_patch");
00269 }