Advertisement
Guest User

Qt Xlib Screenshot

a guest
Feb 27th, 2015
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.26 KB | None | 0 0
  1. #--- pro file ---#
  2. #-------------------------------------------------
  3. #
  4. # Project created by QtCreator 2015-02-20T11:34:42
  5. #
  6. #-------------------------------------------------
  7.  
  8. QT       += core gui
  9.  
  10. TARGET = scrns1
  11. CONFIG   += console
  12. CONFIG   -= app_bundle
  13.  
  14. TEMPLATE = app
  15.  
  16.  
  17. SOURCES += main.cpp
  18.  
  19. INCLUDEPATH += /usr/include/KDE
  20. LIBS += -lkdecore -lkdeui -lX11 -lXrender -lXcomposite -lXdamage
  21.  
  22. HEADERS +=
  23. #-------------------------------------------------
  24. #
  25. # Project created by QtCreator 2015-02-20T11:34:42
  26. #
  27. #-------------------------------------------------
  28.  
  29. QT       += core gui
  30.  
  31. TARGET = scrns1
  32. CONFIG   += console
  33. CONFIG   -= app_bundle
  34.  
  35. TEMPLATE = app
  36.  
  37.  
  38. SOURCES += main.cpp
  39.  
  40. INCLUDEPATH += /usr/include/KDE
  41. LIBS += -lkdecore -lkdeui -lX11 -lXrender -lXcomposite -lXdamage
  42.  
  43. HEADERS +=
  44.  
  45. //--- main.cpp ---//
  46. #include <QApplication>
  47. #include <QTimer>
  48. #include <QPixmap>
  49. #include <QPainter>
  50. #include <QIODevice>
  51. #include <QFile>
  52. #include <QMap>
  53. #include <QWidget>
  54. #include <QBuffer>
  55. #include <KWindowSystem>
  56. #include <KWindowInfo>
  57. #include <iostream>
  58. #include <string>
  59. #include <X11/Xlib.h>
  60. #include <X11/Xfuncproto.h>
  61. #include <X11/X.h>
  62. #include <X11/extensions/Xcomposite.h>
  63. #include <X11/extensions/Xrender.h>
  64. #include <X11/extensions/Xdamage.h>
  65. #include <time.h>
  66. #include <QGraphicsBlurEffect>
  67. #include <unistd.h>
  68.  
  69. class MyWidget : public QWidget {
  70.     Q_OBJECT
  71.  
  72. private:
  73.      QMap<WId, QPixmap> images;
  74. public:
  75.     explicit MyWidget(QWidget *parent = 0) : QWidget(parent) {
  76.          QTimer::singleShot(0, this, SLOT(run()));
  77.     }
  78.     ~MyWidget(){
  79.     }
  80.     void paintEvent(QPaintEvent* evt){
  81.         QRect geo = this->geometry();
  82.         QPainter painter(this);
  83.         int width = geo.width();
  84.         int height = geo.height();
  85.         QList<WId> wids = images.keys();
  86.         if (wids.size() == 1){
  87.             const WId wid = wids.at(0);
  88.             QPixmap p = images.value(wid);
  89.             painter.drawPixmap(0, 0, width, height, p);
  90.         } else {
  91.             int i = 0;
  92.             int num = 1;
  93.             for (; num*num < wids.size(); num++){
  94.                 //
  95.             }
  96.             int w = width /num;
  97.             int h = height/num;
  98.             for (int row = 0; row < num; row++){
  99.                 for (int col = 0; col < num; col++){
  100.                     std::cout << col << "x" << row << "\n";
  101.                     if (wids.size() > i){
  102.                         const WId wid = wids.at(i);
  103.                         QPixmap p = images.value(wid);
  104.  
  105.                         if (!p.isNull()){
  106.                             painter.drawPixmap(col*w, row*h, w, h, p);
  107.                         } else {
  108.                             std::cout << "SKIP\n";
  109.                         }
  110.  
  111.                         KWindowInfo info(wid, NET::WMName | NET::WMState | NET::XAWMState);
  112.                         painter.setPen(QColor("red"));
  113.                         painter.drawText(col*w, row*h + h*0.25, QString::number(wid));
  114.                         painter.drawText(col*w, row*h + h*0.5, QString::number(col) + "x" + QString::number(row));
  115.                         painter.drawText(col*w, row*h + h*0.75, info.name());
  116.                     }
  117.                     i++;
  118.                 }
  119.             }
  120.         }
  121.     }
  122.     void clearPixmap(){
  123.         this->images.clear();
  124.     }
  125.  
  126.     void addPixmap(WId wid, QPixmap image){
  127.         this->images.insert(wid, image);
  128.     }
  129. };
  130.  
  131. class Task : public QObject
  132. {
  133.     Q_OBJECT
  134. public:
  135.     Task(QObject *parent = 0) : QObject(parent) {
  136.         display = XOpenDisplay(NULL);
  137.     }
  138.     ~Task(){
  139.         XCloseDisplay(display);
  140.     }
  141.     Display* display;
  142.     MyWidget* wdg;
  143.  
  144. private:
  145. QPixmap makeScreenshot(XID xid, Display *display){
  146.     QPixmap qpxmp;
  147.     XCompositeRedirectWindow (display, xid, CompositeRedirectAutomatic);
  148.     XFlush(display);
  149.     Pixmap pixmap = XCompositeNameWindowPixmap (display, xid);
  150.     XSync(display, false);
  151.     qpxmp = QPixmap::fromX11Pixmap(pixmap).copy();
  152.     XFreePixmap (display, pixmap);
  153.     XCompositeUnredirectWindow (display, xid, CompositeRedirectAutomatic);
  154.     return qpxmp;
  155. }
  156.  
  157. public slots:
  158.     void update(){
  159.         std::cout << "update\n";
  160.         if (!wdg){
  161.             std::cout << "no widget\n";
  162.         } else {
  163.             wdg->clearPixmap();
  164.             if (!display) {
  165.                 std::cout << "display was not opened\n";
  166.             } else {
  167.                 const QList<WId> wndws = KWindowSystem::windows();
  168.                 for (int i = 0; i < wndws.size(); i++){
  169.                     const WId wid = wndws.at(i);
  170.                     KWindowInfo info(wid, NET::WMName | NET::WMState | NET::XAWMState | NET::WMDesktop | NET::WMGeometry);
  171.                     if (!info.isOnCurrentDesktop()){
  172.                         QRect geo = info.geometry();
  173.                         QPixmap p(10, (geo.height() * 10) / geo.width());
  174.                         p.fill(Qt::gray);
  175.                         wdg->addPixmap(wid, p);
  176.                     } else if (info.name().indexOf("plasma") >= 0){
  177.                         //do nothing
  178.                     } else if (info.valid() && info.isMinimized()){
  179.                         QRect geo = info.geometry();
  180.                         QPixmap p(10, (geo.height() * 10) / geo.width());
  181.                         p.fill(Qt::gray);
  182.                         wdg->addPixmap(wid, p);
  183.                     } else {
  184.                         std::cout << wid << " = " << info.name().toStdString() << "\n";
  185.                         QPixmap p = makeScreenshot(wid, display);
  186.                         p = makeScreenshot(wid, display);
  187.                         wdg->addPixmap(wid, p);
  188.                     }
  189.                 }
  190.                 wdg->repaint();
  191.             }
  192.         }
  193.     }
  194.  
  195.     void run()
  196.     {
  197.         wdg = new MyWidget;
  198.         wdg->show();
  199.         QTimer *timer = new QTimer(this);
  200.         connect(timer, SIGNAL(timeout()), this, SLOT(update()));
  201.         timer->start(1000);
  202.     }
  203.  
  204. signals:
  205.     void finished();
  206. };
  207.  
  208. #include "main.moc"
  209.  
  210. int main(int argc, char *argv[])
  211. {
  212.     QApplication a(argc, argv);
  213.     Task *task = new Task(&a);
  214.     QObject::connect(task, SIGNAL(finished()), &a, SLOT(quit()));
  215.     QTimer::singleShot(0, task, SLOT(run()));
  216.     return a.exec();
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement