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))
45 char *streamname = NULL;
46 static Zmw_Float_0_1 *slider_vals, lastplayed_val, numplayed_val;
49 static void boxed_text(const char *text, int i)
51 ZMW(zmw_decorator(Zmw_Decorator_Border_Embossed))
55 zmw_horizontal_expand(Zmw_False) ;
56 zmw_vertical_expand(Zmw_False) ;
63 void para_log(int ll, char* fmt,...) /* no logging */
70 for (i = 1; argv[i]; i++)
71 slider_vals[i] = VAL_2_SL_VAL(.5);
74 streamname = para_strdup("slider");
78 static char *get_value_filename(void)
80 char *ret, *home =para_homedir();
81 ret = make_message("%s/.paraslash/slide.values", home);
86 static void load(void)
94 tmp = get_value_filename();
99 while (fscanf(f, "%900s %g", att, &val) == 2) {
100 if (!strcmp(att, "lastplayed")) {
101 lastplayed_val = val;
104 if (!strcmp(att, "numplayed")) {
108 for (i = 1; argv[i]; i++)
109 if (!strcmp(argv[i], att)) {
110 slider_vals[i] = val;
118 static void save(void)
124 filename = get_value_filename();
125 f = fopen(filename, "w");
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]);
137 static void print_score(FILE *fd)
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]);
147 fprintf(fd, "score: 0 ");
148 for (i = 1; argv[i]; i++) {
149 if (slider_vals[i] < .01)
151 fprintf(fd, "+ %i * IS_SET(%s) ",
152 (int) (VAL_2_SCORE(SL_VAL_2_VAL(slider_vals[i]))),
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)));
162 static void stradd(void)
165 char *cmd = make_message("para_client stradd %s", streamname);
167 pipe = popen(cmd, "w");
175 static void b_save(void)
184 system("para_client cs slider");
185 system("para_client stop");
186 system("para_client play");
192 system("para_client cs slider");
193 system("para_client play");
195 zmw_main_quit(EXIT_SUCCESS);
198 static void make_buttons(void)
200 /* Add a frame around the box */
201 ZMW(zmw_decorator(Zmw_Decorator_Border_Embossed)) {
219 zmw_main_quit(EXIT_FAILURE);
223 static void make_stream_input_field(void)
225 static char *text1 = NULL;
226 static int cursor_pos = 1; // Must be static
227 static int text1_length ;
230 if ( text1 == NULL ) {
231 // The initial value must be malloced
232 text1 = strdup("slider") ;
233 text1_length = strlen(text1) ;
238 zmw_label("stream: ");
239 zmw_color(Zmw_Color_Foreground, RGB(63, 63, 63));
243 zmw_entry_with_cursor(&streamname, &cursor_pos);
245 text1_length = strlen(text1);
250 /* the sliders, one for each member in argv */
251 static void make_sliders(void)
256 zmw_height(ZMW_VALUE_UNDEFINED);
257 // zmw_horizontal_expand(Zmw_True);
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)));
265 txt = make_message("np: (%i%%)",
266 (int)(.5 + 100 * SL_VAL_2_VAL(numplayed_val)));
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])));
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],
289 static void anchor(void)
292 zmw_font_family("terminal");
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 */
300 ZMW(zmw_window("para_slider")) {
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 */
312 static void get_atts(void)
315 FILE *p = popen("para_client laa", "r");
316 argv = para_malloc(255 * sizeof(char *)); /* FIXME */
318 argv[0] = para_strdup("all_attributes");
322 while (fgets(buf, 255, p)) {
324 argv[argc] = para_strdup(buf);
331 return; /* success */
334 for (argc = 1; argv[argc]; argc++)
340 int main(int the_argc, char *the_argv[])
349 slider_vals = para_malloc((argc + 1) * sizeof(int*));
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);