JeVois  1.20
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
VideoDisplayBackendX11.C
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2020 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #ifdef JEVOIS_HOST_PRO
19 
21 
22 // Restore None, etc as defined by X11/X.h as we need it here, and only here. See OpenGL.H for what we undef:
23 #ifndef None
24 #define None 0L
25 #endif
26 
27 #ifndef Status
28 #define Status int
29 #endif
30 
31 #ifndef Success
32 #define Success 0
33 #endif
34 
35 #include <X11/Xlib.h>
36 #include <X11/Xatom.h>
37 #include <X11/Xutil.h>
38 #include <X11/extensions/dpms.h>
39 
40 // ##############################################################################################################
41 jevois::VideoDisplayBackendX11::VideoDisplayBackendX11() :
42  jevois::VideoDisplayBackend()
43 { }
44 
45 // ##############################################################################################################
46 jevois::VideoDisplayBackendX11::~VideoDisplayBackendX11()
47 {
48  // It is better to call uninit() explicitly from the same thread that called init() and others, but just in case:
49  uninit();
50 }
51 
52 // ##############################################################################################################
53 void jevois::VideoDisplayBackendX11::init(unsigned short w, unsigned short h, bool fullscreen)
54 {
55  // Open the default display:
56  itsX11display = XOpenDisplay(NULL);
57  if (itsX11display == nullptr) LFATAL("Cannot open X11 display");
58 
59  // Access to the top level window:
60  Window win_root = DefaultRootWindow(itsX11display);
61 
62  // Allow the window manager to control the window that will be created:
63  XSetWindowAttributes xattr { };
64  xattr.override_redirect = false;
65 
66  // Announce events when the keyboard is pressed or the window state changes:
67  xattr.event_mask = ExposureMask | KeyPressMask;
68 
69  // Set the background to white, making it clear when X has the window cleared:
70  xattr.background_pixel = XWhitePixel(itsX11display, XDefaultScreen(itsX11display));
71 
72  // create the native window based on the attributes selected an resolution passed in.
73  itsWindow = XCreateWindow(itsX11display, win_root, 0, 0, w, h, 0,
74  CopyFromParent, InputOutput, CopyFromParent,
75  CWOverrideRedirect | CWEventMask | CWBackPixel, &xattr);
76 
77  // Set the window properties:
78  XSetStandardProperties(itsX11display, itsWindow, itsName.c_str(), itsName.c_str(), None, NULL, 0, NULL);
79 
80  // Disable the display power management to turn keep the display on:
81  DPMSDisable(itsX11display);
82 
83  // Set the window to the background pixel and display it to the front:
84  XClearWindow(itsX11display, itsWindow);
85  XMapRaised (itsX11display, itsWindow);
86 
87  // Optionally request that the window becomes full screen and borderless. It will take some time for the display to
88  // acknowledge the event. An Expose event will occur when this is completed:
89  if (fullscreen)
90  {
91  Atom wm_state = XInternAtom(itsX11display, "_NET_WM_STATE", false);
92  Atom fs = XInternAtom(itsX11display, "_NET_WM_STATE_FULLSCREEN", false);
93 
94  XEvent fs_event { };
95  fs_event.type = ClientMessage;
96  fs_event.xclient.window = itsWindow;
97  fs_event.xclient.message_type = wm_state;
98  fs_event.xclient.format = 32;
99  fs_event.xclient.data.l[0] = 1;
100  fs_event.xclient.data.l[1] = fs;
101  fs_event.xclient.data.l[2] = 0;
102 
103  XSendEvent(itsX11display, win_root, false, SubstructureRedirectMask | SubstructureNotifyMask, &fs_event);
104  }
105 
106  // Initialize OpenGL. Will turn itsDisplayReady to true:
107  jevois::VideoDisplayBackend::init(w, h, (EGLNativeWindowType)(itsWindow));
108 }
109 
110 // ##############################################################################################################
111 void jevois::VideoDisplayBackendX11::uninit()
112 {
113  if (itsX11display)
114  {
115  // Turn display power management back on:
116  DPMSEnable(itsX11display);
117 
118  // Close the application window and release display handle::
119  if (itsWindow) { XDestroyWindow(itsX11display, itsWindow); itsWindow = 0; }
120  XCloseDisplay(itsX11display); itsX11display = nullptr;
121  }
122 
123  // ALso close OpenGL using our base class:
125 }
126 
127 // ##############################################################################################################
128 bool jevois::VideoDisplayBackendX11::pollEvents(bool & shouldclose)
129 {
130  XEvent event;
131  bool gotsome = false;
132 
133  // Process events, one at a time, until there are none remaining:
134  while (XPending(itsX11display))
135  {
136  gotsome = true;
137 
138  XNextEvent(itsX11display, &event);
139  switch (event.type)
140  {
141  case Expose:
142  // Window resized/hidden/shown etc. ignored as we will refresh soon anyway.
143  break;
144 
145  case KeyPress:
146  {
147  // Keyboard events occured, get the key sequence, limit to 10 keys total:
148  char text[11]; KeySym key_press;
149  int keys = XLookupString(&event.xkey, text, 10, &key_press, 0);
150  if (keys == 1 && text[0] == 'q') shouldclose = true;
151  break;
152  }
153 
154  default: break;
155  }
156  }
157 
158  return gotsome;
159 }
160 
161 #endif // JEVOIS_HOST_PRO
jevois
Definition: Concepts.dox:1
jevois::VideoDisplayBackend::uninit
virtual void uninit()
Un-initialize the underlying engine, close windows, etc.
Definition: VideoDisplayBackend.C:209
VideoDisplayBackendX11.H
jevois::VideoDisplayBackend::init
virtual void init(unsigned short w, unsigned short h, bool fullscreen=false)=0
Initialize the underlying engine that will process events, create windows, etc.
LFATAL
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
h
int h
Definition: GUIhelper.C:2373