/*----------------------------------------------------------------------------- * wakeup.cpp * Copyright (C) 2003, 2004, 2005 Akito Nozaki * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; 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 #include NAMESPACE_TANOSHI { Wakeup::Wakeup(Object *parent) : Object(parent) { _init = 0; } Wakeup::~Wakeup() { stop(); } void Wakeup::start() { if(!_init) { socketpair(AF_UNIX, SOCK_STREAM, 0, _pfds); int flags = fcntl( _pfds[0], F_GETFL, 0); fcntl(_pfds[0], F_SETFL, flags | O_NONBLOCK); flags = fcntl( _pfds[1], F_GETFL, 0); fcntl(_pfds[1], F_SETFL, flags | O_NONBLOCK); setFd(_pfds[0]); setEvents(POLLIN); addToEvent(); _init = 1; } } void Wakeup::stop() { removeFromEvent(); if(_init) { close(_pfds[0]); close(_pfds[1]); _init = 0; } } void Wakeup::wakeup() { if(!_init) return; int i; if(i = ::send(_pfds[1], "a", 1, 0) < 0) { Timer::runOnce(0, this)->timeout.connect( sigc::mem_fun(this, &Wakeup::wakeup)); } } bool Wakeup::event (Event *e) { if(e->id() != id()) return false; // NOTE: I'm not sure if this should be grabing a chunk of // data at a time, or wakeup for single byte that is // sent. char buf[10]; int t = recv(_pfds[0], buf, 10, 0); if(t == 0) { removeFromEvent(); close(_pfds[1]); } else if(t > 0) { if(buf[0] == 'a') wakeupCall(); } return true; } }