Newer
Older
scim-wnn / honoka / libhonoka / honokatimer.cpp
/***************************************************************************
 *   Copyright (C) 2005 by TAM(Teppei Tamra)                               *
 *   tam-t@par.odn.ne.jp                                                   *
 *                                                                         *
 *   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 "honokatimer.h"
#include <signal.h>
#include <sys/wait.h>

using namespace Honoka;

HonokaTimer::HonokaTimer()
{
    bef = 1;
    eventCount = 0;
    timer_pid = -1;
    scim::HelperInfo info(
        HONOKA_TIMER_UUID,
        "Honoka Timer",
        "",
        "Honoka Timer",
        SCIM_HELPER_STAND_ALONE);
    String display = getenv("DISPLAY");
    id = agent.open_connection(info,display);
}


HonokaTimer::~HonokaTimer()
{
    if (id != -1) agent.close_connection();
    if (timer_pid > 0) {
        kill(timer_pid,SIGKILL);
        wait(NULL);
    }
}

HonokaTimer * HonokaTimer::_self = 0;



/*!
    \fn Honoka::HonokaTimer::self()
 */
HonokaTimer * Honoka::HonokaTimer::self()
{
    if (!_self) {
        _self = new HonokaTimer();
        _self->exec();
    }
    return _self;
}


/*!
    \fn Honoka::HonokaTimer::destruct()
 */
void Honoka::HonokaTimer::destruct()
{
    if (_self == 0) return;
    delete _self;
    _self = 0;
}


/*!
    \fn Honoka::HonokaTimer::timer()
 */
void Honoka::HonokaTimer::timer()
{
    Transaction trans;
    uint32 counter = 1;
    while(1) {
        usleep(HONOKA_TIMER_INTERVAL);
        trans.clear();
        trans.put_command(HONOKA_TRANS_TIMER);
        trans.put_data(counter);
        agent.send_imengine_event(-1,"8bb03c1c-db6c-41b1-91bd-b7fb7dd70343",trans);
        if ((counter ++) == 0) counter ++;
    }
}


/*!
    \fn Honoka::HonokaTimer::exec()
 */
void Honoka::HonokaTimer::exec()
{
    timer_pid = fork();
    if (timer_pid == 0) timer();
}


/*!
    \fn Honoka::HonokaTimer::decode(const Transaction &trans)
 */
uint32 Honoka::HonokaTimer::decode(const Transaction &trans)
{
    TransactionReader reader(trans);
    int cmd;
    if (reader.get_command(cmd)) {
        if (cmd == HONOKA_TRANS_TIMER) {
            uint32 c;
            reader.get_data(c);
            return c;
        }
    }
    return 0;
}


/*!
    \fn Honoka::HonokaTimer::eventFilter(const Transaction &trans)
 */
vector<int> Honoka::HonokaTimer::eventFilter(const Transaction &trans)
{
    vector<int> res;
    uint32 c = decode(trans);
    if (c == 0) return res;
    for(uint32 i = bef;i != c;i ++) {
        map<uint32,int>::iterator it = events.find(i);
        if (it == events.end()) break;
        res.push_back(it->second);
        events.erase(it);
    }
    bef = c;
    return res;
}


/*!
    \fn Honoka::HonokaTimer::appendDelayEvent(uint32 delay)
 */
int Honoka::HonokaTimer::appendDelayEvent(uint32 delay)
{
    eventCount ++;
    events.insert(pair<uint32,int>(bef + delay,eventCount));
    return eventCount;
}