Testing latest pari + WASM + node.js... and it works?! Wow.
License: GPL3
ubuntu2004
/* Copyright (C) 2000 The PARI group.12This file is part of the PARI/GP package.34PARI/GP is free software; you can redistribute it and/or modify it under the5terms of the GNU General Public License as published by the Free Software6Foundation; either version 2 of the License, or (at your option) any later7version. It is distributed in the hope that it will be useful, but WITHOUT8ANY WARRANTY WHATSOEVER.910Check the License for details. You should have received a copy of it, along11with the package; see the file 'COPYING'. If not, write to the Free Software12Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */1314/*******************************************************************/15/* */16/* HIGH RESOLUTION PLOT */17/* */18/*******************************************************************/1920#include "pari.h"21#include "rect.h"2223#ifdef HPPA24# ifndef __GNUC__25typedef char *caddr_t;26# endif27#endif2829BEGINEXTERN30#include <X11/XKBlib.h>31#include <X11/Xutil.h>32#include <X11/Xos.h>33#ifndef XK_c34# include <X11/keysym.h>35#endif36ENDEXTERN3738static Colormap PARI_Colormap;3940struct data_x41{42Display *display;43Window win;44int numcolors;45GC gc;46};4748/* after fork(), we don't want the child to recover but to exit */49static void50exiterr(const char *str)51{52term_color(c_ERR);53err_printf("\n *** X fatal error: %s\n",str);54term_color(c_NONE); _exit(1);55}5657static long58rgb_to_pixel(Display *display, int r, int g, int b)59{60XColor X;61X.red = r*65535/255;62X.green = g*65535/255;63X.blue = b*65535/255;64X.flags = DoRed | DoGreen | DoBlue;65if (!XAllocColor(display,PARI_Colormap,&X)) exiterr("cannot allocate color");66return X.pixel;67}68static long69colormapindex_to_pixel(Display *display, long i)70{71GEN c = gel(GP_DATA->colormap, i+1);72int r,g,b; color_to_rgb(c, &r,&g,&b);73return rgb_to_pixel(display, r, g, b);74}75static long76rgb_color(Display *display, long c)77{78int r,g,b; long_to_rgb(c, &r, &g, &b);79return rgb_to_pixel(display, r, g, b);80}8182static void SetForeground(void *data, long col)83{84struct data_x *dx = (struct data_x *) data;85XSetForeground(dx->display,dx->gc, rgb_color(dx->display,col));86}8788static void DrawPoint(void *data, long x, long y)89{90struct data_x *dx = (struct data_x *) data;91XDrawPoint(dx->display,dx->win,dx->gc, x,y);92}9394static void DrawLine(void *data, long x1, long y1, long x2, long y2)95{96struct data_x *dx = (struct data_x *) data;97XDrawLine(dx->display,dx->win,dx->gc, x1,y1, x2,y2);98}99100static void DrawRectangle(void *data, long x, long y, long w, long h)101{102struct data_x *dx = (struct data_x *) data;103XDrawRectangle(dx->display,dx->win,dx->gc, x,y, w,h);104}105106static void FillRectangle(void *data, long x, long y, long w, long h)107{108struct data_x *dx = (struct data_x *) data;109XFillRectangle(dx->display,dx->win,dx->gc, x,y, w,h);110}111112static void DrawPoints(void *data, long nb, struct plot_points *p)113{114struct data_x *dx = (struct data_x *) data;115XPoint *xp=(XPoint*)pari_malloc(sizeof(xp)*nb);116long i;117for (i=0;i<nb;i++)118{119xp[i].x=p[i].x;120xp[i].y=p[i].y;121}122XDrawPoints(dx->display,dx->win,dx->gc, xp, nb, 0);123pari_free(xp);124}125126static void DrawLines(void *data, long nb, struct plot_points *p)127{128struct data_x *dx = (struct data_x *) data;129XPoint *xp=(XPoint*)pari_malloc(sizeof(xp)*nb);130long i;131for (i=0;i<nb;i++)132{133xp[i].x=p[i].x;134xp[i].y=p[i].y;135}136XDrawLines(dx->display,dx->win,dx->gc, xp, nb, 0);137pari_free(xp);138}139140static void DrawString(void *data, long x, long y, char *text, long numtext)141{142struct data_x *dx = (struct data_x *) data;143XDrawString(dx->display,dx->win,dx->gc, x,y, text, numtext);144}145146#define MAX_BUF 256147148static int149Xerror(Display *d, XErrorEvent *pari_err) {150char buf[MAX_BUF];151XGetErrorText(d,pari_err->error_code,buf,MAX_BUF);152exiterr(buf); return 0;153}154155static int156IOerror(Display *d) {157char buf[MAX_BUF];158sprintf(buf, "lost display on %s", DisplayString(d));159exiterr(buf); return 0;160}161162static void163draw(PARI_plot *T, GEN w, GEN x, GEN y)164{165long oldwidth,oldheight;166struct plot_eng plotX;167struct data_x dx;168double xs = 1, ys = 1;169int screen, keystate;170Display *display;171GC gc;172Window win;173XEvent event;174XSizeHints size_hints;175XFontStruct *font_info;176XSetWindowAttributes attrib;177Atom wm_delete_window, wm_protocols;178179if (pari_daemon()) return; /* parent process returns */180181display = XOpenDisplay(NULL);182if (!display) exiterr("cannot open Display");183font_info = XLoadQueryFont(display, "7x13");184if (!font_info) exiterr("cannot open 7x13 font");185XSetErrorHandler(Xerror);186XSetIOErrorHandler(IOerror);187PARI_Colormap = DefaultColormap(display, 0);188189screen = DefaultScreen(display);190win = XCreateSimpleWindow191(display, RootWindow(display, screen), 0, 0,192T->width, T->height, 4,193colormapindex_to_pixel(display, 1),194colormapindex_to_pixel(display, 0));195196size_hints.flags = PPosition | PSize;197size_hints.x = 0;198size_hints.y = 0;199size_hints.width = T->width;200size_hints.height = T->height;201XSetStandardProperties202(display, win, "PARI plot", NULL, None, NULL, 0, &size_hints);203204wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False);205wm_protocols = XInternAtom(display, "WM_PROTOCOLS", False);206XSetWMProtocols(display,win,&wm_delete_window, 1);207208XSelectInput (display, win,209ExposureMask | ButtonPressMask | KeyReleaseMask | StructureNotifyMask);210211/* enable backing-store */212attrib.backing_store = Always;213attrib.backing_planes = AllPlanes;214XChangeWindowAttributes(display,win,CWBackingStore|CWBackingPlanes,&attrib);215216gc = XCreateGC(display, win, 0, NULL);217XSetFont(display, gc, font_info->fid);218219XClearWindow(display, win);220XMapWindow(display, win);221oldwidth = T->width;222oldheight = T->height;223dx.display= display;224dx.win = win;225dx.numcolors = lg(GP_DATA->colormap)-1;226dx.gc = gc;227plotX.sc = &SetForeground;228plotX.pt = &DrawPoint;229plotX.ln = &DrawLine;230plotX.bx = &DrawRectangle;231plotX.fb = &FillRectangle;232plotX.mp = &DrawPoints;233plotX.ml = &DrawLines;234plotX.st = &DrawString;235plotX.pl = T;236plotX.data = (void*)&dx;237238pari_close();239for(;;)240{241XNextEvent(display, &event);242switch(event.type)243{244case ClientMessage:245if (event.xclient.message_type != wm_protocols ||246(Atom)event.xclient.data.l[0] != wm_delete_window) break;247case ButtonPress:248case DestroyNotify:249EXIT:250XUnloadFont(display,font_info->fid);251XFreeGC(display,gc);252XCloseDisplay(display); _exit(0);253254case KeyRelease:255/* Mod4 == Super on "std" Linux */256keystate = event.xkey.state & (ShiftMask|ControlMask|Mod1Mask|Mod4Mask);257switch (XkbKeycodeToKeysym(display, event.xkey.keycode, 0,0))258{259case XK_q:260if (!keystate || keystate == ControlMask) goto EXIT;261break;262case XK_c:263if (keystate == ControlMask) goto EXIT;264break;265}266break;267268case ConfigureNotify:269{270int width = event.xconfigure.width;271int height = event.xconfigure.height;272273if (width == oldwidth && height == oldheight) break;274oldwidth = width;275oldheight = height;276277/* recompute scale */278xs = ((double)width)/T->width;279ys = ((double)height)/T->height;280}281case Expose:282gen_draw(&plotX, w, x, y, xs, ys);283}284}285}286287INLINE void288gp_get_display_sizes(long *dwidth, long *dheight, long *fwidth, long *fheight)289{290Display *display;291292display = XOpenDisplay(NULL);293if (display)294{295int screen = DefaultScreen(display);296*dwidth = DisplayWidth(display, screen);297*dheight = DisplayHeight(display, screen);298XCloseDisplay(display);299}300else301{302/* Situation looks grim */303*dwidth = 0;304*dheight = 0;305}306*fwidth = 7;307*fheight = 13;308}309310void311gp_get_plot(PARI_plot *T)312{313gp_get_plot_generic(T,gp_get_display_sizes);314T->draw = &draw;315}316317318