GNU Octave  4.4.1
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-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <stdlib.h>
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 "cdisplay.h"
38 
39 // Programming Note: This file exists so that we can hide system
40 // header files that make heavy use of macros and C-style casts in a C
41 // language file and avoid warnings about using old-style casts in C++.
42 // Additionally, on OS X systems, including the Carbon.h header file
43 // results in the declaration of a "panic" function that conflicts with
44 // Octave's global panic function, so Carbon.h can't be included in any
45 // file that also includes Octave's error.h header file.
46 
47 // Please do NOT eliminate this file and move code from here to
48 // display.cc.
49 
50 const char *
51 octave_get_display_info (const char *dpy_name, int *ht, int *wd, int *dp,
52  double *rx, double *ry, int *dpy_avail)
53 {
54  const char *msg = NULL;
55 
56  *dpy_avail = 0;
57 
58 #if defined (OCTAVE_USE_WINDOWS_API)
59 
60  octave_unused_parameter (dpy_name);
61 
62  HDC hdc = GetDC (0);
63 
64  if (hdc)
65  {
66  *dp = GetDeviceCaps (hdc, BITSPIXEL);
67 
68  *ht = GetDeviceCaps (hdc, VERTRES);
69  *wd = GetDeviceCaps (hdc, HORZRES);
70 
71  double ht_mm = GetDeviceCaps (hdc, VERTSIZE);
72  double wd_mm = GetDeviceCaps (hdc, HORZSIZE);
73 
74  *rx = *wd * 25.4 / wd_mm;
75  *ry = *ht * 25.4 / ht_mm;
76 
77  *dpy_avail = 1;
78  }
79  else
80  msg = "no graphical display found";
81 
82 #elif defined (HAVE_FRAMEWORK_CARBON)
83 
84  octave_unused_parameter (dpy_name);
85 
86  CGDirectDisplayID display = CGMainDisplayID ();
87 
88  if (display)
89  {
90 #if defined (HAVE_CARBON_CGDISPLAYBITSPERPIXEL)
91 
92  *dp = CGDisplayBitsPerPixel (display);
93 
94 #else
95 
96  /* FIXME: This will only work for MacOS > 10.5. For earlier versions
97  this code is not needed (use CGDisplayBitsPerPixel instead). */
98 
99  CGDisplayModeRef mode = CGDisplayCopyDisplayMode (display);
100  CFStringRef pixelEncoding = CGDisplayModeCopyPixelEncoding (mode);
101 
102  if (CFStringCompare (pixelEncoding, CFSTR (IO32BitDirectPixels), 0) == 0)
103  *dp = 32;
104  else if (CFStringCompare (pixelEncoding,
105  CFSTR (IO16BitDirectPixels), 0) == 0)
106  *dp = 16;
107  else
108  *dp = 8;
109 
110 #endif
111 
112  *ht = CGDisplayPixelsHigh (display);
113  *wd = CGDisplayPixelsWide (display);
114 
115  CGSize sz_mm = CGDisplayScreenSize (display);
116 
117  /* For MacOS >= 10.6, CGSize is a struct keeping 2 CGFloat
118  values, but the CGFloat typedef is not present on older
119  systems, so use double instead. */
120 
121  double ht_mm = sz_mm.height;
122  double wd_mm = sz_mm.width;
123 
124  *rx = *wd * 25.4 / wd_mm;
125  *ry = *ht * 25.4 / ht_mm;
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  double ht_mm = DisplayHeightMM (display, screen_number);
153  double wd_mm = DisplayWidthMM (display, screen_number);
154 
155  *rx = *wd * 25.4 / wd_mm;
156  *ry = *ht * 25.4 / ht_mm;
157  }
158  else
159  msg = "X11 display has no default screen";
160 
161  XCloseDisplay (display);
162 
163  *dpy_avail = 1;
164  }
165  else
166  msg = "unable to open X11 DISPLAY";
167 
168 #else
169 
170  octave_unused_parameter (dpy_name);
171  octave_unused_parameter (ht);
172  octave_unused_parameter (wd);
173  octave_unused_parameter (dp);
174  octave_unused_parameter (rx);
175  octave_unused_parameter (ry);
176 
177  msg = "no graphical display found";
178 
179 #endif
180 
181  return msg;
182 }
An image of characters with associated attributes.
Definition: Screen.h:75
Return the CPU time used by your Octave session The first output is the total time spent executing your process and is equal to the sum of second and third which are the number of CPU seconds spent executing in user mode and the number of CPU seconds spent executing in system mode
Definition: data.cc:6348
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:51