source: variable.c @ 4733d68

Last change on this file since 4733d68 was 1210b54, checked in by Jason Gross <jgross@mit.edu>, 8 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, 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
767int owl_variable_get_type(const owl_variable *v)
768{
769  return v->type;
770}
771
772/* returns 0 on success, prints a status msg if msg is true */
773int owl_variable_set_fromstring(owl_variable *v, const char *value, int msg) {
774  char *tostring;
775  GValue values[] = {G_VALUE_INIT, G_VALUE_INIT};
776  GValue return_box = G_VALUE_INIT;
777  int set_successfully;
778
779  g_value_init(&values[0], G_TYPE_POINTER);
780  g_value_set_pointer(&values[0], NULL);
781  g_value_init(&values[1], G_TYPE_STRING);
782  g_value_set_static_string(&values[1], value);
783  g_value_init(&return_box, G_TYPE_INT);
784  g_closure_invoke(v->set_fromstring_fn, &return_box, 2, values, NULL);
785
786  set_successfully = g_value_get_int(&return_box);
787  if (0 != set_successfully) {
788    if (msg) owl_function_error("Unable to set %s (must be %s)", owl_variable_get_name(v),
789                                owl_variable_get_validsettings(v));
790  } else if (msg) {
791    tostring = owl_variable_get_tostring(v);
792    if (tostring) {
793      owl_function_makemsg("%s = '%s'", owl_variable_get_name(v), tostring);
794    } else {
795      owl_function_makemsg("%s = <null>", owl_variable_get_name(v));
796    }
797    g_free(tostring);
798  }
799
800  g_value_unset(&return_box);
801  g_value_unset(&values[1]);
802  g_value_unset(&values[0]);
803  return set_successfully;
804}
805 
806int owl_variable_set_string(owl_variable *v, const char *newval)
807{
808  g_return_val_if_fail(v->type == OWL_VARIABLE_STRING, -1);
809
810  set_string_t cb = (set_string_t) v->set_fn;
811  return cb(v, newval);
812}
813
814int owl_variable_set_int(owl_variable *v, int newval)
815{
816  g_return_val_if_fail(v->type == OWL_VARIABLE_INT, -1);
817
818  set_int_t cb = (set_int_t) v->set_fn;
819  return cb(v, newval);
820}
821
822int owl_variable_set_bool(owl_variable *v, bool newval) {
823  g_return_val_if_fail(v->type == OWL_VARIABLE_BOOL, -1);
824
825  set_bool_t cb = (set_bool_t) v->set_fn;
826  return cb(v, newval);
827}
828
829int owl_variable_set_bool_on(owl_variable *v)
830{
831  if (v->type != OWL_VARIABLE_BOOL) return -1;
832  return owl_variable_set_bool(v, true);
833}
834
835int owl_variable_set_bool_off(owl_variable *v)
836{
837  if (v->type != OWL_VARIABLE_BOOL) return -1;
838  return owl_variable_set_bool(v, false);
839}
840
841CALLER_OWN char *owl_variable_get_tostring(const owl_variable *v)
842{
843  GValue instance = G_VALUE_INIT;
844  GValue tostring_box = G_VALUE_INIT;
845  char *ret = NULL;
846
847  g_value_init(&instance, G_TYPE_POINTER);
848  g_value_set_pointer(&instance, NULL);
849  g_value_init(&tostring_box, G_TYPE_STRING);
850  g_closure_invoke(v->get_tostring_fn, &tostring_box, 1, &instance, NULL);
851
852  ret = g_value_dup_string(&tostring_box);
853
854  g_value_unset(&tostring_box);
855  g_value_unset(&instance);
856  return ret;
857}
858
859const char *owl_variable_get_default_tostring(const owl_variable *v)
860{
861  return v->default_str;
862}
863
864owl_variable *owl_variable_get_var(const owl_vardict *d, const char *name)
865{
866  return owl_dict_find_element(d, name);
867}
868
869const char *owl_variable_get_string(const owl_variable *v)
870{
871  g_return_val_if_fail(v->type == OWL_VARIABLE_STRING, NULL);
872
873  get_string_t cb = (get_string_t) v->get_fn;
874  return cb(v);
875}
876
877int owl_variable_get_int(const owl_variable *v)
878{
879  g_return_val_if_fail(v->type == OWL_VARIABLE_INT, 0);
880
881  get_int_t cb = (get_int_t) v->get_fn;
882  return cb(v);
883}
884
885bool owl_variable_get_bool(const owl_variable *v)
886{
887  g_return_val_if_fail(v->type == OWL_VARIABLE_BOOL, FALSE);
888
889  get_bool_t cb = (get_bool_t) v->get_fn;
890  return cb(v);
891}
892
893void owl_variable_describe(const owl_variable *v, owl_fmtext *fm)
894{
895  const char *default_str = owl_variable_get_default_tostring(v);
896  char *default_buf;
897
898  if (default_str)
899    default_buf = g_strdup_printf("'%s'", default_str);
900  else
901    default_buf = g_strdup("<null>");
902  owl_fmtext_appendf_normal(fm, OWL_TABSTR "%-20s - %s (default: %s)\n",
903                            owl_variable_get_name(v),
904                            owl_variable_get_summary(v), default_buf);
905  g_free(default_buf);
906}
907
908void owl_variable_get_help(const owl_variable *v, owl_fmtext *fm) {
909  char *tostring;
910  const char *default_str;
911
912  owl_fmtext_append_bold(fm, "OWL VARIABLE\n\n");
913  owl_fmtext_append_normal(fm, OWL_TABSTR);
914  owl_fmtext_append_normal(fm, owl_variable_get_name(v));
915  owl_fmtext_append_normal(fm, " - ");
916  owl_fmtext_append_normal(fm, owl_variable_get_summary(v));
917  owl_fmtext_append_normal(fm, "\n\n");
918
919  owl_fmtext_append_normal(fm, "Current:        ");
920  tostring = owl_variable_get_tostring(v);
921  owl_fmtext_append_normal(fm, (tostring ? tostring : "<null>"));
922  g_free(tostring);
923  owl_fmtext_append_normal(fm, "\n\n");
924
925  default_str = owl_variable_get_default_tostring(v);
926  owl_fmtext_append_normal(fm, "Default:        ");
927  owl_fmtext_append_normal(fm, (default_str ? default_str : "<null>"));
928  owl_fmtext_append_normal(fm, "\n\n");
929
930  owl_fmtext_append_normal(fm, "Valid Settings: ");
931  owl_fmtext_append_normal(fm, owl_variable_get_validsettings(v));
932  owl_fmtext_append_normal(fm, "\n\n");
933
934  if (v->description && *v->description) {
935    owl_fmtext_append_normal(fm, "Description:\n");
936    owl_fmtext_append_normal(fm, owl_variable_get_description(v));
937    owl_fmtext_append_normal(fm, "\n\n");
938  }
939}
940
941
942
943
944/**************************************************************************/
945/*********************** GENERAL TYPE-SPECIFIC ****************************/
946/**************************************************************************/
947
948/* default common functions */
949
950const char *owl_variable_string_get_default(const owl_variable *v) {
951  return g_value_get_string(&(v->val));
952}
953
954int owl_variable_int_get_default(const owl_variable *v) {
955  return g_value_get_int(&(v->val));
956}
957
958bool owl_variable_bool_get_default(const owl_variable *v) {
959  return g_value_get_boolean(&(v->val));
960}
961
962/* default functions for booleans */
963
964int owl_variable_bool_validate_default(const owl_variable *v, bool newval) {
965  return (newval == 1) || (newval == 0);
966}
967
968int owl_variable_bool_set_default(owl_variable *v, bool newval) {
969  if (!((validate_bool_t)v->validate_fn)(v, newval))
970    return -1;
971
972  g_value_set_boolean(&(v->val), newval);
973  return(0);
974}
975
976int owl_variable_bool_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) {
977  bool i;
978  if (!strcmp(newval, "on")) {
979    i = true;
980  } else if (!strcmp(newval, "off")) {
981    i = false;
982  } else {
983    return(-1);
984  }
985
986  return owl_variable_set_bool(v, i);
987}
988
989CALLER_OWN char *owl_variable_bool_get_tostring_default(const owl_variable *v, void *dummy)
990{
991  bool val = owl_variable_get_bool(v);
992  if (val == 0) {
993    return g_strdup("off");
994  } else if (val == 1) {
995    return g_strdup("on");
996  } else {
997    return g_strdup("<invalid>");
998  }
999}
1000
1001/* default functions for integers */
1002
1003int owl_variable_int_validate_default(const owl_variable *v, int newval)
1004{
1005  return (1);
1006}
1007
1008int owl_variable_int_set_default(owl_variable *v, int newval) {
1009  if (!((validate_int_t)v->validate_fn)(v, newval))
1010    return -1;
1011
1012  g_value_set_int(&(v->val), newval);
1013  return(0);
1014}
1015
1016int owl_variable_int_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) {
1017  int i;
1018  char *ep;
1019  i = strtol(newval, &ep, 10);
1020  if (*ep || ep==newval) return(-1);
1021  return owl_variable_set_int(v, i);
1022}
1023
1024CALLER_OWN char *owl_variable_int_get_tostring_default(const owl_variable *v, void *dummy)
1025{
1026  return g_strdup_printf("%d", owl_variable_get_int(v));
1027}
1028
1029/* default functions for enums (a variant of integers) */
1030
1031int owl_variable_enum_validate(const owl_variable *v, int newval) {
1032  char **enums;
1033  int nenums, val;
1034  enums = g_strsplit_set(v->validsettings, ",", 0);
1035  nenums = g_strv_length(enums);
1036  g_strfreev(enums);
1037  val = newval;
1038  if (val < 0 || val >= nenums) {
1039    return(0);
1040  }
1041  return(1);
1042}
1043
1044int owl_variable_enum_set_fromstring(owl_variable *v, const char *newval, void *dummy) {
1045  char **enums;
1046  int i, val=-1;
1047  if (newval == NULL) return(-1);
1048  enums = g_strsplit_set(v->validsettings, ",", 0);
1049  for (i = 0; enums[i] != NULL; i++) {
1050    if (0==strcmp(newval, enums[i])) {
1051      val = i;
1052    }
1053  }
1054  g_strfreev(enums);
1055  if (val == -1) return(-1);
1056  return owl_variable_set_int(v, val);
1057}
1058
1059CALLER_OWN char *owl_variable_enum_get_tostring(const owl_variable *v, void *dummy)
1060{
1061  char **enums;
1062  int nenums, i;
1063  char *tostring;
1064
1065  enums = g_strsplit_set(v->validsettings, ",", 0);
1066  nenums = g_strv_length(enums);
1067  i = owl_variable_get_int(v);
1068  if (i<0 || i>=nenums) {
1069    g_strfreev(enums);
1070    return g_strdup_printf("<invalid:%d>", i);
1071  }
1072  tostring = g_strdup(enums[i]);
1073  g_strfreev(enums);
1074  return tostring;
1075}
1076
1077/* default functions for stringeans */
1078
1079int owl_variable_string_validate_default(const owl_variable *v, const char *newval) {
1080  if (newval == NULL) return(0);
1081  else return (1);
1082}
1083
1084int owl_variable_string_set_default(owl_variable *v, const char *newval) {
1085  if (!((validate_string_t)v->validate_fn)(v, newval))
1086    return -1;
1087
1088  g_value_set_string(&(v->val), newval);
1089  return(0);
1090}
1091
1092int owl_variable_string_set_fromstring_default(owl_variable *v, const char *newval, void *dummy) 
1093{
1094  return owl_variable_set_string(v, newval);
1095}
1096
1097CALLER_OWN char *owl_variable_string_get_tostring_default(const owl_variable *v, void *dummy)
1098{
1099  return g_strdup(owl_variable_get_string(v));
1100}
1101
Note: See TracBrowser for help on using the repository browser.