GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
display.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2009-2013 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <cstdlib>
28 
29 #if defined (OCTAVE_USE_WINDOWS_API)
30 #include <windows.h>
31 #elif defined (HAVE_FRAMEWORK_CARBON)
32 #include <Carbon/Carbon.h>
33 #elif defined (HAVE_X_WINDOWS)
34 #include <X11/Xlib.h>
35 #endif
36 
37 #include "singleton-cleanup.h"
38 
39 #include "display.h"
40 #include "error.h"
41 
43 
44 #if defined (HAVE_FRAMEWORK_CARBON) && ! defined (HAVE_CARBON_CGDISPLAYBITSPERPIXEL)
45 // FIXME: This will only work for MacOS > 10.5. For earlier versions
46 // this code is not needed (use CGDisplayBitsPerPixel instead).
47 size_t DisplayBitsPerPixel (CGDirectDisplayID display)
48 {
49  CGDisplayModeRef mode = CGDisplayCopyDisplayMode (display);
50  CFStringRef pixelEncoding = CGDisplayModeCopyPixelEncoding (mode);
51 
52  if (CFStringCompare (pixelEncoding, CFSTR (IO32BitDirectPixels), 0) == 0)
53  return 32;
54  else if (CFStringCompare (pixelEncoding, CFSTR (IO16BitDirectPixels), 0) == 0)
55  return 16;
56  else
57  return 8;
58 }
59 #endif
60 
61 void
62 display_info::init (bool query)
63 {
64  if (query)
65  {
66 #if defined (OCTAVE_USE_WINDOWS_API)
67 
68  HDC hdc = GetDC (0);
69 
70  if (hdc)
71  {
72  dp = GetDeviceCaps (hdc, BITSPIXEL);
73 
74  ht = GetDeviceCaps (hdc, VERTRES);
75  wd = GetDeviceCaps (hdc, HORZRES);
76 
77  double ht_mm = GetDeviceCaps (hdc, VERTSIZE);
78  double wd_mm = GetDeviceCaps (hdc, HORZSIZE);
79 
80  rx = wd * 25.4 / wd_mm;
81  ry = ht * 25.4 / ht_mm;
82 
83  dpy_avail = true;
84  }
85  else
86  err_msg = "no graphical display found";
87 
88 #elif defined (HAVE_FRAMEWORK_CARBON)
89 
90  CGDirectDisplayID display = CGMainDisplayID ();
91 
92  if (display)
93  {
94 # if defined (HAVE_CARBON_CGDISPLAYBITSPERPIXEL)
95  // For MacOS < 10.7 use the line below
96  dp = CGDisplayBitsPerPixel (display);
97 # else
98  // For MacOS > 10.5 use the line below
99  dp = DisplayBitsPerPixel (display);
100 # endif
101 
102  ht = CGDisplayPixelsHigh (display);
103  wd = CGDisplayPixelsWide (display);
104 
105  CGSize sz_mm = CGDisplayScreenSize (display);
106  // For MacOS >= 10.6, CGSize is a struct keeping 2 CGFloat values,
107  // but the CGFloat typedef is not present on older systems,
108  // so use double instead.
109  double ht_mm = sz_mm.height;
110  double wd_mm = sz_mm.width;
111 
112  rx = wd * 25.4 / wd_mm;
113  ry = ht * 25.4 / ht_mm;
114 
115  dpy_avail = true;
116  }
117  else
118  err_msg = "no graphical display found";
119 
120 #elif defined (HAVE_X_WINDOWS)
121 
122  const char *display_name = getenv ("DISPLAY");
123 
124  if (display_name && *display_name)
125  {
126  Display *display = XOpenDisplay (display_name);
127 
128  if (display)
129  {
130  Screen *screen = DefaultScreenOfDisplay (display);
131 
132  if (screen)
133  {
134  dp = DefaultDepthOfScreen (screen);
135 
136  ht = HeightOfScreen (screen);
137  wd = WidthOfScreen (screen);
138 
139  int screen_number = XScreenNumberOfScreen (screen);
140 
141  double ht_mm = DisplayHeightMM (display, screen_number);
142  double wd_mm = DisplayWidthMM (display, screen_number);
143 
144  rx = wd * 25.4 / wd_mm;
145  ry = ht * 25.4 / ht_mm;
146  }
147  else
148  err_msg = "X11 display has no default screen";
149 
150  XCloseDisplay (display);
151 
152  dpy_avail = true;
153  }
154  else
155  err_msg = "unable to open X11 DISPLAY";
156  }
157  else
158  err_msg = "X11 DISPLAY environment variable not set";
159 #else
160 
161  err_msg = "no graphical display found";
162 
163 #endif
164  }
165 }
166 
167 bool
169 {
170  bool retval = true;
171 
172  if (! instance)
173  {
174  instance = new display_info (query);
175 
176  if (instance)
178  }
179 
180  if (! instance)
181  {
182  ::error ("unable to create display_info object!");
183 
184  retval = false;
185  }
186 
187  return retval;
188 }