comparison src/hotkey/plugin.c @ 2336:ad45d65e9ae7

Hotkey Plugin: Complete GUI rework. Ability to use more than one binding for the same action.
author Sascha Hlusiak <contact@saschahlusiak.de>
date Wed, 23 Jan 2008 19:37:05 +0100
parents 0be42d832217
children 7b284f323ad8
comparison
equal deleted inserted replaced
2335:6b9d5a8b509e 2336:ad45d65e9ae7
92 92
93 loaded = TRUE; 93 loaded = TRUE;
94 } 94 }
95 95
96 /* handle keys */ 96 /* handle keys */
97 gboolean handle_keyevent (int keycode, int state, int type) 97 gboolean handle_keyevent (EVENT event)
98 { 98 {
99 gint current_volume, old_volume; 99 gint current_volume, old_volume;
100 static gint volume_static = 0; 100 static gint volume_static = 0;
101 gboolean play, mute; 101 gboolean play, mute;
102 102
114 /* volume is mute */ 114 /* volume is mute */
115 mute = TRUE; 115 mute = TRUE;
116 } 116 }
117 117
118 /* mute the playback */ 118 /* mute the playback */
119 if ((keycode == plugin_cfg.mute.key) && (state == plugin_cfg.mute.mask) && (type == plugin_cfg.mute.type)) 119 if (event == EVENT_MUTE)
120 { 120 {
121 if (!mute) 121 if (!mute)
122 { 122 {
123 volume_static = current_volume; 123 volume_static = current_volume;
124 audacious_drct_set_main_volume (0); 124 audacious_drct_set_main_volume (0);
129 } 129 }
130 return TRUE; 130 return TRUE;
131 } 131 }
132 132
133 /* decreace volume */ 133 /* decreace volume */
134 if ((keycode == plugin_cfg.vol_down.key) && (state == plugin_cfg.vol_down.mask) && (type == plugin_cfg.vol_down.type)) 134 if (event == EVENT_VOL_DOWN)
135 { 135 {
136 if (mute) 136 if (mute)
137 { 137 {
138 current_volume = old_volume; 138 current_volume = old_volume;
139 old_volume = 0; 139 old_volume = 0;
153 old_volume = current_volume; 153 old_volume = current_volume;
154 return TRUE; 154 return TRUE;
155 } 155 }
156 156
157 /* increase volume */ 157 /* increase volume */
158 if ((keycode == plugin_cfg.vol_up.key) && (state == plugin_cfg.vol_up.mask) && (type == plugin_cfg.vol_up.type)) 158 if (event == EVENT_VOL_UP)
159 { 159 {
160 if (mute) 160 if (mute)
161 { 161 {
162 current_volume = old_volume; 162 current_volume = old_volume;
163 old_volume = 0; 163 old_volume = 0;
177 old_volume = current_volume; 177 old_volume = current_volume;
178 return TRUE; 178 return TRUE;
179 } 179 }
180 180
181 /* play */ 181 /* play */
182 if ((keycode == plugin_cfg.play.key) && (state == plugin_cfg.play.mask) && (type == plugin_cfg.play.type)) 182 if (event == EVENT_PLAY)
183 { 183 {
184 audacious_drct_play (); 184 audacious_drct_play ();
185 return TRUE; 185 return TRUE;
186 } 186 }
187 187
188 /* pause */ 188 /* pause */
189 if ((keycode == plugin_cfg.pause.key) && (state == plugin_cfg.pause.mask) && (type == plugin_cfg.pause.type)) 189 if (event == EVENT_PAUSE)
190 { 190 {
191 if (!play) audacious_drct_play (); 191 if (!play) audacious_drct_play ();
192 else audacious_drct_pause (); 192 else audacious_drct_pause ();
193 193
194 return TRUE; 194 return TRUE;
195 } 195 }
196 196
197 /* stop */ 197 /* stop */
198 if ((keycode == plugin_cfg.stop.key) && (state == plugin_cfg.stop.mask) && (type == plugin_cfg.stop.type)) 198 if (event == EVENT_STOP)
199 { 199 {
200 audacious_drct_stop (); 200 audacious_drct_stop ();
201 return TRUE; 201 return TRUE;
202 } 202 }
203 203
204 /* prev track */ 204 /* prev track */
205 if ((keycode == plugin_cfg.prev_track.key) && (state == plugin_cfg.prev_track.mask) && (type == plugin_cfg.prev_track.type)) 205 if (event == EVENT_PREV_TRACK)
206 { 206 {
207 audacious_drct_playlist_prev (); 207 audacious_drct_playlist_prev ();
208 return TRUE; 208 return TRUE;
209 } 209 }
210 210
211 /* next track */ 211 /* next track */
212 if ((keycode == plugin_cfg.next_track.key) && (state == plugin_cfg.next_track.mask) && (type == plugin_cfg.next_track.type)) 212 if (event == EVENT_NEXT_TRACK)
213 { 213 {
214 audacious_drct_playlist_next (); 214 audacious_drct_playlist_next ();
215 return TRUE; 215 return TRUE;
216 } 216 }
217 217
218 /* forward */ 218 /* forward */
219 if ((keycode == plugin_cfg.forward.key) && (state == plugin_cfg.forward.mask) && (type == plugin_cfg.forward.type)) 219 if (event == EVENT_FORWARD)
220 { 220 {
221 gint time = audacious_drct_get_output_time(); 221 gint time = audacious_drct_get_output_time();
222 time += 5000; /* Jump 5s into future */ 222 time += 5000; /* Jump 5s into future */
223 audacious_drct_jump_to_time(time); 223 audacious_drct_jump_to_time(time);
224 return TRUE; 224 return TRUE;
225 } 225 }
226 226
227 /* backward */ 227 /* backward */
228 if ((keycode == plugin_cfg.backward.key) && (state == plugin_cfg.backward.mask) && (type == plugin_cfg.backward.type)) 228 if (event == EVENT_BACKWARD)
229 { 229 {
230 gint time = audacious_drct_get_output_time(); 230 gint time = audacious_drct_get_output_time();
231 if (time > 5000) time -= 5000; /* Jump 5s back */ 231 if (time > 5000) time -= 5000; /* Jump 5s back */
232 else time = 0; 232 else time = 0;
233 audacious_drct_jump_to_time(time); 233 audacious_drct_jump_to_time(time);
234 return TRUE; 234 return TRUE;
235 } 235 }
236 236
237 /* Open Jump-To-File dialog */ 237 /* Open Jump-To-File dialog */
238 if ((keycode == plugin_cfg.jump_to_file.key) && (state == plugin_cfg.jump_to_file.mask) && (type == plugin_cfg.jump_to_file.type)) 238 if (event == EVENT_JUMP_TO_FILE)
239 { 239 {
240 audacious_drct_show_jtf_box(); 240 audacious_drct_show_jtf_box();
241 return TRUE; 241 return TRUE;
242 } 242 }
243 243
244 /* Toggle Windows */ 244 /* Toggle Windows */
245 if ((keycode == plugin_cfg.toggle_win.key) && (state == plugin_cfg.toggle_win.mask) && (type == plugin_cfg.toggle_win.type)) 245 if (event == EVENT_TOGGLE_WIN)
246 { 246 {
247 static gboolean is_main, is_eq, is_pl; 247 static gboolean is_main, is_eq, is_pl;
248 is_main = audacious_drct_main_win_is_visible(); 248 is_main = audacious_drct_main_win_is_visible();
249 if (is_main) { /* Hide windows */ 249 if (is_main) { /* Hide windows */
250 is_pl = audacious_drct_pl_win_is_visible(); 250 is_pl = audacious_drct_pl_win_is_visible();
260 } 260 }
261 return TRUE; 261 return TRUE;
262 } 262 }
263 263
264 /* Show OSD through AOSD plugin*/ 264 /* Show OSD through AOSD plugin*/
265 if ((keycode == plugin_cfg.show_aosd.key) && (state == plugin_cfg.show_aosd.mask) && (type == plugin_cfg.show_aosd.type)) 265 if (event == EVENT_SHOW_AOSD)
266 { 266 {
267 aud_hook_call("aosd toggle", NULL); 267 aud_hook_call("aosd toggle", NULL);
268 return TRUE; 268 return TRUE;
269 } 269 }
270 270
271 return FALSE; 271 return FALSE;
272 }
273
274 void add_hotkey(HotkeyConfiguration** pphotkey, KeySym keysym, gint mask, gint type, EVENT event)
275 {
276 KeyCode keycode;
277 HotkeyConfiguration *photkey;
278 if (keysym == 0) return;
279 if (pphotkey == NULL) return;
280 photkey = *pphotkey;
281 if (photkey == NULL) return;
282 keycode = XKeysymToKeycode(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), keysym);
283 if (keycode == 0) return;
284 if (photkey->key) {
285 photkey->next = (HotkeyConfiguration*)
286 malloc(sizeof (HotkeyConfiguration));
287 photkey = photkey->next;
288 *pphotkey = photkey;
289 photkey->next = NULL;
290 }
291 photkey->key = (gint)keycode;
292 photkey->mask = mask;
293 photkey->event = event;
294 photkey->type = type;
295 }
296
297 void load_defaults (void)
298 {
299 HotkeyConfiguration* hotkey;
300
301 hotkey = &(plugin_cfg.first);
302
303 add_hotkey(&hotkey, XF86XK_AudioPrev, 0, TYPE_KEY, EVENT_PREV_TRACK);
304 add_hotkey(&hotkey, XF86XK_AudioPlay, 0, TYPE_KEY, EVENT_PLAY);
305 add_hotkey(&hotkey, XF86XK_AudioPause, 0, TYPE_KEY, EVENT_PAUSE);
306 add_hotkey(&hotkey, XF86XK_AudioStop, 0, TYPE_KEY, EVENT_STOP);
307 add_hotkey(&hotkey, XF86XK_AudioNext, 0, TYPE_KEY, EVENT_NEXT_TRACK);
308
309 /* add_hotkey(&hotkey, XF86XK_AudioRewind, 0, TYPE_KEY, EVENT_BACKWARD); */
310
311 add_hotkey(&hotkey, XF86XK_AudioMute, 0, TYPE_KEY, EVENT_MUTE);
312 add_hotkey(&hotkey, XF86XK_AudioRaiseVolume, 0, TYPE_KEY, EVENT_VOL_UP);
313 add_hotkey(&hotkey, XF86XK_AudioLowerVolume, 0, TYPE_KEY, EVENT_VOL_DOWN);
314
315 /* add_hotkey(&hotkey, XF86XK_AudioMedia, 0, TYPE_KEY, EVENT_JUMP_TO_FILE);
316 add_hotkey(&hotkey, XF86XK_Music, 0, TYPE_KEY, EVENT_TOGGLE_WIN); */
272 } 317 }
273 318
274 /* load plugin configuration */ 319 /* load plugin configuration */
275 void load_config (void) 320 void load_config (void)
276 { 321 {
277 ConfigDb *cfdb; 322 ConfigDb *cfdb;
323 HotkeyConfiguration *hotkey;
324 int i,max;
278 325
279 /* default volume level */ 326 /* default volume level */
280 plugin_cfg.vol_increment = 4; 327 plugin_cfg.vol_increment = 4;
281 plugin_cfg.vol_decrement = 4; 328 plugin_cfg.vol_decrement = 4;
282 329
283 #define load_key(hotkey,default) \
284 plugin_cfg.hotkey.key = (default)?(XKeysymToKeycode(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), (default))):0; \
285 plugin_cfg.hotkey.mask = 0; \
286 plugin_cfg.hotkey.type = TYPE_KEY; \
287 aud_cfg_db_get_int (cfdb, "globalHotkey", #hotkey, &plugin_cfg.hotkey.key); \
288 aud_cfg_db_get_int (cfdb, "globalHotkey", #hotkey "_mask", &plugin_cfg.hotkey.mask); \
289 aud_cfg_db_get_int (cfdb, "globalHotkey", #hotkey "_type", &plugin_cfg.hotkey.type);
290
291
292 /* open configuration database */ 330 /* open configuration database */
293 cfdb = aud_cfg_db_open ( ); 331 cfdb = aud_cfg_db_open ( );
294 332 hotkey = &(plugin_cfg.first);
295 load_key(mute, XF86XK_AudioMute); 333 hotkey->next = NULL;
296 load_key(vol_down, XF86XK_AudioLowerVolume); 334 hotkey->key = 0;
297 load_key(vol_up, XF86XK_AudioRaiseVolume); 335 hotkey->mask = 0;
298 load_key(play, XF86XK_AudioPlay); 336 hotkey->event = 0;
299 load_key(pause, XF86XK_AudioPause); 337 hotkey->type = TYPE_KEY;
300 load_key(stop, XF86XK_AudioStop); 338 max = 0;
301 load_key(prev_track, XF86XK_AudioPrev); 339 aud_cfg_db_get_int (cfdb, "globalHotkey", "NumHotkeys", &max);
302 load_key(next_track, XF86XK_AudioNext); 340 if (max == 0)
303 load_key(jump_to_file, XF86XK_AudioMedia); 341 load_defaults();
304 load_key(toggle_win, 0); 342 else for (i=0; i<max; i++)
305 load_key(forward, 0); 343 {
306 load_key(backward, XF86XK_AudioRewind); 344 gchar *text = NULL;
307 load_key(show_aosd, 0); 345 gint value;
346 if (hotkey->key) {
347 hotkey->next = (HotkeyConfiguration*)
348 malloc(sizeof (HotkeyConfiguration));
349 hotkey = hotkey->next;
350 hotkey->next = NULL;
351 hotkey->key = 0;
352 hotkey->mask = 0;
353 hotkey->event = 0;
354 hotkey->type = TYPE_KEY;
355 }
356 text = g_strdup_printf("Hotkey_%d_key", i);
357 aud_cfg_db_get_int (cfdb, "globalHotkey", text, &(hotkey->key));
358 g_free(text);
359
360 text = g_strdup_printf("Hotkey_%d_mask", i);
361 aud_cfg_db_get_int (cfdb, "globalHotkey", text, &(hotkey->mask));
362 g_free(text);
363
364 text = g_strdup_printf("Hotkey_%d_type", i);
365 aud_cfg_db_get_int (cfdb, "globalHotkey", text, &(hotkey->type));
366 g_free(text);
367
368 text = g_strdup_printf("Hotkey_%d_event", i);
369 value = (gint)hotkey->event;
370 aud_cfg_db_get_int (cfdb, "globalHotkey", text, &value);
371 hotkey->event = (EVENT) value;
372 g_free(text);
373 }
308 374
309 aud_cfg_db_close (cfdb); 375 aud_cfg_db_close (cfdb);
310 } 376 }
311 377
312 /* save plugin configuration */ 378 /* save plugin configuration */
313 void save_config (void) 379 void save_config (void)
314 { 380 {
315 ConfigDb *cfdb; 381 ConfigDb *cfdb;
316 382 int max;
317 #define save_key(hotkey) \ 383 HotkeyConfiguration *hotkey;
318 aud_cfg_db_set_int (cfdb, "globalHotkey", #hotkey, plugin_cfg.hotkey.key); \ 384
319 aud_cfg_db_set_int (cfdb, "globalHotkey", #hotkey "_mask", plugin_cfg.hotkey.mask); \
320 aud_cfg_db_set_int (cfdb, "globalHotkey", #hotkey "_type", plugin_cfg.hotkey.type);
321
322 /* open configuration database */ 385 /* open configuration database */
323 cfdb = aud_cfg_db_open ( ); 386 cfdb = aud_cfg_db_open ( );
324 387 hotkey = &(plugin_cfg.first);
325 save_key(mute); 388 max = 0;
326 save_key(vol_up); 389 while (hotkey) {
327 save_key(vol_down); 390 gchar *text = NULL;
328 save_key(play); 391 if (hotkey->key) {
329 save_key(pause); 392 text = g_strdup_printf("Hotkey_%d_key", max);
330 save_key(stop); 393 aud_cfg_db_set_int (cfdb, "globalHotkey", text, hotkey->key);
331 save_key(prev_track); 394 g_free(text);
332 save_key(next_track); 395
333 save_key(jump_to_file); 396 text = g_strdup_printf("Hotkey_%d_mask", max);
334 save_key(forward); 397 aud_cfg_db_set_int (cfdb, "globalHotkey", text, hotkey->mask);
335 save_key(backward); 398 g_free(text);
336 save_key(toggle_win); 399
337 save_key(show_aosd); 400 text = g_strdup_printf("Hotkey_%d_type", max);
401 aud_cfg_db_set_int (cfdb, "globalHotkey", text, hotkey->type);
402 g_free(text);
403
404 text = g_strdup_printf("Hotkey_%d_event", max);
405 aud_cfg_db_set_int (cfdb, "globalHotkey", text, hotkey->event);
406 g_free(text);
407 max++;
408 }
409
410 hotkey = hotkey->next;
411 }
412 aud_cfg_db_set_int (cfdb, "globalHotkey", "NumHotkeys", max);
338 413
339 aud_cfg_db_close (cfdb); 414 aud_cfg_db_close (cfdb);
340 } 415 }
341 416
342 static void cleanup (void) 417 static void cleanup (void)
343 { 418 {
419 HotkeyConfiguration* hotkey;
344 if (!loaded) return; 420 if (!loaded) return;
345 ungrab_keys (); 421 ungrab_keys ();
346 release_filter(); 422 release_filter();
423 hotkey = &(plugin_cfg.first);
424 hotkey = hotkey->next;
425 while (hotkey)
426 {
427 HotkeyConfiguration * old;
428 old = hotkey;
429 hotkey = hotkey->next;
430 free(old);
431 }
432 plugin_cfg.first.next = NULL;
433 plugin_cfg.first.key = 0;
434 plugin_cfg.first.event = 0;
435 plugin_cfg.first.mask = 0;
347 loaded = FALSE; 436 loaded = FALSE;
348 } 437 }
349 438
350 gboolean is_loaded (void) 439 gboolean is_loaded (void)
351 { 440 {