/* * Copyright (c) 2001 Ross Crawford * * 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 * */ #include #include #include #include #include #include #include #include time_t lr_timeoff; // all keys off if no data received before... unsigned int lr_curkeys; // mask of keys currently "ON" unsigned int lr_data; // lnp data byte int lr_dataready = 0; // lr_data valid? lr_handler_t lr_handler; // the user event handler void lr_getdata(unsigned int x) { // If previous data hasn't been processed yet, this will be lost if (lr_dataready == 0) { lr_data = x; lr_dataready = 1; } // Reset timeout lr_timeoff = sys_time + LR_TIMEOUT; } void lr_process() { unsigned int keys_on, keys_off, common_keys, k; // If keys pressed has changed if (lr_data != lr_curkeys) { // Get mask of keys pressed & released since last event common_keys = (lr_data & lr_curkeys); keys_on = lr_data & ~common_keys; keys_off = lr_curkeys & ~common_keys; // send event to user handler for each change if (lr_handler) { for (k=1; k; k<<=1) { if (keys_on & k) lr_handler(LREVT_KEYON,k); if (keys_off & k) lr_handler(LREVT_KEYOFF,k); } } // store key mask for next time lr_curkeys = lr_data; } lr_dataready = 0; } wakeup_t lr_waitdata(wakeup_t data) { // if time runs out, fake "all keys off" if (sys_time > lr_timeoff && lr_curkeys != 0) { lr_data = 0; lr_dataready = 1; } // tell lr_thread whether there's any data available return lr_dataready; } // lr_thread just sits waiting for data, processing it as it arrives int lr_thread(int argc, char *argv[]) { while(1) { wait_event(&lr_waitdata,0); lr_process(); } return 0; } int lr_init() { // start with all keys off, set initial timeout, clear user handler lr_curkeys = 0; lr_timeoff = sys_time + LR_TIMEOUT; lr_handler = NULL; // Start watcher thread, then tell lnp where we want remote data to go execi(&lr_thread,0,0,PRIO_HIGHEST-1,DEFAULT_STACK_SIZE); lnp_remote_set_handler(lr_getdata); // currently always returns 0 return 0; }