2 * Copyright (C) 2004-2006 Andre Noll <maan@systemlinux.org>
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.
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.
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.
19 /** \file slider.c libzmw-based stream creator for paraslash */
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)
33 #define RGB(R,G,B) (((R)<<12) + ((G)<<6) + (B))
36 #define LASTPLAYED_FORMULA(v) \
37 1 / (pow((v), 10) + EPSILON * EPSILON) - 1 / (1 + EPSILON) + EPSILON
39 #define NUMPLAYED_FORMULA(v)\
40 10 * (v) + (v) / (1 - (v) * (1 - EPSILON))
44 char *streamname = NULL;
45 static Zmw_Float_0_1 *slider_vals, lastplayed_val, numplayed_val;
47 void para_log(int ll, const char* fmt,...) /* no logging */
54 for (i = 1; argv[i]; i++)
55 slider_vals[i] = VAL_2_SL_VAL(.5);
58 streamname = para_strdup("slider");
62 static char *get_value_filename(void)
64 char *ret, *home =para_homedir();
65 ret = make_message("%s/.paraslash/slide.values", home);
70 static void load(void)
78 tmp = get_value_filename();
83 while (fscanf(f, "%900s %g", att, &val) == 2) {
84 if (!strcmp(att, "lastplayed")) {
88 if (!strcmp(att, "numplayed")) {
92 for (i = 1; argv[i]; i++)
93 if (!strcmp(argv[i], att)) {
102 static void save(void)
108 filename = get_value_filename();
109 f = fopen(filename, "w");
113 fprintf(f, "lastplayed %g\n", lastplayed_val);
114 fprintf(f, "numplayed %g\n", numplayed_val);
115 for (i = 1; argv[i]; i++)
116 fprintf(f, "%s %g\n", argv[i], slider_vals[i]);
121 static void print_score(FILE *fd)
125 for (i = 1; argv[i]; i++) {
126 if (SL_VAL_2_VAL(slider_vals[i]) > .99)
127 fprintf(fd, "deny: IS_N_SET(%s)\n", argv[i]);
128 if (slider_vals[i] < .01)
129 fprintf(fd, "deny: IS_SET(%s)\n", argv[i]);
131 fprintf(fd, "score: 0 ");
132 for (i = 1; argv[i]; i++) {
133 if (slider_vals[i] < .01)
135 fprintf(fd, "+ %i * IS_SET(%s) ",
136 (int) (VAL_2_SCORE(SL_VAL_2_VAL(slider_vals[i]))),
140 fprintf(fd, " + round((LASTPLAYED()/1440 - 1000 / (LASTPLAYED()/1440 + 1)) / %f"
141 " - %f * NUMPLAYED(), 2)\n",
142 LASTPLAYED_FORMULA(SL_VAL_2_VAL(lastplayed_val)),
143 NUMPLAYED_FORMULA(SL_VAL_2_VAL(numplayed_val)));
146 static void stradd(void)
149 char *cmd = make_message("para_client stradd %s", streamname);
151 pipe = popen(cmd, "w");
159 static void b_save(void)
168 system("para_client cs slider");
169 system("para_client stop");
170 system("para_client play");
176 system("para_client cs slider");
177 system("para_client play");
179 zmw_main_quit(EXIT_SUCCESS);
182 static void make_buttons(void)
184 /* Add a frame around the box */
185 ZMW(zmw_decorator(Zmw_Decorator_Border_Embossed)) {
203 zmw_main_quit(EXIT_FAILURE);
207 static void make_stream_input_field(void)
209 static char *text1 = NULL;
210 static int cursor_pos = 1; // Must be static
211 static int text1_length ;
214 if ( text1 == NULL ) {
215 // The initial value must be malloced
216 text1 = strdup("slider") ;
217 text1_length = strlen(text1) ;
222 zmw_label("stream: ");
223 zmw_color(Zmw_Color_Foreground, RGB(63, 63, 63));
227 zmw_entry_with_cursor(&streamname, &cursor_pos);
229 text1_length = strlen(text1);
234 /* the sliders, one for each member in argv */
235 static void make_sliders(void)
240 zmw_height(ZMW_VALUE_UNDEFINED);
241 // zmw_horizontal_expand(Zmw_True);
244 zmw_color(Zmw_Color_Foreground, RGB(0, 63, 63)); /* font */
245 txt = make_message("lp: (%i%%)",
246 (int)(.5 + 100 * SL_VAL_2_VAL(lastplayed_val)));
249 txt = make_message("np: (%i%%)",
250 (int)(.5 + 100 * SL_VAL_2_VAL(numplayed_val)));
253 zmw_color(Zmw_Color_Foreground, RGB(63, 63, 0)); /* font */
254 for (i = 1; argv[i]; i++) {
255 txt = make_message("%s: (%i%%)", argv[i],
256 (int)(.5 + 100 * SL_VAL_2_VAL(slider_vals[i])));
263 zmw_hscrollbar(&lastplayed_val, 1.0 / SLIDER_RATIO);
264 zmw_hscrollbar(&numplayed_val, 1.0 / SLIDER_RATIO);
265 for (i = 1; argv[i]; i++) {
266 zmw_hscrollbar(&slider_vals[i],
273 static void anchor(void)
276 zmw_font_family("terminal");
278 zmw_rgb(0.1, 0.1, 0.1);
279 zmw_color(Zmw_Color_Foreground, RGB(63, 63, 0)); /* font */
280 zmw_color(Zmw_Color_Background_Pushed, RGB(0, 10, 4)); /* slider bg */
281 zmw_color(Zmw_Color_Background_Poped, RGB(0, 42, 42)); /* button bg */
282 zmw_color(Zmw_Color_Border_Light, RGB(0, 7, 63)); /* slider/button border */
284 ZMW(zmw_window("para_slider")) {
287 make_stream_input_field();
288 zmw_color(Zmw_Color_Foreground, RGB(0, 63, 63)); /* font */
289 // make_special_sliders();
290 zmw_color(Zmw_Color_Foreground, RGB(63, 63, 0)); /* font */
296 static void get_atts(void)
299 FILE *p = popen("para_client laa", "r");
300 argv = para_malloc(255 * sizeof(char *)); /* FIXME */
302 argv[0] = para_strdup("all_attributes");
306 while (fgets(buf, 255, p)) {
308 argv[argc] = para_strdup(buf);
315 return; /* success */
318 for (argc = 1; argv[argc]; argc++)
324 int main(int the_argc, char *the_argv[])
333 slider_vals = para_malloc((argc + 1) * sizeof(int*));
336 // printf("argc: %d\n", argc);
337 // for (i = 1; argv[i]; i++)
338 // printf("argv[%d]: %s\n", i, argv[i]);
339 zmw_init(&argc, &argv);