KDELibs4Support

kapplication_win.cpp
1 /*
2  This file is part of the KDE libraries
3  Copyright (C) 2004-2008 JarosÅ‚aw Staniek <[email protected]>
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 <QApplication>
21 #include <klocale.h>
22 #include <kwindowsystem.h>
23 
24 #include <QTranslator>
25 #include <QLocale>
26 #include <QLibraryInfo>
27 #include <QLibrary>
28 
29 #include <stdio.h>
30 
42 void KApplication_init_windows()
43 {
44  //QString qt_transl_file = ::locate( "locale", KLocale::global()->language()
45  // + "/LC_MESSAGES/qt_" + KLocale::global()->language() + ".qm" );
46 
47  QString qt_transl_file = QString("qt_") + QLocale::system().name();
48  qt_transl_file.truncate(5);
49  QTranslator *qt_transl = new QTranslator();
50  if (qt_transl->load(qt_transl_file,
52  qApp->installTranslator(qt_transl);
53  } else {
54  delete qt_transl;
55  }
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 <QList>
74 #include <QtDebug>
75 
76 static PPERF_OBJECT_TYPE FirstObject(PPERF_DATA_BLOCK PerfData)
77 {
78  return (PPERF_OBJECT_TYPE)((PBYTE)PerfData + PerfData->HeaderLength);
79 }
80 
81 static PPERF_INSTANCE_DEFINITION FirstInstance(PPERF_OBJECT_TYPE PerfObj)
82 {
83  return (PPERF_INSTANCE_DEFINITION)((PBYTE)PerfObj + PerfObj->DefinitionLength);
84 }
85 
86 static PPERF_OBJECT_TYPE NextObject(PPERF_OBJECT_TYPE PerfObj)
87 {
88  return (PPERF_OBJECT_TYPE)((PBYTE)PerfObj + PerfObj->TotalByteLength);
89 }
90 
91 static PPERF_COUNTER_DEFINITION FirstCounter(PPERF_OBJECT_TYPE PerfObj)
92 {
93  return (PPERF_COUNTER_DEFINITION)((PBYTE)PerfObj + PerfObj->HeaderLength);
94 }
95 
96 static 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 
103 static PPERF_COUNTER_DEFINITION NextCounter(PPERF_COUNTER_DEFINITION PerfCntr)
104 {
105  return (PPERF_COUNTER_DEFINITION)((PBYTE)PerfCntr + PerfCntr->ByteLength);
106 }
107 
108 static 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 
118 QString 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
125 void 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  // get a buffer that is big enough
145  bufSize += GETPID_BYTEINCREMENT;
146  perfData = (PPERF_DATA_BLOCK) realloc(perfData, bufSize);
147  }
148 
149  // Get the first object type.
150  perfObject = FirstObject(perfData);
151 
152  // Process all objects.
153  for (uint i = 0; i < perfData->NumObjectTypes; i++) {
154  if (perfObject->ObjectNameTitleIndex != GETPID_PROCESS_OBJECT_INDEX) {
155  perfObject = NextObject(perfObject);
156  continue;
157  }
158  pids.clear();
159  perfCounter = FirstCounter(perfObject);
160  perfInstance = FirstInstance(perfObject);
161  // retrieve the instances
162  qDebug() << "INSTANCES: " << perfObject->NumInstances;
163  for (int instance = 0; instance < perfObject->NumInstances; instance++) {
164  curCounter = perfCounter;
165  const QString foundProcessName(
166  fromWChar((wchar_t *)((PBYTE)perfInstance + perfInstance->NameOffset)));
167  qDebug() << "foundProcessName: " << foundProcessName;
168  if (foundProcessName == processName) {
169  // retrieve the counters
170  for (uint counter = 0; counter < perfObject->NumCounters; counter++) {
171  if (curCounter->CounterNameTitleIndex == GETPID_PROC_ID_COUNTER) {
172  counterPtr = CounterBlock(perfInstance);
173  DWORD *value = (DWORD *)((LPBYTE) counterPtr + curCounter->CounterOffset);
174  pids.append(int(*value));
175  qDebug() << "found PID: " << int(*value);
176  break;
177  }
178  curCounter = NextCounter(curCounter);
179  }
180  }
181  perfInstance = NextInstance(perfInstance);
182  }
183  }
184  free(perfData);
185  RegCloseKey(HKEY_PERFORMANCE_DATA);
186 }
187 
188 bool KApplication_otherProcessesExist(const QString &processName)
189 {
190  QList<int> pids;
191  KApplication_getProcessesIdForName(processName, pids);
192  int myPid = getpid();
193  foreach (int pid, pids) {
194  if (myPid != pid) {
195 // kDebug() << "Process ID is " << pid;
196  return true;
197  }
198  }
199  return false;
200 }
201 
202 bool KApplication_killProcesses(const QString &processName)
203 {
204  QList<int> pids;
205  KApplication_getProcessesIdForName(processName, pids);
206  if (pids.empty()) {
207  return true;
208  }
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  }
216  result = kill(pid, SIGKILL);
217  if (result != 0) {
218  overallResult = result;
219  }
220  }
221  return overallResult == 0;
222 }
223 
224 struct EnumWindowsStruct {
225  EnumWindowsStruct() : windowId(0) {}
226  int pid;
227  HWND windowId;
228 };
229 
230 BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
231 {
232  if (GetWindowLong(hwnd, GWL_STYLE) & WS_VISIBLE) {
233  DWORD pidwin;
234  GetWindowThreadProcessId(hwnd, &pidwin);
235  if (pidwin == ((EnumWindowsStruct *)lParam)->pid) {
236  ((EnumWindowsStruct *)lParam)->windowId = hwnd;
237  return false;
238  }
239  }
240  return true;
241 }
242 
243 void KApplication_activateWindowForProcess(const QString &executableName)
244 {
245  QList<int> pids;
246  KApplication_getProcessesIdForName(executableName, pids);
247  int myPid = getpid();
248  int foundPid = 0;
249  foreach (int pid, pids) {
250  if (myPid != pid) {
251  qDebug() << "activateWindowForProcess(): PID to activate:" << pid;
252  foundPid = pid;
253  break;
254  }
255  }
256  if (foundPid == 0) {
257  return;
258  }
259  EnumWindowsStruct winStruct;
260  winStruct.pid = foundPid;
261  EnumWindows(EnumWindowsProc, (LPARAM)&winStruct);
262  if (winStruct.windowId == NULL) {
263  return;
264  }
265  KWindowSystem::forceActiveWindow((WId)winStruct.windowId, 0);
266 }
267 #endif
268 
269 // </copy>
void clear()
void truncate(int position)
static void forceActiveWindow(WId win, long time=0)
QLocale system()
void append(const T &value)
bool empty() const const
QString fromUtf16(const ushort *unicode, int size)
QString name() const const
bool load(const QString &filename, const QString &directory, const QString &search_delimiters, const QString &suffix)
QString location(QLibraryInfo::LibraryLocation loc)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat Jul 4 2020 22:58:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.