1 2 3 #include "LEDAnimation.h" 4 5 #include <InterfaceDefs.h> 6 7 #include <stdio.h> 8 9 #define SNOOZE_TIME 150000 10 11 12 LEDAnimation::LEDAnimation() 13 : 14 fThread(-1), 15 fRunning(false), 16 fOrigModifiers(::modifiers()) 17 { 18 } 19 20 21 LEDAnimation::~LEDAnimation() 22 { 23 Stop(); 24 } 25 26 27 void 28 LEDAnimation::Start() 29 { 30 // don't do anything if the thread is already running 31 if (fThread >= 0) 32 return; 33 34 fOrigModifiers = ::modifiers(); 35 ::set_keyboard_locks(0); 36 fRunning = true; 37 fThread = ::spawn_thread(AnimationThread,"LED thread",B_NORMAL_PRIORITY,this); 38 ::resume_thread(fThread); 39 } 40 41 42 void 43 LEDAnimation::Stop() 44 { 45 // don't do anything if the thread doesn't run 46 if (fThread < 0) 47 return; 48 49 fRunning = false; 50 status_t result; 51 ::wait_for_thread(fThread,&result); 52 53 ::set_keyboard_locks(fOrigModifiers); 54 } 55 56 57 int32 58 LEDAnimation::AnimationThread(void* data) 59 { 60 LEDAnimation *anim = (LEDAnimation*)data; 61 62 while (anim->fRunning) { 63 LED(B_NUM_LOCK,true); 64 LED(B_NUM_LOCK,false); 65 66 LED(B_CAPS_LOCK,true); 67 LED(B_CAPS_LOCK,false); 68 69 LED(B_SCROLL_LOCK,true); 70 LED(B_SCROLL_LOCK,false); 71 72 LED(B_CAPS_LOCK,true); 73 LED(B_CAPS_LOCK,false); 74 } 75 anim->fThread = -1; 76 return 0; 77 } 78 79 80 void 81 LEDAnimation::LED(uint32 mod,bool on) 82 { 83 uint32 current_modifiers = ::modifiers(); 84 if (on) 85 current_modifiers |= mod; 86 else 87 current_modifiers &= ~mod; 88 ::set_keyboard_locks(current_modifiers); 89 if (on) 90 ::snooze(SNOOZE_TIME); 91 } 92