Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Testing latest pari + WASM + node.js... and it works?! Wow.

28494 views
License: GPL3
ubuntu2004
1
/* Copyright (C) 2000 The PARI group.
2
3
This file is part of the PARI/GP package.
4
5
PARI/GP is free software; you can redistribute it and/or modify it under the
6
terms of the GNU General Public License as published by the Free Software
7
Foundation; either version 2 of the License, or (at your option) any later
8
version. It is distributed in the hope that it will be useful, but WITHOUT
9
ANY WARRANTY WHATSOEVER.
10
11
Check the License for details. You should have received a copy of it, along
12
with the package; see the file 'COPYING'. If not, write to the Free Software
13
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
14
15
/*******************************************************************/
16
/* */
17
/* HIGH RESOLUTION PLOT */
18
/* */
19
/*******************************************************************/
20
21
#include "pari.h"
22
#include "rect.h"
23
24
#ifdef HPPA
25
# ifndef __GNUC__
26
typedef char *caddr_t;
27
# endif
28
#endif
29
30
BEGINEXTERN
31
#include <X11/XKBlib.h>
32
#include <X11/Xutil.h>
33
#include <X11/Xos.h>
34
#ifndef XK_c
35
# include <X11/keysym.h>
36
#endif
37
ENDEXTERN
38
39
static Colormap PARI_Colormap;
40
41
struct data_x
42
{
43
Display *display;
44
Window win;
45
int numcolors;
46
GC gc;
47
};
48
49
/* after fork(), we don't want the child to recover but to exit */
50
static void
51
exiterr(const char *str)
52
{
53
term_color(c_ERR);
54
err_printf("\n *** X fatal error: %s\n",str);
55
term_color(c_NONE); _exit(1);
56
}
57
58
static long
59
rgb_to_pixel(Display *display, int r, int g, int b)
60
{
61
XColor X;
62
X.red = r*65535/255;
63
X.green = g*65535/255;
64
X.blue = b*65535/255;
65
X.flags = DoRed | DoGreen | DoBlue;
66
if (!XAllocColor(display,PARI_Colormap,&X)) exiterr("cannot allocate color");
67
return X.pixel;
68
}
69
static long
70
colormapindex_to_pixel(Display *display, long i)
71
{
72
GEN c = gel(GP_DATA->colormap, i+1);
73
int r,g,b; color_to_rgb(c, &r,&g,&b);
74
return rgb_to_pixel(display, r, g, b);
75
}
76
static long
77
rgb_color(Display *display, long c)
78
{
79
int r,g,b; long_to_rgb(c, &r, &g, &b);
80
return rgb_to_pixel(display, r, g, b);
81
}
82
83
static void SetForeground(void *data, long col)
84
{
85
struct data_x *dx = (struct data_x *) data;
86
XSetForeground(dx->display,dx->gc, rgb_color(dx->display,col));
87
}
88
89
static void DrawPoint(void *data, long x, long y)
90
{
91
struct data_x *dx = (struct data_x *) data;
92
XDrawPoint(dx->display,dx->win,dx->gc, x,y);
93
}
94
95
static void DrawLine(void *data, long x1, long y1, long x2, long y2)
96
{
97
struct data_x *dx = (struct data_x *) data;
98
XDrawLine(dx->display,dx->win,dx->gc, x1,y1, x2,y2);
99
}
100
101
static void DrawRectangle(void *data, long x, long y, long w, long h)
102
{
103
struct data_x *dx = (struct data_x *) data;
104
XDrawRectangle(dx->display,dx->win,dx->gc, x,y, w,h);
105
}
106
107
static void FillRectangle(void *data, long x, long y, long w, long h)
108
{
109
struct data_x *dx = (struct data_x *) data;
110
XFillRectangle(dx->display,dx->win,dx->gc, x,y, w,h);
111
}
112
113
static void DrawPoints(void *data, long nb, struct plot_points *p)
114
{
115
struct data_x *dx = (struct data_x *) data;
116
XPoint *xp=(XPoint*)pari_malloc(sizeof(xp)*nb);
117
long i;
118
for (i=0;i<nb;i++)
119
{
120
xp[i].x=p[i].x;
121
xp[i].y=p[i].y;
122
}
123
XDrawPoints(dx->display,dx->win,dx->gc, xp, nb, 0);
124
pari_free(xp);
125
}
126
127
static void DrawLines(void *data, long nb, struct plot_points *p)
128
{
129
struct data_x *dx = (struct data_x *) data;
130
XPoint *xp=(XPoint*)pari_malloc(sizeof(xp)*nb);
131
long i;
132
for (i=0;i<nb;i++)
133
{
134
xp[i].x=p[i].x;
135
xp[i].y=p[i].y;
136
}
137
XDrawLines(dx->display,dx->win,dx->gc, xp, nb, 0);
138
pari_free(xp);
139
}
140
141
static void DrawString(void *data, long x, long y, char *text, long numtext)
142
{
143
struct data_x *dx = (struct data_x *) data;
144
XDrawString(dx->display,dx->win,dx->gc, x,y, text, numtext);
145
}
146
147
#define MAX_BUF 256
148
149
static int
150
Xerror(Display *d, XErrorEvent *pari_err) {
151
char buf[MAX_BUF];
152
XGetErrorText(d,pari_err->error_code,buf,MAX_BUF);
153
exiterr(buf); return 0;
154
}
155
156
static int
157
IOerror(Display *d) {
158
char buf[MAX_BUF];
159
sprintf(buf, "lost display on %s", DisplayString(d));
160
exiterr(buf); return 0;
161
}
162
163
static void
164
draw(PARI_plot *T, GEN w, GEN x, GEN y)
165
{
166
long oldwidth,oldheight;
167
struct plot_eng plotX;
168
struct data_x dx;
169
double xs = 1, ys = 1;
170
int screen, keystate;
171
Display *display;
172
GC gc;
173
Window win;
174
XEvent event;
175
XSizeHints size_hints;
176
XFontStruct *font_info;
177
XSetWindowAttributes attrib;
178
Atom wm_delete_window, wm_protocols;
179
180
if (pari_daemon()) return; /* parent process returns */
181
182
display = XOpenDisplay(NULL);
183
if (!display) exiterr("cannot open Display");
184
font_info = XLoadQueryFont(display, "7x13");
185
if (!font_info) exiterr("cannot open 7x13 font");
186
XSetErrorHandler(Xerror);
187
XSetIOErrorHandler(IOerror);
188
PARI_Colormap = DefaultColormap(display, 0);
189
190
screen = DefaultScreen(display);
191
win = XCreateSimpleWindow
192
(display, RootWindow(display, screen), 0, 0,
193
T->width, T->height, 4,
194
colormapindex_to_pixel(display, 1),
195
colormapindex_to_pixel(display, 0));
196
197
size_hints.flags = PPosition | PSize;
198
size_hints.x = 0;
199
size_hints.y = 0;
200
size_hints.width = T->width;
201
size_hints.height = T->height;
202
XSetStandardProperties
203
(display, win, "PARI plot", NULL, None, NULL, 0, &size_hints);
204
205
wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False);
206
wm_protocols = XInternAtom(display, "WM_PROTOCOLS", False);
207
XSetWMProtocols(display,win,&wm_delete_window, 1);
208
209
XSelectInput (display, win,
210
ExposureMask | ButtonPressMask | KeyReleaseMask | StructureNotifyMask);
211
212
/* enable backing-store */
213
attrib.backing_store = Always;
214
attrib.backing_planes = AllPlanes;
215
XChangeWindowAttributes(display,win,CWBackingStore|CWBackingPlanes,&attrib);
216
217
gc = XCreateGC(display, win, 0, NULL);
218
XSetFont(display, gc, font_info->fid);
219
220
XClearWindow(display, win);
221
XMapWindow(display, win);
222
oldwidth = T->width;
223
oldheight = T->height;
224
dx.display= display;
225
dx.win = win;
226
dx.numcolors = lg(GP_DATA->colormap)-1;
227
dx.gc = gc;
228
plotX.sc = &SetForeground;
229
plotX.pt = &DrawPoint;
230
plotX.ln = &DrawLine;
231
plotX.bx = &DrawRectangle;
232
plotX.fb = &FillRectangle;
233
plotX.mp = &DrawPoints;
234
plotX.ml = &DrawLines;
235
plotX.st = &DrawString;
236
plotX.pl = T;
237
plotX.data = (void*)&dx;
238
239
pari_close();
240
for(;;)
241
{
242
XNextEvent(display, &event);
243
switch(event.type)
244
{
245
case ClientMessage:
246
if (event.xclient.message_type != wm_protocols ||
247
(Atom)event.xclient.data.l[0] != wm_delete_window) break;
248
case ButtonPress:
249
case DestroyNotify:
250
EXIT:
251
XUnloadFont(display,font_info->fid);
252
XFreeGC(display,gc);
253
XCloseDisplay(display); _exit(0);
254
255
case KeyRelease:
256
/* Mod4 == Super on "std" Linux */
257
keystate = event.xkey.state & (ShiftMask|ControlMask|Mod1Mask|Mod4Mask);
258
switch (XkbKeycodeToKeysym(display, event.xkey.keycode, 0,0))
259
{
260
case XK_q:
261
if (!keystate || keystate == ControlMask) goto EXIT;
262
break;
263
case XK_c:
264
if (keystate == ControlMask) goto EXIT;
265
break;
266
}
267
break;
268
269
case ConfigureNotify:
270
{
271
int width = event.xconfigure.width;
272
int height = event.xconfigure.height;
273
274
if (width == oldwidth && height == oldheight) break;
275
oldwidth = width;
276
oldheight = height;
277
278
/* recompute scale */
279
xs = ((double)width)/T->width;
280
ys = ((double)height)/T->height;
281
}
282
case Expose:
283
gen_draw(&plotX, w, x, y, xs, ys);
284
}
285
}
286
}
287
288
INLINE void
289
gp_get_display_sizes(long *dwidth, long *dheight, long *fwidth, long *fheight)
290
{
291
Display *display;
292
293
display = XOpenDisplay(NULL);
294
if (display)
295
{
296
int screen = DefaultScreen(display);
297
*dwidth = DisplayWidth(display, screen);
298
*dheight = DisplayHeight(display, screen);
299
XCloseDisplay(display);
300
}
301
else
302
{
303
/* Situation looks grim */
304
*dwidth = 0;
305
*dheight = 0;
306
}
307
*fwidth = 7;
308
*fheight = 13;
309
}
310
311
void
312
gp_get_plot(PARI_plot *T)
313
{
314
gp_get_plot_generic(T,gp_get_display_sizes);
315
T->draw = &draw;
316
}
317
318