• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • kernel
kapplication_win.cpp
Go to the documentation of this file.
1/*
2 This file is part of the KDE libraries
3 Copyright (C) 2004-2008 Jarosław Staniek <staniek@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include <QtGui/QApplication>
21#include <kstandarddirs.h>
22#include <klocale.h>
23#include <kwindowsystem.h>
24
25#include <QTranslator>
26#include <QLocale>
27#include <QLibraryInfo>
28#include <QLibrary>
29
30#include <stdio.h>
31
43void KApplication_init_windows()
44{
45 //QString qt_transl_file = ::locate( "locale", KGlobal::locale()->language()
46 // + "/LC_MESSAGES/qt_" + KGlobal::locale()->language() + ".qm" );
47
48 QString qt_transl_file = QString("qt_") + QLocale::system().name();
49 qt_transl_file.truncate(5);
50 QTranslator *qt_transl = new QTranslator();
51 if (qt_transl->load( qt_transl_file,
52 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
53 qApp->installTranslator( qt_transl );
54 else
55 delete qt_transl;
56
57 // For apps like KMail which have lots of open files, the default is too low
58 // so increase it to the maximum.
59#ifndef _WIN32_WCE
60 _setmaxstdio(2048);
61#endif
62
63}
64
65// <copy of kdepim/libkdepim/utils.cpp, TODO: move to a shared helper library>
66
67#include <windows.h>
68#include <winperf.h>
69#include <psapi.h>
70#include <signal.h>
71#include <unistd.h>
72
73#include <QtCore/QList>
74#include <QtCore/QtDebug>
75
76static PPERF_OBJECT_TYPE FirstObject( PPERF_DATA_BLOCK PerfData )
77{
78 return (PPERF_OBJECT_TYPE)((PBYTE)PerfData + PerfData->HeaderLength);
79}
80
81static PPERF_INSTANCE_DEFINITION FirstInstance( PPERF_OBJECT_TYPE PerfObj )
82{
83 return (PPERF_INSTANCE_DEFINITION)((PBYTE)PerfObj + PerfObj->DefinitionLength);
84}
85
86static PPERF_OBJECT_TYPE NextObject( PPERF_OBJECT_TYPE PerfObj )
87{
88 return (PPERF_OBJECT_TYPE)((PBYTE)PerfObj + PerfObj->TotalByteLength);
89}
90
91static PPERF_COUNTER_DEFINITION FirstCounter( PPERF_OBJECT_TYPE PerfObj )
92{
93 return (PPERF_COUNTER_DEFINITION) ((PBYTE)PerfObj + PerfObj->HeaderLength);
94}
95
96static PPERF_INSTANCE_DEFINITION NextInstance( PPERF_INSTANCE_DEFINITION PerfInst )
97{
98 PPERF_COUNTER_BLOCK PerfCntrBlk
99 = (PPERF_COUNTER_BLOCK)((PBYTE)PerfInst + PerfInst->ByteLength);
100 return (PPERF_INSTANCE_DEFINITION)((PBYTE)PerfCntrBlk + PerfCntrBlk->ByteLength);
101}
102
103static PPERF_COUNTER_DEFINITION NextCounter( PPERF_COUNTER_DEFINITION PerfCntr )
104{
105 return (PPERF_COUNTER_DEFINITION)((PBYTE)PerfCntr + PerfCntr->ByteLength);
106}
107
108static PPERF_COUNTER_BLOCK CounterBlock(PPERF_INSTANCE_DEFINITION PerfInst)
109{
110 return (PPERF_COUNTER_BLOCK) ((LPBYTE) PerfInst + PerfInst->ByteLength);
111}
112
113#define GETPID_TOTAL 64 * 1024
114#define GETPID_BYTEINCREMENT 1024
115#define GETPID_PROCESS_OBJECT_INDEX 230
116#define GETPID_PROC_ID_COUNTER 784
117
118QString fromWChar(const wchar_t *string, int size = -1)
119{
120 return (sizeof(wchar_t) == sizeof(QChar)) ? QString::fromUtf16((ushort *)string, size)
121 : QString::fromUcs4((uint *)string, size);
122}
123
124#ifndef _WIN32_WCE
125void KApplication_getProcessesIdForName( const QString& processName, QList<int>& pids )
126{
127 qDebug() << "KApplication_getProcessesIdForName" << processName;
128 PPERF_OBJECT_TYPE perfObject;
129 PPERF_INSTANCE_DEFINITION perfInstance;
130 PPERF_COUNTER_DEFINITION perfCounter, curCounter;
131 PPERF_COUNTER_BLOCK counterPtr;
132 DWORD bufSize = GETPID_TOTAL;
133 PPERF_DATA_BLOCK perfData = (PPERF_DATA_BLOCK) malloc( bufSize );
134
135 char key[64];
136 sprintf(key,"%d %d", GETPID_PROCESS_OBJECT_INDEX, GETPID_PROC_ID_COUNTER);
137 LONG lRes;
138 while( (lRes = RegQueryValueExA( HKEY_PERFORMANCE_DATA,
139 key,
140 NULL,
141 NULL,
142 (LPBYTE) perfData,
143 &bufSize )) == ERROR_MORE_DATA )
144 {
145 // get a buffer that is big enough
146 bufSize += GETPID_BYTEINCREMENT;
147 perfData = (PPERF_DATA_BLOCK) realloc( perfData, bufSize );
148 }
149
150 // Get the first object type.
151 perfObject = FirstObject( perfData );
152
153 // Process all objects.
154 for( uint i = 0; i < perfData->NumObjectTypes; i++ ) {
155 if (perfObject->ObjectNameTitleIndex != GETPID_PROCESS_OBJECT_INDEX) {
156 perfObject = NextObject( perfObject );
157 continue;
158 }
159 pids.clear();
160 perfCounter = FirstCounter( perfObject );
161 perfInstance = FirstInstance( perfObject );
162 // retrieve the instances
163 qDebug() << "INSTANCES: " << perfObject->NumInstances;
164 for( int instance = 0; instance < perfObject->NumInstances; instance++ ) {
165 curCounter = perfCounter;
166 const QString foundProcessName(
167 fromWChar( (wchar_t *)( (PBYTE)perfInstance + perfInstance->NameOffset ) ) );
168 qDebug() << "foundProcessName: " << foundProcessName;
169 if ( foundProcessName == processName ) {
170 // retrieve the counters
171 for( uint counter = 0; counter < perfObject->NumCounters; counter++ ) {
172 if (curCounter->CounterNameTitleIndex == GETPID_PROC_ID_COUNTER) {
173 counterPtr = CounterBlock(perfInstance);
174 DWORD *value = (DWORD*)((LPBYTE) counterPtr + curCounter->CounterOffset);
175 pids.append( int( *value ) );
176 qDebug() << "found PID: " << int( *value );
177 break;
178 }
179 curCounter = NextCounter( curCounter );
180 }
181 }
182 perfInstance = NextInstance( perfInstance );
183 }
184 }
185 free(perfData);
186 RegCloseKey(HKEY_PERFORMANCE_DATA);
187}
188
189bool KApplication_otherProcessesExist( const QString& processName )
190{
191 QList<int> pids;
192 KApplication_getProcessesIdForName( processName, pids );
193 int myPid = getpid();
194 foreach ( int pid, pids ) {
195 if (myPid != pid) {
196// kDebug() << "Process ID is " << pid;
197 return true;
198 }
199 }
200 return false;
201}
202
203bool KApplication_killProcesses( const QString& processName )
204{
205 QList<int> pids;
206 KApplication_getProcessesIdForName( processName, pids );
207 if ( pids.empty() )
208 return true;
209 qWarning() << "Killing process \"" << processName << " (pid=" << pids[0] << ")..";
210 int overallResult = 0;
211 foreach( int pid, pids ) {
212 int result = kill( pid, SIGTERM );
213 if ( result == 0 )
214 continue;
215 result = kill( pid, SIGKILL );
216 if ( result != 0 )
217 overallResult = result;
218 }
219 return overallResult == 0;
220}
221
222struct EnumWindowsStruct
223{
224 EnumWindowsStruct() : windowId( 0 ) {}
225 int pid;
226 HWND windowId;
227};
228
229BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam )
230{
231 if ( GetWindowLong( hwnd, GWL_STYLE ) & WS_VISIBLE ) {
232 DWORD pidwin;
233 GetWindowThreadProcessId(hwnd, &pidwin);
234 if ( pidwin == ((EnumWindowsStruct*)lParam)->pid ) {
235 ((EnumWindowsStruct*)lParam)->windowId = hwnd;
236 return false;
237 }
238 }
239 return true;
240}
241
242void KApplication_activateWindowForProcess( const QString& executableName )
243{
244 QList<int> pids;
245 KApplication_getProcessesIdForName( executableName, pids );
246 int myPid = getpid();
247 int foundPid = 0;
248 foreach ( int pid, pids ) {
249 if (myPid != pid) {
250 qDebug() << "activateWindowForProcess(): PID to activate:" << pid;
251 foundPid = pid;
252 break;
253 }
254 }
255 if ( foundPid == 0 )
256 return;
257 EnumWindowsStruct winStruct;
258 winStruct.pid = foundPid;
259 EnumWindows( EnumWindowsProc, (LPARAM)&winStruct );
260 if ( winStruct.windowId == NULL )
261 return;
262 KWindowSystem::forceActiveWindow( winStruct.windowId, 0 );
263}
264#endif
265
266// </copy>
KWindowSystem::forceActiveWindow
static void forceActiveWindow(WId win, long time=0)
Sets window win to be the active window.
Definition: kwindowsystem_mac.cpp:366
QList
QTranslator
GETPID_BYTEINCREMENT
#define GETPID_BYTEINCREMENT
Definition: kapplication_win.cpp:114
GETPID_PROC_ID_COUNTER
#define GETPID_PROC_ID_COUNTER
Definition: kapplication_win.cpp:116
EnumWindowsProc
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
Definition: kapplication_win.cpp:229
KApplication_otherProcessesExist
bool KApplication_otherProcessesExist(const QString &processName)
Definition: kapplication_win.cpp:189
GETPID_TOTAL
#define GETPID_TOTAL
Definition: kapplication_win.cpp:113
NextInstance
static PPERF_INSTANCE_DEFINITION NextInstance(PPERF_INSTANCE_DEFINITION PerfInst)
Definition: kapplication_win.cpp:96
KApplication_init_windows
void KApplication_init_windows()
MS Windows-related actions for KApplication startup.
Definition: kapplication_win.cpp:43
FirstObject
static PPERF_OBJECT_TYPE FirstObject(PPERF_DATA_BLOCK PerfData)
Definition: kapplication_win.cpp:76
CounterBlock
static PPERF_COUNTER_BLOCK CounterBlock(PPERF_INSTANCE_DEFINITION PerfInst)
Definition: kapplication_win.cpp:108
NextCounter
static PPERF_COUNTER_DEFINITION NextCounter(PPERF_COUNTER_DEFINITION PerfCntr)
Definition: kapplication_win.cpp:103
fromWChar
QString fromWChar(const wchar_t *string, int size=-1)
Definition: kapplication_win.cpp:118
NextObject
static PPERF_OBJECT_TYPE NextObject(PPERF_OBJECT_TYPE PerfObj)
Definition: kapplication_win.cpp:86
KApplication_killProcesses
bool KApplication_killProcesses(const QString &processName)
Definition: kapplication_win.cpp:203
GETPID_PROCESS_OBJECT_INDEX
#define GETPID_PROCESS_OBJECT_INDEX
Definition: kapplication_win.cpp:115
FirstInstance
static PPERF_INSTANCE_DEFINITION FirstInstance(PPERF_OBJECT_TYPE PerfObj)
Definition: kapplication_win.cpp:81
KApplication_activateWindowForProcess
void KApplication_activateWindowForProcess(const QString &executableName)
Definition: kapplication_win.cpp:242
FirstCounter
static PPERF_COUNTER_DEFINITION FirstCounter(PPERF_OBJECT_TYPE PerfObj)
Definition: kapplication_win.cpp:91
KApplication_getProcessesIdForName
void KApplication_getProcessesIdForName(const QString &processName, QList< int > &pids)
Definition: kapplication_win.cpp:125
klocale.h
kstandarddirs.h
BOOL
typedef BOOL(WINAPI *PtrTzSpecificLocalTimeToSystemTime)(LPTIME_ZONE_INFORMATION lpTimeZoneInformation
kwindowsystem.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal