fixed path to external icons
[shermanaquarium] / sherman-aquarium / shermans / leds.c
1
2
3 #include <X11/XKBlib.h>
4 #include <stdio.h>
5 #include "leds.h"
6 #include "aquarium.h"
7 #include "draw.h"
8
9 static SA_Image *led_images = NULL;
10 static int num_led_images = 0;
11 static Leds_settings leds_settings;
12 static AquariumData *ad;
13
14 Leds_settings *leds_get_settings_ptr(void)
15 {
16     return &leds_settings;
17 }
18
19 /*
20   Returns the turned on leds:
21   Bit 0 is Capslock
22   Bit 1 is Numlock
23   Bit 2 is Scrollock
24 */
25
26 /*
27   This code is based upon some lines(actually two lines :-) 
28   in E-Leds by Mathias Meisfjordskar<mathiasm@ifi.uio.no>
29   Released under GPL.
30 */
31
32 static int check_kleds(void)
33 {
34     unsigned int states;
35
36     if (XkbGetIndicatorState(GDK_DISPLAY(), XkbUseCoreKbd, &states) != Success) {
37         perror("Error while reading Indicator status\n");
38         return -1;
39     }
40     return (states & 0x7);
41 }
42
43 static int get_capslock(void)
44 {
45     if((check_kleds() & 1) == 1) return 1;
46     else return 0;
47 }
48
49 static int get_numlock(void)
50 {
51     if((check_kleds() & 2) == 2) return 1;
52     else return 0;
53 }
54
55 static int get_scrollock(void)
56 {
57     if((check_kleds() & 4) == 4) return 1;
58     else return 0;
59 }
60
61
62 static void do_led(int func, int colour, int show_off, int x, int y, int alpha)
63 {
64     int is_on;
65     
66     switch(func){
67     case LEDS_OFF:
68         return;
69     case LEDS_CAPSLOCK:
70         is_on = get_capslock();
71         break;
72     case LEDS_NUMLOCK:
73         is_on = get_numlock();
74         break;
75     case LEDS_SCROLLOCK:
76         is_on = get_scrollock();
77         break;
78     default:
79         is_on=0;
80         break;
81     }
82     
83     
84     if((!is_on && show_off) || is_on)
85         draw_pic_alpha(led_images[colour].image,
86                        led_images[colour].width,
87                        led_images[colour].height,
88                        x, y, !is_on, alpha);
89
90 }
91
92 void leds_exit(void)
93 {
94     int i;
95
96     if(led_images == NULL) 
97         return;
98
99     for(i = 0; i < num_led_images; i++){
100         if(led_images[i].image != NULL)
101             g_object_unref(led_images[i].pixbuf);
102     }
103
104     g_free(led_images);
105     led_images = NULL;
106     num_led_images = 0;
107
108 }
109
110 void leds_init(void)
111 {
112     int i;
113     char *led_files[] = {
114     "leds/blue.png",
115     "leds/bluegreen.png",
116     "leds/orange.png",
117     "leds/red.png",
118     "leds/violet.png",
119     "leds/yellow.png",
120     "leds/pink.png",
121     "leds/green.png",
122     "leds/darkblue.png",
123     "leds/lightblue.png",
124     "leds/yellowgreen.png",
125     NULL};
126
127     ad = aquarium_get_settings_ptr();
128
129     if(led_images != NULL)
130         leds_exit();
131
132     for(num_led_images=0; led_files[num_led_images] != NULL; num_led_images++);
133
134     led_images = g_malloc0(sizeof(SA_Image) * num_led_images);
135
136     for(i = 0; i < num_led_images; i++)
137         load_image(led_files[i], &led_images[i], 2);
138 }
139
140
141
142
143
144 void leds_update(int beforeorafter)
145 {
146     int numleds=0;
147     int x = 2, y = 2, i;
148
149     if(beforeorafter != leds_settings.draw)
150         return;
151
152     for(i = 0; i < 3 ; i++)
153         if(leds_settings.leds_func[i] != LEDS_OFF)
154             numleds++;
155
156     switch(leds_settings.horz){
157     case LEFT:
158         x = 2;
159         break;
160     case RIGHT:
161         x = ad->xmax-numleds * led_images[0].width - 2;
162         break;
163     case CENTER:
164         x = (ad->xmax-numleds * led_images[0].width) / 2;
165         break;
166     default:
167         break;
168     }
169
170     switch(leds_settings.vert){
171     case TOP:
172         y = 2;
173         break;
174     case BOTTOM:
175         y = ad->ymax-numleds * led_images[0].height - 2;
176         break;
177     case CENTER:
178         y = (ad->ymax-numleds * led_images[0].height) / 2;
179         break;
180     default:
181         break;
182     }
183
184     for(i = 0; i < NUMLEDS; i++)
185         do_led(leds_settings.leds_func[i], 
186                leds_settings.leds_colour[i],
187                leds_settings.leds_show_off[i], 
188                x + !leds_settings.vert_horz * led_images[0].width * i, 
189                y + leds_settings.vert_horz * led_images[0].height * i, leds_settings.alpha);
190
191 }