summaryrefslogtreecommitdiff
path: root/src/hotkey/plugin.c
blob: 1b42cfe37b67602af8fe142bc73fa8dca789a31a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 *  This file is part of audacious-hotkey plugin for audacious
 *
 *  Copyright (c) 2007 - 2008  Sascha Hlusiak <contact@saschahlusiak.de>
 *  Name: plugin.c
 *  Description: plugin.c
 *
 *  Part of this code is from itouch-ctrl plugin.
 *  Authors of itouch-ctrl are listed below:
 *
 *  Copyright (c) 2006 - 2007 Vladimir Paskov <vlado.paskov@gmail.com>
 *
 *  Part of this code are from xmms-itouch plugin.
 *  Authors of xmms-itouch are listed below:
 *
 *  Copyright (C) 2000-2002 Ville Syrjälä <syrjala@sci.fi>
 *                         Bryn Davies <curious@ihug.com.au>
 *                         Jonathan A. Davis <davis@jdhouse.org>
 *                         Jeremy Tan <nsx@nsx.homeip.net>
 *
 *  audacious-hotkey is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  audacious-hotkey is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with audacious-hotkey; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <config.h>

#include <stdio.h>
#include <X11/XF86keysym.h>

#include <gdk/gdk.h>
#include <gdk/gdkx.h>

#include <audacious/configdb.h>
#include <audacious/drct.h>
#include <audacious/i18n.h>
#include <audacious/plugin.h>
#include <libaudcore/hook.h>

#include "plugin.h"
#include "gui.h"
#include "grab.h"


/* func defs */
static void init (void);
static void cleanup (void);


/* global vars */
static PluginConfig plugin_cfg;
static gboolean loaded = FALSE;



static GeneralPlugin audacioushotkey =
{
	.description = "Global Hotkey",
	.init = init,
	.about = show_about,
	.configure = show_configure,
	.cleanup = cleanup
};

GeneralPlugin *hotkey_gplist[] = { &audacioushotkey, NULL };
SIMPLE_GENERAL_PLUGIN(hotkey, hotkey_gplist);



PluginConfig* get_config(void)
{
	return &plugin_cfg;
}


/*
 * plugin activated
 */
static void init (void)
{
	setup_filter();
	load_config ( );
	grab_keys ( );

	loaded = TRUE;
}

/* handle keys */
gboolean handle_keyevent (EVENT event)
{
	gint current_volume, old_volume;
	static gint volume_static = 0;
	gboolean play, mute;

	/* playing or not */
	play = aud_drct_get_playing ();

	/* get current volume */
	aud_drct_get_volume_main (&current_volume);
	old_volume = current_volume;
	if (current_volume)
	{
		/* volume is not mute */
		mute = FALSE;
	} else {
		/* volume is mute */
		mute = TRUE;
	}

	/* mute the playback */
	if (event == EVENT_MUTE)
	{
		if (!mute)
		{
			volume_static = current_volume;
			aud_drct_set_volume_main (0);
			mute = TRUE;
		} else {
			aud_drct_set_volume_main (volume_static);
			mute = FALSE;
		}
		return TRUE;
	}

	/* decreace volume */
	if (event == EVENT_VOL_DOWN)
	{
		if (mute)
		{
			current_volume = old_volume;
			old_volume = 0;
			mute = FALSE;
		}

		if ((current_volume -= plugin_cfg.vol_decrement) < 0)
		{
			current_volume = 0;
		}

		if (current_volume != old_volume)
		{
			aud_drct_set_volume_main (current_volume);
		}

		old_volume = current_volume;
		return TRUE;
	}

	/* increase volume */
	if (event == EVENT_VOL_UP)
	{
		if (mute)
		{
			current_volume = old_volume;
			old_volume = 0;
			mute = FALSE;
		}

		if ((current_volume += plugin_cfg.vol_increment) > 100)
		{
			current_volume = 100;
		}

		if (current_volume != old_volume)
		{
			aud_drct_set_volume_main (current_volume);
		}

		old_volume = current_volume;
		return TRUE;
	}

	/* play */
	if (event == EVENT_PLAY)
	{
		aud_drct_play ();
		return TRUE;
	}

	/* pause */
	if (event == EVENT_PAUSE)
	{
		if (!play) aud_drct_play ();
		else aud_drct_pause ();

		return TRUE;
	}

	/* stop */
	if (event == EVENT_STOP)
	{
		aud_drct_stop ();
		return TRUE;
	}

	/* prev track */
	if (event == EVENT_PREV_TRACK)
	{
		aud_drct_pl_prev ();
		return TRUE;
	}

	/* next track */
	if (event == EVENT_NEXT_TRACK)
	{
		aud_drct_pl_next ();
		return TRUE;
	}

	/* forward */
	if (event == EVENT_FORWARD)
	{
		aud_drct_seek (aud_drct_get_time () + 5000);
		return TRUE;
	}

	/* backward */
	if (event == EVENT_BACKWARD)
	{
		gint time = aud_drct_get_time ();
		if (time > 5000) time -= 5000; /* Jump 5s back */
			else time = 0;
		aud_drct_seek (time);
		return TRUE;
	}

	/* Open Jump-To-File dialog */
	if (event == EVENT_JUMP_TO_FILE)
	{
		hook_call ("interface show jump to track", NULL);
		return TRUE;
	}

	/* Toggle Windows */
	if (event == EVENT_TOGGLE_WIN)
	{
		hook_call ("interface toggle visibility", NULL);
		return TRUE;
	}

	/* Show OSD through AOSD plugin*/
	if (event == EVENT_SHOW_AOSD)
	{
		hook_call("aosd toggle", NULL);
		return TRUE;
	}

	return FALSE;
}

void add_hotkey(HotkeyConfiguration** pphotkey, KeySym keysym, gint mask, gint type, EVENT event)
{
	KeyCode keycode;
	HotkeyConfiguration *photkey;
	if (keysym == 0) return;
	if (pphotkey == NULL) return;
	photkey = *pphotkey;
	if (photkey == NULL) return;
	keycode = XKeysymToKeycode(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()), keysym);
	if (keycode == 0) return;
	if (photkey->key) {
		photkey->next = (HotkeyConfiguration*)
			malloc(sizeof (HotkeyConfiguration));
		photkey = photkey->next;
		*pphotkey = photkey;
		photkey->next = NULL;
	}
	photkey->key = (gint)keycode;
	photkey->mask = mask;
	photkey->event = event;
	photkey->type = type;
}

void load_defaults (void)
{
	HotkeyConfiguration* hotkey;

	hotkey = &(plugin_cfg.first);

	add_hotkey(&hotkey, XF86XK_AudioPrev, 0, TYPE_KEY, EVENT_PREV_TRACK);
	add_hotkey(&hotkey, XF86XK_AudioPlay, 0, TYPE_KEY, EVENT_PLAY);
	add_hotkey(&hotkey, XF86XK_AudioPause, 0, TYPE_KEY, EVENT_PAUSE);
	add_hotkey(&hotkey, XF86XK_AudioStop, 0, TYPE_KEY, EVENT_STOP);
	add_hotkey(&hotkey, XF86XK_AudioNext, 0, TYPE_KEY, EVENT_NEXT_TRACK);

/*	add_hotkey(&hotkey, XF86XK_AudioRewind, 0, TYPE_KEY, EVENT_BACKWARD); */

	add_hotkey(&hotkey, XF86XK_AudioMute, 0, TYPE_KEY, EVENT_MUTE);
	add_hotkey(&hotkey, XF86XK_AudioRaiseVolume, 0, TYPE_KEY, EVENT_VOL_UP);
	add_hotkey(&hotkey, XF86XK_AudioLowerVolume, 0, TYPE_KEY, EVENT_VOL_DOWN);

/*	add_hotkey(&hotkey, XF86XK_AudioMedia, 0, TYPE_KEY, EVENT_JUMP_TO_FILE);
	add_hotkey(&hotkey, XF86XK_Music, 0, TYPE_KEY, EVENT_TOGGLE_WIN); */
}

/* load plugin configuration */
void load_config (void)
{
	mcs_handle_t *cfdb;
	HotkeyConfiguration *hotkey;
	int i,max;

	/* default volume level */
	plugin_cfg.vol_increment = 4;
	plugin_cfg.vol_decrement = 4;

	/* open configuration database */
	cfdb = aud_cfg_db_open ( );
	hotkey = &(plugin_cfg.first);
	hotkey->next = NULL;
	hotkey->key = 0;
	hotkey->mask = 0;
	hotkey->event = 0;
	hotkey->type = TYPE_KEY;
	max = 0;
	aud_cfg_db_get_int (cfdb, "globalHotkey", "NumHotkeys", &max);
	if (max == 0)
		load_defaults();
	else for (i=0; i<max; i++)
	{
		gchar *text = NULL;
		gint value;
		if (hotkey->key) {
			hotkey->next = (HotkeyConfiguration*)
				malloc(sizeof (HotkeyConfiguration));
			hotkey = hotkey->next;
			hotkey->next = NULL;
			hotkey->key = 0;
			hotkey->mask = 0;
			hotkey->event = 0;
			hotkey->type = TYPE_KEY;
		}
		text = g_strdup_printf("Hotkey_%d_key", i);
		aud_cfg_db_get_int (cfdb, "globalHotkey", text, &(hotkey->key));
		g_free(text);

		text = g_strdup_printf("Hotkey_%d_mask", i);
		aud_cfg_db_get_int (cfdb, "globalHotkey", text, &(hotkey->mask));
		g_free(text);

		text = g_strdup_printf("Hotkey_%d_type", i);
		aud_cfg_db_get_int (cfdb, "globalHotkey", text, &(hotkey->type));
		g_free(text);

		text = g_strdup_printf("Hotkey_%d_event", i);
		value = (gint)hotkey->event;
		aud_cfg_db_get_int (cfdb, "globalHotkey", text, &value);
		hotkey->event = (EVENT) value;
		g_free(text);
	}

	aud_cfg_db_close (cfdb);
}

/* save plugin configuration */
void save_config (void)
{
	mcs_handle_t *cfdb;
	int max;
	HotkeyConfiguration *hotkey;

	/* open configuration database */
	cfdb = aud_cfg_db_open ( );
	hotkey = &(plugin_cfg.first);
	max = 0;
	while (hotkey) {
		gchar *text = NULL;
		if (hotkey->key) {
			text = g_strdup_printf("Hotkey_%d_key", max);
			aud_cfg_db_set_int (cfdb, "globalHotkey", text, hotkey->key);
			g_free(text);

			text = g_strdup_printf("Hotkey_%d_mask", max);
			aud_cfg_db_set_int (cfdb, "globalHotkey", text, hotkey->mask);
			g_free(text);

			text = g_strdup_printf("Hotkey_%d_type", max);
			aud_cfg_db_set_int (cfdb, "globalHotkey", text, hotkey->type);
			g_free(text);

			text = g_strdup_printf("Hotkey_%d_event", max);
			aud_cfg_db_set_int (cfdb, "globalHotkey", text, hotkey->event);
			g_free(text);
			max++;
		}

		hotkey = hotkey->next;
	}
	aud_cfg_db_set_int (cfdb, "globalHotkey", "NumHotkeys", max);

	aud_cfg_db_close (cfdb);
}

static void cleanup (void)
{
	HotkeyConfiguration* hotkey;
	if (!loaded) return;
	ungrab_keys ();
	release_filter();
	hotkey = &(plugin_cfg.first);
	hotkey = hotkey->next;
	while (hotkey)
	{
		HotkeyConfiguration * old;
		old = hotkey;
		hotkey = hotkey->next;
		free(old);
	}
	plugin_cfg.first.next = NULL;
	plugin_cfg.first.key = 0;
	plugin_cfg.first.event = 0;
	plugin_cfg.first.mask = 0;
	loaded = FALSE;
}

gboolean is_loaded (void)
{
	return loaded;
}