"Fossies" - the Fresh Open Source Software Archive 
Member "xosview-1.23/xwin.cc" (11 Jul 2020, 13999 Bytes) of package /linux/misc/xosview-1.23.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "xwin.cc" see the
Fossies "Dox" file reference documentation.
1 #include "xwin.h"
2 #include "Xrm.h"
3 #include <X11/Xatom.h>
4 #include <X11/xpm.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <iostream>
9
10 //-----------------------------------------------------------------------------
11
12 // argc is a reference, so that the changes to argc by XrmParseCommand are
13 // noticed by the caller (XOSView, in this case). BCG
14 XWin::XWin() {
15 }
16 //-----------------------------------------------------------------------------
17
18 XWin::XWin( int argc, char *argv[], int x, int y, int width, int height ) {
19
20 std::cerr << "This constructor call is not supported! (" << __FILE__
21 << ":" << __LINE__ << ")" << std::endl;
22 exit (-1);
23 // FIXME BCG This constructor needs to do much of the work of the above
24 // one. Or, we need to drop this as a constructor. As it is, it is
25 // VERY MUCH out of date.
26 x_ = x;
27 y_ = y;
28 width_ = width;
29 height_ = height;
30 (void) argc;
31 (void) argv;
32 }
33 //-----------------------------------------------------------------------------
34
35 void XWin::XWinInit (int argc, char** argv, char* geometry, Xrm* xrm) {
36 (void) argc;
37 (void) argv; // Avoid gcc warnings about unused variables.
38 // Eventually, we may want to have XWin handle some arguments other
39 // than resources, so argc and argv are left as parameters. BCG
40
41 geometry_ = geometry; // Save for later use.
42 width_ = height_ = x_ = y_ = 0;
43 xrmptr_ = xrm;
44
45 name_ = (char *)malloc(0);
46 font_ = NULL;
47 done_ = 0;
48
49 // Set up the default Events
50 events_ = NULL;
51 addEvent( new Event( this, ClientMessage, &XWin::deleteEvent ) );
52 addEvent( new Event( this, MappingNotify, &XWin::mappingNotify ) );
53
54 //openDisplay(); // Done explicitly in xosview.cc.
55 }
56
57 XWin::~XWin( void ){
58
59 // delete the events
60 Event *event = events_;
61 while ( event != NULL ){
62 Event *save = event->next_;
63 delete event;
64 event = save;
65 }
66
67 XFree( title_.value );
68 XFree( iconname_.value );
69 XFree( sizehints_ );
70 XFree( wmhints_ );
71 XFree( classhints_ );
72 XFreeGC( display_, gc_ );
73 XFreeFont( display_, font_ );
74 XDestroyWindow( display_, window_ );
75 // close the connection to the display
76 XCloseDisplay( display_ );
77 }
78 //-----------------------------------------------------------------------------
79
80 void XWin::init( int argc, char **argv ){
81 XGCValues gcv;
82 XSetWindowAttributes xswa;
83 Pixmap background_pixmap;
84
85 setFont();
86 setColors();
87 getGeometry();
88
89 borderwidth_ = atoi(getResourceOrUseDefault("borderwidth", "1"));
90
91 window_ = XCreateSimpleWindow(display_, DefaultRootWindow(display_),
92 sizehints_->x, sizehints_->y,
93 sizehints_->width, sizehints_->height,
94 borderwidth_,
95 fgcolor_, bgcolor_);
96
97
98 setHints( argc, argv );
99
100 // Finally, create a graphics context for the main window
101 gcv.font = font_->fid;
102 gcv.foreground = fgcolor_;
103 gcv.background = bgcolor_;
104 gc_ = XCreateGC(display_, window_,
105 (GCFont | GCForeground | GCBackground), &gcv);
106
107 // Set main window's attributes (colormap, bit_gravity)
108 xswa.colormap = colormap_;
109 xswa.bit_gravity = NorthWestGravity;
110 XChangeWindowAttributes(display_, window_,
111 (CWColormap | CWBitGravity), &xswa);
112
113 // If there is a pixmap file, set it as the background
114 if (getPixmap(&background_pixmap))
115 XSetWindowBackgroundPixmap(display_,window_,background_pixmap);
116
117 // add the events
118 Event *tmp = events_;
119 while ( tmp != NULL ){
120 selectEvents( tmp->mask_ );
121 tmp = tmp->next_;
122 }
123
124 // Map the main window
125 map();
126 flush();
127 if(XGetWindowAttributes(display_, window_, &attr_) == 0){
128 std::cerr <<"Error getting attributes of Main." <<std::endl;
129 exit(2);
130 }
131
132 // Create stipple pixmaps.
133 stipples_[0] = createPixmap("\000\000", 2, 2);
134 stipples_[1] = createPixmap("\002\000\001\000", 2, 4);
135 stipples_[2] = createPixmap("\002\001", 2, 2);
136 stipples_[3] = createPixmap("\002\003\001\003", 2, 4);
137 doStippling_ = isResourceTrue("enableStipple");
138 }
139 //-----------------------------------------------------------------------------
140
141 void XWin::setFont( void ){
142 // Set up the font
143 if ( font_ != NULL )
144 return;
145 const char* fontName = getResource("font");
146
147 if ((font_ = XLoadQueryFont(display_, fontName)) == NULL){
148 std::cerr <<name_ <<": display " <<DisplayString(display_)
149 <<" cannot load font " << fontName << std::endl;
150 exit(1);
151 }
152 }
153 //-----------------------------------------------------------------------------
154
155 void XWin::setHints( int argc, char *argv[] ){
156 // Set up class hint
157 if((classhints_ = XAllocClassHint()) == NULL){
158 std::cerr <<"Error allocating class hint!" <<std::endl;
159 exit(1);
160 }
161 // We have to cast away the const's.
162 classhints_->res_name = (char*) xrmptr_->instanceName();
163 classhints_->res_class = (char*) xrmptr_->className();
164
165 // Set up the window manager hints
166 if((wmhints_ = XAllocWMHints()) == NULL){
167 std::cerr <<"Error allocating Window Manager hints!" <<std::endl;
168 exit(1);
169 }
170 wmhints_->flags = (InputHint|StateHint);
171 wmhints_->input = True;
172 wmhints_->initial_state = NormalState;
173
174 // Set up XTextProperty for window name and icon name
175 if(XStringListToTextProperty(&name_, 1, &title_) == 0){
176 std::cerr <<"Error creating XTextProperty!" <<std::endl;
177 exit(1);
178 }
179 if(XStringListToTextProperty(&name_, 1, &iconname_) == 0){
180 std::cerr <<"Error creating XTextProperty!" <<std::endl;
181 exit(1);
182 }
183
184 XSetWMProperties(display_, window_, &title_, &iconname_, argv, argc,
185 sizehints_, wmhints_, classhints_);
186
187 // Set up the Atoms for delete messages
188 wm_ = XInternAtom( display(), "WM_PROTOCOLS", False );
189 wmdelete_ = XInternAtom( display(), "WM_DELETE_WINDOW", False );
190 XChangeProperty( display(), window(), wm_, XA_ATOM, 32,
191 PropModeReplace, (unsigned char *)(&wmdelete_), 1 );
192 }
193 //-----------------------------------------------------------------------------
194
195 void XWin::openDisplay( void ){
196 // Open connection to display selected by user
197 if ((display_ = XOpenDisplay (display_name_)) == NULL) {
198 std::cerr <<"Can't open display named " << display_name_ <<std::endl;
199 exit(1);
200 }
201
202 colormap_ = DefaultColormap( display_, screen() );
203 }
204 //-----------------------------------------------------------------------------
205
206 void XWin::setColors( void ){
207 XColor color;
208
209 // Main window's background color
210 if (XParseColor(display_, colormap_, getResource("background"),
211 &color) == 0 ||
212 XAllocColor(display_, colormap_, &color) == 0)
213 bgcolor_ = WhitePixel(display_, DefaultScreen(display_));
214 else
215 bgcolor_ = color.pixel;
216
217 // Main window's foreground color */
218 if (XParseColor(display_, colormap_, getResource("foreground"),
219 &color) == 0 ||
220 XAllocColor(display_, colormap_, &color) == 0)
221 fgcolor_ = BlackPixel(display_, DefaultScreen(display_));
222 else
223 fgcolor_ = color.pixel;
224 }
225 //-----------------------------------------------------------------------------
226
227 int XWin::getPixmap(Pixmap *pixmap)
228 {
229 char *pixmap_file;
230 XWindowAttributes root_att;
231 XpmAttributes pixmap_att;
232
233 pixmap_file = (char*) getResourceOrUseDefault("pixmapName",NULL);
234
235 if (!pixmap_file)
236 return 0;
237
238 XGetWindowAttributes(display_, DefaultRootWindow(display_),&root_att);
239 pixmap_att.closeness=30000;
240 pixmap_att.colormap=root_att.colormap;
241 pixmap_att.valuemask=XpmSize|XpmReturnPixels|XpmColormap|XpmCloseness;
242
243 if (XpmReadFileToPixmap(display_,DefaultRootWindow(display_),pixmap_file,
244 pixmap, NULL, &pixmap_att))
245 {
246 std::cerr << "Pixmap " << pixmap_file << " not found" << std::endl;
247 std::cerr << "Defaulting to blank" << std::endl;
248 pixmap = NULL;
249 return 0;
250 }
251
252 return 1;
253 }
254
255 //-----------------------------------------------------------------------------
256 void XWin::getGeometry( void ){
257 char default_geometry[80];
258 int bitmask;
259
260 // Fill out a XsizeHints structure to inform the window manager
261 // of desired size and location of main window.
262 if((sizehints_ = XAllocSizeHints()) == NULL){
263 std::cerr <<"Error allocating size hints!" <<std::endl;
264 exit(1);
265 }
266 sizehints_->flags = PSize;
267 sizehints_->height = height_;
268 sizehints_->min_height = sizehints_->height;
269 sizehints_->width = width_;
270 sizehints_->min_width = sizehints_->width;
271 sizehints_->x = x_;
272 sizehints_->y = y_;
273
274 // Construct a default geometry string
275 snprintf(default_geometry, 80, "%dx%d+%d+%d", sizehints_->width,
276 sizehints_->height, sizehints_->x, sizehints_->y);
277
278 // Process the geometry specification
279 bitmask = XGeometry(display_, DefaultScreen(display_),
280 getResourceOrUseDefault("geometry", geometry_),
281 default_geometry,
282 0,
283 1, 1, 0, 0, &(sizehints_->x), &(sizehints_->y),
284 &(sizehints_->width), &(sizehints_->height));
285
286 // Check bitmask and set flags in XSizeHints structure
287 if (bitmask & (WidthValue | HeightValue)){
288 sizehints_->flags |= PPosition;
289 width_ = sizehints_->width;
290 height_ = sizehints_->height;
291 }
292
293 if (bitmask & (XValue | YValue)){
294 sizehints_->flags |= USPosition;
295 x_ = sizehints_->x;
296 y_ = sizehints_->y;
297 }
298 }
299 //-----------------------------------------------------------------------------
300
301 void XWin::selectEvents( long mask ){
302 XWindowAttributes xAttr;
303 XSetWindowAttributes xSwAttr;
304
305 if ( XGetWindowAttributes( display_, window_, &xAttr ) != 0 ){
306 xSwAttr.event_mask = xAttr.your_event_mask | mask;
307 XChangeWindowAttributes( display_, window_, CWEventMask, &xSwAttr );
308 }
309 }
310
311 void XWin::ignoreEvents( long mask ){
312 XWindowAttributes xAttr;
313 XSetWindowAttributes xSwAttr;
314
315 if ( XGetWindowAttributes( display_, window_, &xAttr ) != 0 ){
316 xSwAttr.event_mask = xAttr.your_event_mask & mask;
317 XChangeWindowAttributes( display_, window_, CWEventMask, &xSwAttr );
318 }
319 }
320 //-----------------------------------------------------------------------------
321
322 void XWin::checkevent( void ){
323 XEvent event;
324
325 while ( XEventsQueued( display_, QueuedAfterReading ) ){
326 XNextEvent( display_, &event );
327
328 // call all of the Event's call back functions to process this event
329 Event *tmp = events_;
330 while ( tmp != NULL ){
331 tmp->callBack( event );
332 tmp = tmp->next_;
333 }
334 }
335 }
336 //-----------------------------------------------------------------------------
337
338 void XWin::addEvent( Event *event ){
339 Event *tmp = events_;
340
341 if ( events_ == NULL )
342 events_ = event;
343 else {
344 while ( tmp->next_ != NULL )
345 tmp = tmp->next_;
346 tmp->next_ = event;
347 }
348 }
349 //-----------------------------------------------------------------------------
350
351 const char *XWin::getResourceOrUseDefault( const char *name, const char* defaultVal ){
352
353 const char* retval = xrmptr_->getResource (name);
354 if (retval)
355 return retval;
356 else
357 return defaultVal;
358 }
359
360 //-----------------------------------------------------------------------------
361
362 const char *XWin::getResource( const char *name ){
363 const char* retval = xrmptr_->getResource (name);
364 if (retval)
365 return retval;
366 else
367 {
368 std::cerr << "Error: Couldn't find '" << name << "' resource in the resource database!\n";
369 exit (-1);
370 /* Some compilers aren't smart enough to know that exit() exits. */
371 return NULL;
372 }
373 }
374
375 //-----------------------------------------------------------------------------
376
377 void XWin::dumpResources( std::ostream &os ){
378 std::cerr << "Function not implemented!\n"; // BCG FIXME Need to make this.
379 (void) os; // Keep gcc happy.
380 }
381 //-----------------------------------------------------------------------------
382
383 unsigned long XWin::allocColor( const char *name ){
384 XColor exact, closest;
385
386 if ( XAllocNamedColor( display_, colormap(), name, &closest, &exact ) == 0 )
387 std::cerr <<"XWin::allocColor() : failed to alloc : " <<name <<std::endl;
388
389 return exact.pixel;
390 }
391 //-----------------------------------------------------------------------------
392
393 void XWin::deleteEvent( XEvent &event ){
394 if ( (event.xclient.message_type == wm_ ) &&
395 ((unsigned)event.xclient.data.l[0] == wmdelete_) )
396 done( 1 );
397 }
398 //-----------------------------------------------------------------------------
399
400 XWin::Event::Event( XWin *parent, int event, EventCallBack callBack ){
401 next_ = NULL;
402 parent_ = parent;
403 event_ = event;
404 callBack_ = callBack;
405
406 switch ( event_ ){
407 case ButtonPress:
408 mask_ = ButtonPressMask;
409 break;
410 case ButtonRelease:
411 mask_ = ButtonReleaseMask;
412 break;
413 case EnterNotify:
414 mask_ = EnterWindowMask;
415 break;
416 case LeaveNotify:
417 mask_ = LeaveWindowMask;
418 break;
419 case MotionNotify:
420 mask_ = PointerMotionMask;
421 break;
422 case FocusIn:
423 case FocusOut:
424 mask_ = FocusChangeMask;
425 break;
426 case KeymapNotify:
427 mask_ = KeymapStateMask;
428 break;
429 case KeyPress:
430 mask_ = KeyPressMask;
431 break;
432 case KeyRelease:
433 mask_ = KeyReleaseMask;
434 break;
435 case MapNotify:
436 case SelectionClear:
437 case SelectionNotify:
438 case SelectionRequest:
439 case ClientMessage:
440 case MappingNotify:
441 mask_ = NoEventMask;
442 break;
443 case Expose:
444 case GraphicsExpose:
445 case NoExpose:
446 mask_ = ExposureMask;
447 break;
448 case ColormapNotify:
449 mask_ = ColormapChangeMask;
450 break;
451 case PropertyNotify:
452 mask_ = PropertyChangeMask;
453 break;
454 case UnmapNotify:
455 case ReparentNotify:
456 case GravityNotify:
457 case DestroyNotify:
458 case CirculateNotify:
459 case ConfigureNotify:
460 mask_ = StructureNotifyMask | SubstructureNotifyMask;
461 break;
462 case CreateNotify:
463 mask_ = SubstructureNotifyMask;
464 break;
465 case VisibilityNotify:
466 mask_ = VisibilityChangeMask;
467 break;
468 // The following are used by window managers
469 case CirculateRequest:
470 case ConfigureRequest:
471 case MapRequest:
472 mask_ = SubstructureRedirectMask;
473 break;
474 case ResizeRequest:
475 mask_ = ResizeRedirectMask;
476 break;
477 default:
478 std::cerr <<"XWin::Event::Event() : unknown event type : " <<event_ <<std::endl;
479 mask_ = NoEventMask;
480 break;
481 }
482 }