Compare commits

...

34 commits

Author SHA1 Message Date
9979e40d76 0.5.1 2023-05-22 00:36:16 +03:00
860498b8da Remove package.json import 2023-05-22 00:36:02 +03:00
1c0e29e4b7 Update README.md 2023-05-22 00:34:56 +03:00
ad163ef466
Merge pull request #1 from He4eT/emglken-0-5-2
Emglken 0.5.2
2023-05-21 00:19:32 +03:00
96fe7e7671 0.5.0 2023-05-21 00:16:31 +03:00
93db8c2224 Update README.md 2023-05-21 00:13:49 +03:00
e8ad18ffe4 Remove unused params 2023-05-21 00:10:20 +03:00
fcc3a3b296 Add eslint config file 2023-05-21 00:09:38 +03:00
3499c54ea5 Add eslint package 2023-05-21 00:08:58 +03:00
1e569811a9 Copy glkOte/glkapi from https://github.com/erkyrath/glkote 2023-05-21 00:07:37 +03:00
cb7d6e51f9 Use emglken 0.5.2 2023-05-20 23:39:01 +03:00
a594cb1f74 Update emglken to 0.5.2 2023-05-20 23:37:55 +03:00
c9d8c7acc1 0.4.1 2021-07-19 17:44:17 +05:00
74b8e7ef2f stdio: fix the fileref_prompt implementation 2021-07-19 17:44:05 +05:00
528e7ae729 fakeFS: use passed loggers 2021-07-19 17:42:44 +05:00
89f5caae5a 0.4.0 2021-07-19 00:44:41 +05:00
1c35fe3fec 0.3.2 2021-07-19 00:44:30 +05:00
15f54b48fa Update README.md 2021-07-19 00:44:13 +05:00
b51dc35e75 stdio: Add basic grid windows support 2021-07-19 00:21:48 +05:00
38e8e34ba8 stdio: Update the stdio implementation 2021-07-18 23:33:28 +05:00
10891cf269 cheapGlkOte: passing the full input data to onUpdateInputs 2021-07-18 23:31:57 +05:00
df05e82e8b 0.3.1 2021-07-12 19:17:48 +05:00
f45a865c0a Update README.md 2021-07-12 19:17:24 +05:00
706cf53627 0.3.0 2021-07-12 19:10:26 +05:00
5b822ec2d7 package-lock.json 2021-07-12 19:10:04 +05:00
734e573705 0.2.6 2021-07-12 19:07:06 +05:00
14163877bc package.json: add the "files" attribute 2021-07-12 19:06:53 +05:00
d283f27e81 Add the possibility to specify the screen size 2021-07-12 19:04:23 +05:00
af71e2f706 Add default screen size for glkote-term 2021-07-12 18:51:22 +05:00
edc43b6382 Update version: 0.2.5 2021-03-24 17:39:20 +05:00
57361565a2 Add file_remove_ref placeholder 2021-03-24 17:39:20 +05:00
8ca4b4279b Update version: 0.2.4 2021-03-24 17:39:20 +05:00
5f4a64422e Add special processing of saved files 2021-03-24 17:39:20 +05:00
2ff042e58f
Update README.md 2021-02-25 12:33:44 +05:00
11 changed files with 2487 additions and 267 deletions

35
.eslintrc.json Normal file
View file

@ -0,0 +1,35 @@
{
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": "eslint:recommended",
"overrides": [
],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"ignorePatterns": [
"src/glkOte/*.js"
],
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
]
}
}

View file

@ -1,4 +1,5 @@
# cheap-glkote
[![NPM Version](https://img.shields.io/npm/v/cheap-glkote.svg?style=flat-square)](https://www.npmjs.org/package/cheap-glkote)
This is an abstract implementation of the [GlkOte](https://github.com/erkyrath/glkote) library interface designed to be used with [Emglken](https://github.com/curiousdannii/emglken).<br>
Can be used in Node.js or in a web browser.
@ -10,22 +11,24 @@ This repository includes examples of [stdio interface implementation](https://gi
### Initialization
```js
const { glkInterface, sendFn } = CheapGlkOte(handlers [, loggers])
const { Dialog, GlkOte, send } =
CheapGlkOte(handlers [, { loggers, size }])
```
### Input
```js
sendFn('open door', windowObject)
send('open door', inputType, targetWindow)
```
You can received `windowObject` in `onUpdateWindows` handler.<br>
You should respect input type setted by `onUpdateInputs`.
You can obtain `inputType` and `id` of `targetWindow` inside the `onUpdateInputs` handler.<br>
You can specify `targetWindow` by its `id` inside the `onUpdateWindows` handler.<br>
As I know, `inputType` can be `line` or `char`.
### Output and lifecycle
```js
const handlers = {
onInit: () => {
/**
* It's time to prepare the user interface.
* It's time to initialize the user interface.
*/
},
onUpdateWindows: windows => {
@ -33,10 +36,11 @@ const handlers = {
* Game wants to change the number of windows.
*/
},
onUpdateInputs: type => {
onUpdateInputs: data => {
/**
* Game wants to change input type.
* Supported types: 'char', 'line'.
* 'data' is a list with info about
* the target window and the input type.
*/
},
onUpdateContent: messages => {
@ -83,7 +87,16 @@ Default loggers:
const defaultLoggers = {
log: console.log,
warning: console.warn,
error: console.error
error: console.error,
}
```
### Size
Default sizes:
```js
const defaultSize = {
width: 80,
height: 25,
}
```

View file

@ -4,11 +4,12 @@
* @see: https://github.com/curiousdannii/emglken/blob/master/bin/emglken.js
*/
const fs = require('fs')
const minimist = require('minimist')
import { readFileSync } from 'fs'
import minimist from 'minimist'
const CheapGlkOte = require('../src/')
const { handlers } = require('./stdio')
import CheapGlkOte from '../src/index.js'
import { handlers } from './stdio.js'
const formats = [
{
@ -50,13 +51,19 @@ if (!format) {
process.exit(0)
}
const { glkInterface, sendFn } = CheapGlkOte(handlers)
handlers.setSend(sendFn)
import(`emglken/src/${format.id}.js`)
.then(({default: engine}) => engine)
.then((engine) => new engine())
.then((vm) => {
const cheapGlkOte = CheapGlkOte(handlers)
const engine = require('emglken/src/' + format.engine)
const vm = new engine()
handlers.setSend(cheapGlkOte.send)
vm.prepare(
fs.readFileSync(storyfile),
glkInterface)
vm.start()
vm.init(readFileSync(storyfile), {
Dialog: cheapGlkOte.Dialog,
GlkOte: cheapGlkOte.GlkOte,
Glk: {},
wasmBinary: readFileSync(new URL(`../node_modules/emglken/build/${format.id}-core.wasm`, import.meta.url))
})
vm.start()
})

View file

@ -2,20 +2,22 @@
* @see: https://github.com/curiousdannii/glkote-term/blob/master/src/glkote-dumb.js
*/
const readline = require('readline')
const MuteStream = require('mute-stream')
const ansiEscapes = require('ansi-escapes')
import { createInterface, emitKeypressEvents } from 'readline'
import MuteStream from 'mute-stream'
import ansiEsc from 'ansi-escapes'
const stdin = process.stdin
const stdout = new MuteStream()
stdout.pipe(process.stdout)
const rl = readline.createInterface({
const rl = createInterface({
input: stdin,
output: stdout,
prompt: ''
})
let currentWindowId = null
let currentWindow = null
let currentInputType = null
let send = _ => _
@ -27,28 +29,38 @@ const onInit = () => {
if (stdin.isTTY) {
stdin.setRawMode(true)
}
readline.emitKeypressEvents(stdin)
emitKeypressEvents(stdin)
rl.resume()
clearScreen()
}
const onUpdateWindows = windows => {
currentWindow = windows
.filter(x => x.type === 'buffer')
.slice(-1)[0]
currentWindow = currentWindowId
? windows
.find(x => x.id === currentWindowId)
: windows
.filter(x => x.type === 'buffer')
.slice(-1)[0]
}
const onUpdateInputs = type => {
type
? attach_handlers(type)
: detach_handlers()
const onUpdateInputs = data => {
if (data.length === 0) return null
const { id, type } = data[0]
currentWindowId = id
if (['char', 'line'].includes(type)) {
detach_handlers()
attach_handlers(type)
}
}
const onUpdateContent = allMessages => {
const messages = allMessages.filter(
content => content.id === currentWindow.id
)[0]
const clearScreen = () => {
stdout.write('\u001B[2J\u001B[0;0f')
}
return messages.text.forEach(({ append, content }) => {
const drawBuffer = messages => {
messages.text.forEach(({ append, content }) => {
if (!append) {
stdout.write('\n')
}
@ -59,7 +71,7 @@ const onUpdateContent = allMessages => {
if (x.style === 'input') {
if (stdout.isTTY) {
stdout.write(ansiEscapes.eraseLines(2))
stdout.write(ansiEsc.eraseLines(2))
stdout.write('> ')
}
}
@ -73,10 +85,32 @@ const onUpdateContent = allMessages => {
})
}
const onDisable = disable =>
disable
? detach_handlers()
: attach_handlers()
const drawGrid = messages => {
clearScreen()
messages.lines
.map(x => x.content)
.map(([x]) => x)
.map(({text}) => text)
.map(x => x.trim())
.forEach(x => stdout.write(`${x}\n`))
}
const onUpdateContent = allMessages => {
detach_handlers()
const messages = allMessages.find(
content => content.id === currentWindow.id
)
;({
'buffer': drawBuffer,
'grid': drawGrid
})[currentWindow.type](messages)
}
const onDisable = disable => {
if (disable) detach_handlers()
}
const onExit = () => {
detach_handlers()
@ -85,6 +119,8 @@ const onExit = () => {
}
const onFileNameRequest = (tosave, usage, gameid, callback) => {
stdout.write('\n')
stdout.write(gameid, tosave)
stdout.write('\n')
rl.question(
'Please enter a file name: ',
@ -93,10 +129,10 @@ const onFileNameRequest = (tosave, usage, gameid, callback) => {
: null))
}
const onFileRead = (dirent, israw) =>
const onFileRead = (dirent) =>
void console.log('onFileRead:', dirent)
const onFileWrite = (dirent, content, israw) =>
const onFileWrite = (dirent, content) =>
void console.log('onFileWrite:', dirent, content.length)
const handle_char_input = (str, key) => {
@ -105,8 +141,6 @@ const handle_char_input = (str, key) => {
'\t': 'tab',
}
detach_handlers()
// Make sure this char isn't being remembered for the next line input
rl._line_buffer = null
rl.line = ''
@ -117,10 +151,12 @@ const handle_char_input = (str, key) => {
str ||
key.name.replace(/f(\d+)/, 'func$1')
send(res, currentWindow)
send(res, currentInputType, currentWindow)
detach_handlers()
}
const attach_handlers = type => {
currentInputType = type
if (type === 'char') {
stdout.mute()
stdin.on('keypress', handle_char_input)
@ -134,18 +170,18 @@ const detach_handlers = () => {
stdin.removeListener('keypress', handle_char_input)
rl.removeListener('line', handle_line_input)
stdout.unmute()
currentInputType = null
}
const handle_line_input = line => {
if (stdout.isTTY) {
stdout.write(ansiEscapes.eraseLines(1))
stdout.write(ansiEsc.eraseLines(1))
}
send(line, currentInputType, currentWindow)
detach_handlers()
send(line, currentWindow)
}
module.exports.handlers = {
export const handlers = {
onInit,
onUpdateWindows,
onUpdateInputs,

2003
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
{
"name": "cheap-glkote",
"version": "0.2.3",
"version": "0.5.1",
"description": "Abstract JavaScript implementation of GlkOte",
"author": "He4eT <He4eTHb1u@gmail.com>",
"license": "MIT",
@ -10,17 +10,26 @@
"interactive fiction",
"interactive-fiction"
],
"type": "module",
"engines": {
"node": ">=14.0.0"
},
"main": "src/index.js",
"files": [
"/src"
],
"repository": "https://github.com/He4eT/cheap-glkote",
"bugs": "https://github.com/He4eT/cheap-glkote/issues",
"dependencies": {},
"devDependencies": {
"ansi-escapes": "^4.0.0",
"emglken": "^0.3.3",
"emglken": "^0.5.2",
"eslint": "^8.41.0",
"minimist": "^1.2.5",
"mute-stream": "0.0.8"
},
"scripts": {
"lint": "eslint bin/ src/",
"lint:fix": "eslint --fix bin/ src/",
"play": "node ./bin/player.stdio.js",
"test": "./tests/runtests.sh"
}

View file

@ -2,41 +2,34 @@
* @see: https://github.com/curiousdannii/glkote-term/blob/master/src/glkote-dumb.js
*/
const GlkOte = require('./glkOte/glkote-term')
import GlkOte from './glkOte/glkote-term.js'
class CheapGlkOte extends GlkOte {
constructor(handlers, loggers) {
super()
this.current_input_type = null
constructor(handlers, loggers, size) {
super(size)
this.handlers = handlers
this.loggers = loggers
}
sendFn(message, window) {
sendFn (message, type, window) {
this.send_response(
this.current_input_type,
type,
window,
message)
this.current_input_type = null
}
init(iface) {
init (iface) {
this.handlers.onInit()
super.init(iface)
}
update_inputs(data) {
if (!data.length) return null
const { type } = data[0]
if (['char', 'line'].includes(type)) {
this.current_input_type = type
this.handlers.onUpdateInputs(type)
}
update_inputs (data) {
if (!data.length) return []
this.handlers.onUpdateInputs(data)
}
accept_specialinput(data) {
accept_specialinput (data) {
if (data.type === 'fileref_prompt') {
const callback = ref =>
this.send_response(
@ -53,44 +46,38 @@ class CheapGlkOte extends GlkOte {
}
}
update_content(messages) {
update_content (messages) {
this.handlers.onUpdateContent(messages)
}
exit() {
exit () {
this.handlers.onExit()
super.exit()
}
cancel_inputs(data) {
if (data.length === 0) {
this.current_input_type = null
this.handlers.onUpdateInputs(null)
}
cancel_inputs (data) {
this.handlers.onUpdateInputs(data)
}
disable(disable) {
this.disabled = disable
this.handlers.onDisable(disable)
disable (data) {
this.handlers.onDisable(data)
}
update_windows(windows) {
if (windows.length) {
this.handlers.onUpdateWindows(windows)
}
update_windows (windows) {
this.handlers.onUpdateWindows(windows)
}
log(message) {
loggers.log(message)
log (message) {
this.loggers.log(message)
}
warning(message) {
loggers.warn(message)
warning (message) {
this.loggers.warn(message)
}
error(message) {
loggers.error(message)
error (message) {
this.loggers.error(message)
}
}
module.exports = CheapGlkOte
export default CheapGlkOte

View file

@ -7,11 +7,20 @@ class FakeDialog {
constructor(handlers, loggers) {
this.streaming = false
this.handlers = handlers
this.loggers = loggers
}
file_ref_exists = ref => false
file_ref_exists({ usage }) {
return usage === 'save'
? true
: false
}
file_construct_ref(filename, usage, gameid) {
file_remove_ref () {
return true
}
file_construct_ref(filename, usage) {
return {
filename,
usage: usage || ''
@ -32,16 +41,16 @@ class FakeDialog {
}
log(message) {
loggers.log(message)
this.loggers.log(message)
}
warning(message) {
loggers.warn(message)
this.loggers.warn(message)
}
error(message) {
loggers.error(message)
this.loggers.error(message)
}
}
module.exports = FakeDialog
export default FakeDialog

View file

@ -1,10 +1,12 @@
'use strict';
/* GlkAPI -- a Javascript Glk API for IF interfaces
* GlkOte Library: version 2.2.3.
* GlkOte Library: version 2.3.2.
* Glk API which this implements: version 0.7.4.
* Designed by Andrew Plotkin <erkyrath@eblong.com>
* <http://eblong.com/zarf/glk/glkote.html>
*
* This Javascript library is copyright 2010-16 by Andrew Plotkin.
* This Javascript library is copyright 2010-20 by Andrew Plotkin.
* It is distributed under the MIT license; see the "LICENSE" file.
*
* This file is a Glk API compatibility layer for glkote.js. It offers a
@ -38,21 +40,16 @@
and will also double the write-count in a stream.
*/
/* Put everything inside the Glk namespace. */
/* All state is contained in GlkClass. */
var GlkClass = function() {
Glk = function() {
var GlkOte = null; /* imported API object */
var VM = null; /* imported API object (the VM interface) */
var GiDispa = null; /* imported API object (the dispatch layer) */
var Blorb = null; /* imported API object (the resource layer) */
/* The VM interface object. */
var VM = null;
/* References to external libraries */
var Dialog;
var GiDispa;
var GiLoad;
var GlkOte;
/* Environment capabilities */
var support = {};
/* Environment capabilities. (Checked at init time.) */
var has_canvas;
/* Options from the vm_options object. */
var option_exit_warning;
@ -70,61 +67,7 @@ var event_generation = 0;
var current_partial_inputs = null;
var current_partial_outputs = null;
// Set external variable references
function set_references( vm_options )
{
if ( vm_options.Dialog )
{
Dialog = vm_options.Dialog;
}
if ( !Dialog )
{
if ( typeof window !== 'undefined' && window.Dialog )
{
Dialog = window.Dialog;
}
else
{
throw new Error( 'No reference to Dialog' );
}
}
if ( vm_options.GiDispa )
{
GiDispa = vm_options.GiDispa;
}
else if ( !GiDispa && typeof window !== 'undefined' && window.GiDispa )
{
GiDispa = window.GiDispa;
}
if ( vm_options.GiLoad )
{
GiLoad = vm_options.GiLoad;
}
else if ( !GiLoad && typeof window !== 'undefined' && window.GiLoad )
{
GiLoad = window.GiLoad;
}
if ( vm_options.GlkOte )
{
GlkOte = vm_options.GlkOte;
}
if ( !GlkOte )
{
if ( typeof window !== 'undefined' && window.GlkOte )
{
GlkOte = window.GlkOte;
}
else
{
throw new Error('No reference to GlkOte');
}
}
}
/* Initialize the library, initialize the VM, and set it running. (It will
/* Initialize the library, initialize the VM, and set it running. (It will
run until the first glk_select() or glk_exit() call.)
The vm_options argument must have a vm_options.vm field, which must be an
@ -139,17 +82,27 @@ function set_references( vm_options )
library sets that up for you.)
*/
function init(vm_options) {
/* Set references to external libraries */
set_references( vm_options );
/* Either GlkOte was passed in or we must create one. */
if (vm_options.GlkOte) {
GlkOte = vm_options.GlkOte;
}
else if (window.GlkOteClass) {
GlkOte = new window.GlkOteClass();
}
/* Either Blorb was passed in or we don't have one. */
if (vm_options.Blorb) {
Blorb = vm_options.Blorb;
}
/* Check for canvas support. We don't rely on jquery here. */
has_canvas = (document.createElement('canvas').getContext != undefined);
VM = vm_options.vm;
if (GiDispa)
GiDispa.set_vm(VM);
GiDispa = vm_options.GiDispa; /* may be null/undefined */
vm_options.accept = accept_ui_event;
GlkOte.init(vm_options);
option_exit_warning = vm_options.exit_warning;
option_do_vm_autosave = vm_options.do_vm_autosave;
option_before_select_hook = vm_options.before_select_hook;
@ -159,6 +112,16 @@ function init(vm_options) {
if (option_before_select_hook) {
option_before_select_hook();
}
/* Initialize the lower levels. */
if (GiDispa)
GiDispa.init({ io:vm_options.io, vm:vm_options.vm });
GlkOte.init(vm_options);
}
function is_inited() {
return (VM != null && GlkOte != null);
}
function accept_ui_event(obj) {
@ -184,12 +147,10 @@ function accept_ui_event(obj) {
switch (obj.type) {
case 'init':
content_metrics = obj.metrics;
/* Process the support array */
if (obj.support) {
obj.support.forEach(function(item) {support[item] = 1;});
}
VM.init();
content_metrics = complete_metrics(obj.metrics);
/* We ignore the support array. This library is updated in sync
with GlkOte, so we know what it supports. */
VM.start();
break;
case 'external':
@ -231,7 +192,7 @@ function accept_ui_event(obj) {
break;
case 'arrange':
content_metrics = obj.metrics;
content_metrics = complete_metrics(obj.metrics);
box = {
left: content_metrics.outspacingx,
top: content_metrics.outspacingy,
@ -255,6 +216,134 @@ function accept_ui_event(obj) {
}
}
/* Given a partial metrics object, return one with all the required
values. Missing values will default to 0 or the standard inherited
terms. (E.g., if "inspacingx" is missing it will default to
"inspacing", then "spacing", then 0. See measure_window() in
glkote.js or data_metrics_parse() in RemGlk.)
All values in the given object will be copied over; defaulting only
applies to missing values from the required set.
*/
function complete_metrics(metrics) {
// Default values if absolutely nothing is specified.
var res = {
width: 80,
height: 50,
gridcharwidth: 1,
gridcharheight: 1,
buffercharwidth: 1,
buffercharheight: 1,
gridmarginx: 0,
gridmarginy: 0,
buffermarginx: 0,
buffermarginy: 0,
graphicsmarginx: 0,
graphicsmarginy: 0,
outspacingx: 0,
outspacingy: 0,
inspacingx: 0,
inspacingy: 0,
};
// Various ways of specifying defaults.
var val;
val = metrics.charwidth;
if (val !== undefined) {
res.gridcharwidth = val;
res.buffercharwidth = val;
}
val = metrics.charheight;
if (val !== undefined) {
res.gridcharheight = val;
res.buffercharheight = val;
}
val = metrics.margin;
if (val !== undefined) {
res.gridmarginx = val;
res.gridmarginy = val;
res.buffermarginx = val;
res.buffermarginy = val;
res.graphicsmarginx = val;
res.graphicsmarginy = val;
}
val = metrics.gridmargin;
if (val !== undefined) {
res.gridmarginx = val;
res.gridmarginy = val;
}
val = metrics.buffermargin;
if (val !== undefined) {
res.buffermarginx = val;
res.buffermarginy = val;
}
val = metrics.graphicsmargin;
if (val !== undefined) {
res.graphicsmarginx = val;
res.graphicsmarginy = val;
}
val = metrics.marginx;
if (val !== undefined) {
res.gridmarginx = val;
res.buffermarginx = val;
res.graphicsmarginx = val;
}
val = metrics.marginy;
if (val !== undefined) {
res.gridmarginy = val;
res.buffermarginy = val;
res.graphicsmarginy = val;
}
val = metrics.spacing;
if (val !== undefined) {
res.inspacingx = val;
res.inspacingy = val;
res.outspacingx = val;
res.outspacingy = val;
}
val = metrics.inspacing;
if (val !== undefined) {
res.inspacingx = val;
res.inspacingy = val;
}
val = metrics.outspacing;
if (val !== undefined) {
res.outspacingx = val;
res.outspacingy = val;
}
val = metrics.spacingx;
if (val !== undefined) {
res.inspacingx = val;
res.outspacingx = val;
}
val = metrics.spacingy;
if (val !== undefined) {
res.inspacingy = val;
res.outspacingy = val;
}
// Copy over all the supplied fields. These override the defaults above.
res = Object.assign(res, metrics);
return res;
}
function handle_arrange_input() {
if (!gli_selectref)
return;
@ -453,8 +542,8 @@ function handle_line_input(disprock, input, termkey) {
VM.resume();
}
function update(type) {
var dataobj = { type: type || 'update', gen: event_generation };
function update() {
var dataobj = { type: 'update', gen: event_generation };
var winarray = null;
var contentarray = null;
var inputarray = null;
@ -705,10 +794,27 @@ function update(type) {
}
}
/* Return the library interface object that we were passed or created.
Call this if you want to use, e.g., the same Dialog object that GlkOte
is using.
*/
function get_library(val) {
switch (val) {
case 'VM': return VM;
case 'GlkOte': return GlkOte;
case 'GiDispa': return GiDispa;
case 'Blorb': return Blorb;
case 'Dialog': return GlkOte.getlibrary('Dialog');
}
/* Unrecognized library name. */
return null;
}
/* Wrap up the current display state as a (JSONable) object. This is
called from Quixe.vm_autosave.
*/
function save_allstate() {
var Dialog = GlkOte.getlibrary('Dialog');
var res = {};
if (gli_rootwin)
@ -886,6 +992,8 @@ function save_allstate() {
*/
function restore_allstate(res)
{
var Dialog = GlkOte.getlibrary('Dialog');
if (gli_windowlist || gli_streamlist || gli_filereflist)
throw('restore_allstate: glkapi module has already been launched');
@ -1058,7 +1166,7 @@ function restore_allstate(res)
case strtype_Resource:
str.resfilenum = obj.resfilenum;
var el = GiLoad.find_data_chunk(str.resfilenum);
var el = Blorb.get_data_chunk(str.resfilenum);
if (el) {
str.buf = el.data;
}
@ -1143,6 +1251,11 @@ function restore_allstate(res)
function fatal_error(msg) {
has_exited = true;
ui_disabled = true;
if (!GlkOte) {
// We haven't been initialized yet, so we can only try to log the error and hope someone sees it.
console.log('Fatal error:', msg);
return;
}
GlkOte.error(msg);
var dataobj = { type: 'update', gen: event_generation, disable: true };
dataobj.input = [];
@ -2693,8 +2806,10 @@ function UniArrayToBE32(arr) {
up in Safari, in Opera, and in Firefox if you have Firebug installed.)
*/
function qlog(msg) {
if (typeof console !== 'undefined' && console.log)
if (window.console && console.log)
console.log(msg);
else if (window.opera && opera.postError)
opera.postError(msg);
}
/* RefBox: Simple class used for "call-by-reference" Glk arguments. The object
@ -2896,13 +3011,13 @@ function gli_window_put_string(win, val) {
gli_window_grid_canonicalize(), but I've inlined it. */
if (win.cursorx < 0)
win.cursorx = 0;
else if (win.cursorx >= win.gridwidth) {
if (win.cursorx >= win.gridwidth) {
win.cursorx = 0;
win.cursory++;
}
if (win.cursory < 0)
win.cursory = 0;
else if (win.cursory >= win.gridheight)
if (win.cursory >= win.gridheight)
break; /* outside the window */
if (ch == "\n") {
@ -2912,7 +3027,7 @@ function gli_window_put_string(win, val) {
continue;
}
lineobj = win.lines[win.cursory];
var lineobj = win.lines[win.cursory];
lineobj.dirty = true;
lineobj.chars[win.cursorx] = ch;
lineobj.styles[win.cursorx] = win.style;
@ -2935,13 +3050,13 @@ function gli_window_put_string(win, val) {
function gli_window_grid_canonicalize(win) {
if (win.cursorx < 0)
win.cursorx = 0;
else if (win.cursorx >= win.gridwidth) {
if (win.cursorx >= win.gridwidth) {
win.cursorx = 0;
win.cursory++;
}
if (win.cursory < 0)
win.cursory = 0;
else if (win.cursory >= win.gridheight)
if (win.cursory >= win.gridheight)
return true; /* outside the window */
return false;
}
@ -3094,7 +3209,7 @@ function gli_window_close(win, recurse) {
function gli_window_rearrange(win, box) {
var width, height, oldwidth, oldheight;
var min, max, diff, splitwid, ix, cx, lineobj;
var min, max, diff, split, splitwid, ix, cx, lineobj;
var box1, box2, ch1, ch2;
geometry_changed = true;
@ -3380,6 +3495,8 @@ function gli_stream_dirty_file(str) {
buffer out.
*/
function gli_stream_flush_file(str) {
var Dialog = GlkOte.getlibrary('Dialog');
if (str.streaming)
GlkOte.log('### gli_stream_flush_file called for streaming file!');
if (!(str.timer_id === null)) {
@ -3390,6 +3507,8 @@ function gli_stream_flush_file(str) {
}
function gli_new_fileref(filename, usage, rock, ref) {
var Dialog = GlkOte.getlibrary('Dialog');
var fref = {};
fref.filename = filename;
fref.rock = rock;
@ -3510,7 +3629,7 @@ function gli_put_char(str, ch) {
var len = arr.length;
if (len > str.buflen-str.bufpos)
len = str.buflen-str.bufpos;
for (ix=0; ix<len; ix++)
for (var ix=0; ix<len; ix++)
str.buf[str.bufpos+ix] = arr[ix];
str.bufpos += len;
if (str.bufpos > str.bufeof)
@ -3813,6 +3932,7 @@ function gli_get_line(str, buf, want_unicode) {
return 0;
var len = buf.length;
var lx, ch;
var gotnewline;
switch (str.type) {
@ -4076,7 +4196,6 @@ function glk_exit() {
gli_selectref = null;
if (option_exit_warning)
GlkOte.warning(option_exit_warning);
update('exit');
return DidNotReturn;
}
@ -4133,20 +4252,22 @@ function glk_gestalt_ext(sel, val, arr) {
return 2; // gestalt_CharOutput_ExactPrint
case 4: // gestalt_MouseInput
if (val == Const.wintype_TextGrid)
if (val == Const.wintype_TextBuffer)
return 1;
if (support.graphics && val == Const.wintype_Graphics)
if (val == Const.wintype_Graphics && has_canvas)
return 1;
return 0;
case 5: // gestalt_Timer
return support.timer || 0;
return 1;
case 6: // gestalt_Graphics
return support.graphics || 0;
return 1;
case 7: // gestalt_DrawImage
if (support.graphics && (val == Const.wintype_TextBuffer || val == Const.wintype_Graphics))
if (val == Const.wintype_TextBuffer)
return 1;
if (val == Const.wintype_Graphics && has_canvas)
return 1;
return 0;
@ -4160,10 +4281,10 @@ function glk_gestalt_ext(sel, val, arr) {
return 0;
case 11: // gestalt_Hyperlinks
return support.hyperlinks || 0;
return 1;
case 12: // gestalt_HyperlinkInput
if (support.hyperlinks && (val == Const.wintype_TextBuffer || val == Const.wintype_TextGrid))
if (val == 3 || val == 4) // TextBuffer or TextGrid
return 1;
else
return 0;
@ -4172,7 +4293,7 @@ function glk_gestalt_ext(sel, val, arr) {
return 0;
case 14: // gestalt_GraphicsTransparency
return support.graphics || 0;
return 1;
case 15: // gestalt_Unicode
return 1;
@ -4311,7 +4432,7 @@ function glk_window_open(splitwin, method, size, wintype, rock) {
newwin.cursory = 0;
break;
case Const.wintype_Graphics:
if (!support.graphics) {
if (!has_canvas) {
/* Graphics windows not supported; silently return null */
gli_delete_window(newwin);
return null;
@ -4691,6 +4812,8 @@ function glk_stream_get_rock(str) {
}
function glk_stream_open_file(fref, fmode, rock) {
var Dialog = GlkOte.getlibrary('Dialog');
if (!fref)
throw('glk_stream_open_file: invalid fileref');
@ -4793,21 +4916,20 @@ function glk_stream_open_memory(buf, fmode, rock) {
function glk_stream_open_resource(filenum, rock) {
var str;
if (!GiLoad || !GiLoad.find_data_chunk)
if (!Blorb)
return null;
var el = GiLoad.find_data_chunk(filenum);
var el = Blorb.get_data_chunk(filenum);
if (!el)
return null;
var buf = el.data;
var isbinary = (el.type == 'BINA');
str = gli_new_stream(strtype_Resource,
true,
false,
rock);
str.unicode = false;
str.isbinary = isbinary;
str.isbinary = el.binary;
str.resfilenum = filenum;
@ -4832,21 +4954,20 @@ function glk_stream_open_resource(filenum, rock) {
function glk_stream_open_resource_uni(filenum, rock) {
var str;
if (!GiLoad || !GiLoad.find_data_chunk)
if (!Blorb)
return null;
var el = GiLoad.find_data_chunk(filenum);
var el = Blorb.get_data_chunk(filenum);
if (!el)
return null;
var buf = el.data;
var isbinary = (el.type == 'BINA');
str = gli_new_stream(strtype_Resource,
true,
false,
rock);
str.unicode = true;
str.isbinary = isbinary;
str.isbinary = el.binary;
str.resfilenum = filenum;
@ -4869,6 +4990,8 @@ function glk_stream_open_resource_uni(filenum, rock) {
}
function glk_stream_close(str, result) {
var Dialog = GlkOte.getlibrary('Dialog');
if (!str)
throw('glk_stream_close: invalid stream');
@ -4949,18 +5072,21 @@ function glk_stream_get_current() {
}
function glk_fileref_create_temp(usage, rock) {
var Dialog = GlkOte.getlibrary('Dialog');
var filetype = (usage & Const.fileusage_TypeMask);
var filetypename = FileTypeMap[filetype];
var ref = Dialog.file_construct_temp_ref(filetypename);
fref = gli_new_fileref(ref.filename, usage, rock, ref);
var fref = gli_new_fileref(ref.filename, usage, rock, ref);
return fref;
}
function glk_fileref_create_by_name(usage, filename, rock) {
var Dialog = GlkOte.getlibrary('Dialog');
/* Filenames that do not come from the user must be cleaned up. */
filename = Dialog.file_clean_fixed_name(filename, (usage & Const.fileusage_TypeMask));
fref = gli_new_fileref(filename, usage, rock, null);
var fref = gli_new_fileref(filename, usage, rock, null);
return fref;
}
@ -5010,19 +5136,19 @@ function glk_fileref_create_by_prompt(usage, fmode, rock) {
function gli_fileref_create_by_prompt_callback(obj) {
var ref = obj.value;
/* This "value" field will be a dialog.js fileref object if we are
connected to GlkOte. However, if we are connected to RegTest,
it will be a plain string. We attempt to handle both cases. */
var usage = ui_specialcallback.usage;
var rock = ui_specialcallback.rock;
var fref = null;
if (ref) {
if (ref && typeof(ref) == 'object') {
fref = gli_new_fileref(ref.filename, usage, rock, ref);
}
// If reading a file which doesn't exist, return null
if ( ui_specialinput.filemode === 'read' && !Dialog.file_ref_exists( fref.ref ) )
{
glk_fileref_destroy( fref );
fref = null;
else if (ref && typeof(ref) == 'string') {
fref = gli_new_fileref(ref, usage, rock, null);
}
ui_specialinput = null;
@ -5063,12 +5189,14 @@ function glk_fileref_get_rock(fref) {
}
function glk_fileref_delete_file(fref) {
var Dialog = GlkOte.getlibrary('Dialog');
if (!fref)
throw('glk_fileref_delete_file: invalid fileref');
Dialog.file_remove_ref(fref.ref);
}
function glk_fileref_does_file_exist(fref) {
var Dialog = GlkOte.getlibrary('Dialog');
if (!fref)
throw('glk_fileref_does_file_exist: invalid fileref');
if (Dialog.file_ref_exists(fref.ref))
@ -5369,10 +5497,10 @@ function glk_request_timer_events(msec) {
/* Graphics functions. */
function glk_image_get_info(imgid, widthref, heightref) {
if (!GiLoad || !GiLoad.get_image_info)
if (!Blorb || !Blorb.get_image_info)
return null;
var info = GiLoad.get_image_info(imgid);
var info = Blorb.get_image_info(imgid);
if (info) {
if (widthref)
widthref.set_value(info.width);
@ -5391,9 +5519,9 @@ function glk_image_draw(win, imgid, val1, val2) {
if (!win)
throw('glk_image_draw: invalid window');
if (!GiLoad || !GiLoad.get_image_info)
if (!Blorb || !Blorb.get_image_info)
return 0;
var info = GiLoad.get_image_info(imgid);
var info = Blorb.get_image_info(imgid);
if (!info)
return 0;
@ -5441,9 +5569,9 @@ function glk_image_draw_scaled(win, imgid, val1, val2, width, height) {
if (!win)
throw('glk_image_draw_scaled: invalid window');
if (!GiLoad || !GiLoad.get_image_info)
if (!Blorb || !Blorb.get_image_info)
return 0;
var info = GiLoad.get_image_info(imgid);
var info = Blorb.get_image_info(imgid);
if (!info)
return 0;
@ -5954,6 +6082,8 @@ function glk_get_line_stream_uni(str, buf) {
}
function glk_stream_open_file_uni(fref, fmode, rock) {
var Dialog = GlkOte.getlibrary('Dialog');
if (!fref)
throw('glk_stream_open_file_uni: invalid fileref');
@ -6241,11 +6371,13 @@ function glk_date_to_simple_time_local(dateref, factor) {
/* End of Glk namespace function. Return the object which will
become the Glk global. */
var api = {
version: '2.2.3', /* GlkOte/GlkApi version */
set_references: set_references,
return {
classname: 'Glk',
version: '2.3.2', /* GlkOte/GlkApi version */
init : init,
inited : is_inited,
update : update,
getlibrary : get_library,
save_allstate : save_allstate,
restore_allstate : restore_allstate,
fatal_error : fatal_error,
@ -6385,12 +6517,12 @@ var api = {
glk_stream_open_resource_uni : glk_stream_open_resource_uni
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = api;
}
};
return api;
/* Glk is an instance of GlkClass, ready to init. */
var Glk = new GlkClass();
}();
// Node-compatible behavior
try { exports.Glk = Glk; exports.GlkClass = GlkClass; } catch (ex) {};
/* End of Glk library. */
/* End of Glk library. */

View file

@ -3,16 +3,21 @@
*/
class GlkOte {
constructor() {
constructor({width, height}) {
this.width = width
this.height = height
this.current_metrics = null
this.disabled = false
this.generation = 0
this.interface = null
this.version = require('../../package.json').version
this.version = '0.5.1'
}
measure_window() {
return {
width: this.width,
height: this.height,
buffercharheight: 1,
buffercharwidth: 1,
buffermarginx: 0,
@ -23,12 +28,10 @@ class GlkOte {
gridcharwidth: 1,
gridmarginx: 0,
gridmarginy: 0,
height: 0,
inspacingx: 0,
inspacingy: 0,
outspacingx: 0,
outspacingy: 0,
width: 0,
outspacingy: 0
}
}
@ -146,4 +149,4 @@ class GlkOte {
}
}
module.exports = GlkOte
export default GlkOte

View file

@ -1,9 +1,9 @@
const FakeDialog = require('./fakeDialog')
const CheapGlkOte = require('./cheapGlkOte')
import FakeDialog from './fakeDialog.js'
import CheapGlkOte from './cheapGlkOte.js'
const noop = () => void null
const noopHandlers = [
const defaultHandlers = [
'onInit',
'onUpdateWindows',
'onUpdateInputs',
@ -12,30 +12,36 @@ const noopHandlers = [
'onFileNameRequest',
'onFileRead',
'onFileWrite',
'onExit'
'onExit',
].reduce((acc, x) => ((acc[x] = noop), acc), {})
const defaultLoggers = {
log: console.log,
warning: console.warn,
error: console.error
error: console.error,
}
module.exports = (handlers_, loggers = defaultLoggers) => {
const defaultSize = {
width: 80,
height: 25,
}
export default (handlers_, {loggers: loggers_, size: size_ } = {}) => {
const handlers =
Object.assign({}, noopHandlers, handlers_)
Object.assign({}, defaultHandlers, handlers_)
const loggers =
Object.assign({}, defaultLoggers, loggers_)
const size =
Object.assign({}, defaultSize, size_)
const Dialog = new FakeDialog(handlers, loggers)
const GlkOte = new CheapGlkOte(handlers, loggers)
const GlkOte = new CheapGlkOte(handlers, loggers, size)
const sendFn = GlkOte.sendFn.bind(GlkOte)
const send = GlkOte.sendFn.bind(GlkOte)
return {
sendFn,
glkInterface: {
Dialog,
GlkOte,
Glk: {}
}
Dialog,
GlkOte,
send,
}
}