GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
cdisplay.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2009-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <stdlib.h>
31 
32 #if defined (OCTAVE_USE_WINDOWS_API)
33 #include <windows.h>
34 #elif defined (HAVE_FRAMEWORK_CARBON)
35 #include <Carbon/Carbon.h>
36 #elif defined (HAVE_X_WINDOWS)
37 #include <X11/Xlib.h>
38 #endif
39 
40 #include "cdisplay.h"
41 
42 // Programming Note: This file exists so that we can hide system
43 // header files that make heavy use of macros and C-style casts in a C
44 // language file and avoid warnings about using old-style casts in C++.
45 // Additionally, on OS X systems, including the Carbon.h header file
46 // results in the declaration of a "panic" function that conflicts with
47 // Octave's global panic function, so Carbon.h can't be included in any
48 // file that also includes Octave's error.h header file.
49 
50 // Please do NOT eliminate this file and move code from here to
51 // display.cc.
52 
53 const char *
54 octave_get_display_info (const char *dpy_name, int *ht, int *wd, int *dp,
55  double *rx, double *ry, int *dpy_avail)
56 {
57  const char *msg = NULL;
58 
59  *dpy_avail = 0;
60 
61  double ht_mm = 0.0;
62  double wd_mm = 0.0;
63 
64 #if defined (OCTAVE_USE_WINDOWS_API)
65 
66  octave_unused_parameter (dpy_name);
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  ht_mm = GetDeviceCaps (hdc, VERTSIZE);
78  wd_mm = GetDeviceCaps (hdc, HORZSIZE);
79 
80  *dpy_avail = 1;
81  }
82  else
83  msg = "no graphical display found";
84 
85 #elif defined (HAVE_FRAMEWORK_CARBON)
86 
87  octave_unused_parameter (dpy_name);
88 
89  CGDirectDisplayID display = CGMainDisplayID ();
90 
91  if (display)
92  {
93 #if defined (HAVE_CARBON_CGDISPLAYBITSPERPIXEL)
94 
95  *dp = CGDisplayBitsPerPixel (display);
96 
97 #else
98 
99  /* FIXME: This will only work for MacOS > 10.5. For earlier versions
100  this code is not needed (use CGDisplayBitsPerPixel instead). */
101 
102  CGDisplayModeRef mode = CGDisplayCopyDisplayMode (display);
103  CFStringRef pixelEncoding = CGDisplayModeCopyPixelEncoding (mode);
104 
105  if (CFStringCompare (pixelEncoding, CFSTR (IO32BitDirectPixels), 0) == 0)
106  *dp = 32;
107  else if (CFStringCompare (pixelEncoding,
108  CFSTR (IO16BitDirectPixels), 0) == 0)
109  *dp = 16;
110  else
111  *dp = 8;
112 
113 #endif
114 
115  *ht = CGDisplayPixelsHigh (display);
116  *wd = CGDisplayPixelsWide (display);
117 
118  CGSize sz_mm = CGDisplayScreenSize (display);
119 
120  /* For MacOS >= 10.6, CGSize is a struct keeping 2 CGFloat
121  values, but the CGFloat typedef is not present on older
122  systems, so use double instead. */
123 
124  ht_mm = sz_mm.height;
125  wd_mm = sz_mm.width;
126 
127  *dpy_avail = 1;
128  }
129  else
130  msg = "no graphical display found";
131 
132 #elif defined (HAVE_X_WINDOWS)
133 
134  /* If dpy_name is NULL, XopenDisplay will look for DISPLAY in the
135  environment. */
136 
137  Display *display = XOpenDisplay (dpy_name);
138 
139  if (display)
140  {
141  Screen *screen = DefaultScreenOfDisplay (display);
142 
143  if (screen)
144  {
145  *dp = DefaultDepthOfScreen (screen);
146 
147  *ht = HeightOfScreen (screen);
148  *wd = WidthOfScreen (screen);
149 
150  int screen_number = XScreenNumberOfScreen (screen);
151 
152  ht_mm = DisplayHeightMM (display, screen_number);
153  wd_mm = DisplayWidthMM (display, screen_number);
154 
155  *dpy_avail = 1;
156  }
157  else
158  msg = "X11 display has no default screen";
159 
160  XCloseDisplay (display);
161  }
162  else
163  msg = "unable to open X11 DISPLAY";
164 
165 #else
166 
167  octave_unused_parameter (dpy_name);
168  octave_unused_parameter (ht);
169  octave_unused_parameter (wd);
170  octave_unused_parameter (dp);
171  octave_unused_parameter (rx);
172  octave_unused_parameter (ry);
173 
174  msg = "no graphical display found";
175 
176 #endif
177 
178  if (*dpy_avail)
179  {
180  if (wd_mm == 0 || ht_mm == 0)
181  {
182  msg = "screen width or height reported to be zero";
183 
184  // Sizes reported as zero have been found on some systems.
185  // For example, X/Wayland running inside virtualbox.
186 
187  // Guess a DPI.
188 
189  *rx = 96.0;
190  *ry = 96.0;
191  }
192  else
193  {
194  *rx = *wd * 25.4 / wd_mm;
195  *ry = *ht * 25.4 / ht_mm;
196  }
197  }
198 
199  return msg;
200 }
const char * octave_get_display_info(const char *dpy_name, int *ht, int *wd, int *dp, double *rx, double *ry, int *dpy_avail)
Definition: cdisplay.c:54
An image of characters with associated attributes.
Definition: Screen.h:76