1 /*
2  *  Tvheadend - property system (part of idnode)
3  *
4  *  Copyright (C) 2013 Andreas Öman
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef __TVH_PROP_H__
21 #define __TVH_PROP_H__
22 
23 #include <stddef.h>
24 
25 #include "htsmsg.h"
26 
27 /*
28  * Property types
29  */
30 typedef enum {
31   PT_NONE,
32   PT_BOOL,
33   PT_STR,
34   PT_INT,
35   PT_U16,
36   PT_U32,
37   PT_S64,
38   PT_S64_ATOMIC,
39   PT_DBL,
40   PT_TIME,
41   PT_LANGSTR,
42   PT_PERM,                // like PT_U32 but with the special save
43 } prop_type_t;
44 
45 /*
46  * Property options
47  */
48 #define PO_NONE      (0<<0)
49 #define PO_RDONLY    (1<<1)  // Property is read-only
50 #define PO_NOSAVE    (1<<2)  // Property is transient (not saved)
51 #define PO_WRONCE    (1<<3)  // Property is write-once (i.e. on creation)
52 #define PO_ADVANCED  (1<<4)  // Property is advanced
53 #define PO_EXPERT    (1<<5)  // Property is for experts
54 #define PO_NOUI      (1<<6)  // Property should not be presented in the user interface
55 #define PO_HIDDEN    (1<<7)  // Property is hidden (by default)
56 #define PO_USERAW    (1<<8)  // Only save the RAW (off) value if it exists
57 #define PO_SORTKEY   (1<<9)  // Sort using key (not display value)
58 #define PO_PASSWORD  (1<<10) // String is a password
59 #define PO_DURATION  (1<<11) // For PT_TIME - differentiate between duration and datetime
60 #define PO_HEXA      (1<<12) // Hexadecimal value
61 #define PO_DATE      (1<<13) // Show date only
62 #define PO_LOCALE    (1<<14) // Call tvh_locale_lang on string
63 #define PO_LORDER    (1<<15) // Manage order in lists
64 #define PO_MULTILINE (1<<16) // Multiline string
65 #define PO_PERSIST   (1<<17) // Persistent value (return back on save)
66 #define PO_DOC       (1<<18) // Use doc callback instead description if exists
67 #define PO_DOC_NLIST (1<<19) // Do not show list in doc
68 #define PO_TRIM      (1<<20) // Trim whitespaces (left & right) on load
69 
70 /*
71  * min/max/step helpers
72  */
73 #define INTEXTRA_RANGE(min, max, step) \
74   ((1<<31)|(((step)&0x7f)<<24)|(((max)&0xfff)<<12)|((min)&0xfff))
75 
76 #define INTEXTRA_IS_RANGE(e) (((e) & (1<<31)) != 0)
77 #define INTEXTRA_IS_SPLIT(e) !INTEXTRA_IS_RANGE(e)
78 #define INTEXTRA_GET_STEP(e) (((e)>>24)&0x7f)
79 #define INTEXTRA_GET_MAX(e)  ((e)&(1<<23)?-(((e)>>12)&0x7ff):(((e)>>12)&0x7ff))
80 #define INTEXTRA_GET_MIN(e)  ((e)&(1<<11)?-((e)&0x7ff):((e)&0x7ff))
81 
82 /*
83  * Property definition
84  */
85 typedef struct property {
86   const char *id;         ///< Property Key
87   const char *name;       ///< Textual description
88   const char *desc;       ///< Verbose description (tooltip)
89   prop_type_t type;       ///< Type
90   uint8_t     islist;     ///< Is a list
91   uint8_t     group;      ///< Visual group ID (like ExtJS FieldSet)
92   size_t      off;        ///< Offset into object
93   uint32_t    opts;       ///< Options
94   uint32_t    intextra;   ///< intsplit: integer/remainder boundary or range: min/max/step
95 
96   /* String based processing */
97   const void *(*get)  (void *ptr);
98   int         (*set)  (void *ptr, const void *v);
99   htsmsg_t   *(*list) (void *ptr, const char *lang);
100   char       *(*rend) (void *ptr, const char *lang);
101                           ///< Provide the rendered value for enum/list
102                           ///< Lists should be CSV. This is used for
103                           ///< sorting and searching in UI API
104 
105   /* Default (for UI) */
106   union {
107     int         i;   // PT_BOOL/PT_INT
108     const char *s;   // PT_STR
109     uint16_t    u16; // PT_U16
110     uint32_t    u32; // PT_U32
111     int64_t     s64; // PT_S64
112     double      d;   // PT_DBL
113     time_t      tm;  // PT_TIME
114     htsmsg_t *(*list)(void); // islist != 0
115   } def;
116 
117   /* Extended options */
118   uint32_t    (*get_opts) (void *ptr);
119 
120   /* Documentation callback */
121   char       *(*doc) ( const struct property *prop, const char *lang );
122 
123   /* Notification callback */
124   void        (*notify)   (void *ptr, const char *lang);
125 
126 } property_t;
127 
128 #define PROP_SBUF_LEN 4096
129 extern char prop_sbuf[PROP_SBUF_LEN];
130 extern const char *prop_sbuf_ptr;
131 extern const char *prop_ptr;
132 
133 const property_t *prop_find(const property_t *p, const char *name);
134 
135 int prop_write_values
136   (void *obj, const property_t *pl, htsmsg_t *m, int optmask, htsmsg_t *updated);
137 
138 void prop_read_values
139   (void *obj, const property_t *pl, htsmsg_t *m, htsmsg_t *list,
140    int optmask, const char *lang);
141 
142 void prop_serialize
143   (void *obj, const property_t *pl, htsmsg_t *m, htsmsg_t *list,
144    int optmask, const char *lang);
145 
prop_intsplit_from_str(const char * s,int64_t intsplit)146 static inline int64_t prop_intsplit_from_str(const char *s, int64_t intsplit)
147 {
148   int64_t s64 = (int64_t)atol(s) * intsplit;
149   if ((s = strchr(s, '.')) != NULL)
150     s64 += (atol(s + 1) % intsplit);
151   return s64;
152 }
153 
154 char *
155 prop_md_doc(const char **md, const char *lang);
156 
157 #define PROP_DOC(name) \
158 extern const char *tvh_doc_##name##_property[]; \
159 static char * \
160 prop_doc_##name(const struct property *p, const char *lang) \
161 { return prop_md_doc(tvh_doc_##name##_property, lang); }
162 
163 
164 #endif /* __TVH_PROP_H__ */
165 
166 /******************************************************************************
167  * Editor Configuration
168  *
169  * vim:sts=2:ts=2:sw=2:et
170  *****************************************************************************/
171