initial git commit
[paraslash.git] / slider.c
1 /*
2 * Copyright (C) 2004-2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file slider.c libzmw-based stream creator for paraslash */
20
21 #include "para.h"
22 #include "zmw/zmw.h"
23 #include "math.h"
24 #include "string.h"
25
26 #define WIDTH 300
27 #define SLIDER_RATIO 6
28 #define VAL_2_SL_VAL(v) (v) * (SLIDER_RATIO - 1.0) / SLIDER_RATIO
29 #define SL_VAL_2_VAL(v) (v) * SLIDER_RATIO / (SLIDER_RATIO - 1.0)
30 #define VAL_2_SCORE(v) (v) > 0.5? 1 / (1.03 - (v)) / (1.03 - (v)) :\
31 - 1 / ((v) + 0.03) / ((v) + 0.03)
32
33 #define RGB(R,G,B) (((R)<<12) + ((G)<<6) + (B))
34
35 #define EPSILON 0.001
36 #define LASTPLAYED_FORMULA(v) \
37 1 / (pow((v), 10) + EPSILON * EPSILON) - 1 / (1 + EPSILON) + EPSILON
38
39 #define NUMPLAYED_FORMULA(v)\
40 10 * (v) + (v) / (1 - (v) * (1 - EPSILON))
41
42
43 static int argc;
44 static char **argv;
45 char *streamname = NULL;
46 static Zmw_Float_0_1 *slider_vals, lastplayed_val, numplayed_val;
47
48 #if 0
49 static void boxed_text(const char *text, int i)
50 {
51 ZMW(zmw_decorator(Zmw_Decorator_Border_Embossed))
52 {
53 if (i == 2)
54 {
55 zmw_horizontal_expand(Zmw_False) ;
56 zmw_vertical_expand(Zmw_False) ;
57 }
58 zmw_label(text) ;
59 }
60 }
61 #endif
62
63 void para_log(int ll, char* fmt,...) /* no logging */
64 {
65 }
66
67 static void rst(void)
68 {
69 int i;
70 for (i = 1; argv[i]; i++)
71 slider_vals[i] = VAL_2_SL_VAL(.5);
72 if (streamname)
73 free(streamname);
74 streamname = para_strdup("slider");
75 //printf("rst: \n");
76 }
77
78 static char *get_value_filename(void)
79 {
80 char *ret, *home =para_homedir();
81 ret = make_message("%s/.paraslash/slide.values", home);
82 free(home);
83 return ret;
84 }
85
86 static void load(void)
87 {
88 char *tmp;
89 char att[999];
90 FILE *f;
91 int i;
92 Zmw_Float_0_1 val;
93
94 tmp = get_value_filename();
95 f = fopen(tmp, "r");
96 free(tmp);
97 if (!f)
98 return;
99 while (fscanf(f, "%900s %g", att, &val) == 2) {
100 if (!strcmp(att, "lastplayed")) {
101 lastplayed_val = val;
102 continue;
103 }
104 if (!strcmp(att, "numplayed")) {
105 numplayed_val = val;
106 continue;
107 }
108 for (i = 1; argv[i]; i++)
109 if (!strcmp(argv[i], att)) {
110 slider_vals[i] = val;
111 break;
112 }
113
114 }
115 fclose(f);
116 }
117
118 static void save(void)
119 {
120 char *filename;
121 FILE *f;
122 int i;
123
124 filename = get_value_filename();
125 f = fopen(filename, "w");
126 free(filename);
127 if (!f)
128 return;
129 fprintf(f, "lastplayed %g\n", lastplayed_val);
130 fprintf(f, "numplayed %g\n", numplayed_val);
131 for (i = 1; argv[i]; i++)
132 fprintf(f, "%s %g\n", argv[i], slider_vals[i]);
133 fclose(f);
134 }
135
136
137 static void print_score(FILE *fd)
138 {
139 int i;
140
141 for (i = 1; argv[i]; i++) {
142 if (SL_VAL_2_VAL(slider_vals[i]) > .99)
143 fprintf(fd, "deny: IS_N_SET(%s)\n", argv[i]);
144 if (slider_vals[i] < .01)
145 fprintf(fd, "deny: IS_SET(%s)\n", argv[i]);
146 }
147 fprintf(fd, "score: 0 ");
148 for (i = 1; argv[i]; i++) {
149 if (slider_vals[i] < .01)
150 continue;
151 fprintf(fd, "+ %i * IS_SET(%s) ",
152 (int) (VAL_2_SCORE(SL_VAL_2_VAL(slider_vals[i]))),
153 argv[i]
154 );
155 }
156 fprintf(fd, " + round((LASTPLAYED()/1440 - 1000 / (LASTPLAYED()/1440 + 1)) / %f"
157 " - %f * NUMPLAYED(), 2)\n",
158 LASTPLAYED_FORMULA(SL_VAL_2_VAL(lastplayed_val)),
159 NUMPLAYED_FORMULA(SL_VAL_2_VAL(numplayed_val)));
160 }
161
162 static void stradd(void)
163 {
164 FILE *pipe;
165 char *cmd = make_message("para_client stradd %s", streamname);
166
167 pipe = popen(cmd, "w");
168 free(cmd);
169 if (!pipe)
170 return;
171 print_score(pipe);
172 pclose(pipe);
173 }
174
175 static void b_save(void)
176 {
177 stradd();
178 save();
179 }
180
181 static void go(void)
182 {
183 stradd();
184 system("para_client cs slider");
185 system("para_client stop");
186 system("para_client play");
187 }
188
189 static void ok()
190 {
191 stradd();
192 system("para_client cs slider");
193 system("para_client play");
194 save();
195 zmw_main_quit(EXIT_SUCCESS);
196 }
197
198 static void make_buttons(void)
199 {
200 /* Add a frame around the box */
201 ZMW(zmw_decorator(Zmw_Decorator_Border_Embossed)) {
202 /* the buttons */
203 ZMW(zmw_hbox()) {
204 zmw_button("save");
205 if (zmw_activated())
206 b_save();
207 zmw_button("ok");
208 if (zmw_activated())
209 ok();
210 zmw_button("go!");
211 if (zmw_activated())
212 go();
213 zmw_button("rst");
214 if (zmw_activated())
215 rst();
216 // zmw_x(WIDTH);
217 zmw_button("abort");
218 if (zmw_activated())
219 zmw_main_quit(EXIT_FAILURE);
220 }
221 }
222 }
223 static void make_stream_input_field(void)
224 {
225 static char *text1 = NULL;
226 static int cursor_pos = 1; // Must be static
227 static int text1_length ;
228
229 ZMW(zmw_vbox()) {
230 if ( text1 == NULL ) {
231 // The initial value must be malloced
232 text1 = strdup("slider") ;
233 text1_length = strlen(text1) ;
234 }
235 ZMW(zmw_fixed()) {
236 zmw_y(6);
237 zmw_x(0);
238 zmw_label("stream: ");
239 zmw_color(Zmw_Color_Foreground, RGB(63, 63, 63));
240 zmw_y(0);
241 zmw_x(100);
242 zmw_width(WIDTH);
243 zmw_entry_with_cursor(&streamname, &cursor_pos);
244 if (zmw_changed())
245 text1_length = strlen(text1);
246 }
247 }
248 }
249
250 /* the sliders, one for each member in argv */
251 static void make_sliders(void)
252 {
253 int i;
254 char *txt;
255
256 zmw_height(ZMW_VALUE_UNDEFINED);
257 // zmw_horizontal_expand(Zmw_True);
258 ZMW(zmw_hbox()) {
259 ZMW(zmw_vbox()) {
260 zmw_color(Zmw_Color_Foreground, RGB(0, 63, 63)); /* font */
261 txt = make_message("lp: (%i%%)",
262 (int)(.5 + 100 * SL_VAL_2_VAL(lastplayed_val)));
263 zmw_label(txt);
264 free(txt);
265 txt = make_message("np: (%i%%)",
266 (int)(.5 + 100 * SL_VAL_2_VAL(numplayed_val)));
267 zmw_label(txt);
268 free(txt);
269 zmw_color(Zmw_Color_Foreground, RGB(63, 63, 0)); /* font */
270 for (i = 1; argv[i]; i++) {
271 txt = make_message("%s: (%i%%)", argv[i],
272 (int)(.5 + 100 * SL_VAL_2_VAL(slider_vals[i])));
273 zmw_label(txt);
274 free(txt);
275 }
276 }
277 ZMW(zmw_vbox()) {
278 zmw_width(200) ;
279 zmw_hscrollbar(&lastplayed_val, 1.0 / SLIDER_RATIO);
280 zmw_hscrollbar(&numplayed_val, 1.0 / SLIDER_RATIO);
281 for (i = 1; argv[i]; i++) {
282 zmw_hscrollbar(&slider_vals[i],
283 1.0 / SLIDER_RATIO);
284 }
285 }
286 }
287 }
288
289 static void anchor(void)
290 {
291
292 zmw_font_family("terminal");
293 zmw_font_size(14);
294 zmw_rgb(0.1, 0.1, 0.1);
295 zmw_color(Zmw_Color_Foreground, RGB(63, 63, 0)); /* font */
296 zmw_color(Zmw_Color_Background_Pushed, RGB(0, 10, 4)); /* slider bg */
297 zmw_color(Zmw_Color_Background_Poped, RGB(0, 42, 42)); /* button bg */
298 zmw_color(Zmw_Color_Border_Light, RGB(0, 7, 63)); /* slider/button border */
299
300 ZMW(zmw_window("para_slider")) {
301 ZMW(zmw_vbox()) {
302 make_buttons();
303 make_stream_input_field();
304 zmw_color(Zmw_Color_Foreground, RGB(0, 63, 63)); /* font */
305 // make_special_sliders();
306 zmw_color(Zmw_Color_Foreground, RGB(63, 63, 0)); /* font */
307 make_sliders();
308 }
309 }
310 }
311
312 static void get_atts(void)
313 {
314 char buf[256];
315 FILE *p = popen("para_client laa", "r");
316 argv = para_malloc(255 * sizeof(char *)); /* FIXME */
317
318 argv[0] = para_strdup("all_attributes");
319 argc = 1;
320 if (!p)
321 goto err_out;
322 while (fgets(buf, 255, p)) {
323 chop(buf);
324 argv[argc] = para_strdup(buf);
325 argc++;
326 }
327 pclose(p);
328 argv[argc] = NULL;
329 argc--;
330 if (argc > 1)
331 return; /* success */
332 err_out:
333 if (argc > 1)
334 for (argc = 1; argv[argc]; argc++)
335 free(argv[argc]);
336 free(argv);
337 argc = 1;
338 }
339
340 int main(int the_argc, char *the_argv[])
341 {
342 argc = the_argc;
343 argv = the_argv;
344 if (argc < 2) {
345 get_atts();
346 if (argc < 2)
347 return -1;
348 }
349 slider_vals = para_malloc((argc + 1) * sizeof(int*));
350 rst();
351 load();
352 // printf("argc: %d\n", argc);
353 // for (i = 1; argv[i]; i++)
354 // printf("argv[%d]: %s\n", i, argv[i]);
355 zmw_init(&argc, &argv);
356 zmw_main(anchor);
357 return 0;
358 }