y_defplot.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. /* This file defines the initialization for any XPlotter object, including
  16. both private data and public methods. There is a one-to-one
  17. correspondence between public methods and user-callable functions in the
  18. C API. */
  19. #include "sys-defines.h"
  20. #include <signal.h> /* for kill() */
  21. #include "extern.h"
  22. /* Sparse array of pointers to XPlotter instances, and its size. Should be
  23. initialized to a NULL pointer, and 0, respectively.
  24. In libplotter these are not global variables: in extern.h, they are
  25. #define'd to be static data members of the XPlotter class.
  26. Accessed by _pl_y_initialize() and _pl_y_terminate() in this file, by
  27. _pl_y_maybe_handle_x_events() in y_openpl.c, and by _pl_y_closepl() in
  28. y_closepl.c. */
  29. XPlotter **_xplotters = NULL;
  30. int _xplotters_len = 0;
  31. /* initial size for "_xplotters", the sparse array of XPlotter instances */
  32. #define INITIAL_XPLOTTERS_LEN 4
  33. /* Mutex for locking _xplotters[] and _xplotters_len. */
  34. #ifdef PTHREAD_SUPPORT
  35. #ifdef HAVE_PTHREAD_H
  36. pthread_mutex_t _xplotters_mutex = PTHREAD_MUTEX_INITIALIZER;
  37. #endif
  38. #endif
  39. #ifndef LIBPLOTTER
  40. /* In libplot, this is the initialization for the function-pointer part of
  41. an XPlotter struct. */
  42. const Plotter _pl_y_default_plotter =
  43. {
  44. /* initialization (after creation) and termination (before deletion) */
  45. _pl_y_initialize, _pl_y_terminate,
  46. /* page manipulation */
  47. _pl_y_begin_page, _pl_y_erase_page, _pl_y_end_page,
  48. /* drawing state manipulation */
  49. _pl_x_push_state, _pl_x_pop_state,
  50. /* internal path-painting methods (endpath() is a wrapper for the first) */
  51. _pl_x_paint_path, _pl_x_paint_paths, _pl_x_path_is_flushable, _pl_x_maybe_prepaint_segments,
  52. /* internal methods for drawing of markers and points */
  53. _pl_g_paint_marker, _pl_x_paint_point,
  54. /* internal methods that plot strings in Hershey, non-Hershey fonts */
  55. _pl_g_paint_text_string_with_escapes, _pl_x_paint_text_string,
  56. _pl_x_get_text_width,
  57. /* private low-level `retrieve font' method */
  58. _pl_x_retrieve_font,
  59. /* `flush output' method, called only if Plotter handles its own output */
  60. _pl_x_flush_output,
  61. /* internal error handlers */
  62. _pl_g_warning,
  63. _pl_g_error,
  64. };
  65. #endif /* not LIBPLOTTER */
  66. /* The private `initialize' method, which is invoked when a Plotter is
  67. created. It is used for such things as initializing capability flags
  68. from the values of class variables, allocating storage, etc. When this
  69. is invoked, _plotter points to the Plotter that has just been
  70. created. */
  71. void
  72. _pl_y_initialize (S___(Plotter *_plotter))
  73. {
  74. bool open_slot = false;
  75. int i, j;
  76. #ifndef LIBPLOTTER
  77. /* in libplot, manually invoke superclass initialization method */
  78. _pl_x_initialize (S___(_plotter));
  79. #endif
  80. #ifdef PTHREAD_SUPPORT
  81. #ifdef HAVE_PTHREAD_H
  82. /* lock the global variables _xplotters[] and _xplotters_len */
  83. pthread_mutex_lock (&_xplotters_mutex);
  84. #endif
  85. #endif
  86. /* If this is the first XPlotter to be created, initialize Xt library.
  87. At least in X11R6, it's OK to initialize it more than once, but we're
  88. careful. */
  89. if (_xplotters_len == 0)
  90. {
  91. /* first initialize Xlib and Xt thread support if any */
  92. #ifdef PTHREAD_SUPPORT
  93. #ifdef HAVE_PTHREAD_H
  94. #ifdef X_THREAD_SUPPORT
  95. XInitThreads ();
  96. XtToolkitThreadInitialize ();
  97. #endif
  98. #endif
  99. #endif
  100. /* initialize Xt library itself */
  101. XtToolkitInitialize ();
  102. }
  103. /* ensure XPlotter array is set up */
  104. if (_xplotters_len == 0)
  105. {
  106. _xplotters = (XPlotter **)_pl_xmalloc (INITIAL_XPLOTTERS_LEN * sizeof(XPlotter *));
  107. for (i = 0; i < INITIAL_XPLOTTERS_LEN; i++)
  108. _xplotters[i] = (XPlotter *)NULL;
  109. _xplotters_len = INITIAL_XPLOTTERS_LEN;
  110. }
  111. /* be sure there is an open slot (slot i) */
  112. for (i = 0; i < _xplotters_len; i++)
  113. if (_xplotters[i] == NULL)
  114. {
  115. open_slot = true;
  116. break;
  117. }
  118. if (!open_slot)
  119. /* expand array, clearing upper half */
  120. {
  121. i = _xplotters_len;
  122. _xplotters =
  123. (XPlotter **)_pl_xrealloc (_xplotters,
  124. 2 * _xplotters_len * sizeof (XPlotter *));
  125. for (j = _xplotters_len; j < 2 * _xplotters_len; j++)
  126. _xplotters[j] = (XPlotter *)NULL;
  127. _xplotters_len *= 2;
  128. }
  129. /* place just-created Plotter in open slot */
  130. _xplotters[i] = _plotter;
  131. #ifdef PTHREAD_SUPPORT
  132. #ifdef HAVE_PTHREAD_H
  133. /* unlock the global variables _xplotters[] and _xplotters_len */
  134. pthread_mutex_unlock (&_xplotters_mutex);
  135. #endif
  136. #endif
  137. /* override superclass initializations, as necessary */
  138. #ifndef LIBPLOTTER
  139. /* tag field, differs in derived classes */
  140. _plotter->data->type = PL_X11;
  141. #endif
  142. /* output model */
  143. _plotter->data->output_model = PL_OUTPUT_VIA_CUSTOM_ROUTINES_TO_NON_STREAM;
  144. /* initialize data members specific to this derived class */
  145. _plotter->y_app_con = (XtAppContext)NULL;
  146. _plotter->y_toplevel = (Widget)NULL;
  147. _plotter->y_canvas = (Widget)NULL;
  148. _plotter->y_drawable4 = (Drawable)0;
  149. _plotter->y_auto_flush = true;
  150. _plotter->y_vanish_on_delete = false;
  151. _plotter->y_pids = (pid_t *)NULL;
  152. _plotter->y_num_pids = 0;
  153. _plotter->y_event_handler_count = 0;
  154. /* initialize certain data members from device driver parameters */
  155. /* determine whether to do an XFlush() after each drawing operation */
  156. {
  157. const char *vanish_s;
  158. vanish_s = (const char *)_get_plot_param (_plotter->data,
  159. "X_AUTO_FLUSH");
  160. if (strcasecmp (vanish_s, "no") == 0)
  161. _plotter->y_auto_flush = false;
  162. else
  163. _plotter->y_auto_flush = true;
  164. }
  165. /* determine whether windows vanish on Plotter deletion */
  166. {
  167. const char *vanish_s;
  168. vanish_s = (const char *)_get_plot_param (_plotter->data,
  169. "VANISH_ON_DELETE");
  170. if (strcasecmp (vanish_s, "yes") == 0)
  171. _plotter->y_vanish_on_delete = true;
  172. else
  173. _plotter->y_vanish_on_delete = false;
  174. }
  175. }
  176. /* The private `terminate' method, which is invoked when a Plotter is
  177. deleted. It may do such things as write to an output stream from
  178. internal storage, deallocate storage, etc. When this is invoked,
  179. _plotter points to the Plotter that is about to be deleted. */
  180. void
  181. _pl_y_terminate (S___(Plotter *_plotter))
  182. {
  183. int i, j;
  184. /* kill forked-off processes that are maintaining XPlotter's popped-up
  185. windows, provided that the VANISH_ON_DELETE parameter was set to "yes"
  186. at creation time */
  187. if (_plotter->y_vanish_on_delete)
  188. {
  189. for (j = 0; j < _plotter->y_num_pids; j++)
  190. kill (_plotter->y_pids[j], SIGKILL);
  191. if (_plotter->y_num_pids > 0)
  192. {
  193. free (_plotter->y_pids);
  194. _plotter->y_pids = (pid_t *)NULL;
  195. }
  196. }
  197. /* remove XPlotter from sparse XPlotter array */
  198. #ifdef PTHREAD_SUPPORT
  199. #ifdef HAVE_PTHREAD_H
  200. /* lock the global variables _xplotters[] and _xplotters_len */
  201. pthread_mutex_lock (&_xplotters_mutex);
  202. #endif
  203. #endif
  204. for (i = 0; i < _xplotters_len; i++)
  205. if (_xplotters[i] == _plotter)
  206. {
  207. _xplotters[i] = (XPlotter *)NULL;
  208. break;
  209. }
  210. #ifdef PTHREAD_SUPPORT
  211. #ifdef HAVE_PTHREAD_H
  212. /* unlock the global variables _xplotters[] and _xplotters_len */
  213. pthread_mutex_unlock (&_xplotters_mutex);
  214. #endif
  215. #endif
  216. #ifndef LIBPLOTTER
  217. /* in libplot, manually invoke superclass termination method */
  218. _pl_x_terminate (S___(_plotter));
  219. #endif
  220. }
  221. #ifdef LIBPLOTTER
  222. XPlotter::XPlotter (FILE *infile, FILE *outfile, FILE *errfile)
  223. :XDrawablePlotter (infile, outfile, errfile)
  224. {
  225. _pl_y_initialize ();
  226. /* add to _xplotters sparse array */
  227. }
  228. XPlotter::XPlotter (FILE *outfile)
  229. :XDrawablePlotter (outfile)
  230. {
  231. _pl_y_initialize ();
  232. /* add to _xplotters sparse array */
  233. }
  234. XPlotter::XPlotter (istream& in, ostream& out, ostream& err)
  235. : XDrawablePlotter (in, out, err)
  236. {
  237. _pl_y_initialize ();
  238. /* add to _xplotters sparse array */
  239. }
  240. XPlotter::XPlotter (ostream& out)
  241. : XDrawablePlotter (out)
  242. {
  243. _pl_y_initialize ();
  244. /* add to _xplotters sparse array */
  245. }
  246. XPlotter::XPlotter ()
  247. {
  248. _pl_y_initialize ();
  249. /* add to _xplotters sparse array */
  250. }
  251. XPlotter::XPlotter (FILE *infile, FILE *outfile, FILE *errfile, PlotterParams &parameters)
  252. :XDrawablePlotter (infile, outfile, errfile, parameters)
  253. {
  254. _pl_y_initialize ();
  255. /* add to _xplotters sparse array */
  256. }
  257. XPlotter::XPlotter (FILE *outfile, PlotterParams &parameters)
  258. :XDrawablePlotter (outfile, parameters)
  259. {
  260. _pl_y_initialize ();
  261. /* add to _xplotters sparse array */
  262. }
  263. XPlotter::XPlotter (istream& in, ostream& out, ostream& err, PlotterParams &parameters)
  264. : XDrawablePlotter (in, out, err, parameters)
  265. {
  266. _pl_y_initialize ();
  267. /* add to _xplotters sparse array */
  268. }
  269. XPlotter::XPlotter (ostream& out, PlotterParams &parameters)
  270. : XDrawablePlotter (out, parameters)
  271. {
  272. _pl_y_initialize ();
  273. /* add to _xplotters sparse array */
  274. }
  275. XPlotter::XPlotter (PlotterParams &parameters)
  276. : XDrawablePlotter (parameters)
  277. {
  278. _pl_y_initialize ();
  279. /* add to _xplotters sparse array */
  280. }
  281. XPlotter::~XPlotter ()
  282. {
  283. /* if luser left the Plotter open, close it */
  284. if (_plotter->data->open)
  285. _API_closepl ();
  286. _pl_y_terminate ();
  287. /* remove from _xplotters sparse array */
  288. }
  289. #endif