source: variable.c @ ee6b30f

Last change on this file since ee6b30f was 03fbf66, checked in by Jason Gross <jasongross9@gmail.com>, 7 years ago
Moved the last of the logging-related variables to perl We seem to have picked logpath as the string variable we test in tester.c. Since this is no longer in C, I've replaced it with personalbell (no particular reason for that choice).
  • Property mode set to 100644
File size: 43.7 KB
Line 
1#include "owl.h"
2#include <stdio.h>
3#include "gmarshal_funcs.h"
4
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
10typedef const char *(*get_string_t)(const owl_variable *);
11typedef int (*get_int_t)(const owl_variable *);
12typedef bool (*get_bool_t)(const owl_variable *);
13
14typedef int (*set_string_t)(owl_variable *, const char *);
15typedef int (*set_int_t)(owl_variable *, int);
16typedef int (*set_bool_t)(owl_variable *, bool);
17
18typedef int (*validate_string_t)(const owl_variable *, const char *);
19typedef int (*validate_int_t)(const owl_variable *, int);
20typedef int (*validate_bool_t)(const owl_variable *, bool);
21
22static void owl_variable_dict_newvar_bool_full(owl_vardict *vd,
23                                               const char *name,
24                                               bool default_val,
25                                               const char *summary,
26                                               const char *description,
27                                               validate_bool_t validate_fn,
28                                               set_bool_t set_fn,
29                                               get_bool_t get_fn);
30
31static void owl_variable_dict_newvar_string_full(owl_vardict *vd,
32                                                 const char *name,
33                                                 const char *default_val,
34                                                 const char *summary,
35                                                 const char *description,
36                                                 const char *validsettings,
37                                                 validate_string_t validate_fn,
38                                                 set_string_t set_fn,
39                                                 get_string_t get_fn);
40
41static void owl_variable_dict_newvar_int_full(owl_vardict *vd,
42                                              const char *name,
43                                              int default_val,
44                                              const char *summary,
45                                              const char *description,
46                                              const char *validsettings,
47                                              validate_int_t validate_fn,
48                                              set_int_t set_fn,
49                                              get_int_t get_fn);
50
51static void owl_variable_dict_newvar_enum_full(owl_vardict *vd,
52                                               const char *name,
53                                               int default_val,
54                                               const char *summary,
55                                               const char *description,
56                                               const char *validsettings,
57                                               validate_int_t validate_fn,
58                                               set_int_t set_fn,
59                                               get_int_t get_fn);
60
61#define OWLVAR_BOOL(name, default, summary, description) \
62        owl_variable_dict_newvar_bool(vd, name, default, summary, description)
63
64#define OWLVAR_BOOL_FULL(name, default, summary, description, validate, set, get) \
65        owl_variable_dict_newvar_bool_full(vd, name, default, summary, description, \
66                                           validate, set, get)
67
68#define OWLVAR_INT(name, default, summary, description) \
69        owl_variable_dict_newvar_int(vd, name, default, summary, description)
70
71#define OWLVAR_INT_FULL(name,default,summary,description,validset,validate,set,get) \
72        owl_variable_dict_newvar_int_full(vd, name, default, summary, description, \
73                                          validset, validate, set, get)
74
75#define OWLVAR_PATH(name, default, summary, description) \
76        owl_variable_dict_newvar_path(vd, name, default, summary, description)
77
78#define OWLVAR_STRING(name, default, summary, description) \
79        owl_variable_dict_newvar_string(vd, name, default, summary, description)
80
81#define OWLVAR_STRING_FULL(name, default, validset, summary, description, validate, set, get) \
82        owl_variable_dict_newvar_string_full(vd, name, default, summary, description, \
83                                             validset, validate, set, get)
84
85/* enums are really integers, but where validset is a comma-separated
86 * list of strings which can be specified.  The tokens, starting at 0,
87 * correspond to the values that may be specified. */
88#define OWLVAR_ENUM(name, default, summary, description, validset) \
89        owl_variable_dict_newvar_enum(vd, name, default, summary, description, validset)
90
91#define OWLVAR_ENUM_FULL(name,default,summary,description,validset,validate, set, get) \
92        owl_variable_dict_newvar_enum_full(vd, name, default, summary, description, \
93                                           validset, validate, set, get)
94
95void owl_variable_add_defaults(owl_vardict *vd)
96{
97  OWLVAR_STRING( "personalbell" /* %OwlVarStub */, "off",
98                 "ring the terminal bell when personal messages are received",
99                 "Can be set to 'on', 'off', or the name of a filter which\n"
100                 "messages need to match in order to ring the bell");
101
102  OWLVAR_BOOL( "bell" /* %OwlVarStub */, 1,
103               "enable / disable the terminal bell", "" );
104
105  OWLVAR_BOOL_FULL( "debug" /* %OwlVarStub */, OWL_DEBUG,
106                    "whether debugging is enabled",
107                    "If set to 'on', debugging messages are logged to the\n"
108                    "file specified by the debugfile variable.\n",
109                    NULL, owl_variable_debug_set, NULL);
110
111  OWLVAR_BOOL( "startuplogin" /* %OwlVarStub */, 1,
112               "send a login message when BarnOwl starts", "" );
113
114  OWLVAR_BOOL( "shutdownlogout" /* %OwlVarStub */, 1,
115               "send a logout message when BarnOwl exits", "" );
116
117  OWLVAR_BOOL( "rxping" /* %OwlVarStub */, 0,
118               "display received pings", "" );
119
120  OWLVAR_BOOL( "txping" /* %OwlVarStub */, 1,
121               "send pings", "" );
122
123  OWLVAR_BOOL( "sepbar_disable" /* %OwlVarStub */, 0,
124               "disable printing information in the separator bar", "" );
125
126  OWLVAR_BOOL( "smartstrip" /* %OwlVarStub */, 1,
127               "strip kerberos instance for reply", "");
128
129  OWLVAR_BOOL( "newlinestrip" /* %OwlVarStub */, 1,
130               "strip leading and trailing newlines", "");
131
132  OWLVAR_BOOL( "displayoutgoing" /* %OwlVarStub */, 1,
133               "display outgoing messages", "" );
134
135  OWLVAR_BOOL( "loginsubs" /* %OwlVarStub */, 1,
136               "load logins from .anyone on startup", "" );
137
138  OWLVAR_BOOL_FULL( "colorztext" /* %OwlVarStub */, 1,
139                    "allow @color() in zephyrs to change color",
140                    "", NULL, owl_variable_colorztext_set, NULL);
141
142  OWLVAR_BOOL( "fancylines" /* %OwlVarStub */, 1,
143               "Use 'nice' line drawing on the terminal.",
144               "If turned off, dashes, pipes and pluses will be used\n"
145               "to draw lines on the screen.  Useful when the terminal\n"
146               "is causing problems" );
147
148  OWLVAR_BOOL( "zcrypt" /* %OwlVarStub */, 1,
149               "Do automatic zcrypt processing",
150               "" );
151
152  OWLVAR_BOOL_FULL( "pseudologins" /* %OwlVarStub */, 0,
153                    "Enable zephyr pseudo logins",
154                    "When this is enabled, BarnOwl will periodically check the zephyr\n"
155                    "location of users in your .anyone file.  If a user is present\n"
156                    "but sent no login message, or a user is not present that sent no\n"
157                    "logout message, a pseudo login or logout message will be created\n",
158                    NULL, owl_variable_pseudologins_set, NULL);
159
160  OWLVAR_BOOL( "ignorelogins" /* %OwlVarStub */, 0,
161               "Enable printing of login notifications",
162               "When this is enabled, BarnOwl will print login and logout notifications\n"
163               "for AIM, zephyr, or other protocols.  If disabled BarnOwl will not print\n"
164               "login or logout notifications.\n");
165
166  OWLVAR_ENUM_FULL( "disable-ctrl-d" /* %OwlVarStub:lockout_ctrld */, 1,
167                    "don't send zephyrs on C-d",
168                    "If set to 'on', C-d won't send a zephyr from the edit\n"
169                    "window.  If set to 'off', C-d will always send a zephyr\n"
170                    "being composed in the edit window.  If set to 'middle',\n"
171                    "C-d will only ever send a zephyr if the cursor is at\n"
172                    "the end of the message being composed.\n\n"
173                    "Note that this works by changing the C-d keybinding\n"
174                    "in the editmulti keymap.\n",
175                    "off,middle,on",
176                    NULL, owl_variable_disable_ctrl_d_set, NULL);
177
178  OWLVAR_PATH( "debug_file" /* %OwlVarStub */, OWL_DEBUG_FILE,
179               "path for logging debug messages when debugging is enabled",
180               "This file will be logged to if 'debug' is set to 'on'.\n"
181               "BarnOwl will append a dot and the current process's pid to the filename.");
182 
183  OWLVAR_PATH( "zsigproc" /* %OwlVarStub:zsigproc */, NULL,
184               "name of a program to run that will generate zsigs",
185               "This program should produce a zsig on stdout when run.\n"
186               "Note that it is important that this program not block.\n\n"
187               "See the documentation for 'zsig' for more information about\n"
188               "how the outgoing zsig is chosen."
189               );
190
191  OWLVAR_PATH( "newmsgproc" /* %OwlVarStub:newmsgproc */, NULL,
192               "name of a program to run when new messages are present",
193               "The named program will be run when BarnOwl receives new\n"
194               "messages.  It will not be run again until the first\n"
195               "instance exits");
196
197  OWLVAR_STRING( "zsender" /* %OwlVarStub */, "",
198             "zephyr sender name",
199         "Allows you to customize the outgoing username in\n"
200         "zephyrs.  If this is unset, it will use your Kerberos\n"
201         "principal. Note that customizing the sender name will\n"
202         "cause your zephyrs to be sent unauthenticated.");
203
204  OWLVAR_STRING( "zsigfunc" /* %OwlVarStub */, "BarnOwl::default_zephyr_signature()",
205                 "zsig perl function",
206                 "Called every time you start a zephyrgram without an\n"
207                 "explicit zsig.  The default setting implements the policy\n"
208                 "described in the documentation for the 'zsig' variable.\n"
209                 "See also BarnOwl::random_zephyr_signature().\n");
210
211  OWLVAR_STRING( "zsig" /* %OwlVarStub */, "",
212                 "zephyr signature",
213                 "The zsig to get on outgoing messages. If this variable is\n"
214                 "unset, 'zsigproc' will be run to generate a zsig. If that is\n"
215                 "also unset, the 'zwrite-signature' zephyr variable will be\n"
216                 "used instead.\n");
217
218  OWLVAR_STRING( "appendtosepbar" /* %OwlVarStub */, "",
219                 "string to append to the end of the sepbar",
220                 "The sepbar is the bar separating the top and bottom\n"
221                 "of the BarnOwl screen.  Any string specified here will\n"
222                 "be displayed on the right of the sepbar\n");
223
224  OWLVAR_BOOL( "zaway" /* %OwlVarStub */, 0,
225               "turn zaway on or off", "" );
226
227  OWLVAR_STRING( "zaway_msg" /* %OwlVarStub */, 
228                 OWL_DEFAULT_ZAWAYMSG,
229                 "zaway msg for responding to zephyrs when away", "" );
230
231  OWLVAR_STRING( "zaway_msg_default" /* %OwlVarStub */, 
232                 OWL_DEFAULT_ZAWAYMSG,
233                 "default zaway message", "" );
234
235  OWLVAR_BOOL_FULL( "aaway" /* %OwlVarStub */, 0,
236                    "Set AIM away status",
237                    "",
238                    NULL, owl_variable_aaway_set, NULL);
239
240  OWLVAR_STRING( "aaway_msg" /* %OwlVarStub */, 
241                 OWL_DEFAULT_AAWAYMSG,
242                 "AIM away msg for responding when away", "" );
243
244  OWLVAR_STRING( "aaway_msg_default" /* %OwlVarStub */, 
245                 OWL_DEFAULT_AAWAYMSG,
246                 "default AIM away message", "" );
247
248  OWLVAR_STRING( "view_home" /* %OwlVarStub */, "all",
249                 "home view to switch to after 'X' and 'V'", 
250                 "SEE ALSO: view, filter\n" );
251
252  OWLVAR_STRING( "alert_filter" /* %OwlVarStub */, "none",
253                 "filter on which to trigger alert actions",
254                 "" );
255
256  OWLVAR_STRING( "alert_action" /* %OwlVarStub */, "nop",
257                 "BarnOwl command to execute for alert actions",
258                 "" );
259
260  OWLVAR_STRING_FULL( "tty" /* %OwlVarStub */, "", "<string>", "tty name for zephyr location", "",
261                      NULL, owl_variable_tty_set, NULL);
262
263  OWLVAR_STRING( "default_style" /* %OwlVarStub */, "default",
264                 "name of the default formatting style",
265                 "This sets the default message formatting style.\n"
266                 "Styles may be created with the 'style' command.\n"
267                 "Some built-in styles include:\n"
268                 "   default  - the default BarnOwl formatting\n"
269                 "   oneline  - one line per-message\n"
270                 "   perl     - legacy perl interface\n"
271                 "\nSEE ALSO: style, show styles, view -s <style>\n"
272                 );
273
274
275  OWLVAR_INT(    "edit:maxfillcols" /* %OwlVarStub:edit_maxfillcols */, 70,
276                 "maximum number of columns for M-q (edit:fill-paragraph) to fill text to",
277                 "This specifies the maximum number of columns for M-q to fill text\n"
278                 "to.  If set to 0, M-q will wrap to the width of the window, and\n"
279                 "values less than 0 disable M-q entirely.\n");
280
281  OWLVAR_INT(    "edit:maxwrapcols" /* %OwlVarStub:edit_maxwrapcols */, 70,
282                 "maximum number of columns for line-wrapping",
283                 "This specifies the maximum number of columns for\n"
284                 "auto-line-wrapping.  If set to 0, text will be wrapped at the\n"
285                 "window width. Values less than 0 disable automatic wrapping.\n"
286                 "\n"
287                 "As a courtesy to recipients, it is recommended that outgoing\n"
288                 "Zephyr messages be no wider than 70 columns.\n");
289
290  OWLVAR_INT( "aim_ignorelogin_timer" /* %OwlVarStub */, 15,
291              "number of seconds after AIM login to ignore login messages",
292              "This specifies the number of seconds to wait after an\n"
293              "AIM login before allowing the receipt of AIM login notifications.\n"
294              "By default this is set to 15.  If you would like to view login\n"
295              "notifications of buddies as soon as you login, set it to 0 instead.");
296
297             
298  OWLVAR_INT_FULL( "typewinsize" /* %OwlVarStub:typwin_lines */, 
299                   OWL_TYPWIN_SIZE,
300                  "number of lines in the typing window", 
301                   "This specifies the height of the window at the\n"
302                   "bottom of the screen where commands are entered\n"
303                   "and where messages are composed.\n",
304                   "int > 0",
305                   owl_variable_int_validate_gt0,
306                   owl_variable_typewinsize_set,
307                   NULL /* use default for get */
308                   );
309
310  OWLVAR_INT( "typewindelta" /* %OwlVarStub */, 0,
311                  "number of lines to add to the typing window when in use",
312                   "On small screens you may want the typing window to\n"
313                   "auto-hide when not entering a command or message.\n"
314                   "This variable is the number of lines to add to the\n"
315           "typing window when it is in use; you can then set\n"
316           "typewinsize to 1.\n\n"
317           "This works a lot better with a non-default scrollmode;\n"
318           "try :set scrollmode pagedcenter.\n");
319
320  OWLVAR_ENUM( "scrollmode" /* %OwlVarStub */, OWL_SCROLLMODE_NORMAL,
321               "how to scroll up and down",
322               "This controls how the screen is scrolled as the\n"
323               "cursor moves between messages being displayed.\n"
324               "The following modes are supported:\n\n"
325               "   normal      - This is the BarnOwl default.  Scrolling happens\n"
326               "                 when it needs to, and an attempt is made to\n"
327               "                 keep the current message roughly near\n"
328               "                 the middle of the screen.\n"
329               "   top         - The current message will always be the\n"
330               "                 the top message displayed.\n"
331               "   neartop     - The current message will be one down\n"
332               "                 from the top message displayed,\n"
333               "                 where possible.\n"
334               "   center      - An attempt is made to keep the current\n"
335               "                 message near the center of the screen.\n"
336               "   paged       - The top message displayed only changes\n"
337               "                 when user moves the cursor to the top\n"
338               "                 or bottom of the screen.  When it moves,\n"
339               "                 the screen will be paged up or down and\n"
340               "                 the cursor will be near the top or\n"
341               "                 the bottom.\n"
342               "   pagedcenter - The top message displayed only changes\n"
343               "                 when user moves the cursor to the top\n"
344               "                 or bottom of the screen.  When it moves,\n"
345               "                 the screen will be paged up or down and\n"
346               "                 the cursor will be near the center.\n",
347               "normal,top,neartop,center,paged,pagedcenter" );
348
349  OWLVAR_BOOL( "narrow-related" /* %OwlVarStub:narrow_related */, 1,
350               "Make smartnarrow use broader filters",
351               "Causes smartfilter to narrow to messages \"related\" to \n"
352               "the current message, as well as ones to the same place.\n\n"
353               "for Zephyr, this controls whether to narrow to e.g. class-help or\n"
354               "class-help.d alone, or to related-class-help, which includes\n"
355               "help, unhelp, help.d, etc.\n\nDefault is true (include unclasses, etc.).\n" );
356
357  OWLVAR_BOOL( "_followlast" /* %OwlVarStub */, 0,
358               "enable automatic following of the last zephyr",
359               "If the cursor is at the last message, it will\n"
360               "continue to follow the last message if this is set.\n"
361               "Note that this is currently risky as you might accidentally\n"
362               "delete a message right as it came in.\n" );
363
364  OWLVAR_STRING_FULL( "default_exposure" /* %OwlVarStub */, "",
365                      "none,opstaff,realm-visible,realm-announced,net-visible,net-announced",
366                      "controls the persistent value for exposure",
367                      "The default exposure level corresponds to the Zephyr exposure value\n"
368                      "in ~/.zephyr.vars.  Defaults to realm-visible if there is no value in\n"
369                      "~/.zephyr.vars.\n"
370                      "See the description of exposure for the values this can be.",
371                      NULL, owl_variable_default_exposure_set, owl_variable_default_exposure_get );
372
373  OWLVAR_STRING_FULL( "exposure" /* %OwlVarStub */, "",
374                      "none,opstaff,realm-visible,realm-announced,net-visible,net-announced",
375                      "controls who can zlocate you",
376                      "The exposure level, defaulting to the value of default_exposure,\n"
377                      "can be one of the following (from least exposure to widest exposure,\n"
378                      "as listed in zctl(1)):\n"
379                      "\n"
380                      "   none            - This completely disables Zephyr for the user. \n"
381                      "                     The user is not registered with Zephyr.  No user\n"
382                      "                     location information is retained by Zephyr.  No\n"
383                      "                     login or logout announcements will be sent.  No\n"
384                      "                     subscriptions will be entered for the user, and\n"
385                      "                     no notices will be displayed by zwgc(1).\n"
386                      "   opstaff         - The user is registered with Zephyr.  No login or\n"
387                      "                     logout announcements will be sent, and location\n"
388                      "                     information will only be visible to Operations\n"
389                      "                     staff.  Default subscriptions and any additional\n"
390                      "                     personal subscriptions will be entered for the\n"
391                      "                     user.\n"
392                      "   realm-visible   - The user is registered with Zephyr.  User\n"
393                      "                     location information is retained by Zephyr and\n"
394                      "                     made available only to users within the user’s\n"
395                      "                     Kerberos realm.  No login or logout\n"
396                      "                     announcements will be sent.  This is the system\n"
397                      "                     default.  Default subscriptions and any\n"
398                      "                     additional personal subscriptions will be\n"
399                      "                     entered for the user.\n"
400                      "   realm-announced - The user is registered with Zephyr.  User\n"
401                      "                     location information is retained by Zephyr and\n"
402                      "                     made available only to users authenticated\n"
403                      "                     within the user’s Kerberos realm.  Login and\n"
404                      "                     logout announcements will be sent, but only to\n"
405                      "                     users within the user’s Kerberos realm who have\n"
406                      "                     explicitly requested such via subscriptions. \n"
407                      "                     Default subscriptions and any additional\n"
408                      "                     personal subscriptions will be entered for the\n"
409                      "                     user.\n"
410                      "   net-visible     - The user is registered with Zephyr.  User\n"
411                      "                     location information is retained by Zephyr and\n"
412                      "                     made available to any authenticated user who\n"
413                      "                     requests such.  Login and logout announcements\n"
414                      "                     will be sent only to users within the user’s\n"
415                      "                     Kerberos realm who have explicitly requested\n"
416                      "                     such via subscriptions.  Default subscriptions\n"
417                      "                     and any additional personal subscriptions will\n"
418                      "                     be entered for the user.\n"
419                      "   net-announced   - The user is registered with Zephyr.  User\n"
420                      "                     location information is retained by Zephyr and\n"
421                      "                     made available to any authenticated user who\n"
422                      "                     requests such.  Login and logout announcements\n"
423                      "                     will be sent to any user has requested such. \n"
424                      "                     Default subscriptions and any additional\n"
425                      "                     personal subscriptions will be entered for the\n"
426                      "                     user.\n",
427                      NULL, owl_variable_exposure_set, NULL /* use default for get */ );
428}
429
430/**************************************************************************/
431/*********************** SPECIFIC TO VARIABLES ****************************/
432/**************************************************************************/
433
434
435/* commonly useful */
436
437int owl_variable_int_validate_gt0(const owl_variable *v, int newval)
438{
439  return !(newval < 1);
440}
441
442int owl_variable_int_validate_positive(const owl_variable *v, int newval)
443{
444  return !(newval < 0);
445}
446
447/* typewinsize */
448int owl_variable_typewinsize_set(owl_variable *v, int newval)
449{
450  int rv;
451  rv = owl_variable_int_set_default(v, newval);
452  if (0 == rv) owl_mainpanel_layout_contents(&g.mainpanel);
453  return(rv);
454}
455
456/* debug (cache value in g->debug) */
457int owl_variable_debug_set(owl_variable *v, bool newval)
458{
459  g.debug = newval;
460  return owl_variable_bool_set_default(v, newval);
461}
462
463/* When 'aaway' is changed, need to notify the AIM server */
464int owl_variable_aaway_set(owl_variable *v, bool newval)
465{
466  if (newval) {
467    owl_aim_set_awaymsg(owl_global_get_aaway_msg(&g));
468  } else {
469    owl_aim_set_awaymsg("");
470  }
471  return owl_variable_bool_set_default(v, newval);
472}
473
474int owl_variable_colorztext_set(owl_variable *v, bool newval)
475{
476  int ret = owl_variable_bool_set_default(v, newval);
477  /* flush the format cache so that we see the update, but only if we're done initializing BarnOwl */
478  if (owl_global_get_msglist(&g) != NULL)
479    owl_messagelist_invalidate_formats(owl_global_get_msglist(&g));
480  if (owl_global_get_mainwin(&g) != NULL) {
481    owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
482    owl_mainwin_redisplay(owl_global_get_mainwin(&g));
483  }
484  return ret;
485}
486
487int owl_variable_pseudologins_set(owl_variable *v, bool newval)
488{
489  static guint timer = 0;
490  if (newval) {
491    owl_function_zephyr_buddy_check(0);
492    if (timer == 0) {
493      timer = g_timeout_add_seconds(180, owl_zephyr_buddycheck_timer, NULL);
494    }
495  } else {
496    if (timer != 0) {
497      g_source_remove(timer);
498      timer = 0;
499    }
500  }
501  return owl_variable_bool_set_default(v, newval);
502}
503
504/* note that changing the value of this will clobber
505 * any user setting of this */
506int owl_variable_disable_ctrl_d_set(owl_variable *v, int newval)
507{
508  if (!owl_context_is_startup(owl_global_get_context(&g))) {
509    if (newval == 2) {
510      owl_function_command_norv("bindkey editmulti C-d command edit:delete-next-char");
511    } else if (newval == 1) {
512      owl_function_command_norv("bindkey editmulti C-d command edit:done-or-delete");
513    } else {
514      owl_function_command_norv("bindkey editmulti C-d command edit:done");
515    }
516  } 
517  return owl_variable_int_set_default(v, newval);
518}
519
520int owl_variable_tty_set(owl_variable *v, const char *newval)
521{
522  owl_zephyr_set_locationinfo(g_get_host_name(), newval);
523  return owl_variable_string_set_default(v, newval);
524}
525
526int owl_variable_default_exposure_set(owl_variable *v, const char *newval)
527{
528  return owl_zephyr_set_default_exposure(newval);
529}
530
531const char *owl_variable_default_exposure_get(const owl_variable *v)
532{
533  return owl_zephyr_get_default_exposure();
534}
535
536int owl_variable_exposure_set(owl_variable *v, const char *newval)
537{
538  int ret = owl_zephyr_set_exposure(newval);
539  if (ret != 0)
540    return ret;
541  return owl_variable_string_set_default(v, owl_zephyr_normalize_exposure(newval));
542}
543
544/**************************************************************************/
545/****************************** GENERAL ***********************************/
546/**************************************************************************/
547
548void owl_variable_dict_setup(owl_vardict *vd) {
549  owl_dict_create(vd);
550  owl_variable_add_defaults(vd);
551}
552
553CALLER_OWN GClosure *owl_variable_make_closure(owl_variable *v,
554                                               GCallback fn,
555                                               GClosureMarshal marshal) {
556  GClosure *closure = g_cclosure_new_swap(fn, v, NULL);
557  g_closure_set_marshal(closure,marshal);
558  g_closure_ref(closure);
559  g_closure_sink(closure);
560  return closure;
561}
562
563void owl_variable_dict_add_variable(owl_vardict * vardict,
564                                    owl_variable * var) {
565  char *oldvalue = NULL;
566  owl_variable *oldvar = owl_variable_get_var(vardict, var->name);
567  /* Save the old value as a string. */
568  if (oldvar) {
569    oldvalue = owl_variable_get_tostring(oldvar);
570  }
571  owl_dict_insert_element(vardict, var->name, var, (void (*)(void *))owl_variable_delete);
572  /* Restore the old value. */
573  if (oldvalue) {
574    owl_variable_set_fromstring(var, oldvalue, 0);
575    g_free(oldvalue);
576  }
577}
578
579static owl_variable *owl_variable_newvar(int type, const char *name, const char *summary, const char *description, const char *validsettings) {
580  owl_variable *var = g_slice_new0(owl_variable);
581  var->type = type;
582  var->name = g_strdup(name);
583  var->summary = g_strdup(summary);
584  var->description = g_strdup(description);
585  var->validsettings = g_strdup(validsettings);
586  return var;
587}
588
589static void owl_variable_dict_newvar_int_full(owl_vardict *vd, const char *name, int default_val, const char *summary, const char *description, const char *validsettings, validate_int_t validate_fn, set_int_t set_fn, get_int_t get_fn)
590{
591  owl_variable *var = owl_variable_newvar(OWL_VARIABLE_INT, name, summary,
592                                          description, validsettings);
593  var->takes_on_off = false;
594  var->get_fn = G_CALLBACK(get_fn ? get_fn : owl_variable_int_get_default);
595  var->set_fn = G_CALLBACK(set_fn ? set_fn : owl_variable_int_set_default);
596  var->validate_fn = G_CALLBACK(validate_fn ? validate_fn : owl_variable_int_validate_default);
597
598  var->get_tostring_fn = owl_variable_make_closure(
599      var, G_CALLBACK(owl_variable_int_get_tostring_default),
600      g_cclosure_user_marshal_STRING__VOID);
601  var->set_fromstring_fn = owl_variable_make_closure(
602      var, G_CALLBACK(owl_variable_int_set_fromstring_default),
603      g_cclosure_user_marshal_INT__STRING);
604
605  g_value_init(&var->val, G_TYPE_INT);
606  owl_variable_set_int(var, default_val);
607
608  var->default_str = owl_variable_get_tostring(var);
609  owl_variable_dict_add_variable(vd, var);
610}
611
612void owl_variable_dict_newvar_int(owl_vardict *vd, const char *name, int default_val, const char *summary, const char *description) {
613  owl_variable_dict_newvar_int_full(vd, name, default_val, summary, description,
614                                    "<int>", NULL, NULL, NULL);
615}
616
617static void owl_variable_dict_newvar_bool_full(owl_vardict *vd, const char *name, bool default_val, const char *summary, const char *description, validate_bool_t validate_fn, set_bool_t set_fn, get_bool_t get_fn)
618{
619  owl_variable *var = owl_variable_newvar(OWL_VARIABLE_BOOL, name, summary,
620                                          description, "on,off");
621  var->takes_on_off = true;
622  var->get_fn = G_CALLBACK(get_fn ? get_fn : owl_variable_bool_get_default);
623  var->set_fn = G_CALLBACK(set_fn ? set_fn : owl_variable_bool_set_default);
624  var->validate_fn = G_CALLBACK(validate_fn ? validate_fn : owl_variable_bool_validate_default);
625
626  var->get_tostring_fn = owl_variable_make_closure(
627      var, G_CALLBACK(owl_variable_bool_get_tostring_default),
628      g_cclosure_user_marshal_STRING__VOID);
629  var->set_fromstring_fn = owl_variable_make_closure(
630      var, G_CALLBACK(owl_variable_bool_set_fromstring_default),
631      g_cclosure_user_marshal_INT__STRING);
632
633  g_value_init(&var->val, G_TYPE_BOOLEAN);
634  owl_variable_set_bool(var, default_val);
635
636  var->default_str = owl_variable_get_tostring(var);
637  owl_variable_dict_add_variable(vd, var);
638}
639
640void owl_variable_dict_newvar_bool(owl_vardict *vd, const char *name, bool default_val, const char *summary, const char *description) {
641  owl_variable_dict_newvar_bool_full(vd, name, default_val, summary, description,
642                                     NULL, NULL, NULL);
643}
644
645static void owl_variable_dict_newvar_string_full(owl_vardict *vd, const char *name, const char *default_val, const char *summary, const char *description, const char *validsettings, validate_string_t validate_fn, set_string_t set_fn, get_string_t get_fn)
646{
647  owl_variable *var = owl_variable_newvar(OWL_VARIABLE_STRING, name, summary,
648                                          description, validsettings);
649  var->takes_on_off = false;
650  var->get_fn = G_CALLBACK(get_fn ? get_fn : owl_variable_string_get_default);
651  var->set_fn = G_CALLBACK(set_fn ? set_fn : owl_variable_string_set_default);
652  var->validate_fn = G_CALLBACK(validate_fn ? validate_fn : owl_variable_string_validate_default);
653
654  var->get_tostring_fn = owl_variable_make_closure(
655      var, G_CALLBACK(owl_variable_string_get_tostring_default),
656      g_cclosure_user_marshal_STRING__VOID);
657  var->set_fromstring_fn = owl_variable_make_closure(
658      var, G_CALLBACK(owl_variable_string_set_fromstring_default),
659      g_cclosure_user_marshal_INT__STRING);
660
661  g_value_init(&var->val, G_TYPE_STRING);
662  owl_variable_set_string(var, default_val);
663
664  var->default_str = owl_variable_get_tostring(var);
665  owl_variable_dict_add_variable(vd, var);
666}
667
668void owl_variable_dict_newvar_string(owl_vardict *vd, const char *name, const char *default_val, const char *summary, const char *description) {
669  owl_variable_dict_newvar_string_full(vd, name, default_val, summary, description,
670                                       "<string>", NULL, NULL, NULL);
671}
672
673void owl_variable_dict_newvar_path(owl_vardict *vd, const char *name, const char *default_val, const char *summary, const char *description) {
674  owl_variable_dict_newvar_string_full(vd, name, default_val, summary, description,
675                                       "<path>", NULL, NULL, NULL);
676}
677
678static void owl_variable_dict_newvar_enum_full(owl_vardict *vd, const char *name, int default_val, const char *summary, const char *description, const char *validsettings, validate_int_t validate_fn, set_int_t set_fn, get_int_t get_fn)
679{
680  owl_variable *var = owl_variable_newvar(OWL_VARIABLE_INT, name, summary,
681                                          description, validsettings);
682  var->takes_on_off = false;
683  var->get_fn = G_CALLBACK(get_fn ? get_fn : owl_variable_int_get_default);
684  var->set_fn = G_CALLBACK(set_fn ? set_fn : owl_variable_int_set_default);
685  var->validate_fn = G_CALLBACK(validate_fn ? validate_fn : owl_variable_enum_validate);
686
687  var->get_tostring_fn = owl_variable_make_closure(
688      var, G_CALLBACK(owl_variable_enum_get_tostring),
689      g_cclosure_user_marshal_STRING__VOID);
690  var->set_fromstring_fn = owl_variable_make_closure(
691      var, G_CALLBACK(owl_variable_enum_set_fromstring),
692      g_cclosure_user_marshal_INT__STRING);
693
694  g_value_init(&var->val, G_TYPE_INT);
695  owl_variable_set_int(var, default_val);
696
697  var->default_str = owl_variable_get_tostring(var);
698  owl_variable_dict_add_variable(vd, var);
699}
700
701void owl_variable_dict_newvar_enum(owl_vardict *vd, const char *name, int default_val, const char *summary, const char *description, const char *validset) {
702  owl_variable_dict_newvar_enum_full(vd, name, default_val, summary, description,
703                                     validset, NULL, NULL, NULL);
704}
705
706void owl_variable_dict_newvar_other(owl_vardict *vd, const char *name, const char *summary, const char *description, const char *validsettings, bool takes_on_off, GClosure *get_tostring_fn, GClosure *set_fromstring_fn)
707{
708  owl_variable *var = owl_variable_newvar(OWL_VARIABLE_OTHER, name, summary,
709                                          description, validsettings);
710  var->takes_on_off = takes_on_off;
711
712  var->get_tostring_fn = g_closure_ref(get_tostring_fn);
713  g_closure_sink(get_tostring_fn);
714
715  var->set_fromstring_fn = g_closure_ref(set_fromstring_fn);
716  g_closure_sink(set_fromstring_fn);
717
718  var->default_str = owl_variable_get_tostring(var);
719
720  /* Note: this'll overwrite any existing variable of that name, even a C one,
721     but it's consistent with previous behavior and commands. */
722  owl_variable_dict_add_variable(vd, var);
723}
724
725void owl_variable_dict_cleanup(owl_vardict *d)
726{
727  owl_dict_cleanup(d, (void (*)(void *))owl_variable_delete);
728}
729
730CALLER_OWN GPtrArray *owl_variable_dict_get_names(const owl_vardict *d) {
731  return owl_dict_get_keys(d);
732}
733
734void owl_variable_delete(owl_variable *v)
735{
736  g_free(v->name);
737  g_free(v->summary);
738  g_free(v->description);
739  g_free(v->default_str);
740  g_free(v->validsettings);
741  if (v->type != OWL_VARIABLE_OTHER)
742    g_value_unset(&(v->val));
743  g_closure_unref(v->get_tostring_fn);
744  g_closure_unref(v->set_fromstring_fn);
745
746  g_slice_free(owl_variable, v);
747}
748
749
750const char *owl_variable_get_name(const owl_variable *v)
751{
752  return v->name;
753}
754
755const char *owl_variable_get_description(const owl_variable *v) {
756  return v->description;
757}
758
759const char *owl_variable_get_summary(const owl_variable *v) {
760  return v->summary;
761}
762
763const char *owl_variable_get_validsettings(const owl_variable *v) {
764  return v->validsettings;
765}
766
767bool owl_variable_takes_on_off(const owl_variable *v) {
768  return v->takes_on_off;
769}
770
771int owl_variable_get_type(const owl_variable *v)
772{
773  return v->type;
774}
775
776/* returns 0 on success, prints a status msg if msg is true */
777int owl_variable_set_fromstring(owl_variable *v, const char *value, int msg) {
778  char *tostring;
779  GValue values[] = {G_VALUE_INIT, G_VALUE_INIT};
780  GValue return_box = G_VALUE_INIT;
781  int set_successfully;
782
783  g_value_init(&values[0], G_TYPE_POINTER);
784  g_value_set_pointer(&values[0], NULL);
785  g_value_init(&values[1], G_TYPE_STRING);
786  g_value_set_static_string(&values[1], value);
787  g_value_init(&return_box, G_TYPE_INT);
788  g_closure_invoke(v->set_fromstring_fn, &return_box, 2, values, NULL);
789
790  set_successfully = g_value_get_int(&return_box);
791  if (0 != set_successfully) {
792    if (msg) owl_function_error("Unable to set %s (must be %s)", owl_variable_get_name(v),
793                                owl_variable_get_validsettings(v));
794  } else if (msg) {
795    tostring = owl_variable_get_tostring(v);
796    if (tostring) {
797      owl_function_makemsg("%s = '%s'", owl_variable_get_name(v), tostring);
798    } else {
799      owl_function_makemsg("%s = <null>", owl_variable_get_name(v));
800    }
801    g_free(tostring);
802  }
803
804  g_value_unset(&return_box);
805  g_value_unset(&values[1]);
806  g_value_unset(&values[0]);
807  return set_successfully;
808}
809 
810int owl_variable_set_string(owl_variable *v, const char *newval)
811{
812  g_return_val_if_fail(v->type == OWL_VARIABLE_STRING, -1);
813
814  set_string_t cb = (set_string_t) v->set_fn;
815  return cb(v, newval);
816}
817
818int owl_variable_set_int(owl_variable *v, int newval)
819{
820  g_return_val_if_fail(v->type == OWL_VARIABLE_INT, -1);
821
822  set_int_t cb = (set_int_t) v->set_fn;
823  return cb(v, newval);
824}
825
826int owl_variable_set_bool(owl_variable *v, bool newval) {
827  g_return_val_if_fail(v->type == OWL_VARIABLE_BOOL, -1);
828
829  set_bool_t cb = (set_bool_t) v->set_fn;
830  return cb(v, newval);
831}
832
833int owl_variable_set_bool_on(owl_variable *v)
834{
835  if (v->type != OWL_VARIABLE_BOOL) return -1;
836  return owl_variable_set_bool(v, true);
837}
838
839int owl_variable_set_bool_off(owl_variable *v)
840{
841  if (v->type != OWL_VARIABLE_BOOL) return -1;
842  return owl_variable_set_bool(v, false);
843}
844
845CALLER_OWN char *owl_variable_get_tostring(const owl_variable *v)
846{
847  GValue instance = G_VALUE_INIT;
848  GValue tostring_box = G_VALUE_INIT;
849  char *ret = NULL;
850
851  g_value_init(&instance, G_TYPE_POINTER);
852  g_value_set_pointer(&instance, NULL);
853  g_value_init(&tostring_box, G_TYPE_STRING);
854  g_closure_invoke(v->get_tostring_fn, &tostring_box, 1, &instance, NULL);
855
856  ret = g_value_dup_string(&tostring_box);
857
858  g_value_unset(&tostring_box);
859  g_value_unset(&instance);
860  return ret;
861}
862
863const char *owl_variable_get_default_tostring(const owl_variable *v)
864{
865  return v->default_str;
866}
867
868owl_variable *owl_variable_get_var(const owl_vardict *d, const char *name)
869{
870  return owl_dict_find_element(d, name);
871}
872
873const char *owl_variable_get_string(const owl_variable *v)
874{
875  g_return_val_if_fail(v->type == OWL_VARIABLE_STRING, NULL);
876
877  get_string_t cb = (get_string_t) v->get_fn;
878  return cb(v);
879}
880
881int owl_variable_get_int(const owl_variable *v)
882{
883  g_return_val_if_fail(v->type == OWL_VARIABLE_INT, 0);
884
885  get_int_t cb = (get_int_t) v->get_fn;
886  return cb(v);
887}
888
889bool owl_variable_get_bool(const owl_variable *v)
890{
891  g_return_val_if_fail(v->type == OWL_VARIABLE_BOOL, FALSE);
892
893  get_bool_t cb = (get_bool_t) v->get_fn;
894  return cb(v);
895}
896
897void owl_variable_describe(const owl_variable *v, owl_fmtext *fm)
898{
899  const char *default_str = owl_variable_get_default_tostring(v);
900  char *default_buf;
901
902  if (default_str)
903    default_buf = g_strdup_printf("'%s'", default_str);
904  else
905    default_buf = g_strdup("<null>");
906  owl_fmtext_appendf_normal(fm, OWL_TABSTR "%-20s - %s (default: %s)\n",
907                            owl_variable_get_name(v),
908                            owl_variable_get_summary(v), default_buf);
909  g_free(default_buf);
910}
911
912void owl_variable_get_help(const owl_variable *v, owl_fmtext *fm) {
913  char *tostring;
914  const char *default_str;
915
916  owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n");
917  owl_fmtext_append_normal(fm, OWL_TABSTR);
918  owl_fmtext_append_normal(fm, owl_variable_get_name(v));
919  owl_fmtext_append_normal(fm, " - ");
920  owl_fmtext_append_normal(fm, owl_variable_get_summary(v));
921  owl_fmtext_append_normal(fm, "\n\n");
922
923  owl_fmtext_append_normal(fm, "Current:        ");
924  tostring = owl_variable_get_tostring(v);
925  owl_fmtext_append_normal(fm, (tostring ? tostring : "<null>"));
926  g_free(tostring);
927  owl_fmtext_append_normal(fm, "\n\n");
928
929  default_str = owl_variable_get_default_tostring(v);
930  owl_fmtext_append_normal(fm, "Default:        ");
931  owl_fmtext_append_normal(fm, (default_str ? default_str : "<null>"));
932  owl_fmtext_append_normal(fm, "\n\n");
933
934  owl_fmtext_append_normal(fm, "Valid Settings: ");
935  owl_fmtext_append_normal(fm, owl_variable_get_validsettings(v));
936  owl_fmtext_append_normal(fm, "\n\n");
937
938  if (v->description && *v->description) {
939    owl_fmtext_append_normal(fm, "Description:\n");
940    owl_fmtext_append_normal(fm, owl_variable_get_description(v));
941    owl_fmtext_append_normal(fm, "\n\n");
942  }
943}
944
945
946
947
948/**************************************************************************/
949/*********************** GENERAL TYPE-SPECIFIC ****************************/
950/**************************************************************************/
951
952/* default common functions */
953
954const char *owl_variable_string_get_default(const owl_variable *v) {
955  return g_value_get_string(&(v->val));
956}
957
958int owl_variable_int_get_default(const owl_variable *v) {
959  return g_value_get_int(&(v->val));
960}
961
962bool owl_variable_bool_get_default(const owl_variable *v) {
963  return g_value_get_boolean(&(v->val));
964}
965
966/* default functions for booleans */
967
968int owl_variable_bool_validate_default(const owl_variable *v, bool newval) {
969  return (newval == 1) || (newval == 0);
970}
971
972int owl_variable_bool_set_default(owl_variable *v, bool newval) {
973  if (!((validate_bool_t)v->validate_fn)(v, newval))
974    return -1;
975
976  g_value_set_boolean(&(v->val), newval);
977  return(0);
978}
979
980int owl_variable_bool_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) {
981  bool i;
982  if (!strcmp(newval, "on")) {
983    i = true;
984  } else if (!strcmp(newval, "off")) {
985    i = false;
986  } else {
987    return(-1);
988  }
989
990  return owl_variable_set_bool(v, i);
991}
992
993CALLER_OWN char *owl_variable_bool_get_tostring_default(const owl_variable *v, void *dummy)
994{
995  return g_strdup(owl_variable_get_bool(v) ? "on" : "off");
996}
997
998/* default functions for integers */
999
1000int owl_variable_int_validate_default(const owl_variable *v, int newval)
1001{
1002  return (1);
1003}
1004
1005int owl_variable_int_set_default(owl_variable *v, int newval) {
1006  if (!((validate_int_t)v->validate_fn)(v, newval))
1007    return -1;
1008
1009  g_value_set_int(&(v->val), newval);
1010  return(0);
1011}
1012
1013int owl_variable_int_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) {
1014  int i;
1015  char *ep;
1016  i = strtol(newval, &ep, 10);
1017  if (*ep || ep==newval) return(-1);
1018  return owl_variable_set_int(v, i);
1019}
1020
1021CALLER_OWN char *owl_variable_int_get_tostring_default(const owl_variable *v, void *dummy)
1022{
1023  return g_strdup_printf("%d", owl_variable_get_int(v));
1024}
1025
1026/* default functions for enums (a variant of integers) */
1027
1028int owl_variable_enum_validate(const owl_variable *v, int newval) {
1029  char **enums;
1030  int nenums, val;
1031  enums = g_strsplit_set(v->validsettings, ",", 0);
1032  nenums = g_strv_length(enums);
1033  g_strfreev(enums);
1034  val = newval;
1035  if (val < 0 || val >= nenums) {
1036    return(0);
1037  }
1038  return(1);
1039}
1040
1041int owl_variable_enum_set_fromstring(owl_variable *v, const char *newval, void *dummy) {
1042  char **enums;
1043  int i, val=-1;
1044  if (newval == NULL) return(-1);
1045  enums = g_strsplit_set(v->validsettings, ",", 0);
1046  for (i = 0; enums[i] != NULL; i++) {
1047    if (0==strcmp(newval, enums[i])) {
1048      val = i;
1049    }
1050  }
1051  g_strfreev(enums);
1052  if (val == -1) return(-1);
1053  return owl_variable_set_int(v, val);
1054}
1055
1056CALLER_OWN char *owl_variable_enum_get_tostring(const owl_variable *v, void *dummy)
1057{
1058  char **enums;
1059  int nenums, i;
1060  char *tostring;
1061
1062  enums = g_strsplit_set(v->validsettings, ",", 0);
1063  nenums = g_strv_length(enums);
1064  i = owl_variable_get_int(v);
1065  if (i<0 || i>=nenums) {
1066    g_strfreev(enums);
1067    return g_strdup_printf("<invalid:%d>", i);
1068  }
1069  tostring = g_strdup(enums[i]);
1070  g_strfreev(enums);
1071  return tostring;
1072}
1073
1074/* default functions for stringeans */
1075
1076int owl_variable_string_validate_default(const owl_variable *v, const char *newval) {
1077  if (newval == NULL) return(0);
1078  else return (1);
1079}
1080
1081int owl_variable_string_set_default(owl_variable *v, const char *newval) {
1082  if (!((validate_string_t)v->validate_fn)(v, newval))
1083    return -1;
1084
1085  g_value_set_string(&(v->val), newval);
1086  return(0);
1087}
1088
1089int owl_variable_string_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) 
1090{
1091  return owl_variable_set_string(v, newval);
1092}
1093
1094CALLER_OWN char *owl_variable_string_get_tostring_default(const owl_variable *v, void *dummy)
1095{
1096  return g_strdup(owl_variable_get_string(v));
1097}
1098
Note: See TracBrowser for help on using the repository browser.