1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
/*
Actionaz
Copyright (C) 2008-2014 Jonathan Mercier-Ganady
Actionaz is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Actionaz is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Contact : jmgr@jmgr.info
*/
#include "crossplatform.h"
#include "windowhandle.h"
#include <QWidget>
#include <QElapsedTimer>
#include <QDebug>
#include <QDir>
#ifdef Q_WS_X11
#include <X11/Xlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <QX11Info>
#endif
#ifdef Q_WS_WIN
#include <Windows.h>
#include <Tlhelp32.h>
#endif
namespace ActionTools
{
QString CrossPlatform::mLastErrorString;
int CrossPlatform::mLastError = -1;
void CrossPlatform::setForegroundWindow(QWidget *window)
{
#ifdef Q_WS_X11
XRaiseWindow(QX11Info::display(), window->winId());
#endif
#ifdef Q_WS_WIN
if(IsIconic(window->winId()))
ShowWindow(window->winId(), SW_RESTORE);
else
{
if(!SetForegroundWindow(window->winId()))
setupLastError();
if(!SetWindowPos(window->winId(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE))
setupLastError();
}
#endif
}
bool CrossPlatform::killProcess(int id, KillMode killMode, int timeout)
{
#ifdef Q_WS_X11
switch(killMode)
{
case Graceful:
{
int ret = kill(id, SIGTERM);
if(ret != 0)
{
setupLastError();
return false;
}
return true;
}
case Forceful:
{
int ret = kill(id, SIGKILL);
if(ret != 0)
{
setupLastError();
return false;
}
return true;
}
case GracefulThenForceful:
{
if(kill(id, SIGTERM) != 0)
{
setupLastError();
return false;
}
QElapsedTimer timer;
timer.start();
while(true)
{
if(processStatus(id) == Stopped)
return true;
if(timer.elapsed() >= timeout)
{
int ret = kill(id, SIGKILL);
if(ret != 0)
{
if(ret == ESRCH) //No such process
return true;
setupLastError();
return false;
}
timespec req;
req.tv_sec = 0;
req.tv_nsec = 10000; //10 msec
nanosleep(&req, 0);
return (processStatus(id) == Stopped);
}
timespec req;
req.tv_sec = 0;
req.tv_nsec = 100000; //100 msec
nanosleep(&req, 0);
}
}
}
return false;
#endif
#ifdef Q_WS_WIN
HANDLE process = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION, FALSE, id);
if(!process)
{
setupLastError();
return false;
}
if(killMode == Forceful)
{
CloseHandle(process);
bool ret = TerminateProcess(process, 0);
if(!ret)
setupLastError();
return ret;
}
foreach(const ActionTools::WindowHandle &windowHandle, ActionTools::WindowHandle::windowList())
{
if(windowHandle.processId() == id)
windowHandle.close();
}
QElapsedTimer timer;
timer.start();
DWORD exitCode;
do
{
if(!GetExitCodeProcess(process, &exitCode))
{
CloseHandle(process);
setupLastError();
return false;
}
if(timer.elapsed() >= timeout)
break;
Sleep(100);
}
while(exitCode == STILL_ACTIVE);
if(exitCode == STILL_ACTIVE)
{
if(killMode == Graceful)
{
CloseHandle(process);
setupLastError();
return false;
}
if(!TerminateProcess(process, 0))
{
CloseHandle(process);
setupLastError();
return false;
}
}
CloseHandle(process);
return true;
#endif
}
CrossPlatform::ProcessStatus CrossPlatform::processStatus(int id)
{
#ifdef Q_WS_X11
return (kill(id, 0) == 0) ? Running : Stopped;
#endif
#ifdef Q_WS_WIN
HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, id);
if(process)
{
CloseHandle(process);
return Running;
}
return Stopped;
#endif
}
QList<int> CrossPlatform::runningProcesses()
{
#ifdef Q_WS_X11
QDir procDir("/proc");
QList<int> back;
if(!procDir.exists())
return back;
QStringList processes = procDir.entryList(QDir::Dirs);
foreach(const QString &processId, processes)
{
bool success;
int id = processId.toInt(&success);
if(success)
back.append(id);
}
return back;
#endif
#ifdef Q_WS_WIN
QList<int> back;
HANDLE handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(!handle)
{
setupLastError();
return back;
}
PROCESSENTRY32 info = {0};
info.dwSize = sizeof(PROCESSENTRY32);
bool first = Process32First(handle, &info);
if(!first)
{
CloseHandle(handle);
setupLastError();
return back;
}
do
{
back.append(info.th32ProcessID);
}
while(Process32Next(handle, &info));
CloseHandle(handle);
return back;
#endif
}
void CrossPlatform::sleep(int milliseconds)
{
#ifdef Q_WS_X11
struct timespec timeout0;
struct timespec timeout1;
struct timespec* tmp;
struct timespec* t0 = &timeout0;
struct timespec* t1 = &timeout1;
t0->tv_sec = milliseconds / 1000;
t0->tv_nsec = (milliseconds % 1000) * (1000 * 1000);
while ((nanosleep(t0, t1) == (-1)) && (errno == EINTR))
{
tmp = t0;
t0 = t1;
t1 = tmp;
}
#endif
#ifdef Q_WS_WIN
Sleep(milliseconds);
#endif
}
void CrossPlatform::setupLastError()
{
#ifdef Q_WS_X11
mLastError = errno;
char *errorStr = strerror(errno);
mLastErrorString = QString::fromUtf8(errorStr);
#endif
#ifdef Q_WS_WIN
mLastError = GetLastError();
LPTSTR message;
if(!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
0, mLastError, 0, (LPTSTR)&message, 0, 0))
{
qDebug() << "Error: " << mLastError << ", and error " << GetLastError() << " while trying to get the error message";
mLastErrorString = QString();
return;
}
mLastErrorString = QString::fromWCharArray(message).trimmed();
LocalFree(message);
#endif
qDebug() << "Error: " << mLastErrorString << " (" << mLastError << ")";
}
}
|