medit/moo/mooterm/mootermpt.c

253 lines
5.5 KiB
C
Raw Normal View History

2005-06-22 11:20:32 -07:00
/*
2007-06-24 10:56:20 -07:00
* mootermpt.c
2005-06-22 11:20:32 -07:00
*
2007-04-07 01:21:52 -07:00
* Copyright (C) 2004-2007 by Yevgen Muntyan <muntyan@math.tamu.edu>
2005-06-22 11:20:32 -07:00
*
2007-06-24 10:56:20 -07:00
* 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.
2005-06-22 11:20:32 -07:00
*
* See COPYING file that comes with this distribution.
*/
#define MOOTERM_COMPILATION
#include "mooterm/mootermpt-private.h"
#include "mooterm/mooterm-private.h"
2005-06-22 15:13:12 -07:00
#include "mooutils/moomarshals.h"
2005-06-22 11:20:32 -07:00
2005-07-07 04:06:23 -07:00
static void moo_term_pt_finalize (GObject *object);
2005-06-22 11:20:32 -07:00
2005-07-07 04:06:23 -07:00
/* MOO_TYPE_TERM_PT */
G_DEFINE_TYPE (MooTermPt, moo_term_pt, G_TYPE_OBJECT)
2005-06-22 11:20:32 -07:00
enum {
2005-06-22 15:13:12 -07:00
CHILD_DIED,
2005-06-22 11:20:32 -07:00
LAST_SIGNAL
};
enum {
PROP_0
2005-06-22 11:20:32 -07:00
};
2005-06-22 15:13:12 -07:00
static guint signals[LAST_SIGNAL];
2005-06-22 11:20:32 -07:00
static void
moo_term_pt_class_init (MooTermPtClass *klass)
2005-06-22 11:20:32 -07:00
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
2005-07-07 04:06:23 -07:00
gobject_class->finalize = moo_term_pt_finalize;
2005-06-22 11:20:32 -07:00
2005-06-22 15:13:12 -07:00
signals[CHILD_DIED] =
g_signal_new ("child-died",
G_OBJECT_CLASS_TYPE (gobject_class),
G_SIGNAL_RUN_LAST,
2005-07-07 04:06:23 -07:00
G_STRUCT_OFFSET (MooTermPtClass, child_died),
2005-06-22 15:13:12 -07:00
NULL, NULL,
_moo_marshal_VOID__VOID,
G_TYPE_NONE, 0);
2005-06-22 11:20:32 -07:00
}
static void
moo_term_pt_init (MooTermPt *pt)
2005-06-22 11:20:32 -07:00
{
pt->pending_write = g_queue_new ();
pt->child_alive = FALSE;
pt->alive = FALSE;
pt->width = 80;
pt->height = 24;
pt->echo = TRUE;
pt->priority = PT_READER_PRIORITY;
2005-06-22 11:20:32 -07:00
}
static void
moo_term_pt_finalize (GObject *object)
2005-06-22 11:20:32 -07:00
{
2005-07-07 04:06:23 -07:00
MooTermPt *pt = MOO_TERM_PT (object);
2005-06-22 11:20:32 -07:00
pt_discard_pending_write (pt);
g_queue_free (pt->pending_write);
G_OBJECT_CLASS (moo_term_pt_parent_class)->finalize (object);
2005-06-22 11:20:32 -07:00
}
MooTermPt*
moo_term_pt_new (MooTermIOFunc func,
gpointer data)
2005-06-22 11:20:32 -07:00
{
MooTermPt *pt;
g_return_val_if_fail (func != NULL, NULL);
2005-06-22 15:13:12 -07:00
#ifdef __WIN32__
2006-04-06 14:23:00 -07:00
pt = g_object_new (MOO_TYPE_TERM_PT_CYG, NULL);
2005-06-22 15:13:12 -07:00
#else /* !__WIN32__ */
pt = g_object_new (MOO_TYPE_TERM_PT_UNIX, NULL);
2005-06-22 15:13:12 -07:00
#endif /* !__WIN32__ */
pt->io_func = func;
pt->io_func_data = data;
return pt;
2005-06-22 15:13:12 -07:00
}
void
moo_term_pt_set_priority (MooTermPt *pt,
int priority)
{
g_return_if_fail (MOO_IS_TERM_PT (pt));
pt->priority = priority;
}
void
_moo_term_pt_set_io_size_func (MooTermPt *pt,
MooTermIOSizeFunc size_func,
gpointer data)
{
g_return_if_fail (MOO_IS_TERM_PT (pt));
pt->size_func = size_func;
pt->size_func_data = data;
}
gsize
_moo_term_pt_get_input_chunk_len (MooTermPt *pt,
gsize max_len)
{
gsize len;
if (pt->size_func)
len = pt->size_func (pt->size_func_data);
else
len = 1024;
return MIN (len, max_len);
}
void
_moo_term_pt_process_data (MooTermPt *pt,
const char *data,
int len)
{
g_object_ref (pt);
if (len < 0)
len = strlen (data);
pt->io_func (data, len, pt->io_func_data);
g_object_unref (pt);
}
2005-06-22 15:13:12 -07:00
void
moo_term_pt_set_size (MooTermPt *pt,
guint width,
guint height)
2005-06-22 15:13:12 -07:00
{
2005-07-07 04:06:23 -07:00
g_return_if_fail (MOO_IS_TERM_PT (pt));
g_return_if_fail (width > 0);
g_return_if_fail (height > 0);
2005-07-07 04:06:23 -07:00
MOO_TERM_PT_GET_CLASS(pt)->set_size (pt, width, height);
2005-06-22 15:13:12 -07:00
}
void
moo_term_pt_set_echo_input (MooTermPt *pt,
gboolean echo)
{
g_return_if_fail (MOO_IS_TERM_PT (pt));
g_return_if_fail (MOO_TERM_PT_GET_CLASS(pt)->set_echo_input != NULL);
MOO_TERM_PT_GET_CLASS(pt)->set_echo_input (pt, echo);
}
2005-06-22 15:13:12 -07:00
gboolean
moo_term_pt_fork_command (MooTermPt *pt,
const MooTermCommand *cmd,
GError **error)
2005-06-22 15:13:12 -07:00
{
MooTermCommand *copy;
gboolean result;
2005-07-07 04:06:23 -07:00
g_return_val_if_fail (MOO_IS_TERM_PT (pt), FALSE);
g_return_val_if_fail (cmd != NULL, FALSE);
copy = moo_term_command_copy (cmd);
result = _moo_term_check_cmd (copy, error);
if (!result)
{
moo_term_command_free (copy);
return FALSE;
}
result = MOO_TERM_PT_GET_CLASS(pt)->fork_command (pt, copy, error);
moo_term_command_free (copy);
return result;
2005-07-26 04:12:40 -07:00
}
2006-04-06 14:23:00 -07:00
char
_moo_term_pt_get_erase_char (MooTermPt *pt)
{
g_return_val_if_fail (MOO_IS_TERM_PT (pt), 0);
return MOO_TERM_PT_GET_CLASS(pt)->get_erase_char (pt);
}
2006-04-06 14:23:00 -07:00
void
moo_term_pt_send_intr (MooTermPt *pt)
{
g_return_if_fail (MOO_IS_TERM_PT (pt));
MOO_TERM_PT_GET_CLASS(pt)->send_intr (pt);
}
void
moo_term_pt_kill_child (MooTermPt *pt)
2005-06-22 15:13:12 -07:00
{
2005-07-07 04:06:23 -07:00
g_return_if_fail (MOO_IS_TERM_PT (pt));
MOO_TERM_PT_GET_CLASS(pt)->kill_child (pt);
2005-06-22 11:20:32 -07:00
}
void
moo_term_pt_write (MooTermPt *pt,
const char *data,
gssize len)
2005-06-22 11:20:32 -07:00
{
2005-07-07 04:06:23 -07:00
g_return_if_fail (MOO_IS_TERM_PT (pt));
g_return_if_fail (data != NULL);
2005-07-07 04:06:23 -07:00
MOO_TERM_PT_GET_CLASS(pt)->write (pt, data, len);
2005-06-22 11:20:32 -07:00
}
gboolean
moo_term_pt_child_alive (MooTermPt *pt)
{
return pt->child_alive || pt->alive;
2007-01-18 01:21:30 -08:00
}
gboolean
_moo_term_pt_alive (MooTermPt *pt)
{
return pt->alive;
2007-01-18 01:21:30 -08:00
}
gboolean
_moo_term_pt_set_fd (MooTermPt *pt,
int master)
{
g_return_val_if_fail (MOO_IS_TERM_PT (pt), FALSE);
g_return_val_if_fail (!pt->child_alive, FALSE);
2007-01-18 01:21:30 -08:00
g_return_val_if_fail (MOO_TERM_PT_GET_CLASS(pt)->set_fd != NULL, FALSE);
return MOO_TERM_PT_GET_CLASS(pt)->set_fd (pt, master);
}