| 1 | /* ======================================================================== *\ | 
|---|
| 2 | ! | 
|---|
| 3 | ! * | 
|---|
| 4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction | 
|---|
| 5 | ! * Software. It is distributed to you in the hope that it can be a useful | 
|---|
| 6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes. | 
|---|
| 7 | ! * It is distributed WITHOUT ANY WARRANTY. | 
|---|
| 8 | ! * | 
|---|
| 9 | ! * Permission to use, copy, modify and distribute this software and its | 
|---|
| 10 | ! * documentation for any purpose is hereby granted without fee, | 
|---|
| 11 | ! * provided that the above copyright notice appear in all copies and | 
|---|
| 12 | ! * that both that copyright notice and this permission notice appear | 
|---|
| 13 | ! * in supporting documentation. It is provided "as is" without express | 
|---|
| 14 | ! * or implied warranty. | 
|---|
| 15 | ! * | 
|---|
| 16 | ! | 
|---|
| 17 | ! | 
|---|
| 18 | !   Author(s): Thomas Bretz  05/2002 <mailto:tbretz@astro.uni-wuerzburg.de> | 
|---|
| 19 | ! | 
|---|
| 20 | !   Copyright: MAGIC Software Development, 2000-2002 | 
|---|
| 21 | ! | 
|---|
| 22 | ! | 
|---|
| 23 | \* ======================================================================== */ | 
|---|
| 24 |  | 
|---|
| 25 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 26 | //                                                                          // | 
|---|
| 27 | // MLogo                                                                    // | 
|---|
| 28 | //                                                                          // | 
|---|
| 29 | // X based logo displayer. Displays a given logo after a call to Popup()    // | 
|---|
| 30 | // until Popdown() or the destructor is called, but for a minimum of a      // | 
|---|
| 31 | // given number of milliseconds.                                            // | 
|---|
| 32 | //                                                                          // | 
|---|
| 33 | ////////////////////////////////////////////////////////////////////////////// | 
|---|
| 34 | #ifdef HAVE_XPM | 
|---|
| 35 | #include "MLogo.h" | 
|---|
| 36 |  | 
|---|
| 37 | #include <unistd.h>    // usleep | 
|---|
| 38 | #include <iostream.h>  // cout | 
|---|
| 39 | #include <sys/time.h>  // gettimeofday | 
|---|
| 40 |  | 
|---|
| 41 | #ifndef XpmSuccess | 
|---|
| 42 | #define XpmSuccess       0 | 
|---|
| 43 | #endif | 
|---|
| 44 | #ifndef XpmColorError | 
|---|
| 45 | #define XpmColorError    1 | 
|---|
| 46 | #endif | 
|---|
| 47 |  | 
|---|
| 48 | MLogo::MLogo(int millisec) | 
|---|
| 49 | : fDisplay(0), fLogoWindow(0), fLogoPixmap(0), fMilliSec(millisec) | 
|---|
| 50 | { | 
|---|
| 51 | // millisec: time of minimum stayup | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | void MLogo::Wait() const | 
|---|
| 55 | { | 
|---|
| 56 | struct timeval tv; | 
|---|
| 57 |  | 
|---|
| 58 | gettimeofday(&tv, 0); | 
|---|
| 59 |  | 
|---|
| 60 | tv.tv_sec  -= fPopupTime.tv_sec; | 
|---|
| 61 | tv.tv_usec -= fPopupTime.tv_usec; | 
|---|
| 62 |  | 
|---|
| 63 | while (tv.tv_usec < 0) | 
|---|
| 64 | { | 
|---|
| 65 | tv.tv_usec += 1000000; | 
|---|
| 66 | tv.tv_sec--; | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | while (tv.tv_sec > 0) | 
|---|
| 70 | tv.tv_usec += 1000000; | 
|---|
| 71 |  | 
|---|
| 72 | long sleep = fMilliSec*1000-tv.tv_usec; | 
|---|
| 73 |  | 
|---|
| 74 | if (sleep <= 0) | 
|---|
| 75 | return; | 
|---|
| 76 |  | 
|---|
| 77 | usleep(sleep); | 
|---|
| 78 | } | 
|---|
| 79 |  | 
|---|
| 80 | Pixmap MLogo::GetLogo() const | 
|---|
| 81 | { | 
|---|
| 82 | #ifdef XpmVersion | 
|---|
| 83 | int depth = PlanesOfScreen(XDefaultScreenOfDisplay(fDisplay)); | 
|---|
| 84 |  | 
|---|
| 85 | if (depth <= 1) | 
|---|
| 86 | return 0; | 
|---|
| 87 |  | 
|---|
| 88 | XWindowAttributes win_attr; | 
|---|
| 89 | XGetWindowAttributes(fDisplay, fLogoWindow, &win_attr); | 
|---|
| 90 |  | 
|---|
| 91 | XpmAttributes attr; | 
|---|
| 92 | attr.valuemask = XpmVisual | XpmColormap | XpmDepth; | 
|---|
| 93 | attr.visual    = win_attr.visual; | 
|---|
| 94 | attr.colormap  = win_attr.colormap; | 
|---|
| 95 | attr.depth     = win_attr.depth; | 
|---|
| 96 |  | 
|---|
| 97 | #ifdef XpmColorKey              // Not available in XPM 3.2 and earlier | 
|---|
| 98 |  | 
|---|
| 99 | switch (depth) | 
|---|
| 100 | { | 
|---|
| 101 | case 0: | 
|---|
| 102 | attr.valuemask &= ~XpmColorKey; | 
|---|
| 103 | break; | 
|---|
| 104 | case 1: | 
|---|
| 105 | attr.color_key = XPM_MONO; | 
|---|
| 106 | break; | 
|---|
| 107 | case 2: | 
|---|
| 108 | attr.color_key = XPM_GRAY; | 
|---|
| 109 | break; | 
|---|
| 110 | case 3: | 
|---|
| 111 | case 4: | 
|---|
| 112 | attr.color_key = XPM_GRAY4; | 
|---|
| 113 | break; | 
|---|
| 114 | default: | 
|---|
| 115 | attr.color_key = XPM_COLOR; | 
|---|
| 116 | } | 
|---|
| 117 | #endif // defined(XpmColorKey) | 
|---|
| 118 |  | 
|---|
| 119 |  | 
|---|
| 120 | Pixmap logo; | 
|---|
| 121 | #ifdef USE_MAGICLOGO | 
|---|
| 122 | #include "../magiclogo.xpm" | 
|---|
| 123 | int ret = XpmCreatePixmapFromData(fDisplay, fLogoWindow, | 
|---|
| 124 | mag1, &logo, 0, &attr); | 
|---|
| 125 | #else | 
|---|
| 126 | #include "../marslogo.xpm" | 
|---|
| 127 | int ret = XpmCreatePixmapFromData(fDisplay, fLogoWindow, | 
|---|
| 128 | marslogo, &logo, 0, &attr); | 
|---|
| 129 | #endif | 
|---|
| 130 | XpmFreeAttributes(&attr); | 
|---|
| 131 |  | 
|---|
| 132 | if (ret == XpmSuccess || ret == XpmColorError) | 
|---|
| 133 | return logo; | 
|---|
| 134 |  | 
|---|
| 135 | if (logo) | 
|---|
| 136 | XFreePixmap(fDisplay, logo); | 
|---|
| 137 | #endif | 
|---|
| 138 | return 0; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | void MLogo::Popup() | 
|---|
| 142 | { | 
|---|
| 143 | #ifdef XpmVersion | 
|---|
| 144 | if (fLogoWindow || fLogoPixmap || fDisplay) | 
|---|
| 145 | #endif | 
|---|
| 146 | return; | 
|---|
| 147 |  | 
|---|
| 148 | fDisplay = XOpenDisplay(""); | 
|---|
| 149 | if (!fDisplay) | 
|---|
| 150 | return; | 
|---|
| 151 |  | 
|---|
| 152 | int screen = DefaultScreen(fDisplay); | 
|---|
| 153 |  | 
|---|
| 154 | fLogoWindow = XCreateSimpleWindow(fDisplay, DefaultRootWindow(fDisplay), | 
|---|
| 155 | -100, -100, 50, 50, 0, | 
|---|
| 156 | BlackPixel(fDisplay, screen), | 
|---|
| 157 | WhitePixel(fDisplay, screen)); | 
|---|
| 158 | fLogoPixmap = GetLogo(); | 
|---|
| 159 |  | 
|---|
| 160 | Window root; | 
|---|
| 161 | int x, y; | 
|---|
| 162 | unsigned int w, h, bw, depth; | 
|---|
| 163 | XGetGeometry(fDisplay, fLogoPixmap, | 
|---|
| 164 | &root, &x, &y, &w, &h, &bw, &depth); | 
|---|
| 165 |  | 
|---|
| 166 | Screen *xscreen = XDefaultScreenOfDisplay(fDisplay); | 
|---|
| 167 | x = (WidthOfScreen(xscreen) - w) / 2; | 
|---|
| 168 | y = (HeightOfScreen(xscreen) - h) / 2; | 
|---|
| 169 |  | 
|---|
| 170 | XMoveResizeWindow(fDisplay, fLogoWindow, x, y, w, h); | 
|---|
| 171 | XSync(fDisplay, False);   // make sure move & resize is done before mapping | 
|---|
| 172 |  | 
|---|
| 173 | unsigned long valmask  = CWBackPixmap | CWOverrideRedirect; | 
|---|
| 174 |  | 
|---|
| 175 | XSetWindowAttributes xswa; | 
|---|
| 176 | xswa.background_pixmap = fLogoPixmap; | 
|---|
| 177 | xswa.override_redirect = True; | 
|---|
| 178 | XChangeWindowAttributes(fDisplay, fLogoWindow, valmask, &xswa); | 
|---|
| 179 |  | 
|---|
| 180 | XMapRaised(fDisplay, fLogoWindow); | 
|---|
| 181 | XSync(fDisplay, False); | 
|---|
| 182 |  | 
|---|
| 183 | gettimeofday(&fPopupTime, 0); | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | void MLogo::Popdown() | 
|---|
| 187 | { | 
|---|
| 188 | if (fLogoWindow && fLogoPixmap && fDisplay) | 
|---|
| 189 | Wait(); | 
|---|
| 190 |  | 
|---|
| 191 | if (fLogoWindow) | 
|---|
| 192 | { | 
|---|
| 193 | XUnmapWindow(fDisplay, fLogoWindow); | 
|---|
| 194 | XDestroyWindow(fDisplay, fLogoWindow); | 
|---|
| 195 | fLogoWindow = 0; | 
|---|
| 196 | } | 
|---|
| 197 | if (fLogoPixmap) | 
|---|
| 198 | { | 
|---|
| 199 | XFreePixmap(fDisplay, fLogoPixmap); | 
|---|
| 200 | fLogoPixmap = 0; | 
|---|
| 201 | } | 
|---|
| 202 | if (fDisplay) | 
|---|
| 203 | { | 
|---|
| 204 | XSync(fDisplay, False); | 
|---|
| 205 | XCloseDisplay(fDisplay); | 
|---|
| 206 | fDisplay = 0; | 
|---|
| 207 | } | 
|---|
| 208 | } | 
|---|
| 209 | #endif | 
|---|