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