/*
    GAI-Leds v0.6
    Copyright (C) 2002-2004 Jonas Aaberg <cja@gmx.net>

    This program 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.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Partly based upon E-Leds 0.2 by Mathias Meisfjordskar 
    <mathiasm@ifi.uio.no> and the graphics are taken from it too. 

    * v0.6 prepared for gai 0.6.0
    * v0.5 Updates for gai 0.5.0 - Changes automagically.
    * v0.4 port to gai 0.5.0pre
    * v0.3 includes rotation. Usefull in dockapp mode if you have your
      panel vertical. Gnome takes care of it otherwise.
      The graphic has also some changes.

*/

#include <gai/gai.h>
#include <gdk/gdkx.h>
#include "config.h"
#include <X11/XKBlib.h>

static int rotate_90;

static GdkPixbuf *numlock_on, *numlock_off;
static GdkPixbuf *capslock_on, *capslock_off;
static GdkPixbuf *scrollock_on, *scrollock_off;

static GaiPI leds_pref[] = {{GAI_CHECKBUTTON, "Rotate 90 degrees",&rotate_90, &rotate_90},
			    {GAI_END}};

void leds_update(gpointer d)
{
    unsigned int states;

    if(XkbGetIndicatorState(GDK_DISPLAY(), XkbUseCoreKbd, &states) != Success)
	return;

    if((states&2)==2)
	gai_draw(numlock_on, 0,0,12,11,0,0);
    else
	gai_draw(numlock_off,0,0,12,11,0,0);

    if(rotate_90){
	if((states&1)==1)
	    gai_draw(capslock_on, 0,0,12,11,12,0);
	else
	    gai_draw(capslock_off,0,0,12,11,12,0);

	if((states&4)==4)
	    gai_draw(scrollock_on, 0,0,12,11,24,0);
	else
	    gai_draw(scrollock_off,0,0,12,11,24,0);
    }
    else {

	if((states&1)==1)
	    gai_draw(capslock_on, 0,0,12,11,0,11);
	else
	    gai_draw(capslock_off,0,0,12,11,0,11);

	if((states&4)==4)
	    gai_draw(scrollock_on, 0,0,12,11,0,22);
	else
	    gai_draw(scrollock_off,0,0,12,11,0,22);
    }

    gai_draw_update();
}

void restart_leds(gpointer d)
{
    gai_save_int("leds/rotate",rotate_90);
    if(rotate_90)
	gai_background_from_file("ledsbg_horz.png", GAI_BACKGROUND_MAX_SIZE_IMAGE);
    else
	gai_background_from_file("ledsbg_vert.png", GAI_BACKGROUND_MAX_SIZE_IMAGE);
}


int main(int argc, char **argv)
{

    gai_init2(&applet_defines, &argc, &argv);


    numlock_off = gai_load_image("numlock_off.png");
    numlock_on = gai_load_image("numlock_on.png");

    capslock_off = gai_load_image("capslock_off.png");
    capslock_on = gai_load_image("capslock_on.png");

    scrollock_off = gai_load_image("scrollock_off.png");
    scrollock_on = gai_load_image("scrollock_on.png");


    rotate_90 = gai_load_int_with_default("leds/rotate",0);

    if(rotate_90)
	gai_background_from_file("ledsbg_horz.png", GAI_BACKGROUND_MAX_SIZE_IMAGE);
    else
	gai_background_from_file("ledsbg_vert.png", GAI_BACKGROUND_MAX_SIZE_IMAGE);


    gai_preferences2("Leds preferences", leds_pref, "Press Numlock and look if something happens! ;-)",
			       (GaiCallback0 *)restart_leds, NULL);

    gai_signal_on_update((GaiCallback0 *)leds_update,30, NULL);

    gai_start();

    return 0;    
}


syntax highlighted by Code2HTML, v. 0.9.1