2 * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/types.h>
36 #include <sys/sysctl.h>
37 #include <sys/sysctl.h>
38 #include <sys/socket.h>
40 #include <net/if_mib.h>
45 #include <X11/XKBlib.h>
57 "Copyright (c) 2009 Teodor Sigaev <teodor@sigaev.ru>. All rights reserved.\n"
58 "blinker - blink by numlock/scroll leds accordingly with network activity\n"
79 datalen = sizeof(data);
80 if (sysctl(name, 5, &data, &datalen, NULL, 0) != 0)
81 tlog(TL_CRIT|TL_EXIT,"sysctl failed: %s", strerror(errno));
87 getifmibdata(int row, struct ifmibdata *data)
98 datalen = sizeof(*data);
101 if ((sysctl(name, 6, data, &datalen, NULL, 0) != 0) && (errno != ENOENT)) {
102 tlog(TL_WARN,"sysctl failed: %s", strerror(errno));
112 typedef struct Blinker {
113 void (*exec)(struct Blinker *, int, int);
125 execKbd(struct Blinker * blinker, int RxAction, int TxAction) {
126 if ( !(blinker->RxOn == RxAction && blinker->TxOn == TxAction) ) {
129 if (ioctl(0, KDGETLED, &flag) == -1)
130 tlog(TL_WARN,"unable to get led's statuses: %s",
134 flag |= blinker->RxLed;
136 flag &= ~blinker->RxLed;
138 flag |= blinker->TxLed;
140 flag &= ~blinker->TxLed;
142 if (ioctl(0, KDSETLED, flag) == -1)
143 tlog(TL_WARN,"unable to set NumLock led: %s",
146 blinker->RxOn = RxAction;
147 blinker->TxOn = TxAction;
153 execXKbd(struct Blinker * blinker, int RxAction, int TxAction) {
154 static XKeyboardControl value;
156 if ( blinker->RxOn != RxAction ) {
157 value.led = blinker->RxLed;
158 value.led_mode = (RxAction) ? LedModeOn : LedModeOff;
159 XChangeKeyboardControl((Display*)blinker->arg, KBLed | KBLedMode, &value);
162 if ( blinker->TxOn != TxAction ) {
163 value.led = blinker->TxLed;
164 value.led_mode = (TxAction) ? LedModeOn : LedModeOff;
165 XChangeKeyboardControl((Display*)blinker->arg, KBLed | KBLedMode, &value);
168 if ( !(blinker->RxOn == RxAction && blinker->TxOn == TxAction) )
170 XFlush((Display*)blinker->arg);
171 blinker->RxOn = RxAction;
172 blinker->TxOn = TxAction;
177 typedef struct statData {
182 static int LetWork = 1;
190 main(int argc, char *argv[]) {
191 keyboard_info_t info;
194 struct ifmibdata data;
195 statData curData = {0,0},
198 while( (i=getopt(argc, argv, "d:h"))!=-1) {
206 opentlog(TL_OPEN_STDERR, TL_INFO, NULL);
207 signal(SIGHUP, stopWork);
208 signal(SIGSTOP, stopWork);
209 signal(SIGINT, stopWork);
211 memset(&blinker, 0, sizeof(blinker));
213 if (ioctl(0, KDGKBINFO, &info) == 0) {
214 blinker.TxLed = LED_CAP;
215 blinker.RxLed = LED_NUM;
216 blinker.exec = execKbd;
219 tlog(TL_WARN,"unable to obtain keyboard information: %s%s",
222 (errno == ENOTTY) ? "; Try X-windows interface." :
230 dpy = XOpenDisplay(NULL);
232 tlog(TL_CRIT|TL_EXIT, "Could not open default display");
234 if ( (xkb= XkbGetMap(dpy,0,XkbUseCoreKbd)) == NULL ||
235 XkbGetIndicatorMap(dpy,XkbAllIndicatorsMask,xkb)!=Success ||
236 XkbGetNames(dpy,XkbAllNamesMask,xkb)!=Success)
237 tlog(TL_CRIT|TL_EXIT, "Fail to get indicator's descriptions");
239 for (i=0;i<XkbNumIndicators;i++) {
240 if (xkb->indicators->phys_indicators & (1<<i)) {
241 char *name = XGetAtomName(dpy, xkb->names->indicators[i]);
242 XkbIndicatorMapPtr map= &xkb->indicators->maps[i];
244 if (name == NULL || *name == '\0')
247 if (strcmp(name, "Caps Lock") == 0)
248 blinker.TxLed = i + 1;
249 else if (strcmp(name, "Num Lock") == 0)
250 blinker.RxLed = i + 1;
254 if (map->flags & XkbIM_NoExplicit) {
256 XkbIndicatorMapPtr newmap = (XkbIndicatorMapPtr)tmalloc(sizeof(XkbIndicatorMapRec));
259 newmap->flags &= ~XkbIM_NoExplicit;
261 if (XkbSetNamedIndicator(dpy, xkb->names->indicators[i], False, False, True, newmap) == False)
262 tlog(TL_WARN,"XkbSetNamedIndicator fails");
267 if (blinker.RxLed == 0 || blinker.TxLed == 0)
268 tlog(TL_CRIT|TL_EXIT, "Fail to identify leds");
271 blinker.exec = execXKbd;
277 * switch off leds - initial state
279 blinker.TxOn = blinker.RxOn = 1;
280 blinker.exec(&blinker, 0, 0);
285 memset(&curData, 0, sizeof(curData));
286 for(i=1;i<=getifnum();i++) {
287 if (getifmibdata(i, &data) != 0)
289 if (data.ifmd_name[0] == 'l' && data.ifmd_name[1] == 'o' )
292 curData.ip += data.ifmd_data.ifi_ipackets;
293 curData.op += data.ifmd_data.ifi_opackets;
298 (curData.ip == oldData.ip) ? 0 : 1, /* Tx */
299 (curData.op == oldData.op) ? 0 : 1 /* Rx */
305 blinker.exec(&blinker, 0, 0);
308 XCloseDisplay((Display*)blinker.arg);