/*
 * (C) Copyright 1992, ..., 2014 the "DOSEMU-Development-Team".
 *
 * for details see file COPYING in the DOSEMU distribution
 */

#include <X11/X.h>
#include <X11/Xlib.h>
#include "X.h"
#include "speaker.h"

/*
 * X speaker emulation
 * =============================================================================
 */
static void X_speaker_on(void *gp, unsigned short period)
{
	XKeyboardControl new_state;
	Display *display = gp;

#if 0
	/* Turn off the previous sound, I hope */
	new_state.bell_pitch = 0;
	new_state.bell_duration = 0;
	XChangeKeyboardControl(display, KBBellDuration | KBBellPitch, &new_state);
	XBell(display, -100);
#endif

	/* Make the sound I want */
	new_state.bell_pitch = speaker_period_to_Hz(period);
	/* Prohibit a crashing X server by not allowing to go higher than
	 * 32767 kHz. Since the X server interprets the pitch as a short int
	 */
	if (new_state.bell_pitch >= 0x8000) new_state.bell_pitch = 0x7fff;
	/* long default, it'll be switched off */
	new_state.bell_duration = period == 0 ? 0 : 30000;
	XChangeKeyboardControl(display, KBBellDuration | KBBellPitch, &new_state);

	/* Reset the sound defaults */
	XBell(display, 100); /* I'm not sure how to get the default here so make it loud. */
	new_state.bell_pitch = -1;  /* reset the sound defaults */
	new_state.bell_duration = -1;
	XChangeKeyboardControl(display, KBBellDuration | KBBellPitch, &new_state);
}

static void X_speaker_off(void*gp)
{
	/* Just make a zero length sound */
	X_speaker_on(gp, 0);
}

void X_register_speaker(Display *display)
{
	register_speaker(display, X_speaker_on, X_speaker_off);
}
