Advertisement
Guest User

XWindow screenshot

a guest
Feb 25th, 2015
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.37 KB | None | 0 0
  1. #--- scrns1.pro ---#
  2.  
  3. QT       += core gui
  4.  
  5. TARGET = scrns1
  6. CONFIG   += console
  7. CONFIG   -= app_bundle
  8.  
  9. TEMPLATE = app
  10.  
  11.  
  12. SOURCES += main.cpp
  13.  
  14. INCLUDEPATH += /usr/include/KDE
  15. LIBS += -lkdecore -lkdeui -lX11 -lXrender -lXcomposite
  16.  
  17.  
  18. //--- main.cpp ---//
  19. #include <QApplication>
  20. #include <QTimer>
  21. #include <QPixmap>
  22. #include <QPainter>
  23. #include <QIODevice>
  24. #include <QFile>
  25. #include <QWidget>
  26. #include <QBuffer>
  27. #include <KWindowSystem>
  28. #include <KWindowInfo>
  29. #include <iostream>
  30. #include <string>
  31. #include <X11/Xlib.h>
  32. #include <X11/Xfuncproto.h>
  33. #include <X11/X.h>
  34. #include <X11/extensions/Xcomposite.h>
  35. #include <X11/extensions/Xrender.h>
  36. #include <time.h>
  37.  
  38. class MyWidget : public QWidget {
  39.     Q_OBJECT
  40.  
  41. private:
  42.      QList<QPixmap> images;
  43. public:
  44.     explicit MyWidget(QWidget *parent = 0) : QWidget(parent) {
  45.          QTimer::singleShot(0, this, SLOT(run()));
  46.     }
  47.     ~MyWidget(){
  48.     }
  49.     void paintEvent(QPaintEvent* evt){
  50.         for (int i = 0; i < images.size(); ++i) {
  51.             const QPixmap image = images.at(i);
  52.             if (!image.isNull()){
  53.                 QPainter painter(this);
  54.                 QRect geo = this->geometry();
  55.                 int width = geo.width()/images.size();
  56.                 painter.drawPixmap(width*i, 0, image.scaledToWidth(width));
  57.             }
  58.         }
  59.     }
  60.     void addPixmap(QPixmap image){
  61.         this->images.append(image.copy());
  62.     }
  63. };
  64.  
  65. class MyWid
  66. {
  67.     public:
  68.         MyWid(WId wid){
  69.             this->wid = wid;
  70.         }
  71.  
  72.         bool operator < (const MyWid &other) const
  73.         {
  74.             //return ( wid < other.wid);
  75.             return ( wid > other.wid);
  76.         }
  77.         WId wid;
  78.  
  79. };
  80.  
  81. class Task : public QObject
  82. {
  83.     Q_OBJECT
  84. public:
  85.     Task(QObject *parent = 0) : QObject(parent) {}
  86.  
  87. private:
  88.     QPixmap makeScreenshot(XID xid, Display *display){
  89.         int event_base_return;
  90.         int error_base_return;
  91.         if (XCompositeQueryExtension (display, &event_base_return, &error_base_return)){
  92.             std::cout << "COMPOSITE IS ENABLED!\n";
  93.         }
  94.         XCompositeRedirectWindow (display, xid, CompositeRedirectAutomatic);
  95.         Pixmap pixmap = XCompositeNameWindowPixmap (display, xid);
  96.         XWindowAttributes attr;
  97.         Status s = XGetWindowAttributes (display, xid, &attr);
  98.         if (s == 0){
  99.             std::cout << "Fail to get window attributes!\n";
  100.         }
  101.         XRenderPictFormat *format = XRenderFindVisualFormat (display, attr.visual);
  102.         XRenderPictureAttributes pa;
  103.         pa.subwindow_mode = IncludeInferiors;
  104.         Picture picture = XRenderCreatePicture (display, xid, format, CPSubwindowMode, &pa);
  105.         XRenderComposite (display, PictOpSrc, picture, None, pixmap, 0,0, 0,0, 0,0, attr.width, attr.height);
  106.         QPixmap qpxmp = QPixmap::fromX11Pixmap(pixmap).copy();
  107.  
  108.         XFreePixmap (display, pixmap);
  109.         XCompositeUnredirectWindow (display, xid, CompositeRedirectAutomatic);
  110.         return qpxmp;
  111.     }
  112.  
  113.  
  114.  
  115. public slots:
  116.     void run()
  117.     {
  118.         MyWidget* wdg = new MyWidget;
  119.         wdg->show();
  120.  
  121.         Display* display = XOpenDisplay(NULL);
  122.         if (!display) {
  123.             std::cout << "display was not opened\n";
  124.         } else {
  125.             const QList<WId> wndws = KWindowSystem::windows();
  126.             QList<MyWid> mywids;
  127.             for (int i = 0; i < wndws.size(); i++){
  128.                 WId wid = wndws.at(i);
  129.                 mywids.append(MyWid(wid));
  130.             }
  131.  
  132.             qSort(mywids);
  133.  
  134.             for (int i = 0; i < mywids.size(); i++){
  135.                 MyWid wid = mywids.at(i);
  136.                 std::cout << wid.wid << "\n";
  137.             }
  138.  
  139.             for (int i = 0; i < mywids.size(); i++){
  140.                 const WId wid = mywids.at(i).wid;
  141.                 KWindowInfo info(wid, NET::WMName);
  142.                 if (info.name().indexOf("Dolphin") > -1){
  143.                     QPixmap p = makeScreenshot(wid, display);
  144.                     wdg->addPixmap(p);
  145.                 }
  146.             }
  147.             XCloseDisplay(display);
  148.         }
  149.     }
  150.  
  151. signals:
  152.     void finished();
  153. };
  154.  
  155. #include "main.moc"
  156.  
  157. int main(int argc, char *argv[])
  158. {
  159.     QApplication a(argc, argv);
  160.     Task *task = new Task(&a);
  161.     QObject::connect(task, SIGNAL(finished()), &a, SLOT(quit()));
  162.     QTimer::singleShot(0, task, SLOT(run()));
  163.     return a.exec();
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement