/src/r-source/src/unix/X11.c
Line | Count | Source |
1 | | /* |
2 | | * R : A Computer Language for Statistical Data Analysis |
3 | | * Copyright (C) 1999-2026 The R Core Team |
4 | | * |
5 | | * This program is free software; you can redistribute it and/or modify |
6 | | * it under the terms of the GNU General Public License as published by |
7 | | * the Free Software Foundation; either version 2 of the License, or |
8 | | * (at your option) any later version. |
9 | | * |
10 | | * This program is distributed in the hope that it will be useful, |
11 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | * GNU General Public License for more details. |
14 | | * |
15 | | * You should have received a copy of the GNU General Public License |
16 | | * along with this program; if not, a copy is available at |
17 | | * https://www.R-project.org/Licenses/ |
18 | | */ |
19 | | |
20 | | #ifdef HAVE_CONFIG_H |
21 | | # include <config.h> |
22 | | #endif |
23 | | |
24 | | #include <Defn.h> |
25 | | #include <Internal.h> |
26 | | |
27 | | #include <Rconnections.h> |
28 | | #include <Rdynpriv.h> |
29 | | |
30 | | #ifdef HAVE_X11 |
31 | | |
32 | | #include <Rmodules/RX11.h> /* typedefs for the module routine types */ |
33 | | |
34 | | static R_X11Routines routines, *ptr = &routines; |
35 | | |
36 | | static int initialized = 0; |
37 | | |
38 | | R_X11Routines * R_setX11Routines(R_X11Routines *routines) |
39 | | { |
40 | | R_X11Routines *tmp; |
41 | | tmp = ptr; |
42 | | ptr = routines; |
43 | | return tmp; |
44 | | } |
45 | | |
46 | | attribute_hidden int R_X11_Init(void) |
47 | | { |
48 | | int res; |
49 | | |
50 | | if(initialized) return initialized; |
51 | | |
52 | | initialized = -1; |
53 | | if(strcmp(R_GUIType, "none") == 0) { |
54 | | warning(_("X11 module is not available under this GUI")); |
55 | | return initialized; |
56 | | } |
57 | | res = R_moduleCdynload("R_X11", 1, 1); |
58 | | if(!res) return initialized; |
59 | | if(!ptr->access) |
60 | | error(_("X11 routines cannot be accessed in module")); |
61 | | initialized = 1; |
62 | | return initialized; |
63 | | } |
64 | | |
65 | | /* used in ../main/platform.c */ |
66 | | attribute_hidden bool R_access_X11(void) |
67 | | { |
68 | | R_X11_Init(); |
69 | | return (initialized > 0) ? (*ptr->access)() > 0 : false; |
70 | | } |
71 | | |
72 | | // called from src/library/grDevices/src/stubs.c |
73 | | SEXP do_X11(SEXP call, SEXP op, SEXP args, SEXP rho) |
74 | | { |
75 | | R_X11_Init(); |
76 | | if(initialized > 0) |
77 | | return (*ptr->X11)(call, op, args, rho); |
78 | | else { |
79 | | error(_("X11 module cannot be loaded")); |
80 | | return R_NilValue; |
81 | | } |
82 | | } |
83 | | |
84 | | // called from src/library/grDevices/src/stubs.c |
85 | | SEXP do_saveplot(SEXP call, SEXP op, SEXP args, SEXP rho) |
86 | | { |
87 | | R_X11_Init(); |
88 | | if(initialized > 0) |
89 | | return (*ptr->saveplot)(call, op, args, rho); |
90 | | else { |
91 | | error(_("X11 module cannot be loaded")); |
92 | | return R_NilValue; |
93 | | } |
94 | | } |
95 | | |
96 | | // exported for src/include/R_ext/GetX11Image.h (and package tkrplot) |
97 | | Rboolean R_GetX11Image(int d, void *pximage, int *pwidth, int *pheight) |
98 | | { |
99 | | R_X11_Init(); |
100 | | if(initialized > 0) |
101 | | return (*ptr->image)(d, pximage, pwidth, pheight); |
102 | | else { |
103 | | error(_("X11 module cannot be loaded")); |
104 | | return FALSE; |
105 | | } |
106 | | } |
107 | | |
108 | | attribute_hidden Rboolean R_ReadClipboard(Rclpconn clpcon, char *type) |
109 | | { |
110 | | R_X11_Init(); |
111 | | if(initialized > 0) |
112 | | return (*ptr->readclp)(clpcon, type); |
113 | | else { |
114 | | error(_("X11 module cannot be loaded")); |
115 | | return FALSE; |
116 | | } |
117 | | } |
118 | | |
119 | | SEXP do_bmVersion(void) |
120 | | { |
121 | | SEXP ans = PROTECT(allocVector(STRSXP, 3)), |
122 | | nms = PROTECT(allocVector(STRSXP, 3)); |
123 | | setAttrib(ans, R_NamesSymbol, nms); |
124 | | SET_STRING_ELT(nms, 0, mkChar("libpng")); |
125 | | SET_STRING_ELT(nms, 1, mkChar("jpeg")); |
126 | | SET_STRING_ELT(nms, 2, mkChar("libtiff")); |
127 | | R_X11_Init(); |
128 | | if(initialized > 0) { |
129 | | SET_STRING_ELT(ans, 0, mkChar((*ptr->R_pngVersion)())); |
130 | | SET_STRING_ELT(ans, 1, mkChar((*ptr->R_jpegVersion)())); |
131 | | SET_STRING_ELT(ans, 2, mkChar((*ptr->R_tiffVersion)())); |
132 | | } |
133 | | UNPROTECT(2); |
134 | | return ans; |
135 | | } |
136 | | #else /* No HAVE_X11 */ |
137 | | |
138 | | attribute_hidden bool R_access_X11(void) |
139 | 0 | { |
140 | 0 | return false; |
141 | 0 | } |
142 | | |
143 | | SEXP do_X11(SEXP call, SEXP op, SEXP args, SEXP rho) |
144 | 0 | { |
145 | 0 | error(_("X11 is not available")); |
146 | 0 | return R_NilValue; |
147 | 0 | } |
148 | | |
149 | | SEXP do_saveplot(SEXP call, SEXP op, SEXP args, SEXP rho) |
150 | 0 | { |
151 | 0 | error(_("X11 is not available")); |
152 | 0 | return R_NilValue; |
153 | 0 | } |
154 | | |
155 | | Rboolean R_GetX11Image(int d, void *pximage, int *pwidth, int *pheight) |
156 | 0 | { |
157 | 0 | error(_("X11 is not available")); |
158 | 0 | return FALSE; |
159 | 0 | } |
160 | | |
161 | | attribute_hidden Rboolean R_ReadClipboard(Rclpconn con, char *type) |
162 | 0 | { |
163 | 0 | error(_("X11 is not available")); |
164 | 0 | return FALSE; |
165 | 0 | } |
166 | | |
167 | | SEXP do_bmVersion(void) |
168 | 0 | { |
169 | 0 | SEXP ans = PROTECT(allocVector(STRSXP, 3)), |
170 | 0 | nms = PROTECT(allocVector(STRSXP, 3)); |
171 | 0 | setAttrib(ans, R_NamesSymbol, nms); |
172 | 0 | SET_STRING_ELT(nms, 0, mkChar("libpng")); |
173 | 0 | SET_STRING_ELT(nms, 1, mkChar("jpeg")); |
174 | 0 | SET_STRING_ELT(nms, 2, mkChar("libtiff")); |
175 | 0 | UNPROTECT(2); |
176 | 0 | return ans; |
177 | 0 | } |
178 | | #endif |