g4tools  5.4.0
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
tools::X11::base_session Class Reference
Inheritance diagram for tools::X11::base_session:
Inheritance graph
[legend]

Public Member Functions

 base_session (std::ostream &a_out, unsigned int a_monitor=0)
 
virtual ~base_session ()
 
std::ostream & out () const
 
Atom WM_DELETE_WINDOW_atom () const
 
Atom SESSION_EXIT_STEER_atom () const
 
bool is_valid () const
 
Display * display () const
 
bool steer ()
 
bool sync ()
 
bool event_pending (bool &a_is)
 
bool next_event (bool &a_to_exit)
 
bool flush ()
 
Window create_window (const char *a_title, int a_x, int a_y, unsigned int a_width, unsigned int a_height)
 
void map_raise_window (Window a_window) const
 
void show_window (Window a_window) const
 
void hide_window (Window a_window) const
 
void resize_window (Window a_window, unsigned int a_width, unsigned int a_height) const
 
bool window_size (Window a_window, int &a_width, int &a_height) const
 
void delete_window (Window a_window) const
 
void set_override_redirect (Window a_window) const
 
void set_wm_no_decorations (Window a_window) const
 
bool post (Window a_win, long a_0=0, long a_1=0, long a_2=0, long a_3=0, long a_4=0) const
 
bool post_EXIT_STEER (Window a_win)
 
void add_dispatcher (dispatcher *a_dispatcher)
 
void invalidate_dispatchers_with_window (Window a_win)
 
void remove_dispatchers_with_window (Window a_win)
 
bool dispatch (XEvent &a_event)
 

Protected Member Functions

 base_session (const base_session &a_from)
 
base_sessionoperator= (const base_session &a_from)
 
void clear_dispatchers ()
 

Static Protected Member Functions

static Bool wait_map_notify (Display *, XEvent *a_event, char *a_arg)
 
static Bool wait_unmap_notify (Display *, XEvent *a_event, char *a_arg)
 

Protected Attributes

std::ostream & m_out
 
unsigned int m_monitor
 
Display * m_display
 
Atom m_WM_DELETE_WINDOW_atom
 
Atom m_SESSION_EXIT_STEER_atom
 
std::vector< dispatcher * > m_dispatchers
 

Detailed Description

Definition at line 22 of file base_session.

Constructor & Destructor Documentation

◆ base_session() [1/2]

tools::X11::base_session::base_session ( std::ostream &  a_out,
unsigned int  a_monitor = 0 
)
inline

Definition at line 33 of file base_session.

34  :m_out(a_out)
35  ,m_monitor(a_monitor)
36  ,m_display(0)
39  {
40  //NOTE : macOS : XOpenDisplay leaks 128 bytes.
41  m_display = ::XOpenDisplay(NULL);
42  if(!m_display) {
43  m_out << "tools::X11::base_session::base_session : can't open display." << std::endl;
44  return;
45  }
46 
47  int monitors = ::XScreenCount(m_display);
48  if(int(m_monitor)>=monitors) {
49  m_out << "tools::X11::base_session::base_session : bad monitor index "
50  << m_monitor << ". (#monitors " << monitors << ")." << std::endl;
51  ::XCloseDisplay(m_display);
52  m_display = 0;
53  return;
54  }
55 
56  m_WM_DELETE_WINDOW_atom = ::XInternAtom(m_display,"WM_DELETE_WINDOW",False);
57  if(m_WM_DELETE_WINDOW_atom==None) {
58  m_out << "tools::X11::base_session::base_session : can't create WM_DELETE_WINDOW Atom." << std::endl;
59  ::XCloseDisplay(m_display);
60  m_display = 0;
61  return;
62  }
63 
64  m_SESSION_EXIT_STEER_atom = ::XInternAtom(m_display,"TOOLS_X11_SESSION_EXIT_STEER",False);
65  if(m_SESSION_EXIT_STEER_atom==None) {
66  m_out << "tools::X11::base_session::base_session :"
67  << " can't create TOOLS_X11_SESSION_EXIT_STEER Atom." << std::endl;
68  ::XCloseDisplay(m_display);
69  m_display = 0;
70  return;
71  }
72  }

◆ ~base_session()

virtual tools::X11::base_session::~base_session ( )
inlinevirtual

Definition at line 73 of file base_session.

73  {
75  if(m_display) ::XCloseDisplay(m_display);
76  m_display = 0;
77  //std::cout << "debug : ~base_session" << std::endl;
78  }

◆ base_session() [2/2]

tools::X11::base_session::base_session ( const base_session a_from)
inlineprotected

Definition at line 80 of file base_session.

81  :m_out(a_from.m_out)
82  ,m_monitor(a_from.m_monitor)
83  ,m_display(0)
86  {}

Member Function Documentation

◆ add_dispatcher()

void tools::X11::base_session::add_dispatcher ( dispatcher a_dispatcher)
inline

Definition at line 313 of file base_session.

313  {
314  m_dispatchers.push_back(a_dispatcher); //take ownership.
315  }

◆ clear_dispatchers()

void tools::X11::base_session::clear_dispatchers ( )
inlineprotected

Definition at line 382 of file base_session.

382  {
383  tools_vforit_npp(dispatcher*,m_dispatchers,it) {
384  dispatcher* obj = *it;
385  it = m_dispatchers.erase(it);
386  delete obj;
387  }
388  m_dispatchers.clear();
389  }

◆ create_window()

Window tools::X11::base_session::create_window ( const char *  a_title,
int  a_x,
int  a_y,
unsigned int  a_width,
unsigned int  a_height 
)
inline

Definition at line 166 of file base_session.

166  {
167  if(!m_display) return 0L;
168 
169  XSetWindowAttributes swa;
170  swa.event_mask = StructureNotifyMask | ExposureMask
171  | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
172  | PointerMotionMask
173  | KeyPressMask;
174 
175  swa.background_pixel = ::XWhitePixel(m_display,m_monitor);
176 
177  Window window = ::XCreateWindow(m_display,
178  ::XRootWindow(m_display,m_monitor),
179  a_x,a_y,a_width,a_height,
180  0,
181  (int)CopyFromParent,
182  InputOutput,
183  (Visual*)CopyFromParent,
184  CWEventMask|CWBackPixel,&swa);
185  if(window==0L) {
186  m_out << "tools::X11::base_session::create_window : can't create a X11 window." << std::endl;
187  return 0L;
188  }
189 
190  XTextProperty tp;
191  ::XStringListToTextProperty((char**)&a_title,1,&tp);
192  XSizeHints sh;
193  sh.flags = USPosition | USSize;
194  ::XSetWMProperties(m_display,window,&tp,&tp,0,0,&sh,0,0);
195  ::XFree(tp.value);
196 
197  ::XSetWMProtocols(m_display,window,&m_WM_DELETE_WINDOW_atom,1);
198  return window;
199  }

◆ delete_window()

void tools::X11::base_session::delete_window ( Window  a_window) const
inline

Definition at line 242 of file base_session.

242  {
243  if(!m_display) return;
244  ::XDestroyWindow(m_display,a_window);
245  }

◆ dispatch()

bool tools::X11::base_session::dispatch ( XEvent &  a_event)
inline

Definition at line 335 of file base_session.

335  {
336  {tools_vforit_npp(dispatcher*,m_dispatchers,it) {
337  if(!(*it)->is_valid()) {
338  dispatcher* obj = *it;
339  it = m_dispatchers.erase(it);
340  delete obj;
341  } else {
342  it++;
343  }
344  }}
345  tools_vforit(dispatcher*,m_dispatchers,it) {
346  if((*it)->is_valid()) {
347  if((*it)->dispatch(a_event)) return true;
348  }
349  }
350  return false;
351  }

◆ display()

Display* tools::X11::base_session::display ( ) const
inline

Definition at line 99 of file base_session.

99 {return m_display;}

◆ event_pending()

bool tools::X11::base_session::event_pending ( bool &  a_is)
inline

Definition at line 135 of file base_session.

135  {
136  if(!m_display) {a_is = false;return false;}
137  a_is = ::XPending(m_display)?true:false;
138  return true;
139  }

◆ flush()

bool tools::X11::base_session::flush ( )
inline

Definition at line 156 of file base_session.

156  {
157  if(!m_display) return false;
158  ::XFlush(m_display);
159  return true;
160  }

◆ hide_window()

void tools::X11::base_session::hide_window ( Window  a_window) const
inline

Definition at line 216 of file base_session.

216  {
217  if(!m_display) return;
218  ::XUnmapWindow(m_display,a_window);
219  {XEvent event;
220  ::XIfEvent(m_display,&event,wait_unmap_notify,(char*)a_window);}
221  }

◆ invalidate_dispatchers_with_window()

void tools::X11::base_session::invalidate_dispatchers_with_window ( Window  a_win)
inline

Definition at line 317 of file base_session.

317  {
318  tools_vforit(dispatcher*,m_dispatchers,it) {
319  if((*it)->window()==a_win) (*it)->invalidate();
320  }
321  }

◆ is_valid()

bool tools::X11::base_session::is_valid ( ) const
inline

Definition at line 97 of file base_session.

97 {return m_display?true:false;}

◆ map_raise_window()

void tools::X11::base_session::map_raise_window ( Window  a_window) const
inline

Definition at line 201 of file base_session.

201  {
202  if(!m_display) return;
203  ::XMapWindow(m_display,a_window);
204  ::XRaiseWindow(m_display,a_window);
205  }

◆ next_event()

bool tools::X11::base_session::next_event ( bool &  a_to_exit)
inline

Definition at line 141 of file base_session.

141  {
142  if(!m_display) {a_to_exit = false;return false;}
143  XEvent xevent;
144  ::XNextEvent(m_display,&xevent);
145  if(xevent.type==ClientMessage) {
146  if(xevent.xclient.data.l[0]==(long)m_SESSION_EXIT_STEER_atom) {
147  a_to_exit = true;
148  return true;
149  }
150  }
151  dispatch(xevent);
152  a_to_exit = false;
153  return true;
154  }

◆ operator=()

base_session& tools::X11::base_session::operator= ( const base_session a_from)
inlineprotected

Definition at line 87 of file base_session.

87  {
88  if(&a_from==this) return *this;
89  return *this;
90  }

◆ out()

std::ostream& tools::X11::base_session::out ( ) const
inline

Definition at line 92 of file base_session.

92 {return m_out;}

◆ post()

bool tools::X11::base_session::post ( Window  a_win,
long  a_0 = 0,
long  a_1 = 0,
long  a_2 = 0,
long  a_3 = 0,
long  a_4 = 0 
) const
inline

Definition at line 283 of file base_session.

286  {
287  if(!m_display) return false;
288  XEvent event;
289  event.type = ClientMessage;
290  event.xclient.display = m_display;
291  event.xclient.window = a_win;
292  event.xclient.message_type = XA_INTEGER;
293  event.xclient.format = 8; /* put 8 because bug with 32 on VMCMS */
294  event.xclient.data.l[0] = a_0;
295  event.xclient.data.l[1] = a_1;
296  event.xclient.data.l[2] = a_2;
297  event.xclient.data.l[3] = a_3;
298  event.xclient.data.l[4] = a_4;
299  //lock();
300  Status stat = ::XSendEvent(m_display,a_win,False,0L,&event);
301  ::XFlush(m_display);
302  //unlock();
303  return (stat==0?false:true);
304  }

◆ post_EXIT_STEER()

bool tools::X11::base_session::post_EXIT_STEER ( Window  a_win)
inline

Definition at line 306 of file base_session.

306  {
307  return post(a_win,SESSION_EXIT_STEER_atom());
308  }

◆ remove_dispatchers_with_window()

void tools::X11::base_session::remove_dispatchers_with_window ( Window  a_win)
inline

Definition at line 323 of file base_session.

323  {
324  tools_vforit_npp(dispatcher*,m_dispatchers,it) {
325  if((*it)->window()==a_win) {
326  dispatcher* obj = *it;
327  it = m_dispatchers.erase(it);
328  delete obj;
329  } else {
330  it++;
331  }
332  }
333  }

◆ resize_window()

void tools::X11::base_session::resize_window ( Window  a_window,
unsigned int  a_width,
unsigned int  a_height 
) const
inline

Definition at line 223 of file base_session.

223  {
224  if(!m_display) return;
225  unsigned int mask = CWWidth | CWHeight | CWBorderWidth;
226  XWindowChanges changes;
227  changes.border_width = 0;
228  changes.width = a_width;
229  changes.height = a_height;
230  ::XConfigureWindow(m_display,a_window,mask,&changes);
231  }

◆ SESSION_EXIT_STEER_atom()

Atom tools::X11::base_session::SESSION_EXIT_STEER_atom ( ) const
inline

Definition at line 95 of file base_session.

◆ set_override_redirect()

void tools::X11::base_session::set_override_redirect ( Window  a_window) const
inline

Definition at line 247 of file base_session.

247  {
248  //must be executed before window is mapped.
249  if(!m_display) return;
250  XSetWindowAttributes swa;
251  swa.override_redirect = True;
252  ::XChangeWindowAttributes(m_display,a_window,CWOverrideRedirect,&swa);
253  }

◆ set_wm_no_decorations()

void tools::X11::base_session::set_wm_no_decorations ( Window  a_window) const
inline

Definition at line 255 of file base_session.

255  {
256  //must be executed before window is mapped.
257  if(!m_display) return;
258 
259  // struct, mwm_hints_decorations taken from OpenMotif MwmUtils.h.
260  struct{
261  unsigned long flags;
262  unsigned long functions;
263  unsigned long decorations;
264  long inputMode;
265  unsigned long status;
266  } prop;
267 
268  //unsigned long mwm_hints_functions = 1L << 0;
269  unsigned long mwm_hints_decorations = 1L << 1;
270 
271  Atom atom = ::XInternAtom(m_display,"_MOTIF_WM_HINTS",False);
272  if(atom==None) {
273  m_out << "tools::X11::base_session::set_wm_no_decorations : can't create/get _MOTIF_WM_HINTS Atom." << std::endl;
274  return;
275  }
276  prop.flags = mwm_hints_decorations;
277  prop.functions = 0;
278  prop.decorations = 0;
279 
280  ::XChangeProperty(m_display,a_window,atom,atom,32,PropModeReplace,(unsigned char*)&prop,5);
281  }

◆ show_window()

void tools::X11::base_session::show_window ( Window  a_window) const
inline

Definition at line 207 of file base_session.

207  {
208  if(!m_display) return;
209  // we should test if already mapped ! Else we are going to forever wait in the below XIfEvent.
210  ::XMapWindow(m_display,a_window);
211  ::XRaiseWindow(m_display,a_window);
212  {XEvent event;
213  ::XIfEvent(m_display,&event,wait_map_notify,(char*)a_window);}
214  }

◆ steer()

bool tools::X11::base_session::steer ( )
inline

Definition at line 101 of file base_session.

101  {
102  if(!m_display) return false;
103 
104  while(true) {
105  XEvent xevent;
106  ::XNextEvent(m_display,&xevent);
107  if(xevent.type==ClientMessage) {
108  if(xevent.xclient.data.l[0]==(long)m_SESSION_EXIT_STEER_atom) break;
109  }
110  dispatch(xevent);
111  }
112 
113  return true;
114  }

◆ sync()

bool tools::X11::base_session::sync ( )
inline

Definition at line 116 of file base_session.

116  {
117  if(!m_display) return false;
118  //::glXWaitX();
119  ::XSync(m_display,False);
120  while(true) {
121  XEvent xevent;
122  if(::XPending(m_display)) {
123  ::XNextEvent(m_display,&xevent);
124  if(xevent.type==ClientMessage) {
125  if(xevent.xclient.data.l[0]==(long)m_SESSION_EXIT_STEER_atom) return false;
126  }
127  dispatch(xevent);
128  } else {
129  break;
130  }
131  }
132  return true;
133  }

◆ wait_map_notify()

static Bool tools::X11::base_session::wait_map_notify ( Display *  ,
XEvent *  a_event,
char *  a_arg 
)
inlinestaticprotected

Definition at line 353 of file base_session.

353  {
354  return (a_event->type == MapNotify) && (a_event->xmap.window == (Window)a_arg);
355  }

◆ wait_unmap_notify()

static Bool tools::X11::base_session::wait_unmap_notify ( Display *  ,
XEvent *  a_event,
char *  a_arg 
)
inlinestaticprotected

Definition at line 356 of file base_session.

356  {
357  return (a_event->type == UnmapNotify) && (a_event->xmap.window == (Window)a_arg);
358  }

◆ window_size()

bool tools::X11::base_session::window_size ( Window  a_window,
int &  a_width,
int &  a_height 
) const
inline

Definition at line 233 of file base_session.

233  {
234  if(!m_display) {a_width = 0;a_height = 0;return false;}
235  XWindowAttributes watbs;
236  if(!XGetWindowAttributes(m_display,a_window,&watbs)) {a_width = 0;a_height = 0;return false;}
237  a_width = watbs.width;
238  a_height = watbs.height;
239  return true;
240  }

◆ WM_DELETE_WINDOW_atom()

Atom tools::X11::base_session::WM_DELETE_WINDOW_atom ( ) const
inline

Definition at line 94 of file base_session.

Member Data Documentation

◆ m_dispatchers

std::vector<dispatcher*> tools::X11::base_session::m_dispatchers
protected

Definition at line 397 of file base_session.

◆ m_display

Display* tools::X11::base_session::m_display
protected

Definition at line 394 of file base_session.

◆ m_monitor

unsigned int tools::X11::base_session::m_monitor
protected

Definition at line 393 of file base_session.

◆ m_out

std::ostream& tools::X11::base_session::m_out
protected

Definition at line 392 of file base_session.

◆ m_SESSION_EXIT_STEER_atom

Atom tools::X11::base_session::m_SESSION_EXIT_STEER_atom
protected

Definition at line 396 of file base_session.

◆ m_WM_DELETE_WINDOW_atom

Atom tools::X11::base_session::m_WM_DELETE_WINDOW_atom
protected

Definition at line 395 of file base_session.


The documentation for this class was generated from the following file:
tools::X11::base_session::post
bool post(Window a_win, long a_0=0, long a_1=0, long a_2=0, long a_3=0, long a_4=0) const
Definition: base_session:283
tools::X11::base_session::SESSION_EXIT_STEER_atom
Atom SESSION_EXIT_STEER_atom() const
Definition: base_session:95
tools::X11::base_session::m_out
std::ostream & m_out
Definition: base_session:392
tools::X11::base_session::wait_unmap_notify
static Bool wait_unmap_notify(Display *, XEvent *a_event, char *a_arg)
Definition: base_session:356
tools::X11::base_session::m_WM_DELETE_WINDOW_atom
Atom m_WM_DELETE_WINDOW_atom
Definition: base_session:395
tools_vforit
#define tools_vforit(a__T, a__v, a__it)
Definition: forit:13
tools_vforit_npp
#define tools_vforit_npp(a__T, a__v, a__it)
Definition: forit:25
tools::X11::base_session::m_monitor
unsigned int m_monitor
Definition: base_session:393
tools::X11::base_session::m_dispatchers
std::vector< dispatcher * > m_dispatchers
Definition: base_session:397
tools::X11::base_session::m_display
Display * m_display
Definition: base_session:394
tools::X11::base_session::clear_dispatchers
void clear_dispatchers()
Definition: base_session:382
tools::X11::base_session::m_SESSION_EXIT_STEER_atom
Atom m_SESSION_EXIT_STEER_atom
Definition: base_session:396
tools::X11::base_session::dispatch
bool dispatch(XEvent &a_event)
Definition: base_session:335
tools::X11::base_session::wait_map_notify
static Bool wait_map_notify(Display *, XEvent *a_event, char *a_arg)
Definition: base_session:353