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
|
/*
* LXImage-Qt - a simple and fast image viewer
* Copyright (C) 2013 PCMan <pcman.tw@gmail.com>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "screenshotdialog.h"
#include "screenshotselectarea.h"
#include <QTimer>
#include <QPixmap>
#include <QImage>
#include <QPainter>
#include "application.h"
#include <QDesktopWidget>
#include <QScreen>
#include <QX11Info>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xfixes.h>
using namespace LxImage;
ScreenshotDialog::ScreenshotDialog(QWidget* parent, Qt::WindowFlags f): QDialog(parent, f) {
ui.setupUi(this);
Application* app = static_cast<Application*>(qApp);
app->addWindow();
int event_base, error_base;
hasXfixes_ = XFixesQueryExtension(QX11Info::display(), &event_base, &error_base) ? true : false;
if(!hasXfixes_)
ui.includeCursor->hide();
}
ScreenshotDialog::~ScreenshotDialog() {
Application* app = static_cast<Application*>(qApp);
app->removeWindow();
}
void ScreenshotDialog::done(int r) {
if(r == QDialog::Accepted) {
hide();
QDialog::done(r);
XSync(QX11Info::display(), 0); // is this useful?
int delay = ui.delay->value();
if(delay == 0) {
// NOTE:
// Well, we need to give X and the window manager some time to
// really hide our own dialog from the screen.
// Nobody knows how long it will take, and there is no reliable
// way to ensure that. Let's wait for 400 ms here for it.
delay = 400;
}
else
delay *= 1000;
// the dialog object will be deleted in doScreenshot().
QTimer::singleShot(delay, this, SLOT(doScreenshot()));
}
else {
deleteLater();
}
}
QRect ScreenshotDialog::windowFrame(WId wid) {
QRect result;
XWindowAttributes wa;
if(XGetWindowAttributes(QX11Info::display(), wid, &wa)) {
Window child;
int x, y;
// translate to root coordinate
XTranslateCoordinates(QX11Info::display(), wid, wa.root, 0, 0, &x, &y, &child);
//qDebug("%d, %d, %d, %d", x, y, wa.width, wa.height);
result.setRect(x, y, wa.width, wa.height);
// get the frame widths added by the window manager
Atom atom = XInternAtom(QX11Info::display(), "_NET_FRAME_EXTENTS", false);
unsigned long type, resultLen, rest;
int format;
unsigned char* data = nullptr;
if(XGetWindowProperty(QX11Info::display(), wid, atom, 0, G_MAXLONG, false,
XA_CARDINAL, &type, &format, &resultLen, &rest, &data) == Success) {
}
if(data) { // left, right, top, bottom
long* offsets = reinterpret_cast<long*>(data);
result.setLeft(result.left() - offsets[0]);
result.setRight(result.right() + offsets[1]);
result.setTop(result.top() - offsets[2]);
result.setBottom(result.bottom() + offsets[3]);
XFree(data);
}
}
return result;
}
WId ScreenshotDialog::activeWindowId() {
WId root = WId(QX11Info::appRootWindow());
Atom atom = XInternAtom(QX11Info::display(), "_NET_ACTIVE_WINDOW", false);
unsigned long type, resultLen, rest;
int format;
WId result = 0;
unsigned char* data = nullptr;
if(XGetWindowProperty(QX11Info::display(), root, atom, 0, 1, false,
XA_WINDOW, &type, &format, &resultLen, &rest, &data) == Success) {
result = *reinterpret_cast<long*>(data);
XFree(data);
}
return result;
}
void ScreenshotDialog::doScreenshot() {
WId wid = 0;
int x = 0, y = 0, width = -1, height = -1;
wid = QApplication::desktop()->winId(); // get desktop window
if(ui.currentWindow->isChecked()) {
WId activeWid = activeWindowId();
if(activeWid) {
if(ui.includeFrame->isChecked()) {
QRect rect = windowFrame(activeWid);
x = rect.x();
y = rect.y();
width = rect.width();
height = rect.height();
}
else
wid = activeWid;
}
}
QImage image;
QScreen *screen = QGuiApplication::primaryScreen();
if(screen) {
QPixmap pixmap = screen->grabWindow(wid, x, y, width, height);
image = pixmap.toImage();
if(hasXfixes_ && ui.includeCursor->isChecked()) {
// capture the cursor if needed
XFixesCursorImage* cursor = XFixesGetCursorImage(QX11Info::display());
if(cursor) {
if(cursor->pixels) { // pixles should be an ARGB array
QImage cursorImage;
if(sizeof(long) == 4) {
// FIXME: will we encounter byte-order problems here?
cursorImage = QImage((uchar*)cursor->pixels, cursor->width, cursor->height, QImage::Format_ARGB32);
}
else { // XFixes returns long integers which is not 32 bit on 64 bit systems.
long len = cursor->width * cursor->height;
quint32* buf = new quint32[len];
for(long i = 0; i < len; ++i)
buf[i] = (quint32)cursor->pixels[i];
cursorImage = QImage((uchar*)buf, cursor->width, cursor->height, QImage::Format_ARGB32, [](void* b) { delete[] (quint32*)b; }, buf);
}
// paint the cursor on the current image
QPainter painter(&image);
painter.drawImage(cursor->x - cursor->xhot, cursor->y - cursor->yhot, cursorImage);
}
XFree(cursor);
}
}
}
if(ui.screenArea->isChecked() && !image.isNull()) {
ScreenshotSelectArea selectArea(image);
if(QDialog::Accepted == selectArea.exec()) {
image = image.copy(selectArea.selectedArea());
}
}
Application* app = static_cast<Application*>(qApp);
MainWindow* window = app->createWindow();
if(!image.isNull())
window->pasteImage(image);
window->show();
deleteLater(); // destroy ourself
}
|