summaryrefslogtreecommitdiffstats
path: root/apps/plugins/frotz/frotz.c
blob: 96029b85cbac2df08fe3d27da0b7bd73a2a0bbe5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2009 Torne Wuff
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"
#include "dumb_frotz.h"
#include "lib/pluginlib_exit.h"
#include "lib/pluginlib_actions.h"

PLUGIN_HEADER

extern int frotz_main(void);
extern bool hot_key_quit(void);

f_setup_t f_setup;
extern char save_name[], auxilary_name[], script_name[], command_name[];
extern int story_fp, sfp, rfp, pfp;

static struct viewport vp[NB_SCREENS];

static void atexit_cleanup(void);

enum plugin_status plugin_start(const void* parameter)
{
    int i;
    char* ext;

    PLUGINLIB_EXIT_INIT_ATEXIT(atexit_cleanup);

    if (!parameter)
        return PLUGIN_ERROR;

    rb->lcd_setfont(FONT_SYSFIXED);
#if LCD_DEPTH > 1
    rb->lcd_set_backdrop(NULL);
#endif
    rb->lcd_clear_display();

    FOR_NB_SCREENS(i)
        rb->viewport_set_defaults(&vp[i], i);

    story_name = parameter;
    strcpy(save_name, story_name);
    ext = rb->strrchr(save_name, '.');
    if (ext)
        *ext = '\0';
    strcpy(auxilary_name, save_name);
    strcpy(script_name, save_name);
    strcpy(command_name, save_name);
    rb->strlcat(save_name, ".sav", MAX_PATH);
    rb->strlcat(auxilary_name, ".aux", MAX_PATH);
    rb->strlcat(script_name, ".scr", MAX_PATH);
    rb->strlcat(command_name, ".rec", MAX_PATH);

    frotz_main();

    return PLUGIN_OK;
}

void atexit_cleanup()
{
    if (story_fp != -1)
        fclose(story_fp);
    if (sfp != -1)
        fclose(sfp);
    if (rfp != -1)
        fclose(rfp);
    if (pfp != -1)
        fclose(pfp);
}

MENUITEM_STRINGLIST(ingame_menu, "Frotz", NULL, "Resume", "Undo", "Restart",
                    "Toggle input recording", "Play back input",
                    "Debug options", "Exit");

zchar menu(void)
{
    switch (rb->do_menu(&ingame_menu, NULL, vp, true))
    {
    case 0:
    default:
        dumb_dump_screen();
        return ZC_BAD;
    case 1:
        return ZC_HKEY_UNDO;
    case 2:
        return ZC_HKEY_RESTART;
    case 3:
        return ZC_HKEY_RECORD;
    case 4:
        return ZC_HKEY_PLAYBACK;
    case 5:
        return ZC_HKEY_DEBUG;
    case 6:
        return ZC_HKEY_QUIT;
    }
}

const struct button_mapping* plugin_contexts[]={generic_actions};

void wait_for_key()
{
    int action;

    dumb_show_screen(false);

    for (;;)
    {
        action = pluginlib_getaction(TIMEOUT_BLOCK,
                                     plugin_contexts, 1);
        switch (action)
        {
        case PLA_QUIT:
            hot_key_quit();
            break;
        
        case PLA_FIRE:
            return;
        }
    }
}

zchar do_input(int timeout, bool show_cursor)
{
    int action;
    long timeout_at;
    zchar menu_ret;

    dumb_show_screen(show_cursor);

    /* Convert timeout (tenths of a second) to ticks */
    if (timeout > 0)
        timeout = (timeout * HZ) / 10;
    else
        timeout = TIMEOUT_BLOCK;

    timeout_at = *rb->current_tick + timeout;

    for (;;)
    {
        action = pluginlib_getaction(timeout,
                                     plugin_contexts, 1);
        switch (action)
        {
        case PLA_QUIT:
            return ZC_HKEY_QUIT;
        
        case PLA_MENU:
            menu_ret = menu();
            if (menu_ret != ZC_BAD)
                return menu_ret;
            timeout_at = *rb->current_tick + timeout;
            break;

        case PLA_FIRE:
            return ZC_RETURN;

        case PLA_START:
            return ZC_BAD;

        default:
            if (timeout != TIMEOUT_BLOCK && 
                    !TIME_BEFORE(*rb->current_tick, timeout_at))
                return ZC_TIME_OUT;
        }
    }
}

zchar os_read_key(int timeout, bool show_cursor)
{
    int r;
    char inputbuf[5];
    short key;
    zchar zkey;

    for(;;)
    {
        zkey = do_input(timeout, show_cursor);
        if (zkey != ZC_BAD)
            return zkey;

        inputbuf[0] = '\0';
        r = rb->kbd_input(inputbuf, 5);
        rb->lcd_setfont(FONT_SYSFIXED);
        dumb_dump_screen();
        if (!r)
        {
            rb->utf8decode(inputbuf, &key);
            if (key > 0 && key < 256)
                return (zchar)key;
        }
    }
}

zchar os_read_line(int max, zchar *buf, int timeout, int width, int continued)
{
    (void)continued;
    int r;
    char inputbuf[256];
    const char *in;
    char *out;
    short key;
    zchar zkey;

    for(;;)
    {
        zkey = do_input(timeout, true);
        if (zkey != ZC_BAD)
            return zkey;

        if (max > width)
            max = width;
        strcpy(inputbuf, buf);
        r = rb->kbd_input(inputbuf, 256);
        rb->lcd_setfont(FONT_SYSFIXED);
        dumb_dump_screen();
        if (!r)
        {
            in = inputbuf;
            out = buf;
            while (*in && max)
            {
                in = rb->utf8decode(in, &key);
                if (key > 0 && key < 256)
                {
                    *out++ = key;
                    max--;
                }
            }
            *out = '\0';
            os_display_string(buf);
            return ZC_RETURN;
        }
    }
}

bool read_yes_or_no(const char *s)
{
    char message_line[50];
    const char *message_lines[] = {message_line};
    const struct text_message message = {message_lines, 1};

    rb->strlcpy(message_line, s, 49);
    rb->strcat(message_line, "?");

    if (rb->gui_syncyesno_run(&message, NULL, NULL) == YESNO_YES)
        return TRUE;
    else
        return FALSE;
}

zchar os_read_mouse(void)
{
    return 0;
}

int os_read_file_name(char *file_name, const char *default_name, int flag)
{
    (void)flag;
    strcpy(file_name, default_name);
    return TRUE;
}

void os_beep(int volume)
{
    rb->splashf(HZ/2, "[%s-PITCHED BEEP]", (volume==1) ? "HIGH" : "LOW");
}

static unsigned char unget_buf;
static int unget_file;

int ungetc(int c, int f)
{
    unget_file = f;
    unget_buf = c;
    return c;
}

int fgetc(int f)
{
    unsigned char cb;
    if (unget_file == f)
    {
        unget_file = -1;
        return unget_buf;
    }
    if (rb->read(f, &cb, 1) != 1)
        return EOF;
    return cb;
}

int fputc(int c, int f)
{
    unsigned char cb = c;
    if (rb->write(f, &cb, 1) != 1)
        return EOF;
    return cb;
}