GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
display-available.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2012-2024 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 "display-available.h"
41 
42 const char *
43 display_available (int *dpy_avail)
44 {
45  *dpy_avail = 0;
46 
47  const char *err_msg = "";
48 
49 #if defined (OCTAVE_USE_WINDOWS_API)
50 
51  HDC hdc = GetDC (0);
52 
53  if (hdc)
54  *dpy_avail = 1;
55  else
56  err_msg = "no graphical display found";
57 
58 #elif defined (HAVE_FRAMEWORK_CARBON)
59 
60  CGDirectDisplayID display = CGMainDisplayID ();
61 
62  if (display)
63  *dpy_avail = 1;
64  else
65  err_msg = "no graphical display found";
66 
67 #elif defined (HAVE_X_WINDOWS)
68 
69  const char *display_name = getenv ("DISPLAY");
70 
71  if (display_name && *display_name)
72  {
73  Display *display = XOpenDisplay (display_name);
74 
75  if (display)
76  {
77  Screen *screen = DefaultScreenOfDisplay (display);
78 
79  if (! screen)
80  err_msg = "X11 display has no default screen";
81 
82  XCloseDisplay (display);
83 
84  *dpy_avail = 1;
85  }
86  else
87  err_msg = "unable to open X11 DISPLAY";
88  }
89  else
90  err_msg = "X11 DISPLAY environment variable not set";
91 
92 #else
93 
94  err_msg = "no graphical display found";
95 
96 #endif
97 
98  return err_msg;
99 }
const char * display_available(int *dpy_avail)