VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gxvisual.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 //
18 // gxvisual.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gxdef.h"
23 #include "gxdisplay.h"
24 #include "gxvisual.h"
25 #include "gxerror.h"
26 #include <sstream>
27 
29 {
30  int screen = XDefaultScreen( display.x() ) ;
31  ::Visual * vp = XDefaultVisual( display.x() , screen ) ;
32  if( vp == nullptr ) throw Error( "XDefaultVisual" ) ;
33  init( display , vp->visualid ) ;
34 }
35 
36 GX::Visual::Visual( Display & display , const Info & info )
37 {
38  init( display , info.visual->visualid ) ;
39 }
40 
41 void GX::Visual::init( Display & display , ::VisualID id )
42 {
43  std::list<Info> info_list = infoList( display ) ;
44  std::list<Info>::iterator p = info_list.begin() ;
45  for( ; p != info_list.end() ; ++p )
46  {
47  if( (*p).visualid == id )
48  {
49  m_info = *p ;
50  break ;
51  }
52  }
53  if( p == info_list.end() )
54  throw Error( "unknown visual" ) ;
55 }
56 
58 {
59 }
60 
62 {
63  return m_info.visual ;
64 }
65 
66 std::list<GX::Visual::Info> GX::Visual::infoList( Display & display )
67 {
68  Info template_ ;
69  template_.screen = XDefaultScreen( display.x() ) ;
70 
71  int n = 0 ;
72  XVisualInfo * vp = XGetVisualInfo( display.x() , VisualScreenMask , &template_ , &n ) ;
73  if( vp == nullptr )
74  throw Error( "XGetVisualInfo" ) ;
75 
76  std::list<Info> result ;
77  for( int i = 0 ; i < n ; i++ )
78  {
79  Info copy( vp[i] ) ;
80  result.push_back( copy ) ;
81  }
82  XFree( vp ) ;
83  return result ;
84 }
85 
86 ::VisualID GX::Visual::id() const
87 {
88  check( m_info.visualid == XVisualIDFromVisual(m_info.visual) ) ;
89  return m_info.visualid ;
90 }
91 
92 void GX::Visual::check( bool b )
93 {
94  if( ! b )
95  throw Error( "internal error" ) ;
96 }
97 
98 int GX::Visual::type() const
99 {
100  return m_info.c_class ;
101 }
102 
103 int GX::Visual::depth() const
104 {
105  return m_info.depth ;
106 }
107 
108 std::string GX::Visual::typeName() const
109 {
110  if( type() == PseudoColor ) return "PseudoColour" ;
111  if( type() == GrayScale ) return "GreyScale" ;
112  if( type() == DirectColor ) return "DirectColour" ;
113  if( type() == TrueColor ) return "TrueColour" ;
114  if( type() == StaticColor ) return "StaticColour" ;
115  if( type() == StaticGray ) return "StaticGrey" ;
116  std::stringstream ss ;
117  ss << "unrecognised visual class [" << static_cast<int>(type()) << "]" ;
118  throw Error( ss.str() ) ;
119 }
120 
121 /// \file gxvisual.cpp
::Display * x()
Returns the X object.
Definition: gxdisplay.cpp:53
Visual(Display &)
Constructor for the display's default visual.
Definition: gxvisual.cpp:28
int type() const
Returns the type.
Definition: gxvisual.cpp:98
An Xlib Display wrapper.
Definition: gxdisplay.h:38
std::string typeName() const
Returns the type name.
Definition: gxvisual.cpp:108
static std::list< Info > infoList(Display &)
Returns a list of supported "visuals" for the default screen.
Definition: gxvisual.cpp:66
An exception class for GX classes.
Definition: gxerror.h:37
int depth() const
Returns the depth.
Definition: gxvisual.cpp:103
~Visual()
Destructor.
Definition: gxvisual.cpp:57
An Xlib XVisual wrapper.
Definition: gxvisual.h:38
::VisualID id() const
Returns the id.
Definition: gxvisual.cpp:86
::Visual * x()
Returns the X object.
Definition: gxvisual.cpp:61