source: trunk/MagicSoft/Mars/mbase/MLogo.cc@ 1492

Last change on this file since 1492 was 1337, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 5.5 KB
Line 
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#include "MLogo.h"
35
36#include <unistd.h> // usleep
37#include <iostream.h> // cout
38#include <sys/time.h> // gettimeofday
39
40#ifndef XpmSuccess
41#define XpmSuccess 0
42#endif
43#ifndef XpmColorError
44#define XpmColorError 1
45#endif
46
47MLogo::MLogo(int millisec)
48 : fDisplay(0), fLogoWindow(0), fLogoPixmap(0), fMilliSec(millisec)
49{
50 // millisec: time of minimum stayup
51}
52
53void MLogo::Wait() const
54{
55 struct timeval tv;
56
57 gettimeofday(&tv, 0);
58
59 tv.tv_sec -= fPopupTime.tv_sec;
60 tv.tv_usec -= fPopupTime.tv_usec;
61
62 while (tv.tv_usec < 0)
63 {
64 tv.tv_usec += 1000000;
65 tv.tv_sec--;
66 }
67
68 while (tv.tv_sec > 0)
69 tv.tv_usec += 1000000;
70
71 long sleep = fMilliSec*1000-tv.tv_usec;
72
73 if (sleep <= 0)
74 return;
75
76 usleep(sleep);
77}
78
79Pixmap MLogo::GetLogo() const
80{
81#ifdef XpmVersion
82 int depth = PlanesOfScreen(XDefaultScreenOfDisplay(fDisplay));
83
84 if (depth <= 1)
85 return 0;
86
87 XWindowAttributes win_attr;
88 XGetWindowAttributes(fDisplay, fLogoWindow, &win_attr);
89
90 XpmAttributes attr;
91 attr.valuemask = XpmVisual | XpmColormap | XpmDepth;
92 attr.visual = win_attr.visual;
93 attr.colormap = win_attr.colormap;
94 attr.depth = win_attr.depth;
95
96#ifdef XpmColorKey // Not available in XPM 3.2 and earlier
97
98 switch (depth)
99 {
100 case 0:
101 attr.valuemask &= ~XpmColorKey;
102 break;
103 case 1:
104 attr.color_key = XPM_MONO;
105 break;
106 case 2:
107 attr.color_key = XPM_GRAY;
108 break;
109 case 3:
110 case 4:
111 attr.color_key = XPM_GRAY4;
112 break;
113 default:
114 attr.color_key = XPM_COLOR;
115 }
116#endif // defined(XpmColorKey)
117
118
119 Pixmap logo;
120#ifdef USE_MAGICLOGO
121#include "../magiclogo.xpm"
122 int ret = XpmCreatePixmapFromData(fDisplay, fLogoWindow,
123 mag1, &logo, 0, &attr);
124#else
125#include "../marslogo_neu.xpm"
126 int ret = XpmCreatePixmapFromData(fDisplay, fLogoWindow,
127 marslogo_neu_xpm, &logo, 0, &attr);
128#endif
129 XpmFreeAttributes(&attr);
130
131 if (ret == XpmSuccess || ret == XpmColorError)
132 return logo;
133
134 if (logo)
135 XFreePixmap(fDisplay, logo);
136#endif
137 return 0;
138}
139
140void MLogo::Popup()
141{
142#ifdef XpmVersion
143 if (fLogoWindow || fLogoPixmap || fDisplay)
144#endif
145 return;
146
147 fDisplay = XOpenDisplay("");
148 if (!fDisplay)
149 return;
150
151 int screen = DefaultScreen(fDisplay);
152
153 fLogoWindow = XCreateSimpleWindow(fDisplay, DefaultRootWindow(fDisplay),
154 -100, -100, 50, 50, 0,
155 BlackPixel(fDisplay, screen),
156 WhitePixel(fDisplay, screen));
157 fLogoPixmap = GetLogo();
158
159 Window root;
160 int x, y;
161 unsigned int w, h, bw, depth;
162 XGetGeometry(fDisplay, fLogoPixmap,
163 &root, &x, &y, &w, &h, &bw, &depth);
164
165 Screen *xscreen = XDefaultScreenOfDisplay(fDisplay);
166 x = (WidthOfScreen(xscreen) - w) / 2;
167 y = (HeightOfScreen(xscreen) - h) / 2;
168
169 XMoveResizeWindow(fDisplay, fLogoWindow, x, y, w, h);
170 XSync(fDisplay, False); // make sure move & resize is done before mapping
171
172 unsigned long valmask = CWBackPixmap | CWOverrideRedirect;
173
174 XSetWindowAttributes xswa;
175 xswa.background_pixmap = fLogoPixmap;
176 xswa.override_redirect = True;
177 XChangeWindowAttributes(fDisplay, fLogoWindow, valmask, &xswa);
178
179 XMapRaised(fDisplay, fLogoWindow);
180 XSync(fDisplay, False);
181
182 gettimeofday(&fPopupTime, 0);
183}
184
185void MLogo::Popdown()
186{
187 if (fLogoWindow && fLogoPixmap && fDisplay)
188 Wait();
189
190 if (fLogoWindow)
191 {
192 XUnmapWindow(fDisplay, fLogoWindow);
193 XDestroyWindow(fDisplay, fLogoWindow);
194 fLogoWindow = 0;
195 }
196 if (fLogoPixmap)
197 {
198 XFreePixmap(fDisplay, fLogoPixmap);
199 fLogoPixmap = 0;
200 }
201 if (fDisplay)
202 {
203 XSync(fDisplay, False);
204 XCloseDisplay(fDisplay);
205 fDisplay = 0;
206 }
207}
Note: See TracBrowser for help on using the repository browser.