diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/DrawArrowsNode.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/DrawArrowsNode.java
index 21a2208f1664412db23a9fba42873b2b579ab081..e177c722b19cefe51bc3679c57b7ba22255b7908 100644
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/DrawArrowsNode.java
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/DrawArrowsNode.java
@@ -23,15 +23,15 @@ import com.oracle.truffle.r.library.fastrGrid.device.GridDevice;
 import com.oracle.truffle.r.runtime.data.RList;
 import com.oracle.truffle.r.runtime.data.model.RAbstractContainer;
 
-public class DrawArrowsNode extends Node {
+class DrawArrowsNode extends Node {
     // Structure of an arrow description
-    public static final int ARROWANGLE = 0;
-    public static final int ARROWLENGTH = 1;
-    public static final int ARROWENDS = 2;
-    public static final int ARROWTYPE = 3;
+    private static final int ARROWANGLE = 0;
+    private static final int ARROWLENGTH = 1;
+    private static final int ARROWENDS = 2;
+    private static final int ARROWTYPE = 3;
     // known values of ARROWTYPE
-    public static final int ARROWTYPE_LINES = 1;
-    public static final int ARROWTYPE_POLYGON = 2;
+    private static final int ARROWTYPE_LINES = 1;
+    private static final int ARROWTYPE_POLYGON = 2;
 
     @Child private UnitToInchesNode unitToInches = Unit.createToInchesNode();
 
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/FastRGridExternalLookup.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/FastRGridExternalLookup.java
new file mode 100644
index 0000000000000000000000000000000000000000..5863de72a47db700e3e773967266d68c1312da39
--- /dev/null
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/FastRGridExternalLookup.java
@@ -0,0 +1,151 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.r.library.fastrGrid;
+
+import com.oracle.truffle.r.library.fastrGrid.grDevices.InitWindowedDevice;
+import com.oracle.truffle.r.library.fastrGrid.graphics.CPar;
+import com.oracle.truffle.r.nodes.builtin.RExternalBuiltinNode;
+import com.oracle.truffle.r.nodes.builtin.RInternalCodeBuiltinNode;
+import com.oracle.truffle.r.runtime.RInternalCode;
+import com.oracle.truffle.r.runtime.RInternalError;
+import com.oracle.truffle.r.runtime.RRuntime;
+import com.oracle.truffle.r.runtime.context.RContext;
+import com.oracle.truffle.r.runtime.data.RDataFactory;
+import com.oracle.truffle.r.runtime.data.RList;
+import com.oracle.truffle.r.runtime.data.RNull;
+
+/**
+ * Implements the lookup for externals replaced by the FastR grid package.
+ */
+public class FastRGridExternalLookup {
+
+    public static RExternalBuiltinNode lookupDotExternal(String name) {
+        switch (name) {
+            case "devholdflush":
+                return new IgnoredGridExternal(RNull.instance);
+            case "PDF":
+                return new IgnoredGridExternal(RNull.instance);
+            default:
+                return null;
+        }
+    }
+
+    public static RExternalBuiltinNode lookupDotExternal2(String name) {
+        switch (name) {
+            case "C_par":
+                return new CPar();
+            case "X11":
+                return new InitWindowedDevice();
+            default:
+                return null;
+        }
+    }
+
+    public static RExternalBuiltinNode lookupDotCall(String name) {
+        switch (name) {
+            case "L_gridDirty":
+                return new LGridDirty();
+            case "L_initGrid":
+                return LInitGrid.create();
+            case "L_newpage":
+                return new LNewPage();
+            case "L_convert":
+                return LConvert.create();
+
+            // Viewport management
+            case "L_upviewport":
+                return LUpViewPort.create();
+            case "L_initViewportStack":
+                return new LInitViewPortStack();
+            case "L_unsetviewport":
+                return LUnsetViewPort.create();
+            case "L_setviewport":
+            case "L_downviewport":
+                return getExternalFastRGridBuiltinNode(name);
+
+            // Drawing primitives
+            case "L_rect":
+                return LRect.create();
+            case "L_lines":
+                return LLines.create();
+            case "L_polygon":
+                return LPolygon.create();
+            case "L_text":
+                return LText.create();
+            case "L_textBounds":
+                return LTextBounds.create();
+            case "L_segments":
+                return LSegments.create();
+            case "L_circle":
+                return LCircle.create();
+            case "L_points":
+                return LPoints.create();
+
+            // Simple grid state access
+            case "L_getGPar":
+                return new GridStateGetNode(GridState::getGpar);
+            case "L_setGPar":
+                return GridStateSetNode.create((state, val) -> state.setGpar((RList) val));
+            case "L_getCurrentGrob":
+                return new GridStateGetNode(GridState::getCurrentGrob);
+            case "L_setCurrentGrob":
+                return GridStateSetNode.create(GridState::setCurrentGrob);
+            case "L_currentViewport":
+                return new GridStateGetNode(GridState::getViewPort);
+            case "L_initGPar":
+                return new LInitGPar();
+
+            // Display list stuff: not implemented atm
+            case "L_getDisplayList":
+                return new IgnoredGridExternal(RDataFactory.createList());
+            case "L_getDLindex":
+                return new IgnoredGridExternal(0);
+            case "L_getDLon":
+            case "L_getEngineDLon":
+                return new IgnoredGridExternal(RRuntime.LOGICAL_FALSE);
+            case "L_initDisplayList":
+            case "L_newpagerecording":
+            case "L_setDisplayList":
+            case "L_setDLelt":
+            case "L_setDLindex":
+            case "L_setDLon":
+                return new IgnoredGridExternal(RNull.instance);
+
+            // These methods do not use graphics system or any global state. For now,
+            // we can re-use the native implementation, which in the future should be rewritten
+            // to managed code.
+            case "L_validUnits":
+                return null;
+            default:
+                if (name.startsWith("L_")) {
+                    throw RInternalError.shouldNotReachHere("Unimplemented grid external " + name);
+                } else {
+                    return null;
+                }
+        }
+    }
+
+    private static RExternalBuiltinNode getExternalFastRGridBuiltinNode(String name) {
+        return new RInternalCodeBuiltinNode(RContext.getInstance(), "grid", RInternalCode.loadSourceRelativeTo(LInitGrid.class, "fastrGrid.R"), name);
+    }
+}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GridStateGetNode.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GridStateGetNode.java
index 1da60efecfd3a7867cc552a88242928cf3bc20c0..9006431d6095d25d4a3a97ee0bb2e808565c1eed 100644
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GridStateGetNode.java
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/GridStateGetNode.java
@@ -29,14 +29,14 @@ import com.oracle.truffle.r.nodes.builtin.RExternalBuiltinNode;
 /**
  * Gets a specified attribute of current {@link GridState}.
  */
-public class GridStateGetNode extends RExternalBuiltinNode.Arg0 {
+class GridStateGetNode extends RExternalBuiltinNode.Arg0 {
     private final Function<GridState, Object> getter;
 
     static {
         Casts.noCasts(GridStateGetNode.class);
     }
 
-    public GridStateGetNode(Function<GridState, Object> getter) {
+    GridStateGetNode(Function<GridState, Object> getter) {
         this.getter = getter;
     }
 
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/IgnoredGridExternal.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/IgnoredGridExternal.java
index 067193cf080edb7f114ee13e81fdeac6bb3082bd..9166dac774a24138f1e6b6180d5f7713198ce9f0 100644
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/IgnoredGridExternal.java
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/IgnoredGridExternal.java
@@ -29,14 +29,14 @@ import com.oracle.truffle.r.runtime.data.RArgsValuesAndNames;
  * A node for externals that we ignore, becuase we do not need to implement them or because they
  * support functionallity we do not implement yet, especially record/replay.
  */
-public class IgnoredGridExternal extends RExternalBuiltinNode {
+class IgnoredGridExternal extends RExternalBuiltinNode {
     private final Object result;
 
     static {
         Casts.noCasts(IgnoredGridExternal.class);
     }
 
-    public IgnoredGridExternal(Object result) {
+    IgnoredGridExternal(Object result) {
         this.result = result;
     }
 
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LGridDirty.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LGridDirty.java
index ec70939be01e99aac567db6a5aa4a6c1a1c7a6ac..b187d6bc7d7e6a24838e029b4e7f849c98d35320 100644
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LGridDirty.java
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LGridDirty.java
@@ -19,7 +19,7 @@ import com.oracle.truffle.r.nodes.builtin.RExternalBuiltinNode;
 import com.oracle.truffle.r.runtime.data.RArgsValuesAndNames;
 import com.oracle.truffle.r.runtime.data.RNull;
 
-public class LGridDirty extends RExternalBuiltinNode {
+class LGridDirty extends RExternalBuiltinNode {
     @Child private InitViewPortNode initViewPort = new InitViewPortNode();
     private final ConditionProfile initViewPortProfile = ConditionProfile.createCountingProfile();
 
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LInitGPar.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LInitGPar.java
index 38388451db2b14365d20be6cb84b2504ff6c9369..5bca2d1b315180f5bae3811186f16deafc7c5666 100644
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LInitGPar.java
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LInitGPar.java
@@ -26,7 +26,7 @@ import com.oracle.truffle.r.nodes.builtin.RExternalBuiltinNode;
 import com.oracle.truffle.r.runtime.data.RArgsValuesAndNames;
 import com.oracle.truffle.r.runtime.data.RNull;
 
-public class LInitGPar extends RExternalBuiltinNode {
+class LInitGPar extends RExternalBuiltinNode {
     static {
         Casts.noCasts(LInitGPar.class);
     }
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LInitViewPortStack.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LInitViewPortStack.java
index 83a82b0a4726205cc96a81a1c64e16364883cd0a..8a28365fc774f9f6c6f8f7114beef7dfc7c514cf 100644
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LInitViewPortStack.java
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LInitViewPortStack.java
@@ -18,7 +18,7 @@ import com.oracle.truffle.r.runtime.RInternalError;
 import com.oracle.truffle.r.runtime.data.RArgsValuesAndNames;
 import com.oracle.truffle.r.runtime.data.RNull;
 
-public class LInitViewPortStack extends RExternalBuiltinNode {
+class LInitViewPortStack extends RExternalBuiltinNode {
     @Child private InitViewPortNode initViewPortNode = new InitViewPortNode();
     static {
         Casts.noCasts(LInitViewPortStack.class);
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LNewPage.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LNewPage.java
index 727e10ed9104d8f6b6ef1cd63c98fb9ed1068954..7e03f9b840cb11fc0153e0498880a4989073d918 100644
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LNewPage.java
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/LNewPage.java
@@ -14,7 +14,7 @@ package com.oracle.truffle.r.library.fastrGrid;
 import com.oracle.truffle.r.nodes.builtin.RExternalBuiltinNode;
 import com.oracle.truffle.r.runtime.data.RNull;
 
-public class LNewPage extends RExternalBuiltinNode.Arg0 {
+class LNewPage extends RExternalBuiltinNode.Arg0 {
     static {
         Casts.noCasts(LNewPage.class);
     }
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/device/JFrameDevice.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/device/JFrameDevice.java
index ea216ae30474f8534a26127c187bc5b95c017344..8821b8dd0f0d05d46e9d860e594b480aebe22065 100644
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/device/JFrameDevice.java
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/device/JFrameDevice.java
@@ -25,19 +25,27 @@ package com.oracle.truffle.r.library.fastrGrid.device;
 import static com.oracle.truffle.r.library.fastrGrid.device.DrawingContext.INCH_TO_POINTS_FACTOR;
 
 import java.awt.BasicStroke;
+import java.awt.BorderLayout;
 import java.awt.Color;
+import java.awt.Dimension;
 import java.awt.Graphics2D;
+import java.awt.HeadlessException;
 import java.awt.Paint;
 import java.awt.RenderingHints;
 import java.awt.Shape;
+import java.awt.Toolkit;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
 import java.awt.geom.AffineTransform;
 import java.awt.geom.Ellipse2D;
 import java.awt.geom.Path2D;
 import java.awt.geom.Rectangle2D;
 import java.util.function.Supplier;
 
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+
 import com.oracle.truffle.r.library.fastrGrid.device.DrawingContext.GridLineType;
-import com.oracle.truffle.r.library.graphics.FastRFrame;
 import com.oracle.truffle.r.runtime.RInternalError;
 
 public class JFrameDevice implements GridDevice {
@@ -214,4 +222,41 @@ public class JFrameDevice implements GridDevice {
         twodashedStroke = new BasicStroke(defaultWidth, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10f, new float[]{dashSize / 2f, dashSize}, 0f);
         longdashedStroke = new BasicStroke(defaultWidth, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10f, new float[]{2f * dashSize}, 0f);
     }
+
+    private static class FastRFrame extends JFrame {
+        private static final long serialVersionUID = 1L;
+        private final Dimension framePreferredSize = new Dimension(720, 720);
+        private final JPanel fastRComponent = new JPanel();
+
+        FastRFrame() throws HeadlessException {
+            super("FastR");
+            addCloseListener();
+            createUI();
+            center();
+        }
+
+        private void createUI() {
+            setLayout(new BorderLayout());
+            setSize(framePreferredSize);
+            add(fastRComponent, BorderLayout.CENTER);
+            fastRComponent.setPreferredSize(getSize());
+        }
+
+        private void addCloseListener() {
+            addWindowFocusListener(new WindowAdapter() {
+                @Override
+                public void windowClosing(WindowEvent e) {
+                    dispose();
+                }
+            });
+        }
+
+        private void center() {
+            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
+            Dimension frameSize = getSize();
+            int x = (screenSize.width - frameSize.width) / 2;
+            int y = (screenSize.height - frameSize.height) / 2;
+            setLocation(x, y);
+        }
+    }
 }
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/grDevices/InitWindowedDevice.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/grDevices/InitWindowedDevice.java
new file mode 100644
index 0000000000000000000000000000000000000000..4e9f77c71213c5b596353b7ead381cfe18fb1c10
--- /dev/null
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/grDevices/InitWindowedDevice.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.r.library.fastrGrid.grDevices;
+
+import com.oracle.truffle.r.library.fastrGrid.GridContext;
+import com.oracle.truffle.r.library.fastrGrid.GridState;
+import com.oracle.truffle.r.library.fastrGrid.graphics.RGridGraphicsAdapter;
+import com.oracle.truffle.r.nodes.builtin.RExternalBuiltinNode;
+import com.oracle.truffle.r.runtime.data.RArgsValuesAndNames;
+import com.oracle.truffle.r.runtime.data.RNull;
+
+/**
+ * Node that handles the {@code C_X11} external calls. Those calls may be initiated from either the
+ * {@code X11} function or FastR specific {@code awt} function. In either case the result is that
+ * the AWT window is opened and ready for drawing.
+ */
+public final class InitWindowedDevice extends RExternalBuiltinNode {
+    static {
+        Casts.noCasts(InitWindowedDevice.class);
+    }
+
+    @Override
+    protected Object call(RArgsValuesAndNames args) {
+        GridState gridState = GridContext.getContext().getGridState();
+        if (!gridState.isDeviceInitialized()) {
+            GridContext.getContext().getCurrentDevice().openNewPage();
+            gridState.setDeviceInitialized();
+        }
+        RGridGraphicsAdapter.setCurrentDevice(args.getLength() == 0 ? "awt" : "X11cairo");
+        return RNull.instance;
+    }
+}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/grDevices/R/fastrGridDevices.R b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/grDevices/R/fastrGridDevices.R
new file mode 100644
index 0000000000000000000000000000000000000000..16a15ce9b2ba2313874a6d1e22eca5c237f15dfc
--- /dev/null
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/grDevices/R/fastrGridDevices.R
@@ -0,0 +1,30 @@
+# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+#
+# This code is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License version 2 only, as
+# published by the Free Software Foundation.
+#
+# This code is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+# version 2 for more details (a copy is included in the LICENSE file that
+# accompanied this code).
+#
+# You should have received a copy of the GNU General Public License version
+# 2 along with this work; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+# or visit www.oracle.com if you need additional information or have any
+# questions.
+
+eval(expression({
+    # This should be preffered way of starting the FastR java device.
+    # For compatibility reasons, both X11 and awt end up calling C_X11.
+    # In the future, this function may support extra parameters like a
+    # reference to java 2D graphics object, which will be used for the drawing.
+    awt <- function(...) {
+        .External2(grDevices:::C_X11)
+    }
+}), asNamespace("grDevices"))
\ No newline at end of file
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/grDevices/package-info.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/grDevices/package-info.java
new file mode 100644
index 0000000000000000000000000000000000000000..f8363aacb3318093101e3a1d9ea3b3e1509a9a5e
--- /dev/null
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/grDevices/package-info.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+/**
+ * Compatibility layer for grDevices packages.
+ *
+ * With FastR grid we use {@link com.oracle.truffle.r.library.fastrGrid.device.GridDevice} instead
+ * of the abstraction used by GnuR. This is compatibility layer that provides implementation of some
+ * of the externals that manipulate the device and forwards them to corresponding methods on FastR
+ * grid side.
+ */
+package com.oracle.truffle.r.library.fastrGrid.grDevices;
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/graphics/RGridGraphicsAdapter.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/graphics/RGridGraphicsAdapter.java
new file mode 100644
index 0000000000000000000000000000000000000000..4d4629bc7f1e29d3c33ec63f6b7ad9a7ad8324b3
--- /dev/null
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/graphics/RGridGraphicsAdapter.java
@@ -0,0 +1,73 @@
+/*
+ * This material is distributed under the GNU General Public License
+ * Version 2. You may review the terms of this license at
+ * http://www.gnu.org/licenses/gpl-2.0.html
+ *
+ * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
+ * Copyright (C) 1998 Ross Ihaka
+ * Copyright (c) 1998--2014, The R Core Team
+ * Copyright (c) 2002--2010, The R Foundation
+ * Copyright (C) 2005--2006, Morten Welinder
+ * Copyright (c) 2014, 2017, Oracle and/or its affiliates
+ *
+ * All rights reserved.
+ */
+package com.oracle.truffle.r.library.fastrGrid.graphics;
+
+import com.oracle.truffle.r.library.fastrGrid.FastRGridExternalLookup;
+import com.oracle.truffle.r.runtime.RError;
+import com.oracle.truffle.r.runtime.RError.Message;
+import com.oracle.truffle.r.runtime.ROptions;
+import com.oracle.truffle.r.runtime.ROptions.OptionsException;
+import com.oracle.truffle.r.runtime.context.RContext;
+import com.oracle.truffle.r.runtime.data.RDataFactory;
+import com.oracle.truffle.r.runtime.data.RPairList;
+import com.oracle.truffle.r.runtime.env.REnvironment;
+
+/**
+ * Initialization of graphics package emulation for the purposes of FastR grid package
+ * implementation.
+ *
+ * FastR exposes two devices: the null device and 'awt' device, and adds function 'awt' to the
+ * grDevices package. The 'awt' function ends up calling 'C_X11' (the same as the 'X11' function
+ * from grDevices), we capture that call in {@link FastRGridExternalLookup} and replace it with our
+ * own logic. This way we also "implement" 'X11' device with java awt should anyone try to activate
+ * it.
+ *
+ * Moreover, we change the value of option "device" to our "awt" function so that when e.g. lattice
+ * tries to open the default device it uses 'awt'. If the future this should be either 'awt' for
+ * interactive sessions, or some image format device for batch sessions. We should also honor the
+ * R_INTERACTIVE_DEVICE and R_DEFAULT_DEVICE environment variables.
+ */
+public class RGridGraphicsAdapter {
+    private static final String NULL_DEVICE = "null device";
+    /**
+     * The graphics devices system maintains two variables .Device and .Devices in the base
+     * environment both are always set: .Devices gives a list of character vectors of the names of
+     * open devices, .Device is the element corresponding to the currently active device. The null
+     * device will always be open.
+     */
+    private static final String DOT_DEVICE = ".Device";
+    private static final String DOT_DEVICES = ".Devices";
+
+    public static void initialize() {
+        setCurrentDevice(NULL_DEVICE);
+        ROptions.ContextStateImpl options = RContext.getInstance().stateROptions;
+        try {
+            options.setValue("device", "awt");
+        } catch (OptionsException e) {
+            RError.warning(RError.NO_CALLER, Message.GENERIC, "FastR could not set the 'device' options to awt.");
+        }
+    }
+
+    public static void setCurrentDevice(String name) {
+        REnvironment baseEnv = REnvironment.baseEnv();
+        baseEnv.safePut(DOT_DEVICE, name);
+        Object devices = baseEnv.get(DOT_DEVICES);
+        if (devices instanceof RPairList) {
+            ((RPairList) devices).appendToEnd(RDataFactory.createPairList(name));
+        } else {
+            baseEnv.safePut(DOT_DEVICES, RDataFactory.createPairList(name));
+        }
+    }
+}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/grDevices/DevicesCCalls.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/grDevices/DevicesCCalls.java
deleted file mode 100644
index d7ba80d07c943bd2112a373a122927c0bb1da8dd..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/grDevices/DevicesCCalls.java
+++ /dev/null
@@ -1,125 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2017, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.grDevices;
-
-import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.emptyStringVector;
-import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.stringValue;
-import static com.oracle.truffle.r.nodes.builtin.casts.fluent.CastNodeBuilder.newCastBuilder;
-import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
-import com.oracle.truffle.api.dsl.Specialization;
-import com.oracle.truffle.r.library.grDevices.DevicesCCallsFactory.C_DevOffNodeGen;
-import com.oracle.truffle.r.library.grDevices.pdf.PdfGraphicsDevice;
-import com.oracle.truffle.r.library.graphics.core.GraphicsEngineImpl;
-import com.oracle.truffle.r.nodes.builtin.RExternalBuiltinNode;
-import com.oracle.truffle.r.nodes.unary.CastNode;
-import com.oracle.truffle.r.runtime.data.RArgsValuesAndNames;
-import com.oracle.truffle.r.runtime.data.RNull;
-import com.oracle.truffle.r.runtime.data.model.RAbstractStringVector;
-
-public class DevicesCCalls {
-    public abstract static class C_DevOff extends RExternalBuiltinNode.Arg1 {
-        public static C_DevOff create() {
-            return C_DevOffNodeGen.create();
-        }
-
-        static {
-            Casts casts = new Casts(C_DevOff.class);
-            casts.arg(0).asIntegerVector().findFirst();
-        }
-
-        @Specialization
-        public Object doCall(int deviceIndex) {
-            GraphicsEngineImpl.getInstance().killGraphicsDeviceByIndex(deviceIndex);
-            return RNull.instance;
-        }
-    }
-
-    public static final class C_DevCur extends RExternalBuiltinNode.Arg0 {
-
-        @Override
-        @TruffleBoundary
-        public Object execute() {
-            return GraphicsEngineImpl.getInstance().getCurrentGraphicsDeviceIndex();
-        }
-    }
-
-    public static final class C_PDF extends RExternalBuiltinNode {
-
-        @Child private CastNode extractFontsNode = newCastBuilder().mapNull(emptyStringVector()).mustBe(stringValue()).asStringVector().buildCastNode();
-        @Child private CastNode asStringNode = newCastBuilder().asStringVector().findFirst().buildCastNode();
-        @Child private CastNode asDoubleNode = newCastBuilder().asDoubleVector().findFirst().buildCastNode();
-        @Child private CastNode asLogicalNode = newCastBuilder().asLogicalVector().findFirst().buildCastNode();
-        @Child private CastNode asIntNode = newCastBuilder().asIntegerVector().findFirst().buildCastNode();
-
-        static {
-            Casts.noCasts(C_PDF.class);
-        }
-
-        @SuppressWarnings("unused")
-        @Override
-        @TruffleBoundary
-        public Object call(RArgsValuesAndNames args) {
-            new PdfGraphicsDevice(extractParametersFrom(args));
-            // todo implement devices addition
-            return RNull.instance;
-        }
-
-        private PdfGraphicsDevice.Parameters extractParametersFrom(RArgsValuesAndNames args) {
-            PdfGraphicsDevice.Parameters result = new PdfGraphicsDevice.Parameters();
-            result.filePath = asString(args.getArgument(0));
-            result.paperSize = asString(args.getArgument(1));
-            result.fontFamily = asString(args.getArgument(2));
-            result.encoding = asString(args.getArgument(3));
-            result.bg = asString(args.getArgument(4));
-            result.fg = asString(args.getArgument(5));
-            result.width = asDouble(castVector(args.getArgument(6)));
-            result.height = asDouble(castVector(args.getArgument(7)));
-            result.pointSize = asDouble(castVector(args.getArgument(8)));
-            result.oneFile = asLogical(castVector(args.getArgument(9)));
-            result.pageCenter = asLogical(castVector(args.getArgument(10)));
-            result.title = asString(args.getArgument(11));
-            result.fonts = extractFontsFrom(args.getArgument(12));
-
-            result.majorVersion = asInt(castVector(args.getArgument(13)));
-            result.minorVersion = asInt(castVector(args.getArgument(14)));
-            result.colormodel = asString(args.getArgument(15));
-            result.useDingbats = asLogical(castVector(args.getArgument(16)));
-            result.useKerning = asLogical(castVector(args.getArgument(17)));
-            result.fillOddEven = asLogical(castVector(args.getArgument(18)));
-            result.compress = asLogical(castVector(args.getArgument(19)));
-            return result;
-        }
-
-        private String asString(Object value) {
-            return (String) asStringNode.execute(value);
-        }
-
-        private int asInt(Object value) {
-            return (Integer) asIntNode.execute(value);
-        }
-
-        private double asDouble(Object value) {
-            return (Double) asDoubleNode.execute(value);
-        }
-
-        private byte asLogical(Object value) {
-            return (Byte) asLogicalNode.execute(value);
-        }
-
-        private String[] extractFontsFrom(Object inputArgument) {
-            return ((RAbstractStringVector) extractFontsNode.execute(inputArgument)).materialize().getDataCopy();
-        }
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/grDevices/NullGraphicsDevice.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/grDevices/NullGraphicsDevice.java
deleted file mode 100644
index 0ec439c90287e887c4869771a65ef7bad33ac17f..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/grDevices/NullGraphicsDevice.java
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.grDevices;
-
-import com.oracle.truffle.r.library.graphics.core.DrawingParameters;
-import com.oracle.truffle.r.library.graphics.core.GraphicsDevice;
-import com.oracle.truffle.r.library.graphics.core.geometry.Coordinates;
-
-public final class NullGraphicsDevice implements GraphicsDevice {
-    private static final NullGraphicsDevice instance = new NullGraphicsDevice();
-
-    public static NullGraphicsDevice getInstance() {
-        return instance;
-    }
-
-    @Override
-    public void deactivate() {
-        throw createExceptionForMethod("deactivate");
-    }
-
-    @Override
-    public void activate() {
-        throw createExceptionForMethod("activate");
-    }
-
-    @Override
-    public void close() {
-        throw createExceptionForMethod("close");
-    }
-
-    @Override
-    public DrawingParameters getDrawingParameters() {
-        throw createExceptionForMethod("getDrawingParameters");
-    }
-
-    @Override
-    public void setMode(Mode newMode) {
-        throw createExceptionForMethod("setMode");
-    }
-
-    @Override
-    public Mode getMode() {
-        throw createExceptionForMethod("getMode");
-    }
-
-    @Override
-    public void setClipRect(double x1, double y1, double x2, double y2) {
-        throw createExceptionForMethod("setClipRect");
-    }
-
-    @Override
-    public void drawPolyline(Coordinates coordinates, DrawingParameters drawingParameters) {
-        throw createExceptionForMethod("drawPolyline");
-    }
-
-    private static RuntimeException createExceptionForMethod(String methodName) {
-        return new IllegalStateException("Call to " + methodName + " of Null-device");
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/grDevices/fastrgd/FastRGraphicsDevice.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/grDevices/fastrgd/FastRGraphicsDevice.java
deleted file mode 100644
index ca8976b7907f25bacb5b9ac22c8ee65720fc7f19..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/grDevices/fastrgd/FastRGraphicsDevice.java
+++ /dev/null
@@ -1,137 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.grDevices.fastrgd;
-
-import static com.oracle.truffle.r.library.graphics.core.geometry.AxisDirection.EAST;
-import static com.oracle.truffle.r.library.graphics.core.geometry.AxisDirection.NORTH;
-
-import java.util.Arrays;
-import java.util.function.Function;
-
-import com.oracle.truffle.r.library.graphics.FastRFrame;
-import com.oracle.truffle.r.library.graphics.core.DrawingParameters;
-import com.oracle.truffle.r.library.graphics.core.GraphicsDevice;
-import com.oracle.truffle.r.library.graphics.core.drawables.DrawableObject;
-import com.oracle.truffle.r.library.graphics.core.drawables.PolylineDrawableObject;
-import com.oracle.truffle.r.library.graphics.core.drawables.StringDrawableObject;
-import com.oracle.truffle.r.library.graphics.core.geometry.Axis;
-import com.oracle.truffle.r.library.graphics.core.geometry.CoordinateSystem;
-import com.oracle.truffle.r.library.graphics.core.geometry.Coordinates;
-import com.oracle.truffle.r.library.graphics.core.geometry.CoordinatesFactory;
-import com.oracle.truffle.r.library.graphics.core.geometry.DoubleCoordinates;
-
-/**
- * Default interactive FastR graphics device.
- */
-public class FastRGraphicsDevice implements GraphicsDevice {
-    private static final double GNUR_DEFAULT_MAX_X = 1;
-    private static final Axis GNUR_DEFAULT_X_AXIS = new Axis(0, GNUR_DEFAULT_MAX_X, EAST);
-    private static final Axis GNUR_DEFAULT_Y_AXIS = new Axis(0, 1, NORTH);
-    private static final double MARGIN = GNUR_DEFAULT_MAX_X * 0.1; // the margin for each side of
-    // 10% of a screen
-    // compress resulting image to have a small margin on all sides
-    private static final double COMPRESS_RATION = 1. - MARGIN * 1.8;
-
-    private Mode mode = Mode.GRAPHICS_OFF;
-    private FastRFrame fastRFrame;
-    private CoordinateSystem currentCoordinateSystem = new CoordinateSystem(GNUR_DEFAULT_X_AXIS, GNUR_DEFAULT_Y_AXIS);
-
-    @Override
-    public void deactivate() {
-        // todo impl
-    }
-
-    @Override
-    public void activate() {
-        // todo impl
-    }
-
-    @Override
-    public void close() {
-        // todo impl
-    }
-
-    @Override
-    public DrawingParameters getDrawingParameters() {
-        return null;
-    }
-
-    @Override
-    public void setMode(Mode newMode) {
-        mode = newMode;
-    }
-
-    @Override
-    public Mode getMode() {
-        return mode;
-    }
-
-    @Override
-    public void setClipRect(double x1, double y1, double x2, double y2) {
-        // todo impl
-    }
-
-    @Override
-    public void drawPolyline(Coordinates coordinates, DrawingParameters drawingParameters) {
-        // todo continue from GEPolyline() of engine.c
-        Coordinates convertedCoords = CoordinatesFactory.withRatioAndShift(coordinates, COMPRESS_RATION, MARGIN);
-        addDrawableObject(new PolylineDrawableObject(currentCoordinateSystem, convertedCoords));
-        drawBounds();
-        drawXYLabelsFor(coordinates);
-    }
-
-    private void drawBounds() {
-        // x,y in range [0,1]
-        double[] boundsXYPairs = {0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0};
-        Coordinates bounds = CoordinatesFactory.createByXYPairs(boundsXYPairs);
-        Coordinates compressedBounds = CoordinatesFactory.withRatioAndShift(bounds, COMPRESS_RATION, MARGIN);
-        addDrawableObject(new PolylineDrawableObject(currentCoordinateSystem, compressedBounds));
-    }
-
-    private void drawXYLabelsFor(Coordinates coordinates) {
-        drawLabelsForCoordinates(coordinates.getXCoordinatesAsDoubles(), MARGIN, 0.01, // just small
-                        // shift
-                        d -> CoordinatesFactory.createWithSameY(d, 0));
-        drawLabelsForCoordinates(coordinates.getYCoordinatesAsDoubles(), 0, MARGIN, d -> CoordinatesFactory.createWithSameX(0, d));
-    }
-
-    private void drawLabelsForCoordinates(double[] coordinates, double xShift, double yShift, Function<double[], DoubleCoordinates> xYConverter) {
-        int length = coordinates.length;
-        double[] sortedCoords = new double[length];
-        // copy to avoid side-effects on a caller side
-        System.arraycopy(coordinates, 0, sortedCoords, 0, length);
-        Arrays.sort(sortedCoords);
-        String[] labels = composeLabelsFor(sortedCoords);
-        DoubleCoordinates xYCoords = xYConverter.apply(sortedCoords);
-        Coordinates shiftedCoords = CoordinatesFactory.withRatioAndShift(xYCoords, COMPRESS_RATION, xShift, yShift);
-        addDrawableObject(new StringDrawableObject(currentCoordinateSystem, shiftedCoords, labels));
-    }
-
-    private static String[] composeLabelsFor(double[] doubles) {
-        return Arrays.stream(doubles).mapToObj(String::valueOf).toArray(String[]::new);
-    }
-
-    private FastRFrame getFastRFrame() {
-        if (fastRFrame == null || !fastRFrame.isVisible()) {
-            fastRFrame = new FastRFrame();
-            fastRFrame.setVisible(true);
-        }
-        return fastRFrame;
-    }
-
-    private void addDrawableObject(DrawableObject drawableObject) {
-        getFastRFrame().getFastRComponent().addDrawableObject(drawableObject);
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/grDevices/pdf/PdfGraphicsDevice.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/grDevices/pdf/PdfGraphicsDevice.java
deleted file mode 100644
index 9b7302823dec1669faa446b3a71a2723d628cbe4..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/grDevices/pdf/PdfGraphicsDevice.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.grDevices.pdf;
-
-import com.oracle.truffle.r.library.graphics.core.DrawingParameters;
-import com.oracle.truffle.r.library.graphics.core.GraphicsDevice;
-import com.oracle.truffle.r.library.graphics.core.geometry.Coordinates;
-import com.oracle.truffle.r.runtime.RRuntime;
-
-public class PdfGraphicsDevice implements GraphicsDevice {
-    @SuppressWarnings("unused") private final Parameters deviceParameters;
-
-    public PdfGraphicsDevice(Parameters deviceParameters) {
-        this.deviceParameters = deviceParameters;
-    }
-
-    @Override
-    public void deactivate() {
-
-    }
-
-    @Override
-    public void activate() {
-
-    }
-
-    @Override
-    public void close() {
-
-    }
-
-    @Override
-    public DrawingParameters getDrawingParameters() {
-        return null;
-    }
-
-    @Override
-    public void setMode(Mode newMode) {
-
-    }
-
-    @Override
-    public Mode getMode() {
-        return null;
-    }
-
-    @Override
-    public void setClipRect(double x1, double y1, double x2, double y2) {
-
-    }
-
-    @Override
-    public void drawPolyline(Coordinates coordinates, DrawingParameters drawingParameters) {
-
-    }
-
-    public static class Parameters {
-        public String filePath;
-        public String paperSize = "special";
-        public String fontFamily = "Helvetica";
-        public String encoding = "default";
-        public String bg = "transparent";
-        public String fg = "black";
-        public double width = 7.;
-        public double height = 7.;
-        public double pointSize = 12.;
-        public byte oneFile = RRuntime.LOGICAL_TRUE;
-        public byte pageCenter = RRuntime.LOGICAL_TRUE;
-        public String title = "R Graphics Output";
-        public String[] fonts;
-        public int majorVersion = 1;
-        public int minorVersion = 4;
-        public String colormodel = "srgb";
-        public byte useDingbats = RRuntime.LOGICAL_TRUE;
-        public byte useKerning = RRuntime.LOGICAL_TRUE;
-        public byte fillOddEven = RRuntime.LOGICAL_FALSE;
-        public byte compress = RRuntime.LOGICAL_TRUE;
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/BaseGraphicsSystem.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/BaseGraphicsSystem.java
deleted file mode 100644
index 77d572f534a7ef158c16829b4d7fa953fbaca328..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/BaseGraphicsSystem.java
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics;
-
-import com.oracle.truffle.r.library.graphics.core.AbstractGraphicsSystem;
-
-/**
- * Denotes to the 'base' in GNUR terms graphics system.
- */
-public class BaseGraphicsSystem extends AbstractGraphicsSystem {
-    private final GraphicsEventsListener graphicsEventsListener = (graphicsEvent, graphicsDevice) -> {
-    };
-
-    @Override
-    public GraphicsEventsListener getGraphicsEventsListener() {
-        return graphicsEventsListener;
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/FastRComponent.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/FastRComponent.java
deleted file mode 100644
index 5f395fecd1afe721a1f2f435c417169f7b88fcd0..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/FastRComponent.java
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics;
-
-import java.awt.Dimension;
-import java.awt.Graphics;
-import java.awt.Graphics2D;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-import javax.swing.JComponent;
-
-import com.oracle.truffle.r.library.graphics.core.drawables.DrawableObject;
-import com.oracle.truffle.r.library.graphics.core.geometry.CoordinateSystem;
-
-public class FastRComponent extends JComponent {
-
-    private static final long serialVersionUID = 1L;
-
-    private final List<DrawableObject> displayList = Collections.synchronizedList(new ArrayList<>());
-
-    private boolean shouldDraw;
-    private CoordinateSystem coordinateSystem;
-
-    /**
-     * Note! Called from ED thread.
-     */
-    @Override
-    public void doLayout() {
-        super.doLayout();
-        Dimension size = getSize();
-        coordinateSystem = new CoordinateSystem(0, size.getWidth(), 0, size.getHeight());
-        shouldDraw = true;
-        recalculateDisplayList();
-    }
-
-    private void recalculateDisplayList() {
-        synchronized (displayList) {
-            displayList.stream().forEach(drawableObject -> drawableObject.recalculateForDrawingIn(coordinateSystem));
-        }
-    }
-
-    /**
-     * Note! Called from ED thread.
-     */
-    @Override
-    protected void paintComponent(Graphics g) {
-        super.paintComponent(g);
-        Graphics2D g2 = (Graphics2D) g;
-        if (shouldDraw) {
-            drawDisplayListOn(g2);
-        }
-    }
-
-    private void drawDisplayListOn(Graphics2D g2) {
-        synchronized (displayList) {
-            displayList.stream().forEach(drawableObject -> drawableObject.drawOn(g2));
-        }
-    }
-
-    public void addDrawableObject(DrawableObject drawableObject) {
-        synchronized (displayList) {
-            displayList.add(drawableObject);
-        }
-        shouldDraw = true;
-        if (coordinateSystem != null) {
-            drawableObject.recalculateForDrawingIn(coordinateSystem);
-            repaint();
-        }
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/FastRFrame.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/FastRFrame.java
deleted file mode 100644
index 7446c48d593045bd08e9a437ff882112bb00806f..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/FastRFrame.java
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-package com.oracle.truffle.r.library.graphics;
-
-import java.awt.BorderLayout;
-import java.awt.Dimension;
-import java.awt.HeadlessException;
-import java.awt.Toolkit;
-import java.awt.event.WindowAdapter;
-import java.awt.event.WindowEvent;
-
-import javax.swing.JFrame;
-
-public class FastRFrame extends JFrame {
-
-    private static final long serialVersionUID = 1L;
-
-    private final Dimension framePreferredSize = new Dimension(720, 720);
-    private final FastRComponent fastRComponent = new FastRComponent();
-
-    public FastRFrame() throws HeadlessException {
-        super("FastR");
-        addCloseListener();
-        createUI();
-        center();
-    }
-
-    private void createUI() {
-        setLayout(new BorderLayout());
-        setSize(framePreferredSize);
-        add(fastRComponent, BorderLayout.CENTER);
-        fastRComponent.setPreferredSize(getSize());
-    }
-
-    private void addCloseListener() {
-        addWindowFocusListener(new WindowAdapter() {
-            @Override
-            public void windowClosing(WindowEvent e) {
-                dispose();
-            }
-        });
-    }
-
-    private void center() {
-        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
-        Dimension frameSize = getSize();
-        int x = (screenSize.width - frameSize.width) / 2;
-        int y = (screenSize.height - frameSize.height) / 2;
-        setLocation(x, y);
-    }
-
-    public FastRComponent getFastRComponent() {
-        return fastRComponent;
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/GraphicsCCalls.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/GraphicsCCalls.java
deleted file mode 100644
index adee6f49eac5deef5d74256f2190bad20ace8108..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/GraphicsCCalls.java
+++ /dev/null
@@ -1,137 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2017, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics;
-
-import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.doubleValue;
-import static com.oracle.truffle.r.nodes.builtin.CastBuilder.Predef.size;
-import static com.oracle.truffle.r.nodes.builtin.casts.fluent.CastNodeBuilder.newCastBuilder;
-import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
-import com.oracle.truffle.r.library.graphics.core.DrawingParameters;
-import com.oracle.truffle.r.library.graphics.core.GraphicsDevice;
-import com.oracle.truffle.r.library.graphics.core.GraphicsEngine;
-import com.oracle.truffle.r.library.graphics.core.GraphicsEngineImpl;
-import com.oracle.truffle.r.library.graphics.core.geometry.Coordinates;
-import com.oracle.truffle.r.library.graphics.core.geometry.CoordinatesFactory;
-import com.oracle.truffle.r.nodes.builtin.RExternalBuiltinNode;
-import com.oracle.truffle.r.nodes.unary.CastNode;
-import com.oracle.truffle.r.runtime.RRuntime;
-import com.oracle.truffle.r.runtime.data.RArgsValuesAndNames;
-import com.oracle.truffle.r.runtime.data.RDataFactory;
-import com.oracle.truffle.r.runtime.data.RDoubleVector;
-import com.oracle.truffle.r.runtime.data.RNull;
-
-public class GraphicsCCalls {
-    public static final class C_PlotXY extends RExternalBuiltinNode {
-
-        @Child private CastNode castXYNode = newCastBuilder().mustBe(doubleValue().and(size(2))).asDoubleVector().buildCastNode();
-
-        static {
-            Casts.noCasts(C_PlotXY.class);
-        }
-
-        @Override
-        @TruffleBoundary
-        public RNull call(RArgsValuesAndNames args) {
-            RDoubleVector xyVector = (RDoubleVector) castXYNode.execute(args.getArgument(0));
-            getGraphicsEngine().setCurrentGraphicsDeviceMode(GraphicsDevice.Mode.GRAPHICS_ON);
-            drawWithLines(xyVector);
-            return RNull.instance;
-        }
-
-        private static void drawWithLines(RDoubleVector xyVector) {
-            // todo implement coordinate systems units conversion like in GConvert (graphics.c)
-            setClipRect();
-            DrawingParameters adoptedParameters = adoptCurrentDeviceDrawingParameters();
-            Coordinates coordinates = CoordinatesFactory.createByXYVector(xyVector);
-            getGraphicsEngine().drawPolyline(coordinates, adoptedParameters);
-        }
-
-        private static DrawingParameters adoptCurrentDeviceDrawingParameters() {
-            // todo Now adoption as for today. Transcribe from gcontextFromGM() (graphics.c)
-            return getCurrentGraphicsDevice().getDrawingParameters();
-        }
-
-        private static void setClipRect() {
-            // todo Transcrive from Gclip() (graphics.c)
-            getGraphicsEngine().setCurrentGraphicsDeviceClipRect(0, 0, 0, 0);
-        }
-
-        private static GraphicsDevice getCurrentGraphicsDevice() {
-            return getGraphicsEngine().getCurrentGraphicsDevice();
-        }
-
-        private static GraphicsEngine getGraphicsEngine() {
-            return GraphicsEngineImpl.getInstance();
-        }
-    }
-
-    public static final class C_Par extends RExternalBuiltinNode {
-
-        static {
-            Casts.noCasts(C_Par.class);
-        }
-
-        @Override
-        @TruffleBoundary
-        public Object call(RArgsValuesAndNames args) {
-            // pch
-            return RDataFactory.createIntVectorFromScalar(1);
-        }
-    }
-
-    @SuppressWarnings("unused")
-    public static final class C_mtext extends RExternalBuiltinNode {
-        private Object text;
-        private double side = 3.;
-        private double line = 0.;
-        private boolean outer = true;
-        private double adj = RRuntime.DOUBLE_NA;
-        private double at = RRuntime.DOUBLE_NA;
-        private double padj = RRuntime.DOUBLE_NA;
-        private double cex = RRuntime.DOUBLE_NA;
-        private double col = RRuntime.DOUBLE_NA;
-        private double font = RRuntime.DOUBLE_NA;
-
-        @Child private CastNode firstDoubleCast = newCastBuilder().asDoubleVector().findFirst().buildCastNode();
-
-        static {
-            Casts.noCasts(C_mtext.class);
-        }
-
-        @Override
-        @TruffleBoundary
-        public Object call(RArgsValuesAndNames args) {
-            extractArgumentsFrom(args);
-            return RNull.instance;
-        }
-
-        private void extractArgumentsFrom(RArgsValuesAndNames args) {
-            // text = args.getArgument(0); // postpone for now
-            side = extractFirstDoubleValueFrom(args.getArgument(1));
-            line = extractFirstDoubleValueFrom(args.getArgument(2));
-            // outer = extractFirstDoubleValueFrom(args.getArgument(3));
-            at = extractFirstDoubleValueFrom(args.getArgument(4));
-            adj = extractFirstDoubleValueFrom(args.getArgument(5));
-            padj = extractFirstDoubleValueFrom(args.getArgument(6));
-            cex = extractFirstDoubleValueFrom(args.getArgument(7));
-            // col = extractFirstDoubleValueFrom(args.getArgument(8));
-            font = extractFirstDoubleValueFrom(args.getArgument(9));
-        }
-
-        private double extractFirstDoubleValueFrom(Object arg) {
-            return (Double) firstDoubleCast.execute(arg);
-        }
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/RGraphics.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/RGraphics.java
index 11c4a71ce6b38c8afb385a9a5be2670ce5a16a14..7e9d763981801aa287fc67072cd539d53e96ce6e 100644
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/RGraphics.java
+++ b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/RGraphics.java
@@ -16,50 +16,24 @@ package com.oracle.truffle.r.library.graphics;
 
 import java.util.concurrent.atomic.AtomicBoolean;
 
-import com.oracle.truffle.r.library.graphics.core.GraphicsEngine;
-import com.oracle.truffle.r.library.graphics.core.GraphicsEngineImpl;
+import com.oracle.truffle.r.library.fastrGrid.graphics.RGridGraphicsAdapter;
 import com.oracle.truffle.r.runtime.FastROptions;
-import com.oracle.truffle.r.runtime.context.ConsoleHandler;
-import com.oracle.truffle.r.runtime.context.RContext;
-import com.oracle.truffle.r.runtime.data.RDataFactory;
-import com.oracle.truffle.r.runtime.data.RPairList;
-import com.oracle.truffle.r.runtime.data.RStringVector;
-import com.oracle.truffle.r.runtime.env.REnvironment;
-import com.oracle.truffle.r.runtime.ffi.DLL;
 import com.oracle.truffle.r.runtime.ffi.CallRFFI;
+import com.oracle.truffle.r.runtime.ffi.DLL;
 import com.oracle.truffle.r.runtime.ffi.NativeCallInfo;
 
 /**
- * A placeholder to keep {@code REngine} limited to calling the {@link #initialize} method. Two
- * possible implementations are available:
- * <ul>
- * <li>Native graphics from GnuR</li>
- * <li>Internal (Java) graphics, very incomplete implementation</li>
- * </ul>
- * The default is native graphics, selected by a startup option. Graphics is not virtualized, so
- * multiple contexts all share the same underlying implementation which is initialized exactly once.
- *
+ * A placeholder to keep {@code REngine} limited to calling the {@link #initialize} method. The code
+ * in R has a hard-coded invocation to InitGraphics in it. This initialization either invokes it
+ * too, or it runs a Java version of it if the internal grid package implementation is to be used.
  */
 public class RGraphics {
-    private static final RStringVector NULL_DEVICE = RDataFactory.createStringVectorFromScalar("null device");
-    /**
-     * The graphics devices system maintains two variables .Device and .Devices in the base
-     * environment both are always set: .Devices gives a list of character vectors of the names of
-     * open devices, .Device is the element corresponding to the currently active device. The null
-     * device will always be open.
-     */
-    private static final String DOT_DEVICE = ".Device";
-    private static final String DOT_DEVICES = ".Devices";
     private static final AtomicBoolean initialized = new AtomicBoolean();
 
     public static void initialize() {
         if (initialized.compareAndSet(false, true)) {
-            if (FastROptions.UseInternalGraphics.getBooleanValue()) {
-                REnvironment baseEnv = REnvironment.baseEnv();
-                baseEnv.safePut(DOT_DEVICE, NULL_DEVICE);
-                RPairList devices = RDataFactory.createPairList(NULL_DEVICE);
-                baseEnv.safePut(DOT_DEVICES, devices);
-                registerBaseGraphicsSystem();
+            if (FastROptions.UseInternalGridGraphics.getBooleanValue()) {
+                RGridGraphicsAdapter.initialize();
             } else {
                 DLL.DLLInfo dllInfo = DLL.findLibrary("graphics");
                 DLL.SymbolHandle symbolHandle = DLL.findSymbol("InitGraphics", dllInfo);
@@ -68,18 +42,4 @@ public class RGraphics {
             }
         }
     }
-
-    private static void registerBaseGraphicsSystem() {
-        try {
-            getGraphicsEngine().registerGraphicsSystem(new BaseGraphicsSystem());
-        } catch (Exception e) {
-            e.printStackTrace();
-            ConsoleHandler consoleHandler = RContext.getInstance().getConsoleHandler();
-            consoleHandler.println("Unable to register base graphics system");
-        }
-    }
-
-    private static GraphicsEngine getGraphicsEngine() {
-        return GraphicsEngineImpl.getInstance();
-    }
 }
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/AbstractGraphicsSystem.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/AbstractGraphicsSystem.java
deleted file mode 100644
index b401568ffa06f437c046cded9d3cbc363ed84de2..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/AbstractGraphicsSystem.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core;
-
-public abstract class AbstractGraphicsSystem implements GraphicsSystem {
-    private final GraphicsSystemParameters graphicsSystemParameters = new GraphicsSystemParameters();
-    private int id;
-
-    protected GraphicsSystemParameters getGraphicsSystemParameters() {
-        return graphicsSystemParameters;
-    }
-
-    @Override
-    public void setId(int id) {
-        this.id = id;
-    }
-
-    @Override
-    public int getId() {
-        return id;
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/DrawingParameters.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/DrawingParameters.java
deleted file mode 100644
index 69a1d7075210e9646fe22300db31d18f1678b7ab..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/DrawingParameters.java
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core;
-
-// todo implement GEContext data structure (GraphicsEngine.h)
-public final class DrawingParameters {
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsDevice.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsDevice.java
deleted file mode 100644
index 91e07584d7ea7e37edfe40fa83075849642823cb..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsDevice.java
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core;
-
-import com.oracle.truffle.r.library.graphics.core.geometry.Coordinates;
-
-public interface GraphicsDevice {
-    void deactivate();
-
-    void activate();
-
-    void close();
-
-    DrawingParameters getDrawingParameters();
-
-    void setMode(Mode newMode);
-
-    Mode getMode();
-
-    void setClipRect(double x1, double y1, double x2, double y2);
-
-    void drawPolyline(Coordinates coordinates, DrawingParameters drawingParameters);
-
-    enum Mode {
-        GRAPHICS_ON,    // allow graphics output
-        GRAPHICS_OFF    // disable graphics output
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsEngine.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsEngine.java
deleted file mode 100644
index 57a70b07e2e802fa89ba89a5af7e0702613c7b31..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsEngine.java
+++ /dev/null
@@ -1,63 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core;
-
-import com.oracle.truffle.r.library.graphics.core.geometry.Coordinates;
-
-public interface GraphicsEngine {
-    void registerGraphicsSystem(GraphicsSystem newGraphicsSystem) throws Exception;
-
-    void unRegisterGraphicsSystem(GraphicsSystem graphicsSystem);
-
-    void registerGraphicsDevice(GraphicsDevice newGraphicsDevice) throws Exception;
-
-    void unRegisterGraphicsDevice(GraphicsDevice deviceToUnregister);
-
-    int getGraphicsDevicesAmount();
-
-    /**
-     * @return true if there is only Null graphics device registered
-     */
-    boolean noGraphicsDevices();
-
-    /**
-     * Tries to install one if there is no current device.
-     *
-     * @return current {@link GraphicsDevice}
-     */
-    GraphicsDevice getCurrentGraphicsDevice();
-
-    /**
-     * @return {@link com.oracle.truffle.r.library.grDevices.NullGraphicsDevice} if unable to find
-     *         other
-     */
-    GraphicsDevice getGraphicsDeviceNextTo(GraphicsDevice graphicsDevice);
-
-    /**
-     * @return {@link com.oracle.truffle.r.library.grDevices.NullGraphicsDevice} if unable to find
-     *         other
-     */
-    GraphicsDevice getGraphicsDevicePrevTo(GraphicsDevice graphicsDevice);
-
-    void setCurrentGraphicsDeviceMode(GraphicsDevice.Mode mode);
-
-    void setCurrentGraphicsDeviceClipRect(double x1, double y1, double x2, double y2);
-
-    void drawPolyline(Coordinates coordinates, DrawingParameters drawingParameters);
-
-    void killGraphicsDeviceByIndex(int graphicsDeviceIndex);
-
-    int getCurrentGraphicsDeviceIndex();
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsEngineImpl.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsEngineImpl.java
deleted file mode 100644
index 5efaee6e6765608e465e3d67d2b606db4dec8c0a..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsEngineImpl.java
+++ /dev/null
@@ -1,316 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core;
-
-import static com.oracle.truffle.r.library.graphics.core.GraphicsEvent.GE_FINAL_STATE;
-import static com.oracle.truffle.r.library.graphics.core.GraphicsEvent.GE_INIT_STATE;
-
-import com.oracle.truffle.r.library.grDevices.NullGraphicsDevice;
-import com.oracle.truffle.r.library.grDevices.fastrgd.FastRGraphicsDevice;
-import com.oracle.truffle.r.library.graphics.core.geometry.Coordinates;
-import com.oracle.truffle.r.runtime.Utils;
-
-// todo implement 'active' devices array from devices.c
-public final class GraphicsEngineImpl implements GraphicsEngine {
-    // GNUR: GraphicsEngine.h (original value: 24)
-    private static final int MAX_GRAPHICS_SYSTEMS_AMOUNT = 48;
-    private static final int MAX_GRAPHICS_DEVICES_AMOUNT = 64;
-    private static final int NULL_GRAPHICS_DEVICE_INDEX = 0;
-    private static final int LAST_GRAPHICS_DEVICE_INDEX = MAX_GRAPHICS_DEVICES_AMOUNT - 1;
-    private static final int NOT_FOUND = -1;
-    private static final GraphicsEngine instance = new GraphicsEngineImpl();
-
-    /**
-     * According to GNUR devices.c: 0 - null device, 63 - empty.
-     */
-    private final GraphicsDevice[] graphicsDevices = new GraphicsDevice[MAX_GRAPHICS_DEVICES_AMOUNT];
-    private final GraphicsSystem[] graphicsSystems = new AbstractGraphicsSystem[MAX_GRAPHICS_SYSTEMS_AMOUNT];
-
-    private int graphicsSystemsAmount = 0;
-    private int devicesAmountWithoutNullDevice = 0;
-    private CurrentGraphicsDevice currentGraphicsDevice = new CurrentGraphicsDevice(NullGraphicsDevice.getInstance(), NULL_GRAPHICS_DEVICE_INDEX);
-
-    public static GraphicsEngine getInstance() {
-        return instance;
-    }
-
-    private GraphicsEngineImpl() {
-        initNullGraphicsDevice();
-    }
-
-    /**
-     * According to GNUR 0 index is for the Null graphics device.
-     */
-    private void initNullGraphicsDevice() {
-        graphicsDevices[NULL_GRAPHICS_DEVICE_INDEX] = NullGraphicsDevice.getInstance();
-    }
-
-    @Override
-    public void registerGraphicsSystem(GraphicsSystem newGraphicsSystem) throws Exception {
-        if (newGraphicsSystem == null) {
-            throw new NullPointerException("Graphics system to register is null");
-        }
-        int index = findElementIndexInArray(null, graphicsSystems); // find null in the
-        // graphicsSystems
-        if (NOT_FOUND == index) {
-            throw handleErrorAndMakeException("too many graphics systems registered");
-        }
-        newGraphicsSystem.setId(index);
-        graphicsSystems[index] = newGraphicsSystem;
-        callListenerForEachDevice(newGraphicsSystem.getGraphicsEventsListener(), GE_INIT_STATE);
-        graphicsSystemsAmount++;
-    }
-
-    private void callListenerForEachDevice(AbstractGraphicsSystem.GraphicsEventsListener listener, GraphicsEvent event) {
-        if (noGraphicsDevices()) {
-            return;
-        }
-        for (int i = NULL_GRAPHICS_DEVICE_INDEX + 1; i < LAST_GRAPHICS_DEVICE_INDEX; i++) {
-            GraphicsDevice graphicsDevice = graphicsDevices[i];
-            if (graphicsDevice != null) {
-                listener.onEvent(event, graphicsDevice);
-            }
-        }
-    }
-
-    // todo transcribe error(_(msg)) from errors.c
-    private static Exception handleErrorAndMakeException(String message) {
-        return new Exception(message);
-    }
-
-    // todo implement in GNUR way
-    private static void issueWarning(String warningMessage) {
-        Utils.warn(warningMessage);
-    }
-
-    @Override
-    public void unRegisterGraphicsSystem(GraphicsSystem graphicsSystem) {
-        int graphicsSystemId = graphicsSystem.getId();
-        checkGraphicsSystemIndex(graphicsSystemId);
-        if (graphicsSystemsAmount == 0) {
-            issueWarning("no graphics system to unregister");
-            return;
-        }
-        callListenerForEachDevice(graphicsSystem.getGraphicsEventsListener(), GE_FINAL_STATE);
-        graphicsSystems[graphicsSystemId] = null;
-        graphicsSystemsAmount--;
-    }
-
-    private void checkGraphicsSystemIndex(int graphicsSystemIndex) {
-        if (graphicsSystemIndex < 0 || graphicsSystemIndex >= graphicsSystems.length) {
-            throw new IllegalArgumentException("Wrong graphics system index: " + graphicsSystemIndex);
-        }
-    }
-
-    // todo implement '.Devices' list related logic from GEaddDevices (devices.c)
-    @Override
-    public void registerGraphicsDevice(GraphicsDevice newGraphicsDevice) throws Exception {
-        if (newGraphicsDevice == null) {
-            throw new NullPointerException("Graphics device to register is null");
-        }
-        if (!noGraphicsDevices()) {
-            getCurrentGraphicsDevice().deactivate();
-        }
-        int index = findElementIndexInArray(NULL_GRAPHICS_DEVICE_INDEX + 1, LAST_GRAPHICS_DEVICE_INDEX, null, graphicsDevices);
-        if (index == NOT_FOUND) {
-            throw handleErrorAndMakeException("too many open devices");
-        }
-        graphicsDevices[index] = newGraphicsDevice;
-        devicesAmountWithoutNullDevice++;
-        currentGraphicsDevice = new CurrentGraphicsDevice(newGraphicsDevice, index);
-        notifyEachGraphicsSystem(newGraphicsDevice, GE_INIT_STATE);
-        newGraphicsDevice.activate();
-    }
-
-    private void notifyEachGraphicsSystem(GraphicsDevice graphicsDevice, GraphicsEvent event) {
-        for (int i = 0; i < MAX_GRAPHICS_SYSTEMS_AMOUNT; i++) {
-            GraphicsSystem graphicsSystem = graphicsSystems[i];
-            if (graphicsSystem != null) {
-                graphicsSystem.getGraphicsEventsListener().onEvent(event, graphicsDevice);
-            }
-        }
-    }
-
-    @Override
-    public void unRegisterGraphicsDevice(GraphicsDevice deviceToUnregister) {
-        if (deviceToUnregister == null) {
-            throw new NullPointerException("Graphics device to unregister is null");
-        }
-        doUnregisterGraphicsDevice(deviceToUnregister);
-        GraphicsDevice nextGraphicsDevice = getGraphicsDeviceNextTo(deviceToUnregister);
-        int index = findElementIndexInArray(nextGraphicsDevice, graphicsDevices);
-        currentGraphicsDevice = new CurrentGraphicsDevice(nextGraphicsDevice, index);
-        nextGraphicsDevice.activate();
-        // todo Interesting that in GNUR a GraphicsSystem is not notified when a GraphicsDevice is
-        // killed
-    }
-
-    private void doUnregisterGraphicsDevice(GraphicsDevice deviceToUnregister) {
-        int index = findElementIndexInArray(deviceToUnregister, graphicsDevices);
-        if (index == NOT_FOUND) {
-            issueWarning("no graphics device to unregister");
-            return;
-        }
-        graphicsDevices[index] = null;
-        devicesAmountWithoutNullDevice--;
-        currentGraphicsDevice = new CurrentGraphicsDevice(getNullGraphicsDevice(), NULL_GRAPHICS_DEVICE_INDEX);
-        deviceToUnregister.close();
-    }
-
-    @Override
-    public int getGraphicsDevicesAmount() {
-        return devicesAmountWithoutNullDevice;
-    }
-
-    @Override
-    public boolean noGraphicsDevices() {
-        return devicesAmountWithoutNullDevice == 0;
-    }
-
-    @Override
-    public int getCurrentGraphicsDeviceIndex() {
-        return currentGraphicsDevice.graphicsDeviceIndex;
-    }
-
-    @Override
-    public GraphicsDevice getCurrentGraphicsDevice() {
-        if (isNullDeviceIsCurrent()) {
-            try {
-                // todo transcribe device installation from GNUR GEcurrentDevice (devices.c)
-                installCurrentGraphicsDevice();
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        }
-        return currentGraphicsDevice.graphicsDevice;
-    }
-
-    private boolean isNullDeviceIsCurrent() {
-        return currentGraphicsDevice.graphicsDevice == getNullGraphicsDevice();
-    }
-
-    private void installCurrentGraphicsDevice() throws Exception {
-        registerGraphicsDevice(new FastRGraphicsDevice());
-    }
-
-    @Override
-    public GraphicsDevice getGraphicsDeviceNextTo(GraphicsDevice graphicsDevice) {
-        if (graphicsDevice == null) {
-            throw new NullPointerException("Graphics device is null");
-        }
-        int startIndex = findElementIndexInArray(graphicsDevice, graphicsDevices);
-        if (startIndex == NOT_FOUND) {
-            return getNullGraphicsDevice();
-        }
-        GraphicsDevice foundDevice = findNotNullGraphicsDevice(startIndex + 1, graphicsDevices.length, SearchDirection.FORWARD);
-        if (foundDevice == null) {
-            foundDevice = findNotNullGraphicsDevice(startIndex - 1, NULL_GRAPHICS_DEVICE_INDEX, SearchDirection.BACKWARD);
-        }
-        return foundDevice == null ? getNullGraphicsDevice() : foundDevice;
-    }
-
-    @Override
-    public void setCurrentGraphicsDeviceMode(GraphicsDevice.Mode newMode) {
-        GraphicsDevice currentDevice = getCurrentGraphicsDevice();
-        if (currentDevice.getMode() != newMode) {
-            currentDevice.setMode(newMode);
-        }
-    }
-
-    @Override
-    public GraphicsDevice getGraphicsDevicePrevTo(GraphicsDevice graphicsDevice) {
-        if (graphicsDevice == null) {
-            throw new NullPointerException("Graphics device is null");
-        }
-        int startIndex = findElementIndexInArray(graphicsDevice, graphicsDevices);
-        if (startIndex == NOT_FOUND) {
-            return getNullGraphicsDevice();
-        }
-        GraphicsDevice foundDevice = findNotNullGraphicsDevice(startIndex - 1, NULL_GRAPHICS_DEVICE_INDEX, SearchDirection.BACKWARD);
-        if (foundDevice == null) {
-            foundDevice = findNotNullGraphicsDevice(startIndex + 1, graphicsDevices.length, SearchDirection.FORWARD);
-        }
-        return foundDevice == null ? getNullGraphicsDevice() : foundDevice;
-    }
-
-    private static <T> int findElementIndexInArray(T element, T[] array) {
-        return findElementIndexInArray(0, array.length, element, array);
-    }
-
-    private static <T> int findElementIndexInArray(int startIndexInclusive, int endIndexNotInclusive, T element, T[] array) {
-        for (int i = startIndexInclusive; i < endIndexNotInclusive; i++) {
-            if (array[i] == element) {
-                return i;
-            }
-        }
-        return NOT_FOUND;
-    }
-
-    private GraphicsDevice findNotNullGraphicsDevice(int startIndexInclusive, int endIndexNotInclusive, SearchDirection direction) {
-        switch (direction) {
-            case FORWARD:
-                for (int i = startIndexInclusive; i < endIndexNotInclusive; i++) {
-                    GraphicsDevice graphicsDevice = graphicsDevices[i];
-                    if (graphicsDevice != null) {
-                        return graphicsDevice;
-                    }
-                }
-                break;
-            case BACKWARD:
-                for (int i = startIndexInclusive; i > endIndexNotInclusive; i--) {
-                    GraphicsDevice graphicsDevice = graphicsDevices[i];
-                    if (graphicsDevice != null) {
-                        return graphicsDevice;
-                    }
-                }
-        }
-        return getNullGraphicsDevice();
-    }
-
-    private GraphicsDevice getNullGraphicsDevice() {
-        return graphicsDevices[NULL_GRAPHICS_DEVICE_INDEX];
-    }
-
-    @Override
-    public void setCurrentGraphicsDeviceClipRect(double x1, double y1, double x2, double y2) {
-        // todo transcribe from GESetClip() (engine.c)
-        getCurrentGraphicsDevice().setClipRect(0, 0, 0, 0);
-    }
-
-    @Override
-    public void drawPolyline(Coordinates coordinates, DrawingParameters drawingParameters) {
-        getCurrentGraphicsDevice().drawPolyline(coordinates, drawingParameters);
-    }
-
-    @Override
-    public void killGraphicsDeviceByIndex(int graphicsDeviceIndex) {
-        // todo TBD
-    }
-
-    private final class CurrentGraphicsDevice {
-        private final GraphicsDevice graphicsDevice;
-        private final int graphicsDeviceIndex;
-
-        private CurrentGraphicsDevice(GraphicsDevice graphicsDevice, int graphicsDeviceIndex) {
-            this.graphicsDevice = graphicsDevice;
-            this.graphicsDeviceIndex = graphicsDeviceIndex;
-        }
-    }
-
-    private enum SearchDirection {
-        FORWARD,
-        BACKWARD
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsEvent.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsEvent.java
deleted file mode 100644
index 5ff9cf1c50ef0a89089439190ac045e43bac3521..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsEvent.java
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core;
-
-public enum GraphicsEvent {
-    GE_INIT_STATE,
-    GE_FINAL_STATE
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsSystem.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsSystem.java
deleted file mode 100644
index d0011a8c9a42eb6c9af5976901e6d20a283bea67..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsSystem.java
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core;
-
-public interface GraphicsSystem {
-    GraphicsEventsListener getGraphicsEventsListener();
-
-    void setId(int id);
-
-    int getId();
-
-    public interface GraphicsEventsListener {
-        void onEvent(GraphicsEvent graphicsEvent, GraphicsDevice graphicsDevice);
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsSystemParameters.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsSystemParameters.java
deleted file mode 100644
index 4d3ead19cad9736f8048aa38d04083add1bc47e4..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsSystemParameters.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core;
-
-import java.util.HashMap;
-import java.util.IdentityHashMap;
-import java.util.Map;
-
-class GraphicsSystemParameters {
-    private final Map<GraphicsDevice, HashMap<String, Object>> parametersByDevices = new IdentityHashMap<>();
-
-    void addParameterForDevice(GraphicsDevice graphicsDevice, String parameterName, Object parameterValue) {
-        HashMap<String, Object> parameters = parametersByDevices.get(graphicsDevice);
-        if (parameters == null) {
-            parameters = new HashMap<>();
-            parametersByDevices.put(graphicsDevice, parameters);
-        }
-        parameters.put(parameterName, parameterValue);
-    }
-
-    Object getParameterForDevice(GraphicsDevice graphicsDevice, int parameterName) {
-        HashMap<String, Object> parameters = parametersByDevices.get(graphicsDevice);
-        return parameters == null ? null : parameters.get(parameterName);
-    }
-
-    void removeAllParametersForDevice(GraphicsDevice graphicsDevice) {
-        parametersByDevices.remove(graphicsDevice);
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/drawables/CoordinatesDrawableObject.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/drawables/CoordinatesDrawableObject.java
deleted file mode 100644
index 4f641d7a79cf822ec0291cb4722a212e2d260587..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/drawables/CoordinatesDrawableObject.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core.drawables;
-
-import com.oracle.truffle.r.library.graphics.core.geometry.CoordinateSystem;
-import com.oracle.truffle.r.library.graphics.core.geometry.Coordinates;
-import com.oracle.truffle.r.library.graphics.core.geometry.IntCoordinates;
-
-/**
- * Denotes an object which drawing depends only from {@link Coordinates}. And automates conversion
- * from <code>srcCoordinates</code> to <code>dstCoordinates</code>.
- */
-public abstract class CoordinatesDrawableObject extends DrawableObject {
-    private final Coordinates srcCoordinates;
-
-    private Coordinates dstCoordinates;
-
-    protected CoordinatesDrawableObject(CoordinateSystem coordinateSystem, Coordinates coordinates) {
-        super(coordinateSystem);
-        this.srcCoordinates = coordinates;
-    }
-
-    @Override
-    public void recalculateForDrawingIn(CoordinateSystem dstCoordinateSystem) {
-        Coordinates converted = dstCoordinateSystem.convertCoordinatesFrom(getSrcCoordinateSystem(), srcCoordinates);
-        dstCoordinates = new IntCoordinates(converted.getXCoordinatesAsInts(), converted.getYCoordinatesAsInts());
-    }
-
-    protected final Coordinates getDstCoordinates() {
-        return dstCoordinates;
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/drawables/DrawableObject.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/drawables/DrawableObject.java
deleted file mode 100644
index ec4b0d5824ce7d9b9c677c3884b9755c06898c5d..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/drawables/DrawableObject.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core.drawables;
-
-import java.awt.Graphics2D;
-
-import com.oracle.truffle.r.library.graphics.core.geometry.CoordinateSystem;
-
-/**
- * Denotes an object defined in <code>srcCoordinateSystem</code> that can be drawn in
- * <code>dstCoordinateSystem</code> on {@link Graphics2D}.
- */
-public abstract class DrawableObject {
-    private final CoordinateSystem srcCoordinateSystem;
-
-    protected DrawableObject(CoordinateSystem srcCoordinateSystem) {
-        this.srcCoordinateSystem = srcCoordinateSystem;
-    }
-
-    public abstract void drawOn(Graphics2D g2);
-
-    /**
-     * Override to prepare coordinates given in <code>srcCoordinateSystem</code> to be drawn in
-     * <code>srcCoordinateSystem</code>.
-     */
-    public abstract void recalculateForDrawingIn(CoordinateSystem dstCoordinateSystem);
-
-    protected final CoordinateSystem getSrcCoordinateSystem() {
-        return srcCoordinateSystem;
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/drawables/PolylineDrawableObject.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/drawables/PolylineDrawableObject.java
deleted file mode 100644
index 2cf12e7f6aed8572cb9334b8d4241a24dd505012..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/drawables/PolylineDrawableObject.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core.drawables;
-
-import java.awt.Graphics2D;
-
-import com.oracle.truffle.r.library.graphics.core.geometry.CoordinateSystem;
-import com.oracle.truffle.r.library.graphics.core.geometry.Coordinates;
-
-/**
- * Able to draw a polyline on {@link Graphics2D}.
- */
-public class PolylineDrawableObject extends CoordinatesDrawableObject {
-    public PolylineDrawableObject(CoordinateSystem coordinateSystem, Coordinates coordinates) {
-        super(coordinateSystem, coordinates);
-    }
-
-    @Override
-    public void drawOn(Graphics2D g2) {
-        Coordinates coords = getDstCoordinates();
-        int[] xCoords = coords.getXCoordinatesAsInts();
-        g2.drawPolyline(xCoords, coords.getYCoordinatesAsInts(), xCoords.length);
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/drawables/StringDrawableObject.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/drawables/StringDrawableObject.java
deleted file mode 100644
index ccf4e16876bbd90c3b576366407838592916ff4f..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/drawables/StringDrawableObject.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core.drawables;
-
-import java.awt.Graphics2D;
-import java.util.stream.IntStream;
-
-import com.oracle.truffle.r.library.graphics.core.geometry.CoordinateSystem;
-import com.oracle.truffle.r.library.graphics.core.geometry.Coordinates;
-
-/**
- * Able to render a text on {@link Graphics2D}.
- */
-public class StringDrawableObject extends CoordinatesDrawableObject {
-    private final String[] strings;
-
-    public StringDrawableObject(CoordinateSystem coordinateSystem, Coordinates coordinates, String[] strings) {
-        super(coordinateSystem, coordinates);
-        this.strings = strings;
-    }
-
-    @Override
-    public void drawOn(Graphics2D g2) {
-        Coordinates dstCoordinates = getDstCoordinates();
-        int[] xCoords = dstCoordinates.getXCoordinatesAsInts();
-        int[] yCoords = dstCoordinates.getYCoordinatesAsInts();
-        IntStream.range(0, strings.length).forEach(i -> g2.drawString(strings[i], xCoords[i], yCoords[i]));
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/Axis.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/Axis.java
deleted file mode 100644
index c9a33c65a82da57a5b2f57a5fb1c65dbc2203be4..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/Axis.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core.geometry;
-
-public final class Axis {
-    private final double minValue;
-    private final double maxValue;
-    private final AxisDirection direction;
-
-    public Axis(double minValue, double maxValue, AxisDirection direction) {
-        this.minValue = minValue;
-        this.maxValue = maxValue;
-        this.direction = direction;
-    }
-
-    public double getMinValue() {
-        return minValue;
-    }
-
-    public double getMaxValue() {
-        return maxValue;
-    }
-
-    public AxisDirection getDirection() {
-        return direction;
-    }
-
-    public double getRange() {
-        return maxValue - minValue;
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/AxisDirection.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/AxisDirection.java
deleted file mode 100644
index 8099a58058eae00db737d42cc2c75a918ff9fcb0..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/AxisDirection.java
+++ /dev/null
@@ -1,22 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core.geometry;
-
-public enum AxisDirection {
-    NORTH,
-    SOUTH,
-    WEST,
-    EAST
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/CoordinateSystem.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/CoordinateSystem.java
deleted file mode 100644
index 2c1fca3b817a003160c798290ea87c1d93c0d7f2..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/CoordinateSystem.java
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core.geometry;
-
-import java.util.stream.DoubleStream;
-
-/**
- * Denotes X-Y coordinate system by specifying max and min values for X-Y axis. Able to convert
- * coordinates given in another {@link CoordinateSystem}
- */
-public final class CoordinateSystem {
-    private final Axis xAxis;
-    private final Axis yAxis;
-
-    /**
-     * Uses Java graphics default axis orientation: x increases to the right, y increases to the
-     * bottom.
-     */
-    public CoordinateSystem(double minX, double maxX, double minY, double maxY) {
-        this(minX, maxX, minY, maxY, AxisDirection.EAST, AxisDirection.SOUTH);
-    }
-
-    public CoordinateSystem(double minX, double maxX, double minY, double maxY, AxisDirection xDirection, AxisDirection yDirection) {
-        this(new Axis(minX, maxX, xDirection), new Axis(minY, maxY, yDirection));
-    }
-
-    public CoordinateSystem(Axis xAxis, Axis yAxis) {
-        this.xAxis = xAxis;
-        this.yAxis = yAxis;
-    }
-
-    /**
-     * Transforms <code> otherCoordinates </code> given in <code> otherCoordinateSystem</code> to
-     * this coordinate system. Also applies the affine transformation defined by ratio and shifts.
-     */
-    public Coordinates convertCoordinatesFrom(CoordinateSystem otherCoordinateSystem, Coordinates otherCoordinates, double ratio, double xAxisShift, double yAxisShift) {
-        double[] resultX = convertCoordinatesBetweenAxises(getXAxis(), otherCoordinateSystem.getXAxis(), otherCoordinates.getXCoordinatesAsDoubles(), ratio, xAxisShift);
-        double[] resultY = convertCoordinatesBetweenAxises(getYAxis(), otherCoordinateSystem.getYAxis(), otherCoordinates.getYCoordinatesAsDoubles(), ratio, yAxisShift);
-        return new DoubleCoordinates(resultX, resultY);
-    }
-
-    public Coordinates convertCoordinatesFrom(CoordinateSystem otherCoordinateSystem, Coordinates otherCoordinates) {
-        double noRatio = 1;
-        double noShift = 0;
-        return convertCoordinatesFrom(otherCoordinateSystem, otherCoordinates, noRatio, noShift, noShift);
-    }
-
-    private static double[] convertCoordinatesBetweenAxises(Axis toAxis, Axis fromAxis, double[] coords, double givenRatio, double givenShift) {
-        boolean sameDirection = toAxis.getDirection() == fromAxis.getDirection();
-        double ratio = toAxis.getRange() / fromAxis.getRange();
-        ratio = sameDirection ? ratio : -ratio;
-        ratio *= givenRatio; // adding given ratio
-        double shift = sameDirection ? 0 : toAxis.getMaxValue();
-        shift += givenShift * ratio; // adding given shift
-        return applyShiftAndRatio(coords, ratio, shift);
-    }
-
-    private static double[] applyShiftAndRatio(double[] coords, double ratio, double shift) {
-        return DoubleStream.of(coords).map(d -> d * ratio + shift).toArray();
-    }
-
-    private Axis getXAxis() {
-        return xAxis;
-    }
-
-    private Axis getYAxis() {
-        return yAxis;
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/Coordinates.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/Coordinates.java
deleted file mode 100644
index 95396232f26e67252041de67b3653756097f7c7c..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/Coordinates.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core.geometry;
-
-/**
- * Denotes X-Y coordinates. Instances must be immutable objects.
- */
-public interface Coordinates {
-    double[] getXCoordinatesAsDoubles();
-
-    double[] getYCoordinatesAsDoubles();
-
-    int[] getXCoordinatesAsInts();
-
-    int[] getYCoordinatesAsInts();
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/CoordinatesFactory.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/CoordinatesFactory.java
deleted file mode 100644
index 33e81bb144b46109300d75c7c5881ace2e86b41b..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/CoordinatesFactory.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core.geometry;
-
-import java.util.Arrays;
-import java.util.stream.DoubleStream;
-import java.util.stream.IntStream;
-
-import com.oracle.truffle.r.runtime.data.RDoubleVector;
-
-public final class CoordinatesFactory {
-    private CoordinatesFactory() {
-    }
-
-    public static DoubleCoordinates createWithSameX(double x, double[] yCoords) {
-        double[] xCoords = createdFilledArray(x, yCoords.length);
-        return new DoubleCoordinates(xCoords, yCoords);
-    }
-
-    public static DoubleCoordinates createWithSameY(double[] xCoords, double y) {
-        double[] yCoords = createdFilledArray(y, xCoords.length);
-        return new DoubleCoordinates(xCoords, yCoords);
-    }
-
-    private static double[] createdFilledArray(double value, int size) {
-        double[] result = new double[size];
-        Arrays.fill(result, value);
-        return result;
-    }
-
-    public static DoubleCoordinates createByXYVector(RDoubleVector xyVector) {
-        int length = xyVector.getLength();
-        double[] xCoords = IntStream.range(0, length).filter(i -> i % 2 == 0).mapToDouble(xyVector::getDataAt).toArray();
-        double[] yCoords = IntStream.range(0, length).filter(i -> i % 2 != 0).mapToDouble(xyVector::getDataAt).toArray();
-        return new DoubleCoordinates(xCoords, yCoords);
-    }
-
-    public static DoubleCoordinates createByXYPairs(double[] xyPairs) {
-        int length = xyPairs.length;
-        double[] xCoords = IntStream.range(0, length).filter(i -> i % 2 == 0).mapToDouble(i -> xyPairs[i]).toArray();
-        double[] yCoords = IntStream.range(0, length).filter(i -> i % 2 != 0).mapToDouble(i -> xyPairs[i]).toArray();
-        return new DoubleCoordinates(xCoords, yCoords);
-    }
-
-    public static DoubleCoordinates withRatioAndShift(Coordinates coordinates, double ratio, double shift) {
-        return withRatioAndShift(coordinates, ratio, shift, shift);
-    }
-
-    public static DoubleCoordinates withRatioAndShift(Coordinates coordinates, double ratio, double xShift, double yShift) {
-        double[] convertedX = applyRatioAndShiftTo(coordinates.getXCoordinatesAsDoubles(), ratio, xShift);
-        double[] convertedY = applyRatioAndShiftTo(coordinates.getYCoordinatesAsDoubles(), ratio, yShift);
-        return new DoubleCoordinates(convertedX, convertedY);
-    }
-
-    private static double[] applyRatioAndShiftTo(double[] coordinates, double ratio, double shift) {
-        return DoubleStream.of(coordinates).map(d -> d * ratio + shift).toArray();
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/DoubleCoordinates.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/DoubleCoordinates.java
deleted file mode 100644
index 90dcb51dd61f9b8f234b097f6b5ff97531aff1af..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/DoubleCoordinates.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2016, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core.geometry;
-
-import java.util.stream.DoubleStream;
-
-public final class DoubleCoordinates implements Coordinates {
-
-    private final double[] xCoords;
-    private final double[] yCoords;
-
-    public DoubleCoordinates(double[] xCoords, double[] yCoords) {
-        this.xCoords = xCoords;
-        this.yCoords = yCoords;
-    }
-
-    @Override
-    public double[] getXCoordinatesAsDoubles() {
-        return xCoords;
-    }
-
-    @Override
-    public double[] getYCoordinatesAsDoubles() {
-        return yCoords;
-    }
-
-    @Override
-    public int[] getXCoordinatesAsInts() {
-        return toInt(getXCoordinatesAsDoubles());
-    }
-
-    @Override
-    public int[] getYCoordinatesAsInts() {
-        return toInt(getYCoordinatesAsDoubles());
-    }
-
-    private static int[] toInt(double[] doubleArray) {
-        return DoubleStream.of(doubleArray).mapToInt(d -> (int) d).toArray();
-    }
-}
diff --git a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/IntCoordinates.java b/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/IntCoordinates.java
deleted file mode 100644
index cd62ef35fe6eca47082d6314c2852f7daec5f8c1..0000000000000000000000000000000000000000
--- a/com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/IntCoordinates.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * This material is distributed under the GNU General Public License
- * Version 2. You may review the terms of this license at
- * http://www.gnu.org/licenses/gpl-2.0.html
- *
- * Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
- * Copyright (C) 1998 Ross Ihaka
- * Copyright (c) 1998--2014, The R Core Team
- * Copyright (c) 2002--2010, The R Foundation
- * Copyright (C) 2005--2006, Morten Welinder
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates
- *
- * All rights reserved.
- */
-package com.oracle.truffle.r.library.graphics.core.geometry;
-
-import java.util.stream.IntStream;
-
-public final class IntCoordinates implements Coordinates {
-    private final int[] xCoords;
-    private final int[] yCoords;
-
-    public IntCoordinates(int[] xCoords, int[] yCoords) {
-        this.xCoords = xCoords;
-        this.yCoords = yCoords;
-    }
-
-    @Override
-    public double[] getXCoordinatesAsDoubles() {
-        return toDouble(xCoords);
-    }
-
-    @Override
-    public double[] getYCoordinatesAsDoubles() {
-        return toDouble(yCoords);
-    }
-
-    @Override
-    public int[] getXCoordinatesAsInts() {
-        return xCoords;
-    }
-
-    @Override
-    public int[] getYCoordinatesAsInts() {
-        return yCoords;
-    }
-
-    private static double[] toDouble(int[] intArray) {
-        return IntStream.of(intArray).mapToDouble(i -> i).toArray();
-    }
-}
diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/foreign/CallAndExternalFunctions.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/foreign/CallAndExternalFunctions.java
index cbb9714312f98d8d88db8579c8d3e0825cd8f382..b7bee9a581eb1f2add866a49558752b2e3e1eb72 100644
--- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/foreign/CallAndExternalFunctions.java
+++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/foreign/CallAndExternalFunctions.java
@@ -22,31 +22,7 @@ import com.oracle.truffle.api.dsl.Cached;
 import com.oracle.truffle.api.dsl.Fallback;
 import com.oracle.truffle.api.dsl.Specialization;
 import com.oracle.truffle.api.frame.VirtualFrame;
-import com.oracle.truffle.r.library.fastrGrid.GridState;
-import com.oracle.truffle.r.library.fastrGrid.GridStateGetNode;
-import com.oracle.truffle.r.library.fastrGrid.GridStateSetNode;
-import com.oracle.truffle.r.library.fastrGrid.IgnoredGridExternal;
-import com.oracle.truffle.r.library.fastrGrid.LCircle;
-import com.oracle.truffle.r.library.fastrGrid.LConvert;
-import com.oracle.truffle.r.library.fastrGrid.LGridDirty;
-import com.oracle.truffle.r.library.fastrGrid.LInitGPar;
-import com.oracle.truffle.r.library.fastrGrid.LInitGrid;
-import com.oracle.truffle.r.library.fastrGrid.LInitViewPortStack;
-import com.oracle.truffle.r.library.fastrGrid.LLines;
-import com.oracle.truffle.r.library.fastrGrid.LNewPage;
-import com.oracle.truffle.r.library.fastrGrid.LPoints;
-import com.oracle.truffle.r.library.fastrGrid.LPolygon;
-import com.oracle.truffle.r.library.fastrGrid.LRect;
-import com.oracle.truffle.r.library.fastrGrid.LSegments;
-import com.oracle.truffle.r.library.fastrGrid.LText;
-import com.oracle.truffle.r.library.fastrGrid.LTextBounds;
-import com.oracle.truffle.r.library.fastrGrid.LUnsetViewPort;
-import com.oracle.truffle.r.library.fastrGrid.LUpViewPort;
-import com.oracle.truffle.r.library.fastrGrid.graphics.CPar;
-import com.oracle.truffle.r.library.graphics.GraphicsCCalls;
-import com.oracle.truffle.r.library.graphics.GraphicsCCalls.C_PlotXY;
-import com.oracle.truffle.r.library.grid.GridFunctionsFactory.InitGridNodeGen;
-import com.oracle.truffle.r.library.grid.GridFunctionsFactory.ValidUnitsNodeGen;
+import com.oracle.truffle.r.library.fastrGrid.FastRGridExternalLookup;
 import com.oracle.truffle.r.library.methods.MethodsListDispatchFactory.R_M_setPrimitiveMethodsNodeGen;
 import com.oracle.truffle.r.library.methods.MethodsListDispatchFactory.R_externalPtrPrototypeObjectNodeGen;
 import com.oracle.truffle.r.library.methods.MethodsListDispatchFactory.R_getClassFromCacheNodeGen;
@@ -99,7 +75,6 @@ import com.oracle.truffle.r.runtime.FastROptions;
 import com.oracle.truffle.r.runtime.RError;
 import com.oracle.truffle.r.runtime.RInternalCode;
 import com.oracle.truffle.r.runtime.RInternalError;
-import com.oracle.truffle.r.runtime.RRuntime;
 import com.oracle.truffle.r.runtime.builtins.RBuiltin;
 import com.oracle.truffle.r.runtime.context.RContext;
 import com.oracle.truffle.r.runtime.data.RArgsValuesAndNames;
@@ -262,7 +237,7 @@ public class CallAndExternalFunctions {
         protected RExternalBuiltinNode lookupBuiltin(RList symbol) {
             String name = lookupName(symbol);
             if (FastROptions.UseInternalGridGraphics.getBooleanValue() && name != null) {
-                RExternalBuiltinNode gridExternal = lookupFastRGridBuiltin(name);
+                RExternalBuiltinNode gridExternal = FastRGridExternalLookup.lookupDotCall(name);
                 if (gridExternal != null) {
                     return gridExternal;
                 }
@@ -656,113 +631,11 @@ public class CallAndExternalFunctions {
                 // parallel
                 case "mc_is_child":
                     return MCIsChildNodeGen.create();
-                default:
-                    return FastROptions.UseInternalGraphics.getBooleanValue() ? lookupGraphicsBuiltin(name) : null;
-            }
-        }
-
-        private static RExternalBuiltinNode lookupGraphicsBuiltin(String name) {
-            switch (name) {
-                // grDevices
-                case "cairoProps":
-                    return CairoPropsNodeGen.create();
-                case "makeQuartzDefault":
-                    return new MakeQuartzDefault();
-
-                // grid
-                case "L_initGrid":
-                    return InitGridNodeGen.create();
-                case "L_validUnits":
-                    return ValidUnitsNodeGen.create();
                 default:
                     return null;
             }
         }
 
-        private RExternalBuiltinNode lookupFastRGridBuiltin(String name) {
-            switch (name) {
-                case "L_gridDirty":
-                    return new LGridDirty();
-                case "L_initGrid":
-                    return LInitGrid.create();
-                case "L_newpage":
-                    return new LNewPage();
-                case "L_convert":
-                    return LConvert.create();
-
-                // Viewport management
-                case "L_upviewport":
-                    return LUpViewPort.create();
-                case "L_initViewportStack":
-                    return new LInitViewPortStack();
-                case "L_unsetviewport":
-                    return LUnsetViewPort.create();
-                case "L_setviewport":
-                case "L_downviewport":
-                    return getExternalFastRGridBuiltinNode(name);
-
-                // Drawing primitives
-                case "L_rect":
-                    return LRect.create();
-                case "L_lines":
-                    return LLines.create();
-                case "L_polygon":
-                    return LPolygon.create();
-                case "L_text":
-                    return LText.create();
-                case "L_textBounds":
-                    return LTextBounds.create();
-                case "L_segments":
-                    return LSegments.create();
-                case "L_circle":
-                    return LCircle.create();
-                case "L_points":
-                    return LPoints.create();
-
-                // Simple grid state access
-                case "L_getGPar":
-                    return new GridStateGetNode(GridState::getGpar);
-                case "L_setGPar":
-                    return GridStateSetNode.create((state, val) -> state.setGpar((RList) val));
-                case "L_getCurrentGrob":
-                    return new GridStateGetNode(GridState::getCurrentGrob);
-                case "L_setCurrentGrob":
-                    return GridStateSetNode.create(GridState::setCurrentGrob);
-                case "L_currentViewport":
-                    return new GridStateGetNode(GridState::getViewPort);
-                case "L_initGPar":
-                    return new LInitGPar();
-
-                // Display list stuff: not implemented atm
-                case "L_getDisplayList":
-                    return new IgnoredGridExternal(RDataFactory.createList());
-                case "L_getDLindex":
-                    return new IgnoredGridExternal(0);
-                case "L_getDLon":
-                case "L_getEngineDLon":
-                    return new IgnoredGridExternal(RRuntime.LOGICAL_FALSE);
-                case "L_initDisplayList":
-                case "L_newpagerecording":
-                case "L_setDisplayList":
-                case "L_setDLelt":
-                case "L_setDLindex":
-                case "L_setDLon":
-                    return new IgnoredGridExternal(RNull.instance);
-
-                // These methods do not use graphics system or any global state. For now,
-                // we can re-use the native implementation, which in the future should be rewritten
-                // to managed code.
-                case "L_validUnits":
-                    return null;
-                default:
-                    if (name.startsWith("L_")) {
-                        throw RInternalError.shouldNotReachHere("Unimplemented grid external " + name);
-                    } else {
-                        return null;
-                    }
-            }
-        }
-
         /**
          * {@code .NAME = NativeSymbolInfo} implemented as a builtin.
          */
@@ -850,9 +723,9 @@ public class CallAndExternalFunctions {
         protected RExternalBuiltinNode lookupBuiltin(RList f) {
             String name = lookupName(f);
             if (FastROptions.UseInternalGridGraphics.getBooleanValue()) {
-                switch (name) {
-                    case "devholdflush":
-                        return new IgnoredGridExternal(RNull.instance);
+                RExternalBuiltinNode gridExternal = FastRGridExternalLookup.lookupDotExternal(name);
+                if (gridExternal != null) {
+                    return gridExternal;
                 }
             }
             switch (name) {
@@ -966,13 +839,13 @@ public class CallAndExternalFunctions {
         @Override
         @TruffleBoundary
         protected RExternalBuiltinNode lookupBuiltin(RList symbol) {
+            String name = lookupName(symbol);
             if (FastROptions.UseInternalGridGraphics.getBooleanValue()) {
-                switch (lookupName(symbol)) {
-                    case "C_par":
-                        return new CPar();
+                RExternalBuiltinNode gridExternal = FastRGridExternalLookup.lookupDotExternal2(name);
+                if (gridExternal != null) {
+                    return gridExternal;
                 }
             }
-            String name = lookupName(symbol);
             switch (name) {
                 // tools
                 case "writetable":
@@ -1041,14 +914,6 @@ public class CallAndExternalFunctions {
         @Override
         @TruffleBoundary
         protected RExternalBuiltinNode lookupBuiltin(RList f) {
-            if (FastROptions.UseInternalGraphics.getBooleanValue()) {
-                switch (lookupName(f)) {
-                    case "C_mtext":
-                        return new GraphicsCCalls.C_mtext();
-                    case "C_plotXY":
-                        return new C_PlotXY();
-                }
-            }
             return null;
         }
 
diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/foreign/LookupAdapter.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/foreign/LookupAdapter.java
index 84c0278f50d87e448f4aa0c579bebbd3fd147357..31f230f5226efbc9c0c40a1c00d8991adbb69e06 100644
--- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/foreign/LookupAdapter.java
+++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/foreign/LookupAdapter.java
@@ -28,7 +28,6 @@ import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
 import com.oracle.truffle.api.dsl.Specialization;
 import com.oracle.truffle.api.frame.VirtualFrame;
 import com.oracle.truffle.api.nodes.Node;
-import com.oracle.truffle.r.library.fastrGrid.LInitGrid;
 import com.oracle.truffle.r.library.stats.RandFunctionsNodes;
 import com.oracle.truffle.r.nodes.access.vector.ElementAccessMode;
 import com.oracle.truffle.r.nodes.access.vector.ExtractVectorNode;
@@ -178,10 +177,6 @@ abstract class LookupAdapter extends RBuiltinNode {
         return new RInternalCodeBuiltinNode(RContext.getInstance(), "stats", RInternalCode.loadSourceRelativeTo(RandFunctionsNodes.class, "model.R"), name);
     }
 
-    protected static RExternalBuiltinNode getExternalFastRGridBuiltinNode(String name) {
-        return new RInternalCodeBuiltinNode(RContext.getInstance(), "grid", RInternalCode.loadSourceRelativeTo(LInitGrid.class, "fastrGrid.R"), name);
-    }
-
     protected static final int CallNST = DLL.NativeSymbolType.Call.ordinal();
     protected static final int ExternalNST = DLL.NativeSymbolType.External.ordinal();
 
diff --git a/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/ExtBuiltinsList.java b/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/ExtBuiltinsList.java
index 98eb067b7a6a144ec15572ed5ce698a971c85cd2..1035f57473bb7f0193a228c9faaac48d2cd6fddf 100644
--- a/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/ExtBuiltinsList.java
+++ b/com.oracle.truffle.r.nodes.test/src/com/oracle/truffle/r/nodes/test/ExtBuiltinsList.java
@@ -58,12 +58,6 @@ public class ExtBuiltinsList {
                     com.oracle.truffle.r.library.tools.Rmd5NodeGen.class,
                     com.oracle.truffle.r.library.tools.DirChmodNodeGen.class,
                     com.oracle.truffle.r.library.tools.C_ParseRdNodeGen.class,
-                    com.oracle.truffle.r.library.grDevices.DevicesCCallsFactory.C_DevOffNodeGen.class,
-                    com.oracle.truffle.r.library.grDevices.DevicesCCalls.C_DevCur.class,
-                    com.oracle.truffle.r.library.grDevices.DevicesCCalls.C_PDF.class,
-                    com.oracle.truffle.r.library.graphics.GraphicsCCalls.C_PlotXY.class,
-                    com.oracle.truffle.r.library.graphics.GraphicsCCalls.C_Par.class,
-                    com.oracle.truffle.r.library.graphics.GraphicsCCalls.C_mtext.class,
                     com.oracle.truffle.r.library.stats.WilcoxFreeNode.class,
                     com.oracle.truffle.r.library.stats.StatsFunctionsNodesFactory.Function3_2NodeGen.class,
                     com.oracle.truffle.r.library.stats.StatsFunctionsNodesFactory.Function4_1NodeGen.class,
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/FastROptions.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/FastROptions.java
index 157526ea1d32140397e0b470186f2e6e8dc78574..24f998565a1d465f9bf3e16285bd485ea2cf431a 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/FastROptions.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/FastROptions.java
@@ -51,7 +51,6 @@ public enum FastROptions {
     FullPrecisionSum("Use 128 bit arithmetic in sum builtin", false),
     InvisibleArgs("Argument writes do not trigger state transitions", true),
     RefCountIncrementOnly("Disable reference count decrements for experimental state transition implementation", false),
-    UseInternalGraphics("Whether the internal (Java) graphics subsystem should be used", false),
     UseInternalGridGraphics("Whether the internal (Java) grid graphics implementation should be used", false),
     UseSpecials("Whether the fast-path special call nodes should be created for simple enough arguments.", true),
     ForceSources("Generate source sections for unserialized code", false),
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RPairList.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RPairList.java
index c999db5a78e8e5a78182682ed4d41e9a57e2801b..32d4e8881858f429fa8a630ce55be34b9f4e4001 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RPairList.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RPairList.java
@@ -211,6 +211,17 @@ public final class RPairList extends RSharingAttributeStorage implements RAbstra
         return type;
     }
 
+    /**
+     * Appends given value as the last element of the pairlist.
+     */
+    public void appendToEnd(RPairList value) {
+        RPairList last = null;
+        for (RPairList item : this) {
+            last = item;
+        }
+        last.setCdr(value);
+    }
+
     @Override
     public boolean isComplete() {
         // TODO: is it important to get more precise information here?
diff --git a/mx.fastr/copyrights/overrides b/mx.fastr/copyrights/overrides
index 8dc2bf1018ce4a56a935a0346e28f0c49e17a721..d3c6075238f30f0347d2ed8f3bb85f70613cd644 100644
--- a/mx.fastr/copyrights/overrides
+++ b/mx.fastr/copyrights/overrides
@@ -1,30 +1,5 @@
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/BaseGraphicsSystem.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/AbstractGraphicsSystem.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/drawables/CoordinatesDrawableObject.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/drawables/DrawableObject.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/drawables/PolylineDrawableObject.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/drawables/StringDrawableObject.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/DrawingParameters.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/Axis.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/AxisDirection.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/Coordinates.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/CoordinatesFactory.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/CoordinateSystem.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/DoubleCoordinates.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/geometry/IntCoordinates.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsDevice.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsEngine.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsEngineImpl.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsEvent.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsSystem.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/core/GraphicsSystemParameters.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/FastRComponent.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/GraphicsCCalls.java,gnu_r_graphics.copyright
 com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/graphics/RGraphics.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/grDevices/DevicesCCalls.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/grDevices/fastrgd/FastRGraphicsDevice.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/grDevices/NullGraphicsDevice.java,gnu_r_graphics.copyright
-com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/grDevices/pdf/PdfGraphicsDevice.java,gnu_r_graphics.copyright
+com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/fastrGrid/graphics/RGridGraphicsAdapter.java,gnu_r_graphics.copyright
 com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/grid/GridFunctions.java,gnu_r_murrel_core.copyright
 com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/methods/MethodsListDispatch.java,gnu_r.copyright
 com.oracle.truffle.r.library/src/com/oracle/truffle/r/library/methods/Slot.java,gnu_r.copyright