Back to index

unity  6.0.0
test-minimize-handler.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Canonical Ltd.
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  *
00018  * Authored By:
00019  * Sam Spilsbury <sam.spilsbury@canonical.com>
00020  */
00021 #ifndef _GNU_SOURCE
00022 #define _GNU_SOURCE
00023 #endif
00024 
00025 #include <minimizedwindowhandler.h>
00026 #include <cstdio>
00027 #include <cstdlib>
00028 #include <iostream>
00029 #include <iomanip>
00030 #include <unistd.h> 
00031 #include <sstream>
00032 #include <poll.h>
00033 #include <cstring>
00034 
00035 #include <x11-window-read-transients.h>
00036 
00037 class X11WindowFakeMinimizable :
00038   public X11WindowReadTransients,
00039   public compiz::WindowInputRemoverLockAcquireInterface
00040 {
00041   public:
00042 
00043     X11WindowFakeMinimizable (Display *, Window id = 0);
00044     ~X11WindowFakeMinimizable ();
00045 
00046     unsigned int id () { return mXid; }
00047     bool         minimized ();
00048 
00049     void minimize ();
00050     void unminimize ();
00051 
00052   private:
00053 
00054     compiz::WindowInputRemoverLock::Ptr GetInputRemover ();
00055 
00056     compiz::WindowInputRemoverLock::Weak input_remover_;
00057     compiz::MinimizedWindowHandler::Ptr mMinimizedHandler;
00058 };
00059 
00060 compiz::WindowInputRemoverLock::Ptr
00061 X11WindowFakeMinimizable::GetInputRemover ()
00062 {
00063   if (!input_remover_.expired ())
00064     return input_remover_.lock ();
00065 
00066   compiz::WindowInputRemoverLock::Ptr ret (new compiz::WindowInputRemoverLock (new compiz::WindowInputRemover (mDpy, mXid)));
00067   input_remover_ = ret;
00068   return ret;
00069 }
00070 
00071 X11WindowFakeMinimizable::X11WindowFakeMinimizable (Display *d, Window id) :
00072   X11WindowReadTransients (d, id)
00073 {
00074 }
00075 
00076 X11WindowFakeMinimizable::~X11WindowFakeMinimizable ()
00077 {
00078 }
00079 
00080 bool
00081 X11WindowFakeMinimizable::minimized ()
00082 {
00083   return mMinimizedHandler.get () != NULL;
00084 }
00085 
00086 void
00087 X11WindowFakeMinimizable::minimize ()
00088 {
00089   if (!mMinimizedHandler)
00090   {
00091     printf ("Fake minimize window 0x%x\n", (unsigned int) mXid);
00092     mMinimizedHandler = compiz::MinimizedWindowHandler::Ptr (new compiz::MinimizedWindowHandler (mDpy, mXid, this));
00093     mMinimizedHandler->minimize ();
00094   }
00095 }
00096 
00097 void
00098 X11WindowFakeMinimizable::unminimize ()
00099 {
00100   if (mMinimizedHandler)
00101   {
00102     printf ("Fake unminimize window 0x%x\n", (unsigned int) mXid);
00103     mMinimizedHandler->unminimize ();
00104     mMinimizedHandler.reset ();
00105   }
00106 }
00107 
00108 void usage ()
00109 {
00110   std::cout << "test-minimize-handler [WINDOW]" << std::endl;
00111   std::cout << "prompt will be for op, where op is one of " << std::endl;
00112   std::cout << "[m] minimize, [u] unminimize [q] to quit" << std::endl;
00113 }
00114 
00115 int main (int argc, char **argv)
00116 {
00117   Display                    *dpy;
00118   Window                     xid = 0;
00119   X11WindowFakeMinimizable                  *window;
00120   X11WindowFakeMinimizable                  *transient = NULL;
00121   X11WindowFakeMinimizable                  *hasClientLeader = NULL;
00122   std::string                option = "";
00123   bool                       shapeExt;
00124   int                        shapeEvent;
00125   int                        shapeError;
00126   bool                       terminate = false;
00127 
00128   if ((argc == 2 && std::string (argv[1]) == "--help") || argc > 3)
00129   {
00130     usage ();
00131     return 1;
00132   }
00133 
00134   dpy = XOpenDisplay (NULL);
00135 
00136   if (!dpy)
00137   {
00138     std::cerr << "Failed to open display ... setting test to passed" << std::endl;
00139     return 0;
00140   }
00141 
00142   shapeExt = XShapeQueryExtension (dpy, &shapeEvent, &shapeError);
00143 
00144   if (!shapeExt)
00145   {
00146     std::cerr << "No shape extension .. setting test to passed" << std::endl;
00147     XCloseDisplay (dpy);
00148     return 0;
00149   }
00150 
00151   if (argc > 1)
00152     std::stringstream (argv[1]) >> std::hex >> xid;
00153 
00154   window = new X11WindowFakeMinimizable (dpy, xid);
00155 
00156   if (!xid)
00157   {
00158     transient = new X11WindowFakeMinimizable (dpy, 0);
00159     hasClientLeader = new X11WindowFakeMinimizable (dpy, 0);
00160 
00161     transient->makeTransientFor (window);
00162     window->setClientLeader (window);
00163     hasClientLeader->setClientLeader (window);
00164   }
00165 
00166   std::cout << "[m]inimize [u]nminimize [q]uit?" << std::endl;
00167 
00168   do
00169   {
00170     struct pollfd pfd[2];
00171 
00172     pfd[0].fd = ConnectionNumber (dpy);
00173     pfd[0].events = POLLIN;
00174     pfd[0].revents = 0;
00175     pfd[1].fd = STDIN_FILENO;
00176     pfd[1].events = POLLIN;
00177     pfd[1].revents = 0;
00178 
00179     poll (pfd, 2, -1);
00180 
00181     while (XPending (dpy))
00182     {
00183       XEvent ev;
00184       XNextEvent (dpy, &ev);
00185     }
00186 
00187     if (pfd[1].revents == POLLIN)
00188     {
00189       char buf[2];
00190 
00191       int ret = read (STDIN_FILENO, buf, 1);
00192 
00193       if (ret != EAGAIN || ret != EWOULDBLOCK)
00194       {
00195         buf[1] = '\0';
00196         if (strncmp (buf, "m", 1) == 0)
00197         {
00198           window->minimize ();
00199           std::cout << "[m]inimize [u]nminimize [q]uit?" << std::endl;
00200         }
00201         else if (strncmp (buf, "u", 1) == 0)
00202         {
00203           window->unminimize ();
00204           std::cout << "[m]inimize [u]nminimize [q]uit?" << std::endl;
00205         }
00206         else if (strncmp (buf, "q", 1) == 0)
00207         {
00208           terminate = true;
00209           std::cout << "[m]inimize [u]nminimize [q]uit?" << std::endl;
00210         }
00211       }
00212     }
00213 
00214   } while (!terminate);
00215 
00216   delete window;
00217 
00218   XCloseDisplay (dpy);
00219 
00220   return 0;
00221 }