Changeset 353719a
- Timestamp:
- Feb 19, 2013, 8:29:04 PM (12 years ago)
- Branches:
- master, release-1.10
- Children:
- 5001a3d
- Parents:
- 06e04a9
- git-author:
- David Benjamin <davidben@mit.edu> (06/09/12 16:07:20)
- git-committer:
- David Benjamin <davidben@mit.edu> (02/19/13 20:29:04)
- Files:
-
- 2 deleted
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
Makefile.am
r06e04a9 r353719a 39 39 40 40 CODELIST_SRCS=message.c mainwin.c popwin.c zephyr.c messagelist.c \ 41 commands.c global.c text.c fmtext.c gmarshal_funcs.ceditwin.c \41 commands.c global.c text.c fmtext.c editwin.c \ 42 42 util.c logging.c \ 43 43 perlconfig.c keys.c functions.c zwrite.c viewwin.c help.c filter.c \ … … 52 52 BASE_SRCS = $(CODELIST_SRCS) $(NORMAL_SRCS) 53 53 54 GEN_C = varstubs.c perlglue.c 55 GEN_H = owl_prototypes.h 54 GEN_C = varstubs.c perlglue.c gmarshal_funcs.c 55 GEN_H = owl_prototypes.h gmarshal_funcs.h 56 56 57 57 BUILT_SOURCES = $(GEN_C) $(GEN_H) … … 75 75 $(AM_V_GEN)perl $< $(sort $(filter-out $<,$+)) > $@ 76 76 77 gmarshal_funcs.h: marshal_types 78 glib-genmarshal --header $< > $@ 79 gmarshal_funcs.c: marshal_types 80 glib-genmarshal --body $< > $@ 81 77 82 # For emacs flymake-mode 78 83 check-syntax: proto -
commands.c
r8258ea5 r353719a 1605 1605 if (!silent) owl_function_error("Unknown variable '%s'", var); 1606 1606 } else if (requirebool && owl_variable_get_type(v) != OWL_VARIABLE_BOOL) { 1607 // FIXME: The above won't work when we make perl variables OWL_VARIABLE_SV 1607 1608 if (!silent) owl_function_error("Variable '%s' is not a boolean", var); 1608 1609 } else { … … 1634 1635 if (!silent) owl_function_error("Unknown variable '%s'", var); 1635 1636 } else if (owl_variable_get_type(v) != OWL_VARIABLE_BOOL) { 1637 // FIXME: The above won't work when we make perl variables OWL_VARIABLE_SV 1636 1638 if (!silent) owl_function_error("Variable '%s' is not a boolean", var); 1637 1639 } else { -
marshal_types
r06e04a9 r353719a 1 # set_fn2 INT:STRING3 INT:INT4 INT:BOOL5 # get_fn6 INT:VOID7 BOOL:VOID8 STRING:VOID9 # validate_fn has same signature as set_fn10 1 # set_fromstring_fn 11 2 INT:STRING 12 3 # get_tostring_fn 13 STRING:BOOL 14 STRING:INT 15 STRING:STRING 16 4 STRING:VOID -
owl.h
r06e04a9 r353719a 235 235 char *name; 236 236 int type; /* OWL_VARIABLE_* */ 237 GValue gval_default;237 char *default_str; /* the default value as a string */ 238 238 const char *validsettings; /* documentation of valid settings */ 239 239 char *summary; /* summary of usage */ 240 240 char *description; /* detailed description */ 241 241 GValue val; /* current value */ 242 GClosure *validate_fn;243 /* returns 1 if newval is valid */244 GClosure *set_fn;245 /* sets the variable to a value246 * of the appropriate type.247 * unless documented, this248 * should make a copy.249 * returns 0 on success. */250 242 GClosure *set_fromstring_fn; 251 243 /* sets the variable to a value … … 254 246 * should make a copy. 255 247 * returns 0 on success. */ 256 GClosure *get_fn; 248 GClosure *get_tostring_fn; 249 /* converts val to a string; 250 * caller must free the result */ 251 252 /* These are only valid for OWL_VARIABLE_{INT,BOOL,STRING} */ 253 GCallback get_fn; 257 254 /* returns a reference to the current value. 258 255 * WARNING: this approach is hard to make 259 256 * thread-safe... */ 260 GC losure *get_tostring_fn;261 /* converts val to a string;262 * caller must free the result */ 263 GClosure *delete_fn;264 /* frees val as needed */265 GClosure *get_default_fn; 266 /* return the default value, as set at creation time */ 267 257 GCallback validate_fn; 258 /* returns 1 if newval is valid */ 259 GCallback set_fn; 260 /* sets the variable to a value 261 * of the appropriate type. 262 * unless documented, this 263 * should make a copy. 264 * returns 0 on success. */ 268 265 } owl_variable; 269 266 270 267 typedef struct _owl_variable_init_params { 271 c har *name;268 const char *name; 272 269 int type; /* OWL_VARIABLE_* */ 273 void *pval_default; /* for types other andstring */274 int ival_default; /* for types int and bool */270 const char *pval_default; /* for string */ 271 int ival_default; /* for types int and bool */ 275 272 const char *validsettings; /* documentation of valid settings */ 276 c har *summary; /* summary of usage */277 c har *description;/* detailed description */273 const char *summary; /* summary of usage */ 274 const char *description; /* detailed description */ 278 275 void *val; /* current value */ 279 276 GCallback validate_fn; … … 298 295 /* converts val to a string; 299 296 * caller must free the result */ 300 GCallback delete_fn;301 /* frees val as needed */302 GCallback get_default_fn;303 /* return the default value as set at creation time */304 297 } owl_variable_init_params; 305 298 -
perlconfig.c
r06e04a9 r353719a 559 559 SvREFCNT_dec(v); 560 560 } 561 562 void owl_perl_delete_perl_variable(owl_variable *variable, void *data)563 {564 g_closure_unref(variable->delete_fn);565 variable->delete_fn = NULL;566 SvREFCNT_dec((SV*)data);567 owl_variable_delete(variable);568 } -
perlglue.xs
r06e04a9 r353719a 405 405 MODULE = BarnOwl PACKAGE = BarnOwl::Internal 406 406 407 /* TODO: Revise this after churn is done. 407 408 void 408 409 new_variable_full(name, summary, desc, type, data, default_val, get_fn, get_default_fn, tostring_fn, validate_fn, set_fn, fromstring_fn) … … 474 475 PUTBACK; 475 476 } 477 */ 476 478 477 479 void -
tester.c
r6a20996 r353719a 337 337 int numfailed=0; 338 338 char *value; 339 const void *v;340 339 341 340 printf("# BEGIN testing owl_variable\n"); … … 379 378 owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval"); 380 379 FAIL_UNLESS("get new string var", NULL != (var = owl_variable_get_var(&vd, "stringvar"))); 381 FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(var)));382 380 FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(var))); 383 381 owl_variable_set_string(var, "new val"); … … 386 384 owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47); 387 385 FAIL_UNLESS("get new int var", NULL != (var = owl_variable_get_var(&vd, "intvar"))); 388 FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(var)));389 386 FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(var)); 390 387 owl_variable_set_int(var, 17); … … 393 390 owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1); 394 391 FAIL_UNLESS("get new bool var", NULL != (var = owl_variable_get_var(&vd, "boolvar"))); 395 FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(var)));396 392 FAIL_UNLESS("get new bool val", owl_variable_get_bool(var)); 397 393 owl_variable_set_bool_off(var); -
variable.c
r06e04a9 r353719a 3 3 #include "gmarshal_funcs.h" 4 4 5 static const GType owl_variable_gtype_map[] = {G_TYPE_POINTER, 6 G_TYPE_INT, G_TYPE_BOOLEAN, 7 G_TYPE_STRING }; 5 /* TODO(davidben): When we can require 2.30 and up, remove this. */ 6 #ifndef G_VALUE_INIT 7 #define G_VALUE_INIT { 0, { { 0 } } } 8 #endif 9 10 typedef const char *(*get_string_t)(const owl_variable *); 11 typedef int (*get_int_t)(const owl_variable *); 12 typedef gboolean (*get_bool_t)(const owl_variable *); 13 14 typedef int (*set_string_t)(owl_variable *, const char *); 15 typedef int (*set_int_t)(owl_variable *, int); 16 typedef int (*set_bool_t)(owl_variable *, gboolean); 17 18 typedef int (*validate_string_t)(owl_variable *, const char *); 19 typedef int (*validate_int_t)(owl_variable *, int); 20 typedef int (*validate_bool_t)(owl_variable *, gboolean); 8 21 9 22 #define OWLVAR_BOOL(name,default,summary,description) \ 10 { g_strdup(name), OWL_VARIABLE_BOOL, NULL, default, "on,off", g_strdup(summary), g_strdup(description), NULL, \11 NULL, NULL, NULL, NULL, NULL , NULL, NULL}23 { name, OWL_VARIABLE_BOOL, NULL, default, "on,off", summary, description, NULL, \ 24 NULL, NULL, NULL, NULL, NULL } 12 25 13 26 #define OWLVAR_BOOL_FULL(name,default,summary,description,validate,set,get) \ 14 { g_strdup(name), OWL_VARIABLE_BOOL, NULL, default, "on,off", g_strdup(summary), g_strdup(description), NULL, \15 G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL , NULL, NULL}27 { name, OWL_VARIABLE_BOOL, NULL, default, "on,off", summary, description, NULL, \ 28 G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL } 16 29 17 30 #define OWLVAR_INT(name,default,summary,description) \ 18 { g_strdup(name), OWL_VARIABLE_INT, NULL, default, "<int>", g_strdup(summary), g_strdup(description), NULL, \19 NULL, NULL, NULL, NULL, NULL , NULL, NULL}31 { name, OWL_VARIABLE_INT, NULL, default, "<int>", summary, description, NULL, \ 32 NULL, NULL, NULL, NULL, NULL } 20 33 21 34 #define OWLVAR_INT_FULL(name,default,summary,description,validset,validate,set,get) \ 22 { g_strdup(name), OWL_VARIABLE_INT, NULL, default, validset, g_strdup(summary), g_strdup(description), NULL, \23 G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL , NULL, NULL}35 { name, OWL_VARIABLE_INT, NULL, default, validset, summary, description, NULL, \ 36 G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL } 24 37 25 38 #define OWLVAR_PATH(name,default,summary,description) \ 26 { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, "<path>", g_strdup(summary), g_strdup(description), NULL, \27 NULL, NULL, NULL, NULL, NULL , NULL, NULL}39 { name, OWL_VARIABLE_STRING, default, 0, "<path>", summary, description, NULL, \ 40 NULL, NULL, NULL, NULL, NULL } 28 41 29 42 #define OWLVAR_STRING(name,default,summary,description) \ 30 { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, "<string>", g_strdup(summary), g_strdup(description), NULL, \31 NULL, NULL, NULL, NULL, NULL , NULL, NULL}43 { name, OWL_VARIABLE_STRING, default, 0, "<string>", summary, description, NULL, \ 44 NULL, NULL, NULL, NULL, NULL } 32 45 33 46 #define OWLVAR_STRING_FULL(name,default,validset,summary,description,validate,set,get) \ 34 { g_strdup(name), OWL_VARIABLE_STRING, g_strdup(default), 0, validset, g_strdup(summary), g_strdup(description), NULL, \35 G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL , NULL, NULL}47 { name, OWL_VARIABLE_STRING, default, 0, validset, summary, description, NULL, \ 48 G_CALLBACK(validate), G_CALLBACK(set), NULL, G_CALLBACK(get), NULL } 36 49 37 50 /* enums are really integers, but where validset is a comma-separated … … 39 52 * correspond to the values that may be specified. */ 40 53 #define OWLVAR_ENUM(name,default,summary,description,validset) \ 41 { g_strdup(name), OWL_VARIABLE_INT, NULL, default, validset, g_strdup(summary), g_strdup(description), NULL, \54 { name, OWL_VARIABLE_INT, NULL, default, validset, summary, description, NULL, \ 42 55 G_CALLBACK(owl_variable_enum_validate), \ 43 56 NULL, G_CALLBACK(owl_variable_enum_set_fromstring), \ 44 57 NULL, G_CALLBACK(owl_variable_enum_get_tostring), \ 45 NULL, NULL}58 } 46 59 47 60 #define OWLVAR_ENUM_FULL(name,default,summary,description,validset,validate, set, get) \ 48 { g_strdup(name), OWL_VARIABLE_INT, NULL, default, validset, g_strdup(summary), g_strdup(description), NULL, \61 { name, OWL_VARIABLE_INT, NULL, default, validset, summary, description, NULL, \ 49 62 G_CALLBACK(validate), \ 50 63 G_CALLBACK(set), G_CALLBACK(owl_variable_enum_set_fromstring), \ 51 64 G_CALLBACK(get), G_CALLBACK(owl_variable_enum_get_tostring), \ 52 NULL, NULL}65 } 53 66 54 67 int owl_variable_add_defaults(owl_vardict *vd) … … 439 452 /* This MUST be last... */ 440 453 { NULL, 0, NULL, 0, NULL, NULL, NULL, NULL, 441 NULL, NULL, NULL, NULL, NULL , NULL}454 NULL, NULL, NULL, NULL, NULL } 442 455 443 456 }; 444 457 445 int ret = owl_variable_dict_add_from_list(vd, variables_to_init); 446 owl_variable_init_params *var; 447 for (var = variables_to_init; var->name != NULL; var++) 448 owl_variable_cleanup_initializer(var); 449 return ret; 458 return owl_variable_dict_add_from_list(vd, variables_to_init); 450 459 } 451 460 … … 457 466 /* commonly useful */ 458 467 459 int owl_variable_int_validate_gt0(const owl_variable *v, const int newval, void *dummy)468 int owl_variable_int_validate_gt0(const owl_variable *v, int newval) 460 469 { 461 470 return !(newval < 1); 462 471 } 463 472 464 int owl_variable_int_validate_positive(const owl_variable *v, const int newval, void *dummy)473 int owl_variable_int_validate_positive(const owl_variable *v, int newval) 465 474 { 466 475 return !(newval < 0); … … 468 477 469 478 /* typewinsize */ 470 int owl_variable_typewinsize_set(owl_variable *v, const int newval, void *dummy)479 int owl_variable_typewinsize_set(owl_variable *v, int newval) 471 480 { 472 481 int rv; 473 rv = owl_variable_int_set_default(v, newval , dummy);482 rv = owl_variable_int_set_default(v, newval); 474 483 if (0 == rv) owl_mainpanel_layout_contents(&g.mainpanel); 475 484 return(rv); … … 477 486 478 487 /* debug (cache value in g->debug) */ 479 int owl_variable_debug_set(owl_variable *v, const int newval, void *dummy)488 int owl_variable_debug_set(owl_variable *v, int newval) 480 489 { 481 490 if (newval == 1 || newval == 0) { 482 491 g.debug = newval; 483 492 } 484 return owl_variable_bool_set_default(v, newval , dummy);493 return owl_variable_bool_set_default(v, newval); 485 494 } 486 495 487 496 /* When 'aaway' is changed, need to notify the AIM server */ 488 int owl_variable_aaway_set(owl_variable *v, const gboolean newval, void *dummy)497 int owl_variable_aaway_set(owl_variable *v, gboolean newval) 489 498 { 490 499 if (newval == 1) { … … 493 502 owl_aim_set_awaymsg(""); 494 503 } 495 return owl_variable_bool_set_default(v, newval , dummy);496 } 497 498 int owl_variable_colorztext_set(owl_variable *v, const void *newval , void *dummy)499 { 500 int ret = owl_variable_bool_set_default(v, newval , dummy);504 return owl_variable_bool_set_default(v, newval); 505 } 506 507 int owl_variable_colorztext_set(owl_variable *v, const void *newval) 508 { 509 int ret = owl_variable_bool_set_default(v, newval); 501 510 /* flush the format cache so that we see the update, but only if we're done initializing BarnOwl */ 502 511 if (owl_global_get_msglist(&g) != NULL) … … 509 518 } 510 519 511 int owl_variable_pseudologins_set(owl_variable *v, const int newval, void *dummy)520 int owl_variable_pseudologins_set(owl_variable *v, int newval) 512 521 { 513 522 static guint timer = 0; … … 523 532 } 524 533 } 525 return owl_variable_bool_set_default(v, newval , dummy);534 return owl_variable_bool_set_default(v, newval); 526 535 } 527 536 528 537 /* note that changing the value of this will clobber 529 538 * any user setting of this */ 530 int owl_variable_disable_ctrl_d_set(owl_variable *v, const int newval, void *dummy)539 int owl_variable_disable_ctrl_d_set(owl_variable *v, int newval) 531 540 { 532 541 if (!owl_context_is_startup(owl_global_get_context(&g))) { … … 539 548 } 540 549 } 541 return owl_variable_int_set_default(v, newval , dummy);542 } 543 544 int owl_variable_tty_set(owl_variable *v, const char *newval , void *dummy)550 return owl_variable_int_set_default(v, newval); 551 } 552 553 int owl_variable_tty_set(owl_variable *v, const char *newval) 545 554 { 546 555 owl_zephyr_set_locationinfo(g_get_host_name(), newval); 547 return (owl_variable_string_set_default(v, newval, dummy));548 } 549 550 int owl_variable_default_exposure_set(owl_variable *v, const char *newval , void *dummy)556 return owl_variable_string_set_default(v, newval); 557 } 558 559 int owl_variable_default_exposure_set(owl_variable *v, const char *newval) 551 560 { 552 561 return owl_zephyr_set_default_exposure(newval); 553 562 } 554 563 555 const char *owl_variable_default_exposure_get(const owl_variable *v , void *dummy)564 const char *owl_variable_default_exposure_get(const owl_variable *v) 556 565 { 557 566 return owl_zephyr_get_default_exposure(); 558 567 } 559 568 560 int owl_variable_exposure_set(owl_variable *v, const char *newval , void *dummy)569 int owl_variable_exposure_set(owl_variable *v, const char *newval) 561 570 { 562 571 int ret = owl_zephyr_set_exposure(newval); 563 572 if (ret != 0) 564 573 return ret; 565 return owl_variable_string_set_default(v, owl_zephyr_normalize_exposure(newval) , dummy);574 return owl_variable_string_set_default(v, owl_zephyr_normalize_exposure(newval)); 566 575 } 567 576 … … 576 585 577 586 CALLER_OWN GClosure *owl_variable_make_closure(owl_variable *v, 578 579 580 GClosure *closure = g_cclosure_new (fn, NULL, NULL);587 GCallback fn, 588 GClosureMarshal marshal) { 589 GClosure *closure = g_cclosure_new_swap(fn, v, NULL); 581 590 g_closure_set_marshal(closure,marshal); 582 591 g_closure_ref(closure); … … 594 603 marshal_func); \ 595 604 } while(0) 596 605 606 #define DEFAULT_CB(a, b) (a ? a : G_CALLBACK(b)) 607 608 #define SET_CBS(type) \ 609 newvar->get_fn = DEFAULT_CB(init_params->get_fn, owl_variable_##type##_get_default); \ 610 newvar->set_fn = DEFAULT_CB(init_params->set_fn, owl_variable_##type##_set_default); \ 611 newvar->validate_fn = \ 612 DEFAULT_CB(init_params->validate_fn, owl_variable_##type##_validate_default); 597 613 598 614 int owl_variable_dict_add_from_list(owl_vardict *vd, owl_variable_init_params *variables_to_init) … … 608 624 newvar->description = g_strdup(init_params->description); 609 625 newvar->validsettings = init_params->validsettings; 610 GValue values[] = {{0}, {0}};611 GValue *value = values+1;612 GValue ret = {0};613 626 GCallback fn = NULL; 614 627 switch (init_params->type) { 615 628 case OWL_VARIABLE_STRING: 616 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_fn, 617 G_CALLBACK(owl_variable_string_get_default), 618 g_cclosure_user_marshal_STRING__VOID, fn); 619 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fn, 620 G_CALLBACK(owl_variable_string_set_default), 621 g_cclosure_user_marshal_INT__STRING, fn); 622 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, validate_fn, 623 G_CALLBACK(owl_variable_string_validate_default), 624 g_cclosure_user_marshal_INT__STRING, fn); 629 SET_CBS(string); 625 630 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fromstring_fn, 626 631 G_CALLBACK(owl_variable_string_set_fromstring_default), … … 628 633 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_tostring_fn, 629 634 G_CALLBACK(owl_variable_string_get_tostring_default), 630 g_cclosure_user_marshal_STRING__STRING, fn);631 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_default_fn,632 G_CALLBACK(owl_variable_string_get_default_default),633 635 g_cclosure_user_marshal_STRING__VOID, fn); 634 636 635 g_value_init( value,G_TYPE_STRING);636 g_value_set_string(value, init_params->pval_default);637 g_value_init(&newvar->val, G_TYPE_STRING); 638 owl_variable_set_string(newvar, init_params->pval_default); 637 639 break; 638 640 case OWL_VARIABLE_BOOL: 639 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_fn, 640 G_CALLBACK(owl_variable_bool_get_default), 641 g_cclosure_user_marshal_BOOLEAN__VOID, fn); 642 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fn, 643 G_CALLBACK(owl_variable_bool_set_default), 644 g_cclosure_user_marshal_INT__BOOLEAN, fn); 645 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, validate_fn, 646 G_CALLBACK(owl_variable_bool_validate_default), 647 g_cclosure_user_marshal_INT__BOOLEAN, fn); 641 SET_CBS(bool); 648 642 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fromstring_fn, 649 643 G_CALLBACK(owl_variable_bool_set_fromstring_default), … … 651 645 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_tostring_fn, 652 646 G_CALLBACK(owl_variable_bool_get_tostring_default), 653 g_cclosure_user_marshal_STRING__BOOLEAN, fn); 654 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_default_fn, 655 G_CALLBACK(owl_variable_bool_get_default_default), 656 g_cclosure_user_marshal_BOOLEAN__VOID, fn); 657 658 g_value_init(value,G_TYPE_BOOLEAN); 659 g_value_set_boolean(value, !!(init_params->ival_default)); 647 g_cclosure_user_marshal_STRING__VOID, fn); 648 649 g_value_init(&newvar->val, G_TYPE_BOOLEAN); 650 owl_variable_set_bool(newvar, !!(init_params->ival_default)); 660 651 break; 661 652 case OWL_VARIABLE_INT: 662 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_fn, 663 G_CALLBACK(owl_variable_int_get_default), 664 g_cclosure_user_marshal_INT__VOID, fn); 665 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fn, 666 G_CALLBACK(owl_variable_int_set_default), 667 g_cclosure_user_marshal_INT__INT, fn); 668 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, validate_fn, 669 G_CALLBACK(owl_variable_int_validate_default), 670 g_cclosure_user_marshal_INT__INT, fn); 653 SET_CBS(int); 671 654 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, set_fromstring_fn, 672 655 G_CALLBACK(owl_variable_int_set_fromstring_default), … … 674 657 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_tostring_fn, 675 658 G_CALLBACK(owl_variable_int_get_tostring_default), 676 g_cclosure_user_marshal_STRING__INT, fn); 677 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, get_default_fn, 678 G_CALLBACK(owl_variable_int_get_default_default), 679 g_cclosure_user_marshal_INT__VOID, fn); 680 681 g_value_init(value,G_TYPE_INT); 682 g_value_set_int(value, init_params->ival_default); 659 g_cclosure_user_marshal_STRING__VOID, fn); 660 661 g_value_init(&newvar->val, G_TYPE_INT); 662 owl_variable_set_int(newvar, init_params->ival_default); 683 663 break; 684 664 default: … … 686 666 return(-2); 687 667 } 688 OWL_VARIABLE_SETUP_FUNC(newvar, init_params, delete_fn, 689 G_CALLBACK(owl_variable_delete_default), 690 g_cclosure_marshal_VOID__VOID, fn); 691 692 g_value_init(&(newvar->gval_default), G_VALUE_TYPE(value)); 693 g_value_init(&(newvar->val), G_VALUE_TYPE(value)); 694 g_value_copy(value, &(newvar->gval_default)); 695 /* we have the value boxed up already *anyway*, so... */ 696 g_value_init(values, G_TYPE_POINTER); 697 g_value_set_pointer(values, newvar); 698 g_value_init(&ret, G_TYPE_INT); 699 g_closure_invoke(newvar->set_fn, &ret, 2, values, NULL); 700 g_value_unset(value); 668 669 /* record the initial value as a string */ 670 newvar->default_str = owl_variable_get_tostring(newvar); 671 701 672 owl_dict_insert_element(vd, newvar->name, newvar, NULL); 702 673 } … … 726 697 727 698 #define OWL_VARIABLE_SETUP_DEFAULT_FUNCS(variable, type, gtype) do { \ 728 variable->set_fn = owl_variable_make_closure(variable, \ 729 G_CALLBACK(owl_variable_##type##_set_default), \ 730 g_cclosure_user_marshal_INT__##gtype); \ 731 variable->get_fn = owl_variable_make_closure(variable, \ 732 G_CALLBACK(owl_variable_##type##_get_default), \ 733 g_cclosure_user_marshal_##gtype##__##VOID); \ 734 variable->validate_fn = owl_variable_make_closure(variable, \ 735 G_CALLBACK(owl_variable_##type##_validate_default), \ 736 g_cclosure_user_marshal_INT__##gtype); \ 699 variable->set_fn = G_CALLBACK(owl_variable_##type##_set_default); \ 700 variable->get_fn = G_CALLBACK(owl_variable_##type##_get_default); \ 701 variable->validate_fn = G_CALLBACK(owl_variable_##type##_validate_default); \ 737 702 variable->set_fromstring_fn = owl_variable_make_closure(variable, \ 738 703 G_CALLBACK(owl_variable_##type##_set_fromstring_default), \ … … 740 705 variable->get_tostring_fn = owl_variable_make_closure(variable, \ 741 706 G_CALLBACK(owl_variable_##type##_get_tostring_default), \ 742 g_cclosure_user_marshal_STRING__##gtype); \ 743 variable->get_default_fn = owl_variable_make_closure(variable, \ 744 G_CALLBACK(owl_variable_##type##_get_default_default), \ 745 g_cclosure_user_marshal_##gtype##__##VOID); \ 746 } while(0); 707 g_cclosure_user_marshal_STRING__VOID); \ 708 } while(0) 747 709 748 710 void owl_variable_dict_newvar_string(owl_vardict *vd, const char *name, const char *summ, const char *desc, const char *initval) 749 711 { 750 GValue default_gvals[] = {{0}, {0}};751 GValue *default_gval = default_gvals+1;752 GValue retval = {0};753 g_value_init(default_gval, G_TYPE_STRING);754 g_value_init(&retval, G_TYPE_INT);755 g_value_set_static_string(default_gval, initval);756 712 owl_variable *old = owl_variable_get_var(vd, name); 757 713 if (old && owl_variable_get_type(old) == OWL_VARIABLE_STRING) { 758 714 owl_variable_update(old, summ, desc); 759 g_value_copy(default_gval, &(old->gval_default)); 715 /* g_value_copy(default_gval, &(old->gval_default)); */ 716 /* TODO: Resolve this after churn; this function is only called from perl 717 * anyway. */ 760 718 } else { 761 719 owl_variable * var = owl_variable_newvar(name, summ, desc); 762 720 var->type = OWL_VARIABLE_STRING; 763 721 var->validsettings = "<string>"; 764 g_value_init(&(var->val), G_TYPE_STRING); 765 g_value_init(default_gvals, G_TYPE_POINTER); 766 g_value_set_pointer(default_gvals, var); 767 g_value_init(&(var->gval_default), G_TYPE_STRING); 768 g_value_set_string(&(var->gval_default), initval); 722 g_value_init(&var->val, G_TYPE_STRING); 769 723 OWL_VARIABLE_SETUP_DEFAULT_FUNCS(var, string, STRING); 770 724 771 g_closure_invoke(var->set_fn, &retval, 2, default_gvals, NULL); 725 owl_variable_set_string(var, initval); 726 727 /* record the initial value as a string */ 728 var->default_str = owl_variable_get_tostring(var); 729 772 730 owl_variable_dict_add_variable(vd, var); 773 731 } … … 776 734 void owl_variable_dict_newvar_int(owl_vardict *vd, const char *name, const char *summ, const char *desc, int initval) 777 735 { 778 GValue default_gvals[] = {{0}, {0}};779 GValue *default_gval = default_gvals+1;780 GValue retval = {0};781 g_value_init(default_gval, G_TYPE_INT);782 g_value_init(&retval, G_TYPE_INT);783 g_value_set_int(default_gval, initval);784 736 owl_variable *old = owl_variable_get_var(vd, name); 785 737 if (old && owl_variable_get_type(old) == OWL_VARIABLE_INT) { 786 738 owl_variable_update(old, summ, desc); 787 g_value_copy(default_gval, &(old->gval_default)); 739 /* g_value_copy(default_gval, &(old->gval_default)); */ 740 /* TODO: Resolve this after churn; this function is only called from perl 741 * anyway. */ 788 742 } else { 789 743 owl_variable * var = owl_variable_newvar(name, summ, desc); 790 744 var->type = OWL_VARIABLE_INT; 791 745 var->validsettings = "<int>"; 792 g_value_init(&(var->val), G_TYPE_INT); 793 g_value_init(&(var->gval_default), G_TYPE_INT); 746 g_value_init(&var->val, G_TYPE_INT); 794 747 OWL_VARIABLE_SETUP_DEFAULT_FUNCS(var, int, INT); 795 g_value_init(default_gvals, G_TYPE_POINTER); 796 g_value_set_pointer(default_gvals, var); 797 g_value_set_int(&(var->gval_default), initval); 798 g_closure_invoke(var->set_fn, &retval, 2, default_gvals, NULL); 799 /* g_value_unset(&retval); */ 748 749 owl_variable_set_int(var, initval); 750 751 /* record the initial value as a string */ 752 var->default_str = owl_variable_get_tostring(var); 753 800 754 owl_variable_dict_add_variable(vd, var); 801 755 } … … 804 758 void owl_variable_dict_newvar_bool(owl_vardict *vd, const char *name, const char *summ, const char *desc, gboolean initval) 805 759 { 806 GValue default_gvals[] = {{0}, {0}};807 GValue *default_gval = default_gvals+1;808 GValue retval = {0};809 g_value_init(default_gval, G_TYPE_BOOLEAN);810 g_value_init(&retval, G_TYPE_INT);811 g_value_set_boolean(default_gval, initval);812 760 owl_variable *old = owl_variable_get_var(vd, name); 813 761 if (old && owl_variable_get_type(old) == OWL_VARIABLE_BOOL) { 814 762 owl_variable_update(old, summ, desc); 815 g_value_copy(default_gval, &(old->gval_default)); 763 /* g_value_copy(default_gval, &(old->gval_default)); */ 764 /* TODO: Resolve this after churn; this function is only called from perl 765 * anyway. */ 816 766 } else { 817 767 owl_variable * var = owl_variable_newvar(name, summ, desc); 818 768 var->type = OWL_VARIABLE_BOOL; 819 769 var->validsettings = "on,off"; 820 g_value_init(&(var->val), G_TYPE_BOOLEAN); 821 g_value_init(&(var->gval_default), G_TYPE_BOOLEAN); 770 g_value_init(&var->val, G_TYPE_BOOLEAN); 822 771 OWL_VARIABLE_SETUP_DEFAULT_FUNCS(var, bool, BOOLEAN); 823 772 824 g_value_init(default_gvals, G_TYPE_POINTER); 825 g_value_set_pointer(default_gvals, var); 826 g_value_set_int(&(var->gval_default), initval); 827 g_closure_invoke(var->set_fn, &retval, 2, default_gvals, NULL); 773 owl_variable_set_bool(var, initval); 774 775 /* record the initial value as a string */ 776 var->default_str = owl_variable_get_tostring(var); 777 828 778 owl_variable_dict_add_variable(vd, var); 829 779 } … … 841 791 void owl_variable_cleanup(owl_variable *v) 842 792 { 843 GValue val = {0};844 if (v->delete_fn) {845 g_value_init(&val, G_TYPE_POINTER);846 g_value_set_pointer(&val, v);847 g_closure_invoke(v->delete_fn, NULL, 1, &val, NULL);848 }849 793 g_free(v->name); 850 794 g_free(v->summary); 851 795 g_free(v->description); 852 if (v->type == OWL_VARIABLE_STRING) { 853 g_value_unset(&v->gval_default); 854 } 855 g_closure_unref(v->get_fn); 856 g_closure_unref(v->set_fn); 857 g_closure_unref(v->validate_fn); 796 g_free(v->default_str); 797 g_value_unset(&(v->val)); 858 798 g_closure_unref(v->get_tostring_fn); 859 799 g_closure_unref(v->set_fromstring_fn); 860 800 } 861 862 void owl_variable_cleanup_initializer(owl_variable_init_params *v) {863 g_free(v->name);864 g_free(v->summary);865 g_free(v->description);866 if (v->type == OWL_VARIABLE_STRING)867 g_free(v->pval_default);868 }869 801 870 802 void owl_variable_delete(owl_variable *v) … … 895 827 { 896 828 return v->type; 897 }898 899 /* functions for getting and setting variable values */900 901 static CALLER_OWN char *owl_variable_invoke_tostring(const owl_variable *v,902 const GValue *value)903 {904 GValue values[] = { {0}, {0}};905 GValue *value_box = values+1;906 GValue tostring_box = {0};907 char *ret = NULL;908 gboolean need_to_free = false;909 910 g_value_init(values, G_TYPE_POINTER);911 g_value_set_pointer(values, (gpointer)v);912 g_value_init(&tostring_box, G_TYPE_STRING);913 if(value) {914 g_value_init(value_box, G_VALUE_TYPE(value));915 g_value_copy(value, value_box);916 need_to_free = true;917 } else {918 g_value_init(value_box, owl_variable_gtype_map[v->type]);919 g_closure_invoke(v->get_fn, value_box, 1, values, NULL);920 }921 g_closure_invoke(v->get_tostring_fn,&tostring_box, 2, values, NULL);922 923 ret = g_value_dup_string(&tostring_box);924 g_value_unset(&tostring_box);925 if(need_to_free) {926 g_value_unset(value_box);927 }928 929 return ret;930 829 } 931 830 … … 933 832 int owl_variable_set_fromstring(owl_variable *v, const char *value, int msg) { 934 833 char *tostring; 935 GValue values[] = { {0},{0},{0}};936 GValue *value_box = values+1;937 GValue return_box = {0};834 GValue values[] = {G_VALUE_INIT, G_VALUE_INIT}; 835 GValue *value_box = &values[1]; 836 GValue return_box = G_VALUE_INIT; 938 837 int set_successfully = -1; 939 838 if (!v->set_fromstring_fn) { … … 941 840 return -1; 942 841 } 842 g_value_init(&values[0], G_TYPE_POINTER); 843 g_value_set_pointer(&values[0], NULL); 943 844 g_value_init(value_box, G_TYPE_STRING); 944 g_value_init(values, G_TYPE_POINTER); 945 g_value_set_pointer(values, v); 845 g_value_set_static_string(value_box, value); 946 846 g_value_init(&return_box, G_TYPE_INT); 947 g_value_set_static_string(value_box, value);948 847 g_closure_invoke(v->set_fromstring_fn, &return_box, 2, values, NULL); 949 848 set_successfully = g_value_get_int(&return_box); … … 953 852 } 954 853 if (msg && (0 != set_successfully)) { 955 tostring = owl_variable_ invoke_tostring(v, NULL);854 tostring = owl_variable_get_tostring(v); 956 855 if (tostring) { 957 856 owl_function_makemsg("%s = '%s'", owl_variable_get_name(v), tostring); … … 964 863 return set_successfully; 965 864 } 966 967 static int owl_variable_invoke_setter(owl_variable *v, const GValue *value) {968 GValue values[] = {{0},{0}};969 GValue return_box = {0};970 int ret = -1;971 g_value_init(&return_box, G_TYPE_INT);972 g_value_init(values, G_TYPE_POINTER);973 g_value_set_pointer(values, v);974 g_value_init(values+1, G_VALUE_TYPE(value));975 g_value_copy(value, values+1);976 g_closure_invoke(v->set_fn, &return_box, 2, values, NULL);977 ret = g_value_get_int(&return_box);978 /* g_value_unset(&return_box); */979 g_value_unset(values+1);980 return ret;981 }982 865 983 866 int owl_variable_set_string(owl_variable *v, const char *newval) 984 867 { 985 int ret = -1; 986 GValue value_box = {0}; 987 if (v->type != OWL_VARIABLE_STRING) return -1; 988 g_value_init(&value_box, G_TYPE_STRING); 989 g_value_set_static_string(&value_box, newval); 990 ret = owl_variable_invoke_setter(v, &value_box); 991 g_value_unset(&value_box); 992 return ret; 868 g_return_val_if_fail(v->type == OWL_VARIABLE_STRING, -1); 869 870 set_string_t cb = (set_string_t) v->set_fn; 871 return cb(v, newval); 993 872 } 994 873 995 874 int owl_variable_set_int(owl_variable *v, int newval) 996 875 { 997 int ret = -1; 998 GValue value_box = {0}; 999 if (v->type != OWL_VARIABLE_INT && v->type != OWL_VARIABLE_BOOL) return -1; 1000 g_value_init(&value_box, G_TYPE_INT); 1001 g_value_set_int(&value_box, newval); 1002 ret = owl_variable_invoke_setter(v, &value_box); 1003 /* g_value_unset(&value_box); */ 1004 return ret; 876 g_return_val_if_fail(v->type == OWL_VARIABLE_INT, -1); 877 878 set_int_t cb = (set_int_t) v->set_fn; 879 return cb(v, newval); 1005 880 } 1006 881 1007 882 int owl_variable_set_bool(owl_variable *v, gboolean newval) { 1008 int ret = -1; 1009 GValue value_box = {0}; 1010 if (v->type != OWL_VARIABLE_BOOL) return -1; 1011 g_value_init(&value_box, G_TYPE_BOOLEAN); 1012 g_value_set_int(&value_box, newval); 1013 ret = owl_variable_invoke_setter(v, &value_box); 1014 /* g_value_unset(&value_box); */ 1015 return ret; 883 g_return_val_if_fail(v->type == OWL_VARIABLE_BOOL, -1); 884 885 set_bool_t cb = (set_bool_t) v->set_fn; 886 return cb(v, newval); 1016 887 } 1017 888 … … 1030 901 CALLER_OWN char *owl_variable_get_tostring(const owl_variable *v) 1031 902 { 1032 return owl_variable_invoke_tostring(v, NULL); 1033 } 1034 1035 CALLER_OWN char *owl_variable_get_default_tostring(const owl_variable *v) 1036 { 903 GValue instance = G_VALUE_INIT; 904 GValue tostring_box = G_VALUE_INIT; 1037 905 char *ret = NULL; 1038 GValue default_value_box = {0}; 1039 GValue variable_box = {0};1040 g_value_ init(&variable_box, G_TYPE_POINTER);1041 g_value_init(& default_value_box, owl_variable_gtype_map[v->type]);1042 g_ value_set_pointer(&variable_box, (gpointer)v);1043 g_closure_invoke(v->get_default_fn, &default_value_box, 1, 1044 &variable_box, NULL); 1045 ret = owl_variable_invoke_tostring(v, &default_value_box);1046 g_value_unset(&default_value_box); 906 907 g_value_init(&instance, G_TYPE_POINTER); 908 g_value_set_pointer(&instance, NULL); 909 g_value_init(&tostring_box, G_TYPE_STRING); 910 g_closure_invoke(v->get_tostring_fn, &tostring_box, 1, &instance, NULL); 911 912 ret = g_value_dup_string(&tostring_box); 913 g_value_unset(&tostring_box); 914 1047 915 return ret; 1048 916 } 1049 917 918 const char *owl_variable_get_default_tostring(const owl_variable *v) 919 { 920 return v->default_str; 921 } 922 1050 923 owl_variable *owl_variable_get_var(const owl_vardict *d, const char *name) 1051 924 { … … 1053 926 } 1054 927 1055 const GValue *owl_variable_get(const owl_variable *v, GValue *val)1056 {1057 GValue variable_box = {0};1058 g_value_init(&variable_box, G_TYPE_POINTER);1059 g_value_set_pointer(&variable_box,(gpointer)v);1060 g_closure_invoke(v->get_fn, val, 1, &variable_box, NULL);1061 return val;1062 }1063 1064 928 const char *owl_variable_get_string(const owl_variable *v) 1065 929 { 1066 GValue value = {0}; 1067 if (owl_variable_get_type(v) != OWL_VARIABLE_STRING) { 1068 owl_function_error("Variable '%s' is not a string.", owl_variable_get_name(v)); 1069 return NULL; 1070 } 1071 g_value_init(&value, G_TYPE_STRING); 1072 /* not a leak, since we don't own the underlying string */ 1073 return g_value_get_string(owl_variable_get(v,&value)); 930 g_return_val_if_fail(v->type == OWL_VARIABLE_STRING, NULL); 931 932 get_string_t cb = (get_string_t) v->get_fn; 933 return cb(v); 1074 934 } 1075 935 1076 936 int owl_variable_get_int(const owl_variable *v) 1077 937 { 1078 GValue value = {0}; 1079 if (owl_variable_get_type(v) != OWL_VARIABLE_INT) { 1080 owl_function_error("Variable '%s' is an int.", owl_variable_get_name(v)); 1081 return -1; 1082 } 1083 g_value_init(&value, G_TYPE_INT); 1084 return g_value_get_int(owl_variable_get(v,&value)); 1085 } 1086 1087 int owl_variable_get_bool(const owl_variable *v) 1088 { 1089 GValue value = {0}; 1090 if (owl_variable_get_type(v) != OWL_VARIABLE_BOOL) { 1091 owl_function_error("Variable '%s' is a boolean.", owl_variable_get_name(v)); 1092 return -1; 1093 } 1094 g_value_init(&value, G_TYPE_BOOLEAN); 1095 return g_value_get_boolean(owl_variable_get(v,&value)); 938 g_return_val_if_fail(v->type == OWL_VARIABLE_INT, 0); 939 940 get_int_t cb = (get_int_t) v->get_fn; 941 return cb(v); 942 } 943 944 gboolean owl_variable_get_bool(const owl_variable *v) 945 { 946 g_return_val_if_fail(v->type == OWL_VARIABLE_BOOL, FALSE); 947 948 get_bool_t cb = (get_bool_t) v->get_fn; 949 return cb(v); 1096 950 } 1097 951 1098 952 void owl_variable_describe(const owl_variable *v, owl_fmtext *fm) 1099 953 { 1100 c har *tostring= owl_variable_get_default_tostring(v);954 const char *default_str = owl_variable_get_default_tostring(v); 1101 955 char *default_buf; 1102 956 1103 if ( tostring)1104 default_buf = g_strdup_printf("'%s'", tostring);957 if (default_str) 958 default_buf = g_strdup_printf("'%s'", default_str); 1105 959 else 1106 960 default_buf = g_strdup("<null>"); … … 1109 963 owl_variable_get_summary(v), default_buf); 1110 964 g_free(default_buf); 1111 g_free(tostring);1112 965 } 1113 966 1114 967 void owl_variable_get_help(const owl_variable *v, owl_fmtext *fm) { 1115 968 char *tostring; 969 const char *default_str; 1116 970 1117 971 owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n"); … … 1128 982 owl_fmtext_append_normal(fm, "\n\n"); 1129 983 1130 tostring= owl_variable_get_default_tostring(v);984 default_str = owl_variable_get_default_tostring(v); 1131 985 owl_fmtext_append_normal(fm, "Default: "); 1132 owl_fmtext_append_normal(fm, ( tostring ? tostring: "<null>"));986 owl_fmtext_append_normal(fm, (default_str ? default_str : "<null>")); 1133 987 owl_fmtext_append_normal(fm, "\n\n"); 1134 g_free(tostring);1135 988 1136 989 owl_fmtext_append_normal(fm, "Valid Settings: "); … … 1154 1007 /* default common functions */ 1155 1008 1156 const char *owl_variable_string_get_default(const owl_variable *v , void *dummy) {1009 const char *owl_variable_string_get_default(const owl_variable *v) { 1157 1010 return g_value_get_string(&(v->val)); 1158 1011 } 1159 1012 1160 const char *owl_variable_string_get_default_default(const owl_variable *v, void *dummy) { 1161 return g_value_get_string(&(v->gval_default)); 1162 } 1163 1164 1165 const int owl_variable_int_get_default(const owl_variable *v, void *dummy) { 1013 int owl_variable_int_get_default(const owl_variable *v) { 1166 1014 return g_value_get_int(&(v->val)); 1167 1015 } 1168 1016 1169 const int owl_variable_int_get_default_default(const owl_variable *v, void *dummy) { 1170 return g_value_get_int(&(v->gval_default)); 1171 } 1172 1173 1174 const gboolean owl_variable_bool_get_default(const owl_variable *v, void *dummy) { 1017 gboolean owl_variable_bool_get_default(const owl_variable *v) { 1175 1018 return g_value_get_boolean(&(v->val)); 1176 1019 } 1177 1020 1178 const gboolean owl_variable_bool_get_default_default(const owl_variable *v, void *dummy) {1179 return g_value_get_boolean(&(v->gval_default));1180 }1181 1182 1183 void owl_variable_delete_default(owl_variable *v, void *dummy)1184 {1185 g_value_unset(&(v->val));1186 }1187 1188 1021 /* default functions for booleans */ 1189 1022 1190 int owl_variable_bool_validate_default(const owl_variable *v, const gboolean newval, void *dummy) {1023 int owl_variable_bool_validate_default(const owl_variable *v, gboolean newval) { 1191 1024 return (newval == 1) || (newval == 0); 1192 1025 } 1193 1026 1194 static int owl_variable_invoke_validator(owl_variable *v, const GValue *newval) 1195 { 1196 GValue values[] = {{0},{0}}; 1197 GValue ret = {0}; 1198 g_value_init(&ret, G_TYPE_INT); 1199 g_value_init(values, G_TYPE_POINTER); 1200 g_value_set_pointer(values, v); 1201 g_value_init(values+1, G_VALUE_TYPE(newval)); 1202 g_value_copy(newval, values+1); 1203 g_closure_invoke(v->validate_fn, &ret, 2, values, NULL); 1204 g_value_unset(values+1); 1205 return g_value_get_int(&ret); 1206 } 1207 1208 int owl_variable_bool_set_default(owl_variable *v, const bool newval, void *dummy) { 1209 GValue value = {0}; 1210 g_value_init(&value, G_TYPE_BOOLEAN); 1211 g_value_set_boolean(&value, newval); 1212 if (v->validate_fn) { 1213 if (!owl_variable_invoke_validator(v,&value)) { 1214 return(-1); 1215 } 1216 } 1027 int owl_variable_bool_set_default(owl_variable *v, bool newval) { 1028 if (!((validate_bool_t)v->validate_fn)(v, newval)) 1029 return -1; 1030 1217 1031 g_value_set_boolean(&(v->val), newval); 1218 1032 return(0); … … 1221 1035 int owl_variable_bool_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) { 1222 1036 gboolean i; 1223 GValue value = {0};1224 1037 if (!strcmp(newval, "on")) { 1225 i =1;1038 i = TRUE; 1226 1039 } else if (!strcmp(newval, "off")) { 1227 i =0;1040 i = FALSE; 1228 1041 } else { 1229 1042 return(-1); 1230 1043 } 1231 g_value_init(&value, G_TYPE_BOOLEAN); 1232 g_value_set_boolean(&value, i);1233 return owl_variable_invoke_setter(v,&value); 1234 } 1235 1236 CALLER_OWN char *owl_variable_bool_get_tostring_default(const owl_variable *v, const bool val, void *dummy) 1237 { 1044 1045 return owl_variable_set_bool(v, i); 1046 } 1047 1048 CALLER_OWN char *owl_variable_bool_get_tostring_default(const owl_variable *v, void *dummy) 1049 { 1050 bool val = owl_variable_get_bool(v); 1238 1051 if (val == 0) { 1239 1052 return g_strdup("off"); … … 1247 1060 /* default functions for integers */ 1248 1061 1249 int owl_variable_int_validate_default(const owl_variable *v, const int newval, void *dummy)1062 int owl_variable_int_validate_default(const owl_variable *v, int newval) 1250 1063 { 1251 1064 return (1); 1252 1065 } 1253 1066 1254 int owl_variable_int_set_default(owl_variable *v, const int newval, void *dummy) { 1255 GValue value = {0}; 1256 g_value_init(&value, G_TYPE_INT); 1257 g_value_set_int(&value, newval); 1258 if (v->validate_fn) { 1259 if (!owl_variable_invoke_validator(v,&value)) { 1260 return(-1); 1261 } 1262 } 1067 int owl_variable_int_set_default(owl_variable *v, int newval) { 1068 if (!((validate_int_t)v->validate_fn)(v, newval)) 1069 return -1; 1070 1263 1071 g_value_set_int(&(v->val), newval); 1264 1072 return(0); … … 1268 1076 int i; 1269 1077 char *ep; 1270 GValue value = {0};1271 1078 i = strtol(newval, &ep, 10); 1272 1079 if (*ep || ep==newval) return(-1); 1273 g_value_init(&value, G_TYPE_INT); 1274 g_value_set_int(&value, i); 1275 return owl_variable_invoke_setter(v, &value); 1276 } 1277 1278 CALLER_OWN char *owl_variable_int_get_tostring_default(const owl_variable *v, const int val, void *dummy) 1279 { 1280 return g_strdup_printf("%d", val); 1080 return owl_variable_set_int(v, i); 1081 } 1082 1083 CALLER_OWN char *owl_variable_int_get_tostring_default(const owl_variable *v, void *dummy) 1084 { 1085 return g_strdup_printf("%d", owl_variable_get_int(v)); 1281 1086 } 1282 1087 1283 1088 /* default functions for enums (a variant of integers) */ 1284 1089 1285 int owl_variable_enum_validate(const owl_variable *v, const int newval, void *dummy) {1090 int owl_variable_enum_validate(const owl_variable *v, int newval) { 1286 1091 char **enums; 1287 1092 int nenums, val; … … 1299 1104 char **enums; 1300 1105 int i, val=-1; 1301 GValue box = {0};1302 1106 if (newval == NULL) return(-1); 1303 1107 enums = g_strsplit_set(v->validsettings, ",", 0); … … 1309 1113 g_strfreev(enums); 1310 1114 if (val == -1) return(-1); 1311 g_value_init(&box, G_TYPE_INT); 1312 g_value_set_int(&box, val); 1313 return owl_variable_invoke_setter(v, &box); 1314 } 1315 1316 CALLER_OWN char *owl_variable_enum_get_tostring(const owl_variable *v, const int val, void *dummy) 1115 return owl_variable_set_int(v, val); 1116 } 1117 1118 CALLER_OWN char *owl_variable_enum_get_tostring(const owl_variable *v, void *dummy) 1317 1119 { 1318 1120 char **enums; … … 1322 1124 enums = g_strsplit_set(v->validsettings, ",", 0); 1323 1125 nenums = g_strv_length(enums); 1324 i = val;1126 i = owl_variable_get_int(v); 1325 1127 if (i<0 || i>=nenums) { 1326 1128 g_strfreev(enums); … … 1334 1136 /* default functions for stringeans */ 1335 1137 1336 int owl_variable_string_validate_default(const struct _owl_variable *v, const char *newval, void *dummy) {1138 int owl_variable_string_validate_default(const owl_variable *v, const char *newval) { 1337 1139 if (newval == NULL) return(0); 1338 1140 else return (1); 1339 1141 } 1340 1142 1341 int owl_variable_string_set_default(owl_variable *v, const char *newval, void *dummy) { 1342 GValue value = {0}; 1343 g_value_init(&value, G_TYPE_STRING); 1344 g_value_set_static_string(&value, newval); 1345 if (v->validate_fn) { 1346 if (!owl_variable_invoke_validator(v,&value)) { 1347 return(-1); 1348 } 1349 } 1350 1351 /* set_string dups the string for us */ 1143 int owl_variable_string_set_default(owl_variable *v, const char *newval) { 1144 if (!((validate_string_t)v->validate_fn)(v, newval)) 1145 return -1; 1146 1352 1147 g_value_set_string(&(v->val), newval); 1353 1148 return(0); … … 1356 1151 int owl_variable_string_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) 1357 1152 { 1358 GValue val = {0}; 1359 int ret = -1; 1360 g_value_init(&val, G_TYPE_STRING); 1361 /* we don't need to dup the string because we don't own it and the setter function we invoke will DTRT */ 1362 g_value_set_static_string(&val, newval); 1363 ret = owl_variable_invoke_setter(v, &val); 1364 g_value_unset(&val); 1365 return ret; 1366 } 1367 1368 CALLER_OWN char *owl_variable_string_get_tostring_default(const owl_variable *v, const char *val, void *dummy) 1369 { 1370 if(val) { 1371 return g_strdup(val); 1372 } else { 1373 return g_strdup(""); 1374 } 1375 } 1376 1153 return owl_variable_set_string(v, newval); 1154 } 1155 1156 CALLER_OWN char *owl_variable_string_get_tostring_default(const owl_variable *v, void *dummy) 1157 { 1158 return g_strdup(owl_variable_get_string(v)); 1159 } 1160
Note: See TracChangeset
for help on using the changeset viewer.