xosview  1.23
About: xosview is an X Windows based system monitor (cpu, memory, swap and network usage; interrupt and serial activities; load average).
  Fossies Dox: xosview-1.23.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

xwin.cc
Go to the documentation of this file.
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
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,
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,
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;
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 }
long mask_
Definition: xwin.h:108
void callBack(XEvent &event)
Definition: xwin.h:101
int event_
Definition: xwin.h:107
EventCallBack callBack_
Definition: xwin.h:106
Event * next_
Definition: xwin.h:110
XWin * parent_
Definition: xwin.h:105
Event(XWin *parent, int event, EventCallBack callBack)
Definition: xwin.cc:400
Definition: xwin.h:15
Xrm * xrmptr_
Definition: xwin.h:135
Display * display(void)
Definition: xwin.h:27
Window window_
Definition: xwin.h:117
void XWinInit(int argc, char *argv[], char *geometry, Xrm *xrmp)
Definition: xwin.cc:35
XTextProperty title_
Definition: xwin.h:121
Atom wm_
Definition: xwin.h:131
Colormap colormap(void)
Definition: xwin.h:158
void mappingNotify(XEvent &event)
Definition: xwin.h:154
Window window(void)
Definition: xwin.h:28
unsigned long allocColor(const char *name)
Definition: xwin.cc:383
const char * getResourceOrUseDefault(const char *name, const char *defaultVal)
Definition: xwin.cc:351
char display_name_[256]
Definition: xwin.h:133
int done(void)
Definition: xwin.h:29
int x_
Definition: xwin.h:114
XSizeHints * sizehints_
Definition: xwin.h:127
void setFont(void)
Definition: xwin.cc:141
XFontStruct * font_
Definition: xwin.h:119
XWindowAttributes attr_
Definition: xwin.h:125
Event * events_
Definition: xwin.h:129
int y_
Definition: xwin.h:114
void openDisplay(void)
Definition: xwin.cc:195
int done_
Definition: xwin.h:130
void deleteEvent(XEvent &event)
Definition: xwin.cc:393
virtual void checkevent(void)
Definition: xwin.cc:322
int screen(void)
Definition: xwin.h:159
XClassHint * classhints_
Definition: xwin.h:128
XTextProperty iconname_
Definition: xwin.h:122
int isResourceTrue(const char *name)
Definition: xwin.h:89
GC gc_
Definition: xwin.h:118
int getPixmap(Pixmap *)
Definition: xwin.cc:227
int doStippling_
Definition: xwin.h:136
void addEvent(Event *event)
Definition: xwin.cc:338
virtual ~XWin(void)
Definition: xwin.cc:57
int height(void)
Definition: xwin.h:25
void selectEvents(long mask)
Definition: xwin.cc:301
Pixmap createPixmap(const char *data, unsigned int w, unsigned int h)
Definition: xwin.h:49
const char * getResource(const char *name)
Definition: xwin.cc:362
void setHints(int argc, char *argv[])
Definition: xwin.cc:155
unsigned long fgcolor_
Definition: xwin.h:123
int width_
Definition: xwin.h:115
void setColors(void)
Definition: xwin.cc:206
Display * display_
Definition: xwin.h:116
void init(int argc, char *argv[])
Definition: xwin.cc:80
int width(void)
Definition: xwin.h:23
void flush(void)
Definition: xwin.h:85
char * name_
Definition: xwin.h:120
void ignoreEvents(long mask)
Definition: xwin.cc:311
unsigned long bgcolor_
Definition: xwin.h:124
Atom wmdelete_
Definition: xwin.h:131
XWMHints * wmhints_
Definition: xwin.h:126
void map(void)
Definition: xwin.h:83
void getGeometry(void)
Definition: xwin.cc:256
void dumpResources(std::ostream &os)
Definition: xwin.cc:377
int borderwidth_
Definition: xwin.h:113
char * geometry_
Definition: xwin.h:134
Pixmap stipples_[4]
Definition: xwin.h:137
Colormap colormap_
Definition: xwin.h:132
XWin()
Definition: xwin.cc:14
int height_
Definition: xwin.h:115
Definition: Xrm.h:15
const char * getResource(const char *rname) const
Definition: Xrm.cc:64
const char * instanceName(void) const
Definition: Xrm.h:22
const char * className(void) const
Definition: Xrm.h:21
void(XWin::* EventCallBack)(XEvent &event)
Definition: xwin.h:12