TDE base libraries and programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

krootbacking.cpp 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /***************************************************************************
  2. * Copyright (C) 2011 by Timothy Pearson <kb9vqf@pearsoncomputing.net> *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 2 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
  18. ***************************************************************************/
  19. #include <tqwidget.h>
  20. #include <tqtimer.h>
  21. #include <tqrect.h>
  22. #include <tqimage.h>
  23. #include <tdeapplication.h>
  24. #include <kimageeffect.h>
  25. #include <kpixmapio.h>
  26. #include <twinmodule.h>
  27. #include <twin.h>
  28. #include <kdebug.h>
  29. #include <netwm.h>
  30. #include <dcopclient.h>
  31. #include <dcopref.h>
  32. #include <ksharedpixmap.h>
  33. #include <kstandarddirs.h>
  34. #include <krootbacking.h>
  35. static TQString wallpaperForDesktop(int desktop)
  36. {
  37. return DCOPRef("kdesktop", "KBackgroundIface").call("currentWallpaper", desktop);
  38. }
  39. class KRootBackingData
  40. {
  41. public:
  42. TQWidget *toplevel;
  43. #ifdef Q_WS_X11
  44. KWinModule *twin;
  45. #endif
  46. };
  47. KRootBacking::KRootBacking()
  48. : TQObject(TDEApplication::desktop(), "KRootBacking" ), m_Desk(0), m_timeout(0)
  49. {
  50. enableExports();
  51. init();
  52. }
  53. void KRootBacking::init()
  54. {
  55. d = new KRootBackingData;
  56. m_Fade = 0;
  57. m_pPixmap = new TDESharedPixmap; //ordinary KPixmap on win32
  58. m_pTimer = new TQTimer( this );
  59. m_bInit = false;
  60. m_bActive = false;
  61. connect(kapp, TQT_SIGNAL(backgroundChanged(int)), TQT_SLOT(slotBackgroundChanged(int)));
  62. connect(m_pTimer, TQT_SIGNAL(timeout()), TQT_SLOT(repaint()));
  63. #ifdef Q_WS_X11
  64. connect(m_pPixmap, TQT_SIGNAL(done(bool)), TQT_SLOT(slotDone(bool)));
  65. d->twin = new KWinModule( this );
  66. #endif
  67. m_bInit = true;
  68. }
  69. KRootBacking::~KRootBacking()
  70. {
  71. delete m_pPixmap;
  72. delete d;
  73. }
  74. int KRootBacking::currentDesktop() const
  75. {
  76. #ifdef Q_WS_X11
  77. NETRootInfo rinfo( tqt_xdisplay(), NET::CurrentDesktop );
  78. rinfo.activate();
  79. return rinfo.currentDesktop();
  80. #endif
  81. }
  82. void KRootBacking::start()
  83. {
  84. if (m_bActive)
  85. return;
  86. m_bActive = true;
  87. if ( !isAvailable() )
  88. {
  89. // We should get a KIPC message when the shared pixmap is available...
  90. enableExports();
  91. if (m_timeout < 50) {
  92. TQTimer::singleShot( 100, this, SLOT(show()) ); // ...but it doesn't always work!
  93. m_timeout++;
  94. return;
  95. }
  96. }
  97. if (m_bInit) {
  98. repaint(true);
  99. }
  100. }
  101. void KRootBacking::stop()
  102. {
  103. m_bActive = false;
  104. m_pTimer->stop();
  105. }
  106. void KRootBacking::setFadeEffect(double fade, const TQColor &color)
  107. {
  108. if (fade < 0)
  109. m_Fade = 0;
  110. else if (fade > 1)
  111. m_Fade = 1;
  112. else
  113. m_Fade = fade;
  114. m_FadeColor = color;
  115. if ( m_bActive && m_bInit ) repaint(true);
  116. }
  117. void KRootBacking::repaint()
  118. {
  119. repaint(false);
  120. }
  121. void KRootBacking::repaint(bool force)
  122. {
  123. TQWidget* desktopWidget = TDEApplication::desktop();
  124. TQPoint p1 = desktopWidget->mapToGlobal(desktopWidget->rect().topLeft());
  125. TQPoint p2 = desktopWidget->mapToGlobal(desktopWidget->rect().bottomRight());
  126. if (!force && (m_Rect == TQRect(p1, p2)))
  127. return;
  128. m_Rect = TQRect(p1, p2);
  129. #ifdef Q_WS_X11
  130. m_Desk = currentDesktop();
  131. // TDESharedPixmap will correctly generate a tile for us.
  132. m_pPixmap->loadFromShared(pixmapName(m_Desk), m_Rect);
  133. #else
  134. m_Desk = currentDesktop();
  135. // !x11 note: tile is not generated!
  136. // TODO: pixmapName() is a nonsense now!
  137. m_pPixmap->load( pixmapName(m_Desk) );
  138. if (!m_pPixmap->isNull()) {
  139. m_pPixmap->resize( m_Rect.size() );
  140. slotDone(true);
  141. }
  142. #endif
  143. }
  144. bool KRootBacking::isAvailable() const
  145. {
  146. // #ifdef Q_WS_X11
  147. // return m_pPixmap->isAvailable(pixmapName(m_Desk));
  148. // #else
  149. return m_pPixmap->isNull();
  150. // #endif
  151. }
  152. TQString KRootBacking::pixmapName(int desk) {
  153. TQString pattern = TQString("DESKTOP%1");
  154. #ifdef Q_WS_X11
  155. int screen_number = DefaultScreen(tqt_xdisplay());
  156. if (screen_number) {
  157. pattern = TQString("SCREEN%1-DESKTOP").arg(screen_number) + "%1";
  158. }
  159. #endif
  160. return pattern.arg( desk );
  161. }
  162. void KRootBacking::enableExports()
  163. {
  164. #ifdef Q_WS_X11
  165. kdDebug(270) << k_lineinfo << "activating background exports.\n";
  166. DCOPClient *client = kapp->dcopClient();
  167. if (!client->isAttached())
  168. client->attach();
  169. TQByteArray data;
  170. TQDataStream args( data, IO_WriteOnly );
  171. args << 1;
  172. TQCString appname( "kdesktop" );
  173. int screen_number = DefaultScreen(tqt_xdisplay());
  174. if ( screen_number )
  175. appname.sprintf("kdesktop-screen-%d", screen_number );
  176. client->send( appname, "KBackgroundIface", "setExport(int)", data );
  177. #endif
  178. }
  179. void KRootBacking::slotDone(bool success)
  180. {
  181. if (!success)
  182. {
  183. kdWarning(270) << k_lineinfo << "loading of desktop background failed.\n";
  184. if (m_timeout < 50) {
  185. TQTimer::singleShot( 100, this, SLOT(show()) );
  186. m_timeout++;
  187. return;
  188. }
  189. }
  190. // We need to test active as the pixmap might become available
  191. // after the widget has been destroyed.
  192. if ( m_bActive )
  193. updateBackground( m_pPixmap );
  194. }
  195. void KRootBacking::updateBackground( TDESharedPixmap *spm )
  196. {
  197. TQPixmap pm = *spm;
  198. if (m_Fade > 1e-6)
  199. {
  200. KPixmapIO io;
  201. TQImage img = io.convertToImage(pm);
  202. img = KImageEffect::fade(img, m_Fade, m_FadeColor);
  203. pm = io.convertToPixmap(img);
  204. }
  205. TQString filename = getenv("USER");
  206. filename.prepend("/tmp/tde-");
  207. filename.append("/krootbacking.png");
  208. pm.save(filename, "PNG");
  209. printf("%s\n", filename.ascii()); fflush(stdout);
  210. exit(0);
  211. }
  212. void KRootBacking::slotBackgroundChanged(int desk)
  213. {
  214. if (!m_bInit || !m_bActive)
  215. return;
  216. if (desk == m_Desk)
  217. repaint(true);
  218. }
  219. #include "krootbacking.moc"