comparison src/audacious/legacy/ui_svis.c @ 4700:3a56d2786063

move all files belonging to the legacy UI to legacy/
author mf0102 <0102@gmx.at>
date Sun, 06 Jul 2008 17:55:40 +0200
parents
children 885c21b8684e
comparison
equal deleted inserted replaced
4698:cfc8d1e0c78b 4700:3a56d2786063
1 /*
2 * Audacious - a cross-platform multimedia player
3 * Copyright (c) 2007 Audacious development team.
4 *
5 * Based on:
6 * BMP - Cross-platform multimedia player
7 * Copyright (C) 2003-2004 BMP development team.
8 * XMMS:
9 * Copyright (C) 1998-2003 XMMS development team.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; under version 3 of the License.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses>.
22 *
23 * The Audacious team does not consider modular code linking to
24 * Audacious or using our public API to be a derived work.
25 */
26
27 #include "ui_skin.h"
28 #include "ui_svis.h"
29 #include "ui_vis.h"
30 #include "main.h"
31 #include "util.h"
32 #include "strings.h"
33 #include "playback.h"
34 #include <string.h>
35 #include <ctype.h>
36 #include <gtk/gtkmain.h>
37 #include <gtk/gtkmarshal.h>
38 #include <gtk/gtkimage.h>
39
40 #define UI_TYPE_SVIS (ui_svis_get_type())
41
42 static gint svis_redraw_delays[] = { 1, 2, 4, 8 };
43
44 /* FIXME: Are the svis_scope_colors correct? */
45 static guint8 svis_scope_colors[] = { 20, 19, 18, 19, 20 };
46 static guint8 svis_vu_normal_colors[] = { 17, 17, 17, 12, 12, 12, 2, 2 };
47
48 #define DRAW_DS_PIXEL(ptr,value) \
49 *(ptr) = (value); \
50 *((ptr) + 1) = (value); \
51 *((ptr) + 76) = (value); \
52 *((ptr) + 77) = (value);
53
54 #define SVIS_HEIGHT 5
55 #define SVIS_WIDTH 38
56
57 enum {
58 DOUBLED,
59 LAST_SIGNAL
60 };
61
62 static void ui_svis_class_init (UiSVisClass *klass);
63 static void ui_svis_init (UiSVis *svis);
64 static void ui_svis_destroy (GtkObject *object);
65 static void ui_svis_realize (GtkWidget *widget);
66 static void ui_svis_unrealize (GtkWidget *widget);
67 static void ui_svis_map (GtkWidget *widget);
68 static void ui_svis_unmap (GtkWidget *widget);
69 static void ui_svis_size_request (GtkWidget *widget, GtkRequisition *requisition);
70 static void ui_svis_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
71 static gboolean ui_svis_expose (GtkWidget *widget, GdkEventExpose *event);
72 static void ui_svis_toggle_scaled (UiSVis *svis);
73
74 static GtkWidgetClass *parent_class = NULL;
75 static guint vis_signals[LAST_SIGNAL] = { 0 };
76
77 GType ui_svis_get_type() {
78 static GType vis_type = 0;
79 if (!vis_type) {
80 static const GTypeInfo vis_info = {
81 sizeof (UiSVisClass),
82 NULL,
83 NULL,
84 (GClassInitFunc) ui_svis_class_init,
85 NULL,
86 NULL,
87 sizeof (UiSVis),
88 0,
89 (GInstanceInitFunc) ui_svis_init,
90 };
91 vis_type = g_type_register_static (GTK_TYPE_WIDGET, "UiSVis_", &vis_info, 0);
92 }
93
94 return vis_type;
95 }
96
97 static void ui_svis_class_init(UiSVisClass *klass) {
98 GtkObjectClass *object_class;
99 GtkWidgetClass *widget_class;
100
101 object_class = (GtkObjectClass*) klass;
102 widget_class = (GtkWidgetClass*) klass;
103 parent_class = gtk_type_class (gtk_widget_get_type ());
104
105 object_class->destroy = ui_svis_destroy;
106
107 widget_class->realize = ui_svis_realize;
108 widget_class->unrealize = ui_svis_unrealize;
109 widget_class->map = ui_svis_map;
110 widget_class->unmap = ui_svis_unmap;
111 widget_class->expose_event = ui_svis_expose;
112 widget_class->size_request = ui_svis_size_request;
113 widget_class->size_allocate = ui_svis_size_allocate;
114
115 klass->scaled = ui_svis_toggle_scaled;
116
117 vis_signals[DOUBLED] =
118 g_signal_new ("toggle-scaled", G_OBJECT_CLASS_TYPE (object_class), G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
119 G_STRUCT_OFFSET (UiSVisClass, scaled), NULL, NULL,
120 gtk_marshal_VOID__VOID, G_TYPE_NONE, 0);
121 }
122
123 static void ui_svis_init(UiSVis *svis) {
124
125 }
126
127 GtkWidget* ui_svis_new(GtkWidget *fixed, gint x, gint y) {
128 UiSVis *svis = g_object_new (ui_svis_get_type (), NULL);
129
130 svis->x = x;
131 svis->y = y;
132
133 svis->width = SVIS_WIDTH;
134 svis->height = SVIS_HEIGHT;
135
136 svis->fixed = fixed;
137 svis->scaled = FALSE;
138
139 svis->visible_window = TRUE;
140 svis->event_window = NULL;
141
142 gtk_fixed_put(GTK_FIXED(svis->fixed), GTK_WIDGET(svis), svis->x, svis->y);
143
144 return GTK_WIDGET(svis);
145 }
146
147 static void ui_svis_destroy(GtkObject *object) {
148 UiSVis *svis;
149
150 g_return_if_fail (object != NULL);
151 g_return_if_fail (UI_IS_SVIS (object));
152
153 svis = UI_SVIS (object);
154
155 if (GTK_OBJECT_CLASS (parent_class)->destroy)
156 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
157 }
158
159 static void ui_svis_realize(GtkWidget *widget) {
160 UiSVis *svis;
161 GdkWindowAttr attributes;
162 gint attributes_mask;
163
164 g_return_if_fail (widget != NULL);
165 g_return_if_fail (UI_IS_SVIS(widget));
166
167 GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
168 svis = UI_SVIS(widget);
169
170 attributes.x = widget->allocation.x;
171 attributes.y = widget->allocation.y;
172 attributes.width = widget->allocation.width;
173 attributes.height = widget->allocation.height;
174 attributes.window_type = GDK_WINDOW_CHILD;
175 attributes.event_mask = gtk_widget_get_events(widget);
176 attributes.event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK;
177
178 if (svis->visible_window)
179 {
180 attributes.visual = gtk_widget_get_visual(widget);
181 attributes.colormap = gtk_widget_get_colormap(widget);
182 attributes.wclass = GDK_INPUT_OUTPUT;
183 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
184 widget->window = gdk_window_new(widget->parent->window, &attributes, attributes_mask);
185 GTK_WIDGET_UNSET_FLAGS(widget, GTK_NO_WINDOW);
186 gdk_window_set_user_data(widget->window, widget);
187 }
188 else
189 {
190 widget->window = gtk_widget_get_parent_window (widget);
191 g_object_ref (widget->window);
192
193 attributes.wclass = GDK_INPUT_ONLY;
194 attributes_mask = GDK_WA_X | GDK_WA_Y;
195 svis->event_window = gdk_window_new (widget->window, &attributes, attributes_mask);
196 GTK_WIDGET_SET_FLAGS (widget, GTK_NO_WINDOW);
197 gdk_window_set_user_data(svis->event_window, widget);
198 }
199
200 widget->style = gtk_style_attach(widget->style, widget->window);
201 }
202
203 static void ui_svis_unrealize(GtkWidget *widget) {
204 UiSVis *svis;
205 svis = UI_SVIS(widget);
206
207 if ( svis->event_window != NULL )
208 {
209 gdk_window_set_user_data( svis->event_window , NULL );
210 gdk_window_destroy( svis->event_window );
211 svis->event_window = NULL;
212 }
213
214 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
215 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
216 }
217
218 static void ui_svis_map(GtkWidget *widget)
219 {
220 UiSVis *svis;
221 svis = UI_SVIS(widget);
222
223 if (svis->event_window != NULL)
224 gdk_window_show (svis->event_window);
225
226 if (GTK_WIDGET_CLASS (parent_class)->map)
227 (* GTK_WIDGET_CLASS (parent_class)->map) (widget);
228 }
229
230 static void ui_svis_unmap (GtkWidget *widget)
231 {
232 UiSVis *svis;
233 svis = UI_SVIS(widget);
234
235 if (svis->event_window != NULL)
236 gdk_window_hide (svis->event_window);
237
238 if (GTK_WIDGET_CLASS (parent_class)->unmap)
239 (* GTK_WIDGET_CLASS (parent_class)->unmap) (widget);
240 }
241
242 static void ui_svis_size_request(GtkWidget *widget, GtkRequisition *requisition) {
243 UiSVis *svis = UI_SVIS(widget);
244
245 requisition->width = svis->width * (svis->scaled ? cfg.scale_factor : 1);
246 requisition->height = svis->height*(svis->scaled ? cfg.scale_factor : 1);
247 }
248
249 static void ui_svis_size_allocate(GtkWidget *widget, GtkAllocation *allocation) {
250 UiSVis *svis = UI_SVIS (widget);
251
252 widget->allocation = *allocation;
253 widget->allocation.x *= (svis->scaled ? cfg.scale_factor : 1 );
254 widget->allocation.y *= (svis->scaled ? cfg.scale_factor : 1);
255 if (GTK_WIDGET_REALIZED (widget))
256 {
257 if (svis->event_window != NULL)
258 gdk_window_move_resize(svis->event_window, widget->allocation.x, widget->allocation.y, allocation->width, allocation->height);
259 else
260 gdk_window_move_resize(widget->window, widget->allocation.x, widget->allocation.y, allocation->width, allocation->height);
261 }
262
263 svis->x = widget->allocation.x/(svis->scaled ? cfg.scale_factor : 1);
264 svis->y = widget->allocation.y/(svis->scaled ? cfg.scale_factor : 1);
265 }
266
267 static gboolean ui_svis_expose(GtkWidget *widget, GdkEventExpose *event) {
268 g_return_val_if_fail (widget != NULL, FALSE);
269 g_return_val_if_fail (UI_IS_SVIS (widget), FALSE);
270 g_return_val_if_fail (event != NULL, FALSE);
271
272 UiSVis *svis = UI_SVIS (widget);
273
274 gint x, y, h;
275 guchar svis_color[24][3];
276 guchar rgb_data[SVIS_WIDTH * 2 * SVIS_HEIGHT * 2], *ptr, c;
277 guint32 colors[24];
278 GdkRgbCmap *cmap;
279
280 if (!GTK_WIDGET_VISIBLE(widget))
281 return FALSE;
282
283 if (!svis->visible_window)
284 return FALSE;
285
286 skin_get_viscolor(aud_active_skin, svis_color);
287 for (y = 0; y < 24; y++) {
288 colors[y] =
289 svis_color[y][0] << 16 | svis_color[y][1] << 8 | svis_color[y][2];
290 }
291 cmap = gdk_rgb_cmap_new(colors, 24);
292
293 if (!cfg.scaled) {
294 memset(rgb_data, 0, SVIS_WIDTH * SVIS_HEIGHT);
295 if (cfg.vis_type == VIS_ANALYZER && !playback_get_paused() && playback_get_playing()){
296 for(y=0; y < SVIS_HEIGHT; y++){
297 if (cfg.analyzer_type == ANALYZER_BARS){
298 for(x=0;x< SVIS_WIDTH; x++){
299 if(svis->data[x] > y << 1)
300 {
301 rgb_data[x*3+ (SVIS_HEIGHT - y) * SVIS_WIDTH] = 23;
302 rgb_data[x*3+1 + (SVIS_HEIGHT - y) * SVIS_WIDTH] = 23;
303
304 }
305 }
306 }
307 else{
308 for(x=0;x< SVIS_WIDTH; x++){
309 if(svis->data[x] > y << 1)
310 {
311 rgb_data[x + (SVIS_HEIGHT - y) * SVIS_WIDTH] = 23;
312 }
313 }
314 }
315 }
316 }
317 else if (cfg.vis_type == VIS_VOICEPRINT){
318 switch (cfg.vu_mode) {
319 case VU_NORMAL:
320 for (y = 0; y < 2; y++) {
321 ptr = rgb_data + ((y * 3) * 38);
322 h = (svis->data[y] * 7) / 37;
323 for (x = 0; x < h; x++, ptr += 5) {
324 c = svis_vu_normal_colors[x];
325 *(ptr) = c;
326 *(ptr + 1) = c;
327 *(ptr + 2) = c;
328 *(ptr + 38) = c;
329 *(ptr + 39) = c;
330 *(ptr + 40) = c;
331 }
332 }
333 break;
334 case VU_SMOOTH:
335 for (y = 0; y < 2; y++) {
336 ptr = rgb_data + ((y * 3) * SVIS_WIDTH);
337 for (x = 0; x < svis->data[y]; x++, ptr++) {
338 c = 17 - ((x * 15) / 37);
339 *(ptr) = c;
340 *(ptr + 38) = c;
341 }
342 }
343 break;
344 }
345 }
346 else if (cfg.vis_type == VIS_SCOPE) {
347 for (x = 0; x < 38; x++) {
348 h = svis->data[x << 1] / 3;
349 ptr = rgb_data + ((4 - h) * 38) + x;
350 *ptr = svis_scope_colors[h];
351 }
352 }
353
354 }
355 else { /*svis scaling, this needs some work, since a lot of stuff is hardcoded --majeru*/
356
357 memset(rgb_data, 0, SVIS_WIDTH * cfg.scale_factor * SVIS_HEIGHT * cfg.scale_factor);
358 if (cfg.vis_type == VIS_ANALYZER && !playback_get_paused() && playback_get_playing()){
359 for(y=0; y < SVIS_HEIGHT; y++){
360 if (cfg.analyzer_type == ANALYZER_BARS){
361 for(x=0;x< SVIS_WIDTH; x++){
362 if(svis->data[x] > y << 1)
363 {
364 ptr = rgb_data + x * 6 + (SVIS_HEIGHT * 2 - y * 2) * SVIS_WIDTH *2;
365 DRAW_DS_PIXEL(ptr, 23);
366 DRAW_DS_PIXEL(ptr + 2, 23);
367 }
368 }
369 }
370 else{
371 for(x=0;x< SVIS_WIDTH; x++){
372 if(svis->data[x] > y << 1)
373 {
374 ptr = rgb_data + x * 2 + (SVIS_HEIGHT * 2 - y * 2) * SVIS_WIDTH * 2;
375 DRAW_DS_PIXEL(ptr, 23);
376 }
377 }
378 }
379 }
380 }
381 else if (cfg.vis_type == VIS_VOICEPRINT){
382 switch (cfg.vu_mode) {
383 case VU_NORMAL:
384 for (y = 0; y < 2; y++) {
385 ptr = rgb_data + ((y * 3) * 152);
386 h = (svis->data[y] * 8) / 37;
387 for (x = 0; x < h; x++, ptr += 10) {
388 c = svis_vu_normal_colors[x];
389 DRAW_DS_PIXEL(ptr, c);
390 DRAW_DS_PIXEL(ptr + 2, c);
391 DRAW_DS_PIXEL(ptr + 4, c);
392 DRAW_DS_PIXEL(ptr + 152, c);
393 DRAW_DS_PIXEL(ptr + 154, c);
394 DRAW_DS_PIXEL(ptr + 156, c);
395 }
396 }
397 break;
398 case VU_SMOOTH:
399 for (y = 0; y < 2; y++) {
400 ptr = rgb_data + ((y * 3) * 152);
401 for (x = 0; x < svis->data[y]; x++, ptr += 2) {
402 c = 17 - ((x * 15) / 37);
403 DRAW_DS_PIXEL(ptr, c);
404 DRAW_DS_PIXEL(ptr + 152, c);
405 }
406 }
407 break;
408 }
409 }
410 else if (cfg.vis_type == VIS_SCOPE) {
411 for (x = 0; x < 38; x++) {
412 h = svis->data[x << 1] / 3;
413 ptr = rgb_data + ((4 - h) * 152) + (x << 1);
414 *ptr = svis_scope_colors[h];
415 *(ptr + 1) = svis_scope_colors[h];
416 *(ptr + 76) = svis_scope_colors[h];
417 *(ptr + 77) = svis_scope_colors[h];
418 }
419 }
420
421
422 }
423
424 GdkPixmap *obj = NULL;
425 GdkGC *gc;
426 obj = gdk_pixmap_new(NULL, svis->width* ( svis->scaled ? cfg.scale_factor : 1),
427 svis->height*(svis->scaled ? cfg.scale_factor : 1), gdk_rgb_get_visual()->depth);
428 gc = gdk_gc_new(obj);
429
430 if (!svis->scaled) {
431 gdk_draw_indexed_image(obj, gc, 0, 0, svis->width, svis->height,
432 GDK_RGB_DITHER_NORMAL, (guchar *) rgb_data,
433 38, cmap);
434 } else {
435 gdk_draw_indexed_image(obj, gc,
436 0 << 1, 0 << 1,
437 svis->width << 1, svis->height << 1,
438 GDK_RGB_DITHER_NONE, (guchar *) rgb_data,
439 76, cmap);
440 }
441
442 gdk_rgb_cmap_free(cmap);
443 gdk_draw_drawable (widget->window, gc, obj, 0, 0, 0, 0,
444 svis->width*(svis->scaled ? cfg.scale_factor : 1),
445 svis->height*(svis->scaled ? cfg.scale_factor : 1));
446 g_object_unref(obj);
447 g_object_unref(gc);
448
449 return FALSE;
450 }
451
452 static void ui_svis_toggle_scaled(UiSVis *svis) {
453 GtkWidget *widget = GTK_WIDGET (svis);
454 svis->scaled = !svis->scaled;
455
456 gtk_widget_set_size_request(widget, svis->width* cfg.scale_factor, svis->height * cfg.scale_factor);
457
458 gtk_widget_queue_draw(widget);
459 }
460
461 void ui_svis_set_visible(GtkWidget *widget, gboolean window_is_visible)
462 {
463 UiSVis *svis;
464 gboolean widget_is_visible;
465
466 g_return_if_fail(UI_IS_SVIS(widget));
467
468 svis = UI_SVIS (widget);
469 widget_is_visible = GTK_WIDGET_VISIBLE(widget);
470
471 svis->visible_window = window_is_visible;
472
473 if (GTK_WIDGET_REALIZED (widget))
474 {
475 if ( widget_is_visible )
476 gtk_widget_hide(widget);
477
478 gtk_widget_unrealize(widget);
479 gtk_widget_realize(widget);
480
481 if ( widget_is_visible )
482 gtk_widget_show(widget);
483 }
484
485 if (widget_is_visible)
486 gtk_widget_queue_resize(widget);
487 }
488
489 void ui_svis_clear_data(GtkWidget *widget) {
490 g_return_if_fail(UI_IS_SVIS(widget));
491
492 gint i;
493 UiSVis *svis = UI_SVIS (widget);
494
495 for (i = 0; i < 75; i++) {
496 svis->data[i] = (cfg.vis_type == VIS_SCOPE) ? 6 : 0;
497 }
498 }
499
500 void ui_svis_timeout_func(GtkWidget *widget, guchar * data) {
501 g_return_if_fail(UI_IS_SVIS(widget));
502
503 UiSVis *svis = UI_SVIS (widget);
504 static GTimer *timer = NULL;
505 gulong micros = 9999999;
506 gboolean falloff = FALSE;
507 gint i;
508
509 if (!timer) {
510 timer = g_timer_new();
511 g_timer_start(timer);
512 }
513 else {
514 g_timer_elapsed(timer, &micros);
515 if (micros > 14000)
516 g_timer_reset(timer);
517
518 }
519
520 if (cfg.vis_type == VIS_VOICEPRINT) {
521 if (micros > 14000)
522 falloff = TRUE;
523
524 for (i = 0; i < 2; i++) {
525 if (falloff || data) {
526 if (data && data[i] > svis->data[i])
527 svis->data[i] = data[i];
528 else if (falloff) {
529 if (svis->data[i] >= 2)
530 svis->data[i] -= 2;
531 else
532 svis->data[i] = 0;
533 }
534 }
535
536 }
537 }
538 else if (data) {
539 for (i = 0; i < 75; i++)
540 svis->data[i] = data[i];
541 }
542
543 if (micros > 14000) {
544 if (!svis->refresh_delay) {
545 gtk_widget_queue_draw(widget);
546 svis->refresh_delay = svis_redraw_delays[cfg.vis_refresh];
547 }
548 svis->refresh_delay--;
549 }
550 }