2017-05-22 15:11:07 +08:00
|
|
|
#include "appsnapshot.h"
|
2017-05-23 10:52:06 +08:00
|
|
|
#include "_previewcontainer.h"
|
2017-05-22 15:11:07 +08:00
|
|
|
|
2017-05-22 17:17:29 +08:00
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/X.h>
|
|
|
|
#include <X11/Xutil.h>
|
|
|
|
#include <X11/Xatom.h>
|
|
|
|
|
|
|
|
#include <QX11Info>
|
|
|
|
#include <QPainter>
|
|
|
|
|
|
|
|
AppSnapshot::AppSnapshot(const WId wid, QWidget *parent)
|
|
|
|
: QWidget(parent),
|
|
|
|
|
2017-05-24 11:32:14 +08:00
|
|
|
m_wid(wid)
|
2017-05-22 17:17:29 +08:00
|
|
|
{
|
2017-05-24 11:32:14 +08:00
|
|
|
// QTimer::singleShot(1, this, &AppSnapshot::fetchSnapshot);
|
2017-05-22 17:17:29 +08:00
|
|
|
}
|
|
|
|
|
2017-05-23 15:32:03 +08:00
|
|
|
void AppSnapshot::closeWindow() const
|
|
|
|
{
|
|
|
|
const auto display = QX11Info::display();
|
|
|
|
|
|
|
|
XEvent e;
|
|
|
|
|
|
|
|
memset(&e, 0, sizeof(e));
|
|
|
|
e.xclient.type = ClientMessage;
|
|
|
|
e.xclient.window = m_wid;
|
|
|
|
e.xclient.message_type = XInternAtom(display, "WM_PROTOCOLS", true);
|
|
|
|
e.xclient.format = 32;
|
|
|
|
e.xclient.data.l[0] = XInternAtom(display, "WM_DELETE_WINDOW", false);
|
|
|
|
e.xclient.data.l[1] = CurrentTime;
|
|
|
|
|
|
|
|
XSendEvent(display, m_wid, false, NoEventMask, &e);
|
|
|
|
XFlush(display);
|
|
|
|
}
|
|
|
|
|
2017-05-24 10:56:43 +08:00
|
|
|
void AppSnapshot::setWindowTitle(const QString &title)
|
|
|
|
{
|
|
|
|
m_title = title;
|
|
|
|
}
|
|
|
|
|
2017-05-22 17:17:29 +08:00
|
|
|
void AppSnapshot::fetchSnapshot()
|
|
|
|
{
|
|
|
|
const auto display = QX11Info::display();
|
|
|
|
|
|
|
|
XWindowAttributes attrs;
|
|
|
|
XGetWindowAttributes(display, m_wid, &attrs);
|
|
|
|
XImage *ximage = XGetImage(display, m_wid, 0, 0, attrs.width, attrs.height, AllPlanes, ZPixmap);
|
|
|
|
const QImage qimage((const uchar*)(ximage->data), ximage->width, ximage->height, ximage->bytes_per_line, QImage::Format_RGB32);
|
|
|
|
Q_ASSERT(!qimage.isNull());
|
|
|
|
|
|
|
|
const Atom gtk_frame_extents = XInternAtom(display, "_GTK_FRAME_EXTENTS", true);
|
|
|
|
Atom actual_type_return;
|
|
|
|
int actual_format_return;
|
|
|
|
unsigned long n_items_return;
|
|
|
|
unsigned long bytes_after_return;
|
|
|
|
unsigned char *prop_to_return;
|
|
|
|
|
|
|
|
const auto r = XGetWindowProperty(display, m_wid, gtk_frame_extents, 0, 4, false, XA_CARDINAL,
|
|
|
|
&actual_type_return, &actual_format_return, &n_items_return, &bytes_after_return, &prop_to_return);
|
|
|
|
if (!r && prop_to_return && n_items_return == 4 && actual_format_return == 32)
|
|
|
|
{
|
|
|
|
const unsigned long *extents = reinterpret_cast<const unsigned long *>(prop_to_return);
|
|
|
|
const int left = extents[0];
|
|
|
|
const int right = extents[1];
|
|
|
|
const int top = extents[2];
|
|
|
|
const int bottom = extents[3];
|
|
|
|
const int width = qimage.width();
|
|
|
|
const int height = qimage.height();
|
|
|
|
|
|
|
|
m_snapshot = qimage.copy(left, top, width - left - right, height - top - bottom);
|
|
|
|
} else {
|
2017-05-23 10:52:06 +08:00
|
|
|
m_snapshot = qimage.copy();
|
2017-05-22 17:17:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
XDestroyImage(ximage);
|
|
|
|
XFree(prop_to_return);
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-05-22 17:54:59 +08:00
|
|
|
void AppSnapshot::enterEvent(QEvent *e)
|
|
|
|
{
|
|
|
|
QWidget::enterEvent(e);
|
|
|
|
|
|
|
|
emit entered(m_wid);
|
|
|
|
}
|
|
|
|
|
2017-05-22 17:17:29 +08:00
|
|
|
void AppSnapshot::paintEvent(QPaintEvent *e)
|
|
|
|
{
|
|
|
|
QWidget::paintEvent(e);
|
|
|
|
|
|
|
|
QPainter painter(this);
|
|
|
|
|
|
|
|
if (m_snapshot.isNull())
|
|
|
|
return;
|
|
|
|
|
2017-05-24 10:56:43 +08:00
|
|
|
const QRect r = rect().marginsRemoved(QMargins(8, 8, 8, 8));
|
2017-05-22 17:17:29 +08:00
|
|
|
|
|
|
|
// draw image
|
2017-05-24 10:56:43 +08:00
|
|
|
const QImage im = m_snapshot.scaled(r.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
2017-05-23 10:52:06 +08:00
|
|
|
const QRect ir = im.rect();
|
|
|
|
const QPoint offset = r.center() - ir.center();
|
|
|
|
painter.drawImage(offset.x(), offset.y(), im);
|
2017-05-22 17:17:29 +08:00
|
|
|
}
|
|
|
|
|
2017-05-22 17:54:59 +08:00
|
|
|
void AppSnapshot::mousePressEvent(QMouseEvent *e)
|
|
|
|
{
|
|
|
|
QWidget::mousePressEvent(e);
|
|
|
|
|
|
|
|
emit clicked(m_wid);
|
|
|
|
}
|