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