GNU Octave  4.2.1
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
cdisplay.c
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2009-2017 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 #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 (int *ht, int *wd, int *dp, double *rx, double *ry,
52  int *dpy_avail)
53 {
54  const char *msg = 0;
55 
56  *dpy_avail = 0;
57 
58 #if defined (OCTAVE_USE_WINDOWS_API)
59 
60  HDC hdc = GetDC (0);
61 
62  if (hdc)
63  {
64  *dp = GetDeviceCaps (hdc, BITSPIXEL);
65 
66  *ht = GetDeviceCaps (hdc, VERTRES);
67  *wd = GetDeviceCaps (hdc, HORZRES);
68 
69  double ht_mm = GetDeviceCaps (hdc, VERTSIZE);
70  double wd_mm = GetDeviceCaps (hdc, HORZSIZE);
71 
72  *rx = *wd * 25.4 / wd_mm;
73  *ry = *ht * 25.4 / ht_mm;
74 
75  *dpy_avail = 1;
76  }
77  else
78  msg = "no graphical display found";
79 
80 #elif defined (HAVE_FRAMEWORK_CARBON)
81 
82  CGDirectDisplayID display = CGMainDisplayID ();
83 
84  if (display)
85  {
86 #if defined (HAVE_CARBON_CGDISPLAYBITSPERPIXEL)
87 
88  *dp = CGDisplayBitsPerPixel (display);
89 
90 #else
91 
92  /* FIXME: This will only work for MacOS > 10.5. For earlier versions
93  this code is not needed (use CGDisplayBitsPerPixel instead). */
94 
95  CGDisplayModeRef mode = CGDisplayCopyDisplayMode (display);
96  CFStringRef pixelEncoding = CGDisplayModeCopyPixelEncoding (mode);
97 
98  if (CFStringCompare (pixelEncoding, CFSTR (IO32BitDirectPixels), 0) == 0)
99  *dp = 32;
100  else if (CFStringCompare (pixelEncoding,
101  CFSTR (IO16BitDirectPixels), 0) == 0)
102  *dp = 16;
103  else
104  *dp = 8;
105 
106 #endif
107 
108  *ht = CGDisplayPixelsHigh (display);
109  *wd = CGDisplayPixelsWide (display);
110 
111  CGSize sz_mm = CGDisplayScreenSize (display);
112 
113  /* For MacOS >= 10.6, CGSize is a struct keeping 2 CGFloat
114  values, but the CGFloat typedef is not present on older
115  systems, so use double instead. */
116 
117  double ht_mm = sz_mm.height;
118  double wd_mm = sz_mm.width;
119 
120  *rx = *wd * 25.4 / wd_mm;
121  *ry = *ht * 25.4 / ht_mm;
122 
123  *dpy_avail = 1;
124  }
125  else
126  msg = "no graphical display found";
127 
128 #elif defined (HAVE_X_WINDOWS)
129 
130  const char *display_name = getenv ("DISPLAY");
131 
132  if (display_name && *display_name)
133  {
134  Display *display = XOpenDisplay (display_name);
135 
136  if (display)
137  {
138  Screen *screen = DefaultScreenOfDisplay (display);
139 
140  if (screen)
141  {
142  *dp = DefaultDepthOfScreen (screen);
143 
144  *ht = HeightOfScreen (screen);
145  *wd = WidthOfScreen (screen);
146 
147  int screen_number = XScreenNumberOfScreen (screen);
148 
149  double ht_mm = DisplayHeightMM (display, screen_number);
150  double wd_mm = DisplayWidthMM (display, screen_number);
151 
152  *rx = *wd * 25.4 / wd_mm;
153  *ry = *ht * 25.4 / ht_mm;
154  }
155  else
156  msg = "X11 display has no default screen";
157 
158  XCloseDisplay (display);
159 
160  *dpy_avail = 1;
161  }
162  else
163  msg = "unable to open X11 DISPLAY";
164  }
165  else
166  msg = "X11 DISPLAY environment variable not set";
167 
168 #else
169 
170  octave_unused_parameter (ht);
171  octave_unused_parameter (wd);
172  octave_unused_parameter (dp);
173  octave_unused_parameter (rx);
174  octave_unused_parameter (ry);
175 
176  msg = "no graphical display found";
177 
178 #endif
179 
180  return msg;
181 }
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:6386
const char * octave_get_display_info(int *ht, int *wd, int *dp, double *rx, double *ry, int *dpy_avail)
Definition: cdisplay.c:51