Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements discussed on intfiction.org #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cgdate.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void glk_current_time(glktimeval_t *time)
return;
}

gli_timestamp_to_time(tv.tv_sec, tv.tv_usec, time);
gli_timestamp_to_time(tv.tv_sec+pref_clock_skew, tv.tv_usec, time);
}

glsi32 glk_current_simple_time(glui32 factor)
Expand All @@ -123,7 +123,7 @@ glsi32 glk_current_simple_time(glui32 factor)
return 0;
}

return gli_simplify_time(tv.tv_sec, factor);
return gli_simplify_time(tv.tv_sec+pref_clock_skew, factor);
}

void glk_time_to_date_utc(glktimeval_t *time, glkdate_t *date)
Expand Down
31 changes: 31 additions & 0 deletions glkterm.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ struct glk_fileref_struct {
char *filename;
int filetype;
int textmode;
int readonly;

gidispatch_rock_t disprock;
fileref_t *next, *prev; /* in the big linked list of filerefs */
Expand Down Expand Up @@ -145,6 +146,7 @@ extern window_t *gli_rootwin;
extern window_t *gli_focuswin;
extern grect_t content_box;
extern void (*gli_interrupt_handler)(void);
extern int gli_exited;

/* The following typedefs are copied from cheapglk.h. They support the
tables declared in cgunigen.c. */
Expand Down Expand Up @@ -193,6 +195,35 @@ extern int pref_window_borders;
extern int pref_precise_timing;
extern int pref_historylen;
extern int pref_prompt_defaults;
/* Extended pref */
extern int pref_style_override;
extern int pref_border_graphics;
extern unsigned long pref_border_style;
extern int pref_use_colors;
extern int pref_clear_message;
extern int pref_auto_focus;
extern int pref_more_message;
extern int pref_typable_controls;
extern int pref_typable_specials;
#ifdef OPT_USE_MKSTEMP
extern char*pref_temporary_filename;
#endif
extern int pref_readonly;
extern int pref_auto_suffix;
extern int pref_prompt_raw_filename;
extern signed long pref_clock_skew;
extern int pref_restrict_files;
extern int pref_pause_warning;
extern int pref_more_exit;

/* Filename mapping */
typedef struct {
char*glkname;
char*native;
char writable;
} Filename_Mapping;
extern Filename_Mapping*filename_mapping;
extern int num_filename_mapping;

/* Declarations of library internal functions. */

Expand Down
19 changes: 18 additions & 1 deletion gtevent.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <curses.h>
#include "glk.h"
#include "glkterm.h"
#include "gtw_buf.h"

/* A pointer to the place where the pending glk_select() will store its
event. When not inside a glk_select() call, this will be NULL. */
Expand Down Expand Up @@ -46,21 +47,37 @@ void gli_initialize_events()

void glk_select(event_t *event)
{
window_textbuffer_t *dwin;
int ismore = FALSE;
int needrefresh = TRUE;

curevent = event;
gli_event_clearevent(curevent);

if(pref_clear_message) gli_msgline(gli_exited?"-- Exit --":"");
gli_windows_update();
gli_windows_set_paging(FALSE);
gli_input_guess_focus();
if(pref_auto_focus || !gli_focuswin) gli_input_guess_focus();

while (curevent->type == evtype_None) {
int key;

/* It would be nice to display a "hit any key to continue" message in
all windows which require it. */
if (needrefresh) {
if(pref_more_message && gli_focuswin && gli_focuswin->type==wintype_TextBuffer) {
dwin=gli_focuswin->data;
if(dwin->lastseenline < dwin->numlines - dwin->height) {
gli_msgline("-- More --");
ismore=TRUE;
} else if(ismore) {
gli_msgline(gli_exited?"-- Exit --":"");
ismore=FALSE;
}
} else if(pref_more_message && ismore) {
gli_msgline(gli_exited?"-- Exit --":"");
ismore=FALSE;
}
gli_windows_place_cursor();
refresh();
needrefresh = FALSE;
Expand Down
68 changes: 64 additions & 4 deletions gtfref.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
http://www.eblong.com/zarf/glk/index.html
*/

#ifdef OPT_USE_MKSTEMP
#define _BSD_SOURCE
#endif

#include "gtoption.h"
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -43,6 +47,7 @@ fileref_t *gli_new_fileref(char *filename, glui32 usage, glui32 rock)

fref->textmode = ((usage & fileusage_TextMode) != 0);
fref->filetype = (usage & fileusage_TypeMask);
fref->readonly = pref_readonly;

fref->prev = NULL;
fref->next = gli_filereflist;
Expand Down Expand Up @@ -97,6 +102,8 @@ void glk_fileref_destroy(fileref_t *fref)

static char *gli_suffix_for_usage(glui32 usage)
{
if(!pref_auto_suffix) return "";

switch (usage & fileusage_TypeMask) {
case fileusage_Data:
return ".glkdata";
Expand All @@ -112,17 +119,38 @@ static char *gli_suffix_for_usage(glui32 usage)

frefid_t glk_fileref_create_temp(glui32 usage, glui32 rock)
{
char filename[] = "/tmp/glktempfref-XXXXXX";
char *filename;
fileref_t *fref;

if(pref_readonly) return NULL;

/* This is a pretty good way to do this on Unix systems. It doesn't
make sense on Windows, but anybody compiling this library on
Windows has already set up some kind of Unix-like environment,
I hope. */

mkstemp(filename);

#ifdef OPT_USE_MKSTEMP
if(pref_temporary_filename) {
int i=mkstemp(pref_temporary_filename);
if(i==-1) {
gli_strict_warning("fileref_create_temp: mkstemp() failed.");
return NULL;
}
close(i);
filename = pref_temporary_filename;
} else
#else
filename = tmpnam(NULL);
#endif

fref = gli_new_fileref(filename, usage, rock);
#ifdef OPT_USE_MKSTEMP
if(pref_temporary_filename) {
/* Reset template to end with "XXXXXX" */
int i=strlen(pref_temporary_filename)-6;
memcpy(pref_temporary_filename+i,"XXXXXX",6);
}
#endif
if (!fref) {
gli_strict_warning("fileref_create_temp: unable to create fileref.");
return NULL;
Expand Down Expand Up @@ -159,6 +187,26 @@ frefid_t glk_fileref_create_by_name(glui32 usage, char *name,
int len;
char *cx;
char *suffix;
int wr=0;

/* Check for user filename mappings. */
if(num_filename_mapping) {
int i;
for(i=0;i<num_filename_mapping;i++) {
if(!strcmp(name,filename_mapping[i].glkname)) {
strncpy(buf2,filename_mapping[i].native,2*BUFLEN);
buf2[2*BUFLEN]=0;
wr=filename_mapping[i].writable;
goto filename_done;
}
}
}

if(pref_restrict_files) {
snprintf(buf2,BUFLEN+5,"Unmapped filename: %s",name);
gli_strict_warning(buf2);
return NULL;
}

/* The new spec recommendations: delete all characters in the
string "/\<>:|?*" (including quotes). Truncate at the first
Expand Down Expand Up @@ -192,11 +240,13 @@ frefid_t glk_fileref_create_by_name(glui32 usage, char *name,
suffix = gli_suffix_for_usage(usage);
sprintf(buf2, "%s/%s%s", workingdir, buf, suffix);

filename_done:
fref = gli_new_fileref(buf2, usage, rock);
if (!fref) {
gli_strict_warning("fileref_create_by_name: unable to create fileref.");
return NULL;
}
if(wr) fref->readonly = FALSE;

return fref;
}
Expand Down Expand Up @@ -271,11 +321,14 @@ frefid_t glk_fileref_create_by_prompt(glui32 usage, glui32 fmode,
return NULL;
}

if (cx[0] == '/')
if (cx[0] == '/' || pref_prompt_raw_filename)
strcpy(newbuf, cx);
else
sprintf(newbuf, "%s/%s", workingdir, cx);

/* Acknowledge raw filename preference. */
if(pref_prompt_raw_filename) goto raw_filename;

/* If there is no dot-suffix, add a standard one. */
val = strlen(newbuf);
gotdot = FALSE;
Expand All @@ -291,6 +344,7 @@ frefid_t glk_fileref_create_by_prompt(glui32 usage, glui32 fmode,
strcat(newbuf, suffix);
}

raw_filename:
if (fmode != filemode_Read) {
if (!stat(newbuf, &sbuf) && S_ISREG(sbuf.st_mode)) {
sprintf(prbuf, "Overwrite \"%s\"? [y/n] ", cx);
Expand All @@ -313,6 +367,7 @@ frefid_t glk_fileref_create_by_prompt(glui32 usage, glui32 fmode,
gli_strict_warning("fileref_create_by_prompt: unable to create fileref.");
return NULL;
}
if(fmode != filemode_Read) fref->readonly = FALSE;

return fref;
}
Expand Down Expand Up @@ -374,6 +429,11 @@ void glk_fileref_delete_file(fileref_t *fref)
gli_strict_warning("fileref_delete_file: invalid ref");
return;
}

if (fref->readonly) {
gli_strict_warning("fileref_delete_file: can't delete read-only file");
return;
}

/* If you don't have the unlink() function, obviously, change it
to whatever file-deletion function you do have. */
Expand Down
16 changes: 14 additions & 2 deletions gtgestal.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ glui32 glk_gestalt(glui32 id, glui32 val)

glui32 glk_gestalt_ext(glui32 id, glui32 val, glui32 *arr, glui32 arrlen)
{
static int impl;
int ix;

impl=TRUE;
switch (id) {

case gestalt_Version:
Expand Down Expand Up @@ -49,7 +51,7 @@ glui32 glk_gestalt_ext(glui32 id, glui32 val, glui32 *arr, glui32 arrlen)
|| val == keycode_Up || val == keycode_Down
|| val == keycode_Return || val == keycode_Delete
|| val == keycode_Escape)
return TRUE;
return (val == keycode_Return || pref_typable_specials);
else
return FALSE;
}
Expand Down Expand Up @@ -118,10 +120,11 @@ glui32 glk_gestalt_ext(glui32 id, glui32 val, glui32 *arr, glui32 arrlen)
return TRUE;

case gestalt_LineTerminators:
return TRUE;
return pref_typable_specials;
case gestalt_LineTerminatorKey:
/* GlkTerm never uses the escape or function keys for anything,
so we'll allow them to be line terminators. */
if (!pref_typable_specials) return FALSE;
if (val == keycode_Escape)
return TRUE;
if (val >= keycode_Func12 && val <= keycode_Func1)
Expand All @@ -134,7 +137,16 @@ glui32 glk_gestalt_ext(glui32 id, glui32 val, glui32 *arr, glui32 arrlen)
case gestalt_ResourceStream:
return TRUE;

case 0x1400: /* gestalt_Gestalt */
glk_gestalt_ext(val,0,NULL,0);
return impl;

case 0x1407: /* gestalt_CharInputExt */
if(val=='\031' || val=='\026') return char_typable_table[val]?0x22:0;
else return char_typable_table[val]?0x33:0;

default:
impl=FALSE;
return 0;

}
Expand Down
12 changes: 7 additions & 5 deletions gtinput.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ static command_t *commands_window(window_t *win, int key)
case wintype_TextGrid: {
window_textgrid_t *dwin = win->data;
cmd = commands_textgrid(key);
if (!cmd) {
if (!cmd && !gli_exited) {
if (win->line_request)
cmd = commands_textgrid_line(dwin, key);
else if (win->char_request)
Expand All @@ -348,7 +348,7 @@ static command_t *commands_window(window_t *win, int key)
if (dwin->lastseenline < dwin->numlines - dwin->height) {
cmd = commands_textbuffer_paging(key);
}
if (!cmd) {
if (!cmd && !gli_exited) {
if (win->line_request)
cmd = commands_textbuffer_line(dwin, key);
else if (win->char_request)
Expand Down Expand Up @@ -571,7 +571,7 @@ void gli_input_handle_key(int key)

/* If not, see if there's some other window which has a binding for
the key; if so, set the focus there. */
if (!cmd && gli_rootwin) {
if (!cmd && gli_rootwin && pref_auto_focus) {
window_t *altwin = gli_focuswin;
command_t *altcmd = NULL;
do {
Expand Down Expand Up @@ -599,8 +599,10 @@ void gli_input_handle_key(int key)
arg = cmd->arg;
}
(*cmd->func)(win, arg);
}
else {
} else if(gli_exited) {
/* Exit if a key is pushed */
gli_event_store(evtype_CharInput, 0, 0, 1);
} else {
char buf[256];
char *kbuf = key_to_name(key);
sprintf(buf, "The key <%s> is not currently defined.", kbuf);
Expand Down
10 changes: 9 additions & 1 deletion gtmessag.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ void gli_msgline_warning(char *msg)
if (!pref_messageline)
return;

sprintf(buf, "Glk library error: %s", msg);
snprintf(buf, 256, "Glk library error: %s", msg);
gli_msgline(buf);

if(pref_pause_warning) {
move(content_box.bottom, 0);
refresh();
while(getch()==ERR && !just_killed);
gli_msgline("");
}
if(just_killed) gli_fast_exit();
}

void gli_msgline(char *msg)
Expand Down
Loading