aboutsummaryrefslogtreecommitdiff
path: root/devel.but
diff options
context:
space:
mode:
authorSimon Tatham <anakin@pobox.com>2017-10-01 14:45:12 +0100
committerSimon Tatham <anakin@pobox.com>2017-10-01 16:35:40 +0100
commita58c1b216bb1d4547f7b2ef2703fe2d0cd3b5cac (patch)
treeb9bcdc589ffe1f72e2510237b6e1524f5b96085c /devel.but
parent3276376d1be74b66970b88c3e941dcedf8d22474 (diff)
downloadpuzzles-a58c1b216bb1d4547f7b2ef2703fe2d0cd3b5cac.zip
puzzles-a58c1b216bb1d4547f7b2ef2703fe2d0cd3b5cac.tar.gz
puzzles-a58c1b216bb1d4547f7b2ef2703fe2d0cd3b5cac.tar.bz2
puzzles-a58c1b216bb1d4547f7b2ef2703fe2d0cd3b5cac.tar.xz
Make the code base clean under -Wwrite-strings.
I've also added that warning option and -Werror to the build script, so that I'll find out if I break this property in future.
Diffstat (limited to 'devel.but')
-rw-r--r--devel.but3
1 files changed, 1 insertions, 2 deletions
diff --git a/devel.but b/devel.but
index af69202..131678c 100644
--- a/devel.but
+++ b/devel.but
@@ -3296,8 +3296,7 @@ visually activate and deactivate a redo button.
\H{midend-serialise} \cw{midend_serialise()}
\c void midend_serialise(midend *me,
-\c void (*write)(void *ctx, void *buf, int len),
-\c void *wctx);
+\c void (*write)(void *ctx, const void *buf, int len), void *wctx);
Calling this function causes the mid-end to convert its entire
internal state into a long ASCII text string, and to pass that
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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
/*
 * nestedvm.c: NestedVM front end for my puzzle collection.
 */

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <time.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>

#include <sys/time.h>

#include "puzzles.h"

extern void _pause();
extern int _call_java(int cmd, int arg1, int arg2, int arg3);

void fatal(const char *fmt, ...)
{
    va_list ap;
    fprintf(stderr, "fatal error: ");
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    fprintf(stderr, "\n");
    exit(1);
}

struct frontend {
    // TODO kill unneeded members!
    midend *me;
    bool timer_active;
    struct timeval last_time;
    config_item *cfg;
    int cfg_which;
    bool cfgret;
    int ox, oy, w, h;
};

static frontend *_fe;

void get_random_seed(void **randseed, int *randseedsize)
{
    struct timeval *tvp = snew(struct timeval);
    gettimeofday(tvp, NULL);
    *randseed = (void *)tvp;
    *randseedsize = sizeof(struct timeval);
}

void frontend_default_colour(frontend *fe, float *output)
{
    output[0] = output[1]= output[2] = 0.8f;
}

void nestedvm_status_bar(void *handle, const char *text)
{
    _call_java(4,0,(int)text,0);
}

void nestedvm_start_draw(void *handle)
{
    frontend *fe = (frontend *)handle;
    _call_java(5, 0, fe->w, fe->h);
    _call_java(4, 1, fe->ox, fe->oy);
}

void nestedvm_clip(void *handle, int x, int y, int w, int h)
{
    frontend *fe = (frontend *)handle;
    _call_java(5, w, h, 0);
    _call_java(4, 3, x + fe->ox, y + fe->oy);
}

void nestedvm_unclip(void *handle)
{
    frontend *fe = (frontend *)handle;
    _call_java(4, 4, fe->ox, fe->oy);
}

void nestedvm_draw_text(void *handle, int x, int y, int fonttype, int fontsize,
                        int align, int colour, const char *text)
{
    frontend *fe = (frontend *)handle;
    _call_java(5, x + fe->ox, y + fe->oy, 
	       (fonttype == FONT_FIXED ? 0x10 : 0x0) | align);
    _call_java(7, fontsize, colour, (int)text);
}

void nestedvm_draw_rect(void *handle, int x, int y, int w, int h, int colour)
{
    frontend *fe = (frontend *)handle;
    _call_java(5, w, h, colour);
    _call_java(4, 5, x + fe->ox, y + fe->oy);
}

void nestedvm_draw_line(void *handle, int x1, int y1, int x2, int y2, 
			int colour)
{
    frontend *fe = (frontend *)handle;
    _call_java(5, x2 + fe->ox, y2 + fe->oy, colour);
    _call_java(4, 6, x1 + fe->ox, y1 + fe->oy);
}

void nestedvm_draw_poly(void *handle, int *coords, int npoints,
			int fillcolour, int outlinecolour)
{
    frontend *fe = (frontend *)handle;
    int i;
    _call_java(4, 7, npoints, 0);
    for (i = 0; i < npoints; i++) {
	_call_java(6, i, coords[i*2] + fe->ox, coords[i*2+1] + fe->oy);
    }
    _call_java(4, 8, outlinecolour, fillcolour);
}

void nestedvm_draw_circle(void *handle, int cx, int cy, int radius,
		     int fillcolour, int outlinecolour)
{
    frontend *fe = (frontend *)handle;
    _call_java(5, cx+fe->ox, cy+fe->oy, radius);
    _call_java(4, 9, outlinecolour, fillcolour);
}

struct blitter {
    int handle, w, h, x, y;
};

blitter *nestedvm_blitter_new(void *handle, int w, int h)
{
    blitter *bl = snew(blitter);
    bl->handle = -1;
    bl->w = w;
    bl->h = h;
    return bl;
}

void nestedvm_blitter_free(void *handle, blitter *bl)
{
    if (bl->handle != -1)
	_call_java(4, 11, bl->handle, 0);
    sfree(bl);
}

void nestedvm_blitter_save(void *handle, blitter *bl, int x, int y)
{
    frontend *fe = (frontend *)handle;    
    if (bl->handle == -1)
	bl->handle = _call_java(4,10,bl->w, bl->h);
    bl->x = x;
    bl->y = y;
    _call_java(8, bl->handle, x + fe->ox, y + fe->oy);
}

void nestedvm_blitter_load(void *handle, blitter *bl, int x, int y)
{
    frontend *fe = (frontend *)handle;
    assert(bl->handle != -1);
    if (x == BLITTER_FROMSAVED && y == BLITTER_FROMSAVED) {
        x = bl->x;
        y = bl->y;
    }
    _call_java(9, bl->handle, x + fe->ox, y + fe->oy);
}

void nestedvm_end_draw(void *handle)
{
    _call_java(4,2,0,0);
}

char *nestedvm_text_fallback(void *handle, const char *const *strings,
			     int nstrings)
{
    /*
     * We assume Java can cope with any UTF-8 likely to be emitted
     * by a puzzle.
     */
    return dupstr(strings[0]);
}

const struct drawing_api nestedvm_drawing = {
    nestedvm_draw_text,
    nestedvm_draw_rect,
    nestedvm_draw_line,
    nestedvm_draw_poly,
    nestedvm_draw_circle,
    NULL, // draw_update,
    nestedvm_clip,
    nestedvm_unclip,
    nestedvm_start_draw,
    nestedvm_end_draw,
    nestedvm_status_bar,
    nestedvm_blitter_new,
    nestedvm_blitter_free,
    nestedvm_blitter_save,
    nestedvm_blitter_load,
    NULL, NULL, NULL, NULL, NULL, NULL, /* {begin,end}_{doc,page,puzzle} */
    NULL, NULL,			       /* line_width, line_dotted */
    nestedvm_text_fallback,
};

int jcallback_key_event(int x, int y, int keyval)
{
    frontend *fe = (frontend *)_fe;
    if (fe->ox == -1)
        return 1;
    if (keyval >= 0 &&
        !midend_process_key(fe->me, x - fe->ox, y - fe->oy, keyval))
	return 42;
    return 1;
}

int jcallback_resize(int width, int height)
{
    frontend *fe = (frontend *)_fe;
    int x, y;
    x = width;
    y = height;
    midend_size(fe->me, &x, &y, true);
    fe->ox = (width - x) / 2;
    fe->oy = (height - y) / 2;
    fe->w = x;
    fe->h = y;
    midend_force_redraw(fe->me);
    return 0;
}

int jcallback_timer_func()
{
    frontend *fe = (frontend *)_fe;
    if (fe->timer_active) {
	struct timeval now;
	float elapsed;
	gettimeofday(&now, NULL);
	elapsed = ((now.tv_usec - fe->last_time.tv_usec) * 0.000001F +
		   (now.tv_sec - fe->last_time.tv_sec));
        midend_timer(fe->me, elapsed);	/* may clear timer_active */
	fe->last_time = now;
    }
    return fe->timer_active;
}

void deactivate_timer(frontend *fe)
{
    if (fe->timer_active)
	_call_java(4, 13, 0, 0);
    fe->timer_active = false;
}

void activate_timer(frontend *fe)
{
    if (!fe->timer_active) {
	_call_java(4, 12, 0, 0);
	gettimeofday(&fe->last_time, NULL);
    }
    fe->timer_active = true;
}

void jcallback_config_ok()
{
    frontend *fe = (frontend *)_fe;
    const char *err;

    err = midend_set_config(fe->me, fe->cfg_which, fe->cfg);

    if (err)
	_call_java(2, (int) "Error", (int)err, 1);
    else {
	fe->cfgret = true;
    }
}

void jcallback_config_set_string(int item_ptr, int char_ptr) {
    config_item *i = (config_item *)item_ptr;
    char* newval = (char*) char_ptr;
    assert(i->type == C_STRING);
    sfree(i->u.string.sval);
    i->u.string.sval = dupstr(newval);
    free(newval);
}

void jcallback_config_set_boolean(int item_ptr, int selected) {
    config_item *i = (config_item *)item_ptr;
    assert(i->type == C_BOOLEAN);
    i->u.boolean.bval = selected != 0 ? true : false;
}

void jcallback_config_set_choice(int item_ptr, int selected) {
    config_item *i = (config_item *)item_ptr;
    assert(i->type == C_CHOICES);
    i->u.choices.selected = selected;
}

static bool get_config(frontend *fe, int which)
{
    char *title;
    config_item *i;
    fe->cfg = midend_get_config(fe->me, which, &title);
    fe->cfg_which = which;
    fe->cfgret = false;
    _call_java(10, (int)title, 0, 0);
    for (i = fe->cfg; i->type != C_END; i++) {
	_call_java(5, (int)i, i->type, (int)i->name);