/* $Id: event.c,v 1.2 2009/01/04 15:42:14 kiesling Exp $ -*-c-*-*/

/*
  This file is part of Ctalk.
  Copyright © 2005-2009  Robert Kiesling, ctalk@ctalklang.org.
  Permission is granted to copy this software provided that this copyright
  notice is included in all source code modules.

  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 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 General Public License
  along with this program; if not, write to the Free Software Foundation, 
  Inc., 51 Franklin St., Fifth Floor, Boston, MA 02110-1301 USA.
*/

/*
 *  Event handling.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "object.h"
#include "message.h"
#include "ctalk.h"

#define SIGNALEVENT_CLASS "SignalEvent"
#define APPLICATIONEVENT_CLASS "ApplicationEvent"

int __ctalkNewSignalEventInternal (int signo, int pid, char *text) {
  OBJECT *class_object, *eventlist_object, *new_event,
    *signo_var, *pid_var, *data_var, *t;
  char buf[MAXMSG];

  if ((class_object = __ctalkGetClass ("SignalEvent")) == NULL) {
    __ctalkExceptionInternal (NULL, undefined_class_x, "SignalEvent");
    return ERROR;
  }

  if ((eventlist_object = 
       __ctalkGetClassVariable (class_object, "pendingEvents", TRUE))
      == NULL) {
    return ERROR;
  }

  new_event = __ctalkCreateObjectInit ("__newEvent", 
				       "SignalEvent", "Event",
				       LOCAL_VAR, NULL);
  sprintf (buf, "%d", signo);
  signo_var = __ctalkCreateObjectInit ("sigNo",
				       INTEGER_CLASSNAME,
				       INTEGER_SUPERCLASSNAME,
				       LOCAL_VAR, buf);
  __ctalkAddInstanceVariable (new_event, "sigNo", signo_var);

  sprintf (buf, "%d", pid);
  pid_var = __ctalkCreateObjectInit ("processID",
				       INTEGER_CLASSNAME,
				       INTEGER_SUPERCLASSNAME,
				       LOCAL_VAR, buf);
  __ctalkAddInstanceVariable (new_event, "processID", pid_var);

  data_var = __ctalkCreateObjectInit ("data",
				      STRING_CLASSNAME,
				      STRING_SUPERCLASSNAME,
				      LOCAL_VAR, text);
  __ctalkAddInstanceVariable (new_event, "data", data_var);

  __objRefCntSet (&new_event, eventlist_object -> nrefs);

  if (eventlist_object -> instancevars) {
    for (t = eventlist_object -> instancevars; t && t -> next; t = t -> next)
      ;
    t -> next = new_event;
    new_event -> prev = t;
  } else {
    eventlist_object -> instancevars = new_event;
  }

  return SUCCESS;
}

int __ctalkNewApplicationEventInternal (int pid, char *text) {
  return SUCCESS;
}
