123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873 |
- #include "sys-defines.h"
- #include "extern.h"
- #ifdef HAVE_SYS_TIME_H
- #include <sys/time.h>
- #endif
- #ifdef HAVE_SYS_SELECT_H
- #include <sys/select.h>
- #endif
- #ifdef HAVE_SYS_TYPES_H
- #include <sys/types.h>
- #endif
- #ifdef HAVE_UNISTD_H
- #include <unistd.h>
- #endif
- #ifdef PTHREAD_SUPPORT
- #ifdef HAVE_PTHREAD_H
- extern pthread_mutex_t _xplotters_mutex;
- #endif
- #endif
- #define XPLOT_APP_NAME "xplot"
- #define XPLOT_APP_CLASS "Xplot"
- static const String _xplot_fallback_resources[] =
- {
- (String)"Xplot*geometry: 570x570",
- (String)NULL
- };
- #define MAX_FAKE_ARGV_LENGTH 8
- static const String _xplot_translations_before_forking =
- #ifdef USE_MOTIF
- (String)"<Btn2Down>: ProcessDrag()";
- #else
- (String)"";
- #endif
- static const String _xplot_translations_after_forking =
- #ifdef USE_MOTIF
- (String)"<Btn1Down>: Foldup()\n\
- <Btn2Down>: ProcessDrag()\n\
- <Btn3Down>: Foldup()\n\
- <Key>Q: Foldup()\n\
- <Key>q: Foldup()";
- #else
- (String)"<Btn1Down>: Foldup()\n\
- <Btn3Down>: Foldup()\n\
- <Key>Q: Foldup()\n\
- <Key>q: Foldup()";
- #endif
- static bool _bitmap_size_ok (const char *bitmap_size_s);
- static void Foldup (Widget widget, XEvent *event, String *params, Cardinal *num_params);
- #ifndef HAVE_STRERROR
- static char * _plot_strerror (int errnum);
- #define strerror _plot_strerror
- #endif
- void
- _pl_y_set_data_for_quitting (S___(Plotter *_plotter))
- {
- Arg wargs[1];
- #ifdef USE_MOTIF
- XtSetArg (wargs[0], XmNtranslations,
- XtParseTranslationTable(_xplot_translations_after_forking));
- #else
- XtSetArg (wargs[0], XtNtranslations,
- XtParseTranslationTable(_xplot_translations_after_forking));
- #endif
- XtSetValues (_plotter->y_canvas, wargs, (Cardinal)1);
- }
- static void
- Foldup (Widget widget, XEvent *event, String *params, Cardinal *num_params)
- {
- Display *dpy;
-
- dpy = XtDisplay (widget);
- XtDestroyWidget (XtParent (widget));
- XFlush (dpy);
- exit (EXIT_SUCCESS);
- }
- static const XtActionsRec _xplot_actions[] =
- {
- {(String)"Foldup", Foldup},
- };
- bool
- _pl_y_begin_page (S___(Plotter *_plotter))
- {
- Arg wargs[10];
- Dimension window_height, window_width;
- Screen *screen_struct;
- String fake_argv[MAX_FAKE_ARGV_LENGTH];
- const char *double_buffer_s;
- int fake_argc;
- int screen;
-
-
-
- _plotter->y_app_con = XtCreateApplicationContext();
- if (_plotter->y_app_con == (XtAppContext)NULL)
- {
- _plotter->error (R___(_plotter) "an X application context could not be created");
- return false;
- }
-
- XtAppSetFallbackResources (_plotter->y_app_con,
- (String *)_xplot_fallback_resources);
-
- XtAppAddActions (_plotter->y_app_con, (XtActionsRec *)_xplot_actions,
- XtNumber (_xplot_actions));
-
-
- fake_argc = 0;
- fake_argv[fake_argc++] = (String)XPLOT_APP_NAME;
-
- {
- const char *display_s;
-
- display_s = (char *)_get_plot_param (_plotter->data, "DISPLAY");
- if (display_s == NULL || *display_s == '\0')
- {
- _plotter->error (R___(_plotter)
- "the Plotter could not be opened, as the DISPLAY parameter is null");
- return false;
- }
- fake_argv[fake_argc++] = (String)"-display";
- fake_argv[fake_argc++] = (String)display_s;
- }
-
-
- {
- char *bitmap_size_s;
-
- bitmap_size_s = (char *)_get_plot_param (_plotter->data, "BITMAPSIZE");
- if (bitmap_size_s && _bitmap_size_ok (bitmap_size_s))
- {
- fake_argv[fake_argc++] = (String)"-geometry";
- fake_argv[fake_argc++] = (String)bitmap_size_s;
- }
- }
-
- {
- const char *bg_color_s;
-
- bg_color_s = (char *)_get_plot_param (_plotter->data, "BG_COLOR");
- if (bg_color_s)
- {
- plColor color;
- char rgb[8];
- if (_string_to_color (bg_color_s, &color, _plotter->data->color_name_cache))
-
- {
- if (_plotter->data->emulate_color)
-
- {
- int gray;
- gray = _grayscale_approx (color.red, color.green, color.blue);
- sprintf (rgb, "#%02X%02X%02X", gray, gray, gray);
- }
- else
- sprintf (rgb, "#%02X%02X%02X",
- color.red, color.green, color.blue);
- bg_color_s = rgb;
- }
- else
-
- {
- if (_plotter->x_bg_color_warning_issued == false)
- {
- char *buf;
-
- buf = (char *)_pl_xmalloc (strlen (bg_color_s) + 100);
- sprintf (buf, "substituting \"white\" for undefined background color \"%s\"",
- bg_color_s);
- _plotter->warning (R___(_plotter) buf);
- free (buf);
- _plotter->x_bg_color_warning_issued = true;
-
- bg_color_s = "white";
- }
- }
- fake_argv[fake_argc++] = (String)"-bg";
- fake_argv[fake_argc++] = (String)bg_color_s;
- }
- }
-
- fake_argv[fake_argc] = (String)NULL;
-
- _plotter->x_dpy =
- XtOpenDisplay (_plotter->y_app_con,
-
- (String)NULL,
-
- (String)NULL,
-
- (String)XPLOT_APP_CLASS,
-
- NULL, (Cardinal)0,
-
- &fake_argc, fake_argv);
- if (_plotter->x_dpy == (Display *)NULL)
- {
- char *display_s;
- display_s = (char *)_get_plot_param (_plotter->data, "DISPLAY");
- if (display_s == NULL)
- _plotter->error (R___(_plotter)
- "the X Window System display could not be opened, as it is null");
- else
- {
- char *buf;
- buf = (char *)_pl_xmalloc(strlen(display_s) + 1 + 50);
- sprintf (buf, "the X Window System display \"%s\" could not be opened",
- display_s);
- _plotter->error (R___(_plotter) buf);
- free (buf);
- }
- return false;
- }
-
-
- screen = DefaultScreen (_plotter->x_dpy);
- screen_struct = ScreenOfDisplay (_plotter->x_dpy, screen);
- _plotter->x_visual = DefaultVisualOfScreen (screen_struct);
- _plotter->x_cmap = DefaultColormapOfScreen (screen_struct);
- _plotter->x_cmap_type = X_CMAP_ORIG;
-
-
- _plotter->x_max_polyline_len = XMaxRequestSize(_plotter->x_dpy) / 2;
-
-
- XtSetArg(wargs[0], XtNscreen, screen_struct);
- XtSetArg(wargs[1], XtNargc, fake_argc);
- XtSetArg(wargs[2], XtNargv, fake_argv);
- _plotter->y_toplevel = XtAppCreateShell(NULL,
- (String)XPLOT_APP_CLASS,
- applicationShellWidgetClass,
- _plotter->x_dpy,
-
- wargs, (Cardinal)3);
-
- #ifdef USE_MOTIF
- XtSetArg(wargs[0], XmNmarginHeight, (Dimension)0);
- XtSetArg(wargs[1], XmNmarginWidth, (Dimension)0);
- XtSetArg(wargs[2], XmNmarginLeft, (Dimension)0);
- XtSetArg(wargs[3], XmNmarginRight, (Dimension)0);
- XtSetArg(wargs[4], XmNmarginTop, (Dimension)0);
- XtSetArg(wargs[5], XmNmarginBottom, (Dimension)0);
- XtSetArg(wargs[6], XmNshadowThickness, (Dimension)0);
- XtSetArg(wargs[7], XmNhighlightThickness, (Dimension)0);
- _plotter->y_canvas = XtCreateManagedWidget ((String)"", xmLabelWidgetClass,
- _plotter->y_toplevel,
- wargs, (Cardinal)8);
- #else
- XtSetArg(wargs[0], XtNinternalHeight, (Dimension)0);
- XtSetArg(wargs[1], XtNinternalWidth, (Dimension)0);
- _plotter->y_canvas = XtCreateManagedWidget ((String)"", labelWidgetClass,
- _plotter->y_toplevel,
- wargs, (Cardinal)2);
- #endif
-
-
- XtRealizeWidget (_plotter->y_toplevel);
-
-
- #ifdef USE_MOTIF
- XtSetArg (wargs[0], XmNtranslations,
- XtParseTranslationTable(_xplot_translations_before_forking));
- #else
- XtSetArg (wargs[0], XtNtranslations,
- XtParseTranslationTable(_xplot_translations_before_forking));
- #endif
- XtSetValues (_plotter->y_canvas, wargs, (Cardinal)1);
-
- _plotter->x_drawable2 = (Drawable)XtWindow(_plotter->y_canvas);
-
- #ifdef USE_MOTIF
- XtSetArg (wargs[0], XmNwidth, &window_width);
- XtSetArg (wargs[1], XmNheight, &window_height);
- #else
- XtSetArg (wargs[0], XtNwidth, &window_width);
- XtSetArg (wargs[1], XtNheight, &window_height);
- #endif
- XtGetValues (_plotter->y_canvas, wargs, (Cardinal)2);
- _plotter->data->imin = 0;
- _plotter->data->imax = (int)window_width - 1;
-
- _plotter->data->jmin = (int)window_height - 1;
- _plotter->data->jmax = 0;
-
- _compute_ndc_to_device_map (_plotter->data);
-
- if (DoesBackingStore(screen_struct))
- {
- XSetWindowAttributes attributes;
- unsigned long value_mask;
- attributes.backing_store = Always;
- value_mask = CWBackingStore;
- XChangeWindowAttributes (_plotter->x_dpy, (Window)_plotter->x_drawable2,
- value_mask, &attributes);
- }
-
- _plotter->x_double_buffering = X_DBL_BUF_NONE;
- double_buffer_s = (const char *)_get_plot_param (_plotter->data,
- "USE_DOUBLE_BUFFERING");
-
- if (strcmp (double_buffer_s, "fast") == 0)
- double_buffer_s = "yes";
- #ifdef HAVE_X11_EXTENSIONS_XDBE_H
- #ifdef HAVE_DBE_SUPPORT
- if (strcmp (double_buffer_s, "yes") == 0)
-
- {
- int major_version, minor_version;
- int one = 1;
- XdbeScreenVisualInfo *sv_info;
-
- if (XdbeQueryExtension (_plotter->x_dpy, &major_version, &minor_version)
- && (sv_info = XdbeGetVisualInfo (_plotter->x_dpy,
-
- &_plotter->x_drawable2,
- &one)) != NULL)
-
- {
- bool ok = false;
- int i, num_visuals = sv_info->count;
- XdbeVisualInfo *vis_info = sv_info->visinfo;
- VisualID visual_id = XVisualIDFromVisual (_plotter->x_visual);
-
- for (i = 0; i < num_visuals; i++)
-
- if (vis_info[i].visual == visual_id)
- {
- ok = true;
- break;
- }
- XdbeFreeVisualInfo (sv_info);
- if (ok)
-
- {
- _plotter->x_drawable3 =
- XdbeAllocateBackBufferName (_plotter->x_dpy,
- _plotter->x_drawable2,
- (XdbeSwapAction)XdbeUndefined);
-
- _plotter->x_double_buffering = X_DBL_BUF_DBE;
- }
- }
- }
- #endif
- #endif
- #ifdef HAVE_X11_EXTENSIONS_MULTIBUF_H
- #ifdef HAVE_MBX_SUPPORT
- if (_plotter->x_double_buffering == X_DBL_BUF_NONE
- && strcmp (double_buffer_s, "yes") == 0)
-
- {
- int event_base, error_base;
- int major_version, minor_version;
-
- if (XmbufQueryExtension (_plotter->x_dpy, &event_base, &error_base)
- && XmbufGetVersion (_plotter->x_dpy, &major_version, &minor_version))
-
- {
- Multibuffer multibuf[2];
- int num;
-
- num = XmbufCreateBuffers (_plotter->x_dpy,
- (Window)_plotter->x_drawable2, 2,
- MultibufferUpdateActionUndefined,
- MultibufferUpdateHintFrequent,
- multibuf);
- if (num == 2)
-
- {
- _plotter->x_drawable3 = multibuf[0];
- _plotter->y_drawable4 = multibuf[1];
-
- _plotter->x_double_buffering = X_DBL_BUF_MBX;
- }
- else
- _plotter->warning (R___(_plotter)
- "X server refuses to support multibuffering");
- }
- }
- #endif
- #endif
- if (_plotter->x_double_buffering == X_DBL_BUF_NONE)
-
- {
- Pixmap bg_pixmap;
-
- bg_pixmap = XCreatePixmap(_plotter->x_dpy,
- _plotter->x_drawable2,
- (unsigned int)window_width,
- (unsigned int)window_height,
- (unsigned int)PlanesOfScreen(screen_struct));
-
- if (strcmp (double_buffer_s, "yes") == 0)
- {
- _plotter->x_drawable3 = (Drawable)bg_pixmap;
- _plotter->x_double_buffering = X_DBL_BUF_BY_HAND;
- }
- else
- {
- _plotter->x_drawable1 = (Drawable)bg_pixmap;
- _plotter->x_double_buffering = X_DBL_BUF_NONE;
- }
- }
-
- _pl_x_add_gcs_to_first_drawing_state (S___(_plotter));
-
- _pl_y_erase_page (S___(_plotter));
-
-
- if (_plotter->x_double_buffering != X_DBL_BUF_NONE)
- _pl_y_erase_page (S___(_plotter));
- if (_plotter->x_double_buffering == X_DBL_BUF_NONE
- || _plotter->x_double_buffering == X_DBL_BUF_BY_HAND)
-
- {
- Pixmap bg_pixmap;
-
- bg_pixmap = ((_plotter->x_double_buffering == X_DBL_BUF_BY_HAND) ?
- _plotter->x_drawable3 : _plotter->x_drawable1);
- #ifdef USE_MOTIF
- XtSetArg (wargs[0], XmNlabelPixmap, bg_pixmap);
- XtSetArg (wargs[1], XmNlabelType, XmPIXMAP);
- XtSetValues (_plotter->y_canvas, wargs, (Cardinal)2);
- #else
- XtSetArg (wargs[0], XtNbitmap, bg_pixmap);
- XtSetValues (_plotter->y_canvas, wargs, (Cardinal)1);
- #endif
- }
-
- _pl_x_flush_output (S___(_plotter));
-
- return true;
- }
- static bool
- _bitmap_size_ok (const char *bitmap_size_s)
- {
- int width, height;
-
- if (bitmap_size_s
-
- && (sscanf (bitmap_size_s, "%dx%d", &width, &height) == 2)
- && (width > 0) && (height > 0))
- return true;
- else
- return false;
- }
- void
- _pl_y_maybe_get_new_colormap (S___(Plotter *_plotter))
- {
- Colormap new_pl_x_cmap;
-
-
- if (_plotter->x_cmap_type != X_CMAP_ORIG)
- return;
- _plotter->warning (R___(_plotter)
- "color supply low, switching to private colormap");
- new_pl_x_cmap = XCopyColormapAndFree (_plotter->x_dpy, _plotter->x_cmap);
- if (new_pl_x_cmap == 0)
-
- {
- _plotter->warning (R___(_plotter)
- "unable to create private colormap");
- _plotter->warning (R___(_plotter)
- "color supply exhausted, can't create new colors");
- _plotter->x_colormap_warning_issued = true;
- }
- else
-
- {
- Arg wargs[1];
-
- _plotter->x_cmap = new_pl_x_cmap;
- _plotter->x_cmap_type = X_CMAP_NEW;
-
- XtSetArg (wargs[0], XtNcolormap, _plotter->x_cmap);
- XtSetValues (_plotter->y_toplevel, wargs, (Cardinal)1);
- }
-
- return;
- }
- #define X_EVENT_HANDLING_PERIOD 4
- void
- _pl_y_maybe_handle_x_events(S___(Plotter *_plotter))
- {
- if (_plotter->y_auto_flush)
-
- {
- if (_plotter->drawstate->path == (plPath *)NULL
- || (_plotter->drawstate->line_type == PL_L_SOLID
- && !_plotter->drawstate->dash_array_in_effect
- && _plotter->drawstate->points_are_connected
- && _plotter->drawstate->quantized_device_line_width == 0))
- XFlush (_plotter->x_dpy);
- }
-
- if (_plotter->y_event_handler_count % X_EVENT_HANDLING_PERIOD == 0)
-
- {
- int i;
- #ifdef PTHREAD_SUPPORT
- #ifdef HAVE_PTHREAD_H
-
- pthread_mutex_lock (&_xplotters_mutex);
- #endif
- #endif
-
- for (i = 0; i < _xplotters_len; i++)
- {
- if (_xplotters[i] != NULL
- && _xplotters[i]->data->opened
- && _xplotters[i]->data->open
- && _xplotters[i]->y_app_con != NULL)
-
- {
-
- for ( ; ; )
- {
- bool have_data;
-
- have_data = false;
- if (QLength(_xplotters[i]->x_dpy) > 0)
-
- have_data = true;
-
- else
-
- {
- int connection_number;
- int maxfds, select_return;
- fd_set readfds;
- struct timeval timeout;
-
- timeout.tv_sec = 0;
- timeout.tv_usec = 0;
-
- connection_number =
- ConnectionNumber(_xplotters[i]->x_dpy);
- maxfds = 1 + connection_number;
- FD_ZERO (&readfds);
- FD_SET (connection_number, &readfds);
- select_return =
- select (maxfds, &readfds, NULL, NULL, &timeout);
-
- if (select_return < 0 && errno != EINTR)
- {
- _plotter->error (R___(_plotter) strerror (errno));
- break;
- }
- if (select_return > 0)
-
- have_data = true;
- }
-
- if (have_data == false)
-
- break;
-
-
- if (XtAppPending (_xplotters[i]->y_app_con))
-
- XtAppProcessEvent (_xplotters[i]->y_app_con, XtIMAll);
- }
-
- }
-
- }
-
- #ifdef PTHREAD_SUPPORT
- #ifdef HAVE_PTHREAD_H
-
- pthread_mutex_unlock (&_xplotters_mutex);
- #endif
- #endif
- }
- _plotter->y_event_handler_count++;
- }
- #ifndef HAVE_STRERROR
- extern char *sys_errlist[];
- extern int sys_nerr;
- static char *
- _plot_strerror (int errnum)
- {
- if (errnum < 0 || errnum >= sys_nerr)
- return "unknown error";
- return sys_errlist[errnum];
- }
- #endif
|