Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #--- scrns1.pro ---#
- QT += core gui
- TARGET = scrns1
- CONFIG += console
- CONFIG -= app_bundle
- TEMPLATE = app
- SOURCES += main.cpp
- INCLUDEPATH += /usr/include/KDE
- LIBS += -lkdecore -lkdeui -lX11 -lXrender -lXcomposite
- //--- main.cpp ---//
- #include <QApplication>
- #include <QTimer>
- #include <QPixmap>
- #include <QPainter>
- #include <QIODevice>
- #include <QFile>
- #include <QWidget>
- #include <QBuffer>
- #include <KWindowSystem>
- #include <KWindowInfo>
- #include <iostream>
- #include <string>
- #include <X11/Xlib.h>
- #include <X11/Xfuncproto.h>
- #include <X11/X.h>
- #include <X11/extensions/Xcomposite.h>
- #include <X11/extensions/Xrender.h>
- #include <time.h>
- class MyWidget : public QWidget {
- Q_OBJECT
- private:
- QList<QPixmap> images;
- public:
- explicit MyWidget(QWidget *parent = 0) : QWidget(parent) {
- QTimer::singleShot(0, this, SLOT(run()));
- }
- ~MyWidget(){
- }
- void paintEvent(QPaintEvent* evt){
- for (int i = 0; i < images.size(); ++i) {
- const QPixmap image = images.at(i);
- if (!image.isNull()){
- QPainter painter(this);
- QRect geo = this->geometry();
- int width = geo.width()/images.size();
- painter.drawPixmap(width*i, 0, image.scaledToWidth(width));
- }
- }
- }
- void addPixmap(QPixmap image){
- this->images.append(image.copy());
- }
- };
- class MyWid
- {
- public:
- MyWid(WId wid){
- this->wid = wid;
- }
- bool operator < (const MyWid &other) const
- {
- //return ( wid < other.wid);
- return ( wid > other.wid);
- }
- WId wid;
- };
- class Task : public QObject
- {
- Q_OBJECT
- public:
- Task(QObject *parent = 0) : QObject(parent) {}
- private:
- QPixmap makeScreenshot(XID xid, Display *display){
- int event_base_return;
- int error_base_return;
- if (XCompositeQueryExtension (display, &event_base_return, &error_base_return)){
- std::cout << "COMPOSITE IS ENABLED!\n";
- }
- XCompositeRedirectWindow (display, xid, CompositeRedirectAutomatic);
- Pixmap pixmap = XCompositeNameWindowPixmap (display, xid);
- XWindowAttributes attr;
- Status s = XGetWindowAttributes (display, xid, &attr);
- if (s == 0){
- std::cout << "Fail to get window attributes!\n";
- }
- XRenderPictFormat *format = XRenderFindVisualFormat (display, attr.visual);
- XRenderPictureAttributes pa;
- pa.subwindow_mode = IncludeInferiors;
- Picture picture = XRenderCreatePicture (display, xid, format, CPSubwindowMode, &pa);
- XRenderComposite (display, PictOpSrc, picture, None, pixmap, 0,0, 0,0, 0,0, attr.width, attr.height);
- QPixmap qpxmp = QPixmap::fromX11Pixmap(pixmap).copy();
- XFreePixmap (display, pixmap);
- XCompositeUnredirectWindow (display, xid, CompositeRedirectAutomatic);
- return qpxmp;
- }
- public slots:
- void run()
- {
- MyWidget* wdg = new MyWidget;
- wdg->show();
- Display* display = XOpenDisplay(NULL);
- if (!display) {
- std::cout << "display was not opened\n";
- } else {
- const QList<WId> wndws = KWindowSystem::windows();
- QList<MyWid> mywids;
- for (int i = 0; i < wndws.size(); i++){
- WId wid = wndws.at(i);
- mywids.append(MyWid(wid));
- }
- qSort(mywids);
- for (int i = 0; i < mywids.size(); i++){
- MyWid wid = mywids.at(i);
- std::cout << wid.wid << "\n";
- }
- for (int i = 0; i < mywids.size(); i++){
- const WId wid = mywids.at(i).wid;
- KWindowInfo info(wid, NET::WMName);
- if (info.name().indexOf("Dolphin") > -1){
- QPixmap p = makeScreenshot(wid, display);
- wdg->addPixmap(p);
- }
- }
- XCloseDisplay(display);
- }
- }
- signals:
- void finished();
- };
- #include "main.moc"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Task *task = new Task(&a);
- QObject::connect(task, SIGNAL(finished()), &a, SLOT(quit()));
- QTimer::singleShot(0, task, SLOT(run()));
- return a.exec();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement