Mercurial > audlegacy
annotate Plugins/Visualization/blur_scope/blur_scope.c @ 1100:ee12a67bc8b8 trunk
[svn] - don't need this
author | nenolod |
---|---|
date | Mon, 22 May 2006 17:45:04 -0700 |
parents | c8cf439179b8 |
children | f12d7e208b43 |
rev | line source |
---|---|
61 | 1 /* BMP - Cross-platform multimedia player |
2 * Copyright (C) 2003-2004 BMP development team. | |
3 * | |
4 * Based on XMMS: | |
5 * Copyright (C) 1998-2003 XMMS development team. | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * w | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 # include "config.h" | |
25 #endif | |
26 | |
27 #include <glib.h> | |
28 #include <glib/gi18n.h> | |
29 #include <gtk/gtk.h> | |
30 #include <string.h> | |
31 #include "audacious/plugin.h" | |
32 #include "libaudacious/util.h" | |
33 #include "libaudacious/configdb.h" | |
34 #include "blur_scope.h" | |
35 #include "bscope_logo.xpm" | |
36 | |
37 static GtkWidget *window = NULL, *area; | |
38 static GdkPixmap *bg_pixmap = NULL; | |
39 static gboolean config_read = FALSE; | |
40 | |
41 static void bscope_init(void); | |
42 static void bscope_cleanup(void); | |
43 static void bscope_playback_stop(void); | |
44 static void bscope_render_pcm(gint16 data[2][512]); | |
45 | |
46 BlurScopeConfig bscope_cfg; | |
47 | |
48 GtkItemFactory *blurscope_popup; | |
49 | |
50 enum { SCOPE_TOGGLE, SCOPE_CLOSE }; | |
51 | |
52 void blurscope_callback(gpointer data, guint action, GtkWidget * widget); | |
53 gboolean blurscope_popup_menu(GtkWidget * widget, | |
54 GdkEventButton * event, gpointer user_data); | |
55 | |
56 GtkItemFactoryEntry blurscope_menu_entries[] = { | |
57 {N_("/Toggle Decorations"), NULL, blurscope_callback, SCOPE_TOGGLE, | |
701
d539e5c5f730
[svn] Fixes of the remaining GCC 4.1 warnings from external contributor Diego "Flameeyes" Petteno (Gentoo).
chainsaw
parents:
482
diff
changeset
|
58 "<Item>", 0}, |
d539e5c5f730
[svn] Fixes of the remaining GCC 4.1 warnings from external contributor Diego "Flameeyes" Petteno (Gentoo).
chainsaw
parents:
482
diff
changeset
|
59 {N_("/-"), NULL, NULL, 0, "<Separator>", 0}, |
61 | 60 {N_("/Close"), NULL, blurscope_callback, SCOPE_CLOSE, "<StockItem>", |
61 GTK_STOCK_CLOSE}, | |
62 }; | |
63 | |
64 static const int blurscope_menu_entries_num = | |
65 sizeof(blurscope_menu_entries) / sizeof(blurscope_menu_entries[0]); | |
66 | |
67 | |
68 VisPlugin bscope_vp = { | |
69 NULL, | |
70 NULL, | |
71 0, /* XMMS Session ID, filled in by XMMS */ | |
72 NULL, /* description */ | |
73 1, /* Number of PCM channels wanted */ | |
74 0, /* Number of freq channels wanted */ | |
75 bscope_init, /* init */ | |
76 bscope_cleanup, /* cleanup */ | |
77 NULL, /* about */ | |
78 bscope_configure, /* configure */ | |
79 NULL, /* disable_plugin */ | |
80 NULL, /* playback_start */ | |
81 bscope_playback_stop, /* playback_stop */ | |
82 bscope_render_pcm, /* render_pcm */ | |
83 NULL /* render_freq */ | |
84 }; | |
85 | |
86 VisPlugin * | |
87 get_vplugin_info(void) | |
88 { | |
89 bscope_vp.description = g_strdup("Blur Scope"); | |
90 return &bscope_vp; | |
91 } | |
92 | |
93 #define WIDTH 256 | |
94 #define HEIGHT 128 | |
95 #define min(x,y) ((x)<(y)?(x):(y)) | |
96 #define BPL ((WIDTH + 2)) | |
97 | |
98 static guchar rgb_buf[(WIDTH + 2) * (HEIGHT + 2)]; | |
99 static GdkRgbCmap *cmap = NULL; | |
100 | |
701
d539e5c5f730
[svn] Fixes of the remaining GCC 4.1 warnings from external contributor Diego "Flameeyes" Petteno (Gentoo).
chainsaw
parents:
482
diff
changeset
|
101 inline static void |
61 | 102 draw_pixel_8(guchar * buffer, gint x, gint y, guchar c) |
103 { | |
104 buffer[((y + 1) * BPL) + (x + 1)] = c; | |
105 } | |
106 | |
107 | |
108 void | |
109 bscope_read_config(void) | |
110 { | |
111 ConfigDb *db; | |
112 | |
113 if (!config_read) { | |
114 bscope_cfg.color = 0xFF3F7F; | |
115 db = bmp_cfg_db_open(); | |
116 | |
117 if (db) { | |
118 bmp_cfg_db_get_int(db, "BlurScope", "color", | |
119 (int *) &bscope_cfg.color); | |
120 bmp_cfg_db_close(db); | |
121 } | |
122 config_read = TRUE; | |
123 } | |
124 } | |
125 | |
126 | |
127 void | |
128 bscope_blur_8(guchar * ptr, gint w, gint h, gint bpl) | |
129 { | |
130 register guint i, sum; | |
131 register guchar *iptr; | |
132 | |
133 iptr = ptr + bpl + 1; | |
134 i = bpl * h; | |
135 while (i--) { | |
136 sum = (iptr[-bpl] + iptr[-1] + iptr[1] + iptr[bpl]) >> 2; | |
137 if (sum > 2) | |
138 sum -= 2; | |
139 *(iptr++) = sum; | |
140 } | |
141 | |
142 | |
143 } | |
144 | |
145 void | |
146 generate_cmap(void) | |
147 { | |
148 guint32 colors[256], i, red, blue, green; | |
149 if (window) { | |
150 red = (guint32) (bscope_cfg.color / 0x10000); | |
151 green = (guint32) ((bscope_cfg.color % 0x10000) / 0x100); | |
152 blue = (guint32) (bscope_cfg.color % 0x100); | |
153 for (i = 255; i > 0; i--) { | |
154 colors[i] = | |
155 (((guint32) (i * red / 256) << 16) | | |
156 ((guint32) (i * green / 256) << 8) | | |
157 ((guint32) (i * blue / 256))); | |
158 } | |
159 colors[0] = 0; | |
160 if (cmap) { | |
161 gdk_rgb_cmap_free(cmap); | |
162 } | |
163 cmap = gdk_rgb_cmap_new(colors, 256); | |
164 } | |
165 } | |
166 | |
167 static void | |
168 bscope_destroy_cb(GtkWidget * w, gpointer data) | |
169 { | |
170 bscope_vp.disable_plugin(&bscope_vp); | |
171 } | |
172 | |
173 static void | |
174 bscope_init(void) | |
175 { | |
176 if (window) | |
177 return; | |
178 bscope_read_config(); | |
179 | |
180 window = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
181 gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG); | |
182 | |
183 blurscope_popup = gtk_item_factory_new(GTK_TYPE_MENU, "<Main>", NULL); | |
184 | |
185 gtk_item_factory_create_items(GTK_ITEM_FACTORY(blurscope_popup), | |
186 blurscope_menu_entries_num, | |
187 blurscope_menu_entries, NULL); | |
188 | |
189 gtk_widget_set_events(window, | |
190 GDK_FOCUS_CHANGE_MASK | GDK_BUTTON_MOTION_MASK | | |
191 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | | |
192 GDK_SCROLL_MASK | GDK_VISIBILITY_NOTIFY_MASK); | |
193 | |
194 gtk_window_set_title(GTK_WINDOW(window), _("Blur scope")); | |
195 gtk_window_set_resizable(GTK_WINDOW(window), FALSE); | |
196 gtk_widget_realize(window); | |
197 bg_pixmap = | |
198 gdk_pixmap_create_from_xpm_d(window->window, NULL, NULL, bscope_logo); | |
199 // gdk_window_set_back_pixmap(window->window,bg_pixmap,0); | |
200 | |
201 g_signal_connect(G_OBJECT(window), "destroy", | |
202 G_CALLBACK(bscope_destroy_cb), NULL); | |
203 g_signal_connect(G_OBJECT(window), "destroy", | |
204 G_CALLBACK(gtk_widget_destroyed), &window); | |
205 g_signal_connect(G_OBJECT(window), "button-press-event", | |
206 G_CALLBACK(blurscope_popup_menu), NULL); | |
207 | |
208 gtk_widget_set_size_request(window, WIDTH, HEIGHT); | |
209 area = gtk_drawing_area_new(); | |
210 gtk_container_add(GTK_CONTAINER(window), area); | |
211 gtk_widget_realize(area); | |
212 gdk_window_set_back_pixmap(area->window, bg_pixmap, 0); | |
213 generate_cmap(); | |
214 memset(rgb_buf, 0, (WIDTH + 2) * (HEIGHT + 2)); | |
215 | |
216 | |
217 | |
218 gtk_widget_show(area); | |
219 gtk_widget_show(window); | |
220 gdk_window_clear(window->window); | |
221 gdk_window_clear(area->window); | |
222 | |
223 | |
224 } | |
225 | |
226 static void | |
227 bscope_cleanup(void) | |
228 { | |
813
c8cf439179b8
[svn] - Fix a ton and a half of memory leaks, via the wonderful Leonardo Boshell <leonardop -at- gentoo.org>.
nenolod
parents:
701
diff
changeset
|
229 g_free(bscope_vp.description); |
c8cf439179b8
[svn] - Fix a ton and a half of memory leaks, via the wonderful Leonardo Boshell <leonardop -at- gentoo.org>.
nenolod
parents:
701
diff
changeset
|
230 bscope_vp.description = NULL; |
61 | 231 if (window) |
232 gtk_widget_destroy(window); | |
233 if (bg_pixmap) { | |
234 g_object_unref(bg_pixmap); | |
235 bg_pixmap = NULL; | |
236 } | |
237 if (cmap) { | |
238 gdk_rgb_cmap_free(cmap); | |
239 cmap = NULL; | |
240 } | |
241 } | |
242 | |
243 static void | |
244 bscope_playback_stop(void) | |
245 { | |
246 if (GTK_WIDGET_REALIZED(area)) | |
247 gdk_window_clear(area->window); | |
248 } | |
249 | |
250 static inline void | |
251 draw_vert_line(guchar * buffer, gint x, gint y1, gint y2) | |
252 { | |
253 int y; | |
254 if (y1 < y2) { | |
255 for (y = y1; y <= y2; y++) | |
256 draw_pixel_8(buffer, x, y, 0xFF); | |
257 } | |
258 else if (y2 < y1) { | |
259 for (y = y2; y <= y1; y++) | |
260 draw_pixel_8(buffer, x, y, 0xFF); | |
261 } | |
262 else | |
263 draw_pixel_8(buffer, x, y1, 0xFF); | |
264 } | |
265 | |
266 static void | |
267 bscope_render_pcm(gint16 data[2][512]) | |
268 { | |
269 gint i, y, prev_y; | |
270 | |
271 if (!window) | |
272 return; | |
273 bscope_blur_8(rgb_buf, WIDTH, HEIGHT, BPL); | |
274 prev_y = y = (HEIGHT / 2) + (data[0][0] >> 9); | |
275 for (i = 0; i < WIDTH; i++) { | |
276 y = (HEIGHT / 2) + (data[0][i >> 1] >> 9); | |
277 if (y < 0) | |
278 y = 0; | |
279 if (y >= HEIGHT) | |
280 y = HEIGHT - 1; | |
281 draw_vert_line(rgb_buf, i, prev_y, y); | |
282 prev_y = y; | |
283 } | |
284 | |
285 GDK_THREADS_ENTER(); | |
286 gdk_draw_indexed_image(area->window, area->style->white_gc, 0, 0, | |
287 WIDTH, HEIGHT, GDK_RGB_DITHER_NONE, | |
288 rgb_buf + BPL + 1, (WIDTH + 2), cmap); | |
289 GDK_THREADS_LEAVE(); | |
290 return; | |
291 } | |
292 | |
293 gboolean | |
294 blurscope_popup_menu(GtkWidget * widget, | |
295 GdkEventButton * event, gpointer user_data) | |
296 { | |
297 | |
298 if (event->button == 3) { | |
299 gtk_item_factory_popup(blurscope_popup, | |
300 event->x_root, | |
301 event->y_root, event->button, event->time); | |
302 return TRUE; | |
303 } | |
304 | |
305 return FALSE; | |
306 } | |
307 | |
308 void | |
309 blurscope_callback(gpointer data, guint action, GtkWidget * widget) | |
310 { | |
311 | |
312 switch (action) { | |
313 case SCOPE_TOGGLE: | |
314 gtk_window_set_decorated(GTK_WINDOW(window), | |
315 !gtk_window_get_decorated(GTK_WINDOW | |
316 (window))); | |
317 break; | |
318 case SCOPE_CLOSE: | |
319 gtk_widget_destroy(window); | |
320 break; | |
321 default: | |
322 break; | |
323 } | |
324 return; | |
325 } |