1 /*
2 *
3 * Author: Giacomo Lozito <james@develia.org>, (C) 2005-2007
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
18 *
19 */
20 
21 #include "aosd_cfg.h"
22 #include "aosd_style.h"
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include <libaudcore/audstrings.h>
29 #include <libaudcore/runtime.h>
30 
str_to_color(const char * str)31 static aosd_color_t str_to_color (const char * str)
32 {
33   /* color strings are in format "x,x,x,x", where x are numbers
34      that represent respectively red, green, blue and alpha values (0-65535) */
35   aosd_color_t color = {0, 0, 0, 65535};
36   sscanf (str, "%d,%d,%d,%d", & color.red, & color.green, & color.blue, & color.alpha);
37   return color;
38 }
39 
color_to_str(const aosd_color_t & color)40 static StringBuf color_to_str (const aosd_color_t & color)
41 {
42   /* color strings are in format "x,x,x,x", where x are numbers
43      that represent respectively red, green, blue and alpha values (0-65535) */
44   return str_printf ("%d,%d,%d,%d", color.red, color.green, color.blue, color.alpha);
45 }
46 
47 static const char * const aosd_defaults[] = {
48  "position_placement", aud::numeric_string<AOSD_POSITION_PLACEMENT_TOPLEFT>::str,
49  "position_offset_x", "0",
50  "position_offset_y", "0",
51  "position_maxsize_width", "0",
52  "position_multimon_id", "-1",
53  "animation_timing_display", "3000",
54  "animation_timing_fadein", "300",
55  "animation_timing_fadeout", "300",
56  "text_fonts_name_0", "Sans 26",
57  "text_fonts_color_0", "65535,65535,65535,65535",
58  "text_fonts_draw_shadow_0", "TRUE",
59  "text_fonts_shadow_color_0", "0,0,0,32767",
60  "decoration_code", "0",
61  "decoration_color_0", "0,0,65535,32767",
62  "decoration_color_1", "65535,65535,65535,65535",
63  "trigger_enabled", "1,0,0,0",
64  "transparency_mode", "0",
65  nullptr};
66 
aosd_cfg_load(aosd_cfg_t & cfg)67 void aosd_cfg_load (aosd_cfg_t & cfg)
68 {
69   aud_config_set_defaults ("aosd", aosd_defaults);
70 
71   /* position */
72   cfg.position.placement = aud_get_int ("aosd", "position_placement");
73   cfg.position.offset_x = aud_get_int ("aosd", "position_offset_x");
74   cfg.position.offset_y = aud_get_int ("aosd", "position_offset_y");
75   cfg.position.maxsize_width = aud_get_int ("aosd", "position_maxsize_width");
76   cfg.position.multimon_id = aud_get_int ("aosd", "position_multimon_id");
77 
78   /* animation */
79   cfg.animation.timing_display = aud_get_int ("aosd", "animation_timing_display");
80   cfg.animation.timing_fadein = aud_get_int ("aosd", "animation_timing_fadein");
81   cfg.animation.timing_fadeout = aud_get_int ("aosd", "animation_timing_fadeout");
82 
83   /* text */
84   for (int i = 0; i < AOSD_TEXT_FONTS_NUM; i ++)
85   {
86     char key_str[32];
87 
88     snprintf (key_str, sizeof key_str, "text_fonts_name_%i" , i );
89     cfg.text.fonts_name[i] = aud_get_str ("aosd", key_str);
90 
91     snprintf (key_str, sizeof key_str, "text_fonts_color_%i", i);
92     cfg.text.fonts_color[i] = str_to_color (aud_get_str ("aosd", key_str));
93 
94     snprintf (key_str, sizeof key_str, "text_fonts_draw_shadow_%i", i);
95     cfg.text.fonts_draw_shadow[i] = aud_get_bool ("aosd", key_str);
96 
97     snprintf (key_str, sizeof key_str, "text_fonts_shadow_color_%i", i);
98     cfg.text.fonts_shadow_color[i] = str_to_color (aud_get_str ("aosd", key_str));
99   }
100 
101   /* decoration */
102   cfg.decoration.code = aud_get_int ("aosd", "decoration_code");
103 
104   /* decoration - colors */
105   for (int i = 0; i < AOSD_DECO_STYLE_MAX_COLORS; i ++)
106   {
107     char key_str[32];
108     snprintf (key_str, sizeof key_str, "decoration_color_%i", i);
109     cfg.decoration.colors[i] = str_to_color (aud_get_str ("aosd", key_str));
110   }
111 
112   /* trigger */
113   String trig_enabled_str = aud_get_str ("aosd", "trigger_enabled");
114   str_to_int_array (trig_enabled_str, cfg.trigger.enabled,
115    aud::n_elems (cfg.trigger.enabled));
116 
117   /* miscellanous */
118   cfg.misc.transparency_mode = aud_get_int ("aosd", "transparency_mode");
119 }
120 
121 
aosd_cfg_save(const aosd_cfg_t & cfg)122 void aosd_cfg_save (const aosd_cfg_t & cfg)
123 {
124   /* position */
125   aud_set_int ("aosd", "position_placement", cfg.position.placement);
126   aud_set_int ("aosd", "position_offset_x", cfg.position.offset_x);
127   aud_set_int ("aosd", "position_offset_y", cfg.position.offset_y);
128   aud_set_int ("aosd", "position_maxsize_width", cfg.position.maxsize_width);
129   aud_set_int ("aosd", "position_multimon_id", cfg.position.multimon_id);
130 
131   /* animation */
132   aud_set_int ("aosd", "animation_timing_display", cfg.animation.timing_display);
133   aud_set_int ("aosd", "animation_timing_fadein", cfg.animation.timing_fadein);
134   aud_set_int ("aosd", "animation_timing_fadeout", cfg.animation.timing_fadeout);
135 
136   /* text */
137   for (int i = 0; i < AOSD_TEXT_FONTS_NUM; i ++)
138   {
139     char key_str[32];
140 
141     snprintf (key_str, sizeof key_str, "text_fonts_name_%i", i);
142     aud_set_str ("aosd", key_str, cfg.text.fonts_name[i]);
143 
144     snprintf (key_str, sizeof key_str, "text_fonts_color_%i", i);
145     aud_set_str ("aosd", key_str, color_to_str (cfg.text.fonts_color[i]));
146 
147     snprintf (key_str, sizeof key_str, "text_fonts_draw_shadow_%i", i);
148     aud_set_bool ("aosd", key_str, cfg.text.fonts_draw_shadow[i]);
149 
150     snprintf (key_str, sizeof key_str, "text_fonts_shadow_color_%i", i);
151     aud_set_str ("aosd", key_str, color_to_str (cfg.text.fonts_shadow_color[i]));
152   }
153 
154   /* decoration */
155   aud_set_int ("aosd" ,
156     "decoration_code" , cfg.decoration.code );
157 
158   /* decoration - colors */
159   for (int i = 0; i < AOSD_DECO_STYLE_MAX_COLORS; i ++)
160   {
161     char key_str[32];
162     snprintf (key_str, sizeof key_str, "decoration_color_%i", i);
163     aud_set_str ("aosd", key_str, color_to_str (cfg.decoration.colors[i]));
164   }
165 
166   /* trigger */
167   StringBuf trig_enabled_str = int_array_to_str (cfg.trigger.enabled,
168    aud::n_elems (cfg.trigger.enabled));
169   aud_set_str ("aosd", "trigger_enabled", trig_enabled_str);
170 
171   /* miscellaneous */
172   aud_set_int ("aosd", "transparency_mode", cfg.misc.transparency_mode);
173 }
174