summaryrefslogtreecommitdiffhomepage
path: root/mixgtk/mixgtk_cmd_dispatcher.c
blob: c5fef58d86a9b0930a0346a4e8c359d6119bf80f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* -*-c-*- -------------- mixgtk_cmd_dispatcher.c :
 * Implementation of the functions declared in mixgtk_cmd_dispatcher.h
 * ------------------------------------------------------------------
 *  Last change: Time-stamp: "01/02/26 03:15:18 jose"
 * ------------------------------------------------------------------
 * Copyright (C) 2001 Free Software Foundation, Inc.
 *  
 * 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.
 *  
 */

#define _GNU_SOURCE 1

#include <stdio.h>
#include <mixlib/mix_vm_command.h>
#include "mixgtk_cmd_dispatcher.h"

/* a mix vm command dispatcher */
struct mixgtk_dispatch_
{
  mix_vm_cmd_dispatcher_t *dispatcher;
  FILE *out;
  char *out_buffer;
  size_t out_buffer_size;
  GtkWidget *prompt;
  GtkWidget *log;
};

static struct mixgtk_dispatch_ dis_data_ = {NULL};

/* global hooks for the command dispatcher */
static void
global_pre_hook_ (mix_vm_cmd_dispatcher_t *dis,
		  mix_vm_command_t cmd, const gchar *arg, gpointer data)
{
  if (cmd < MIX_CMD_INVALID)
    {
      gtk_text_insert (GTK_TEXT (dis_data_.log), NULL, NULL, NULL,
		       "MIX> ", -1);
      gtk_text_insert (GTK_TEXT (dis_data_.log), NULL, NULL, NULL,
		       mix_vm_command_to_string (cmd), -1);
      gtk_text_insert (GTK_TEXT (dis_data_.log), NULL, NULL, NULL,
		       " ", -1);
      gtk_text_insert (GTK_TEXT (dis_data_.log), NULL, NULL, NULL,
		       arg, -1);
      gtk_text_insert (GTK_TEXT (dis_data_.log), NULL, NULL, NULL,
		       "\n", -1);
    }
  else
    {
      gtk_text_insert (GTK_TEXT (dis_data_.log), NULL, NULL, NULL,
		       _("*** Invalid command\n"), -1);
    }
}

static void
global_post_hook_ (mix_vm_cmd_dispatcher_t *dis,
		   mix_vm_command_t cmd, const gchar *arg, gpointer data)
{
  fflush (dis_data_.out);
  if (cmd < MIX_CMD_INVALID)
    {
      gtk_text_insert (GTK_TEXT (dis_data_.log), NULL, NULL, NULL,
		       dis_data_.out_buffer, dis_data_.out_buffer_size);
    }
  rewind (dis_data_.out);
}


/* initialise the command dispatcher */
gboolean
mixgtk_cmd_dispatcher_init (void)
{
  if (!dis_data_.prompt)
    {
      dis_data_.prompt = mixgtk_widget_factory_get (MIXGTK_WIDGET_PROMPT);
      if (!dis_data_.prompt) return FALSE;
      dis_data_.log = mixgtk_widget_factory_get (MIXGTK_WIDGET_LOG);
      if (!dis_data_.log) return FALSE;
    }
    
  if (!dis_data_.dispatcher)
    {
      FILE *out = open_memstream (&(dis_data_.out_buffer),
				  &(dis_data_.out_buffer_size));
      if (!out) return FALSE;
      dis_data_.out = out;
      dis_data_.dispatcher = mix_vm_cmd_dispatcher_new (out, out);
      mix_vm_cmd_dispatcher_global_pre_hook(dis_data_.dispatcher,
					    global_pre_hook_, NULL);
      mix_vm_cmd_dispatcher_global_post_hook(dis_data_.dispatcher,
					     global_post_hook_, NULL);
    }
  return TRUE;
}

/* process new command */
void
on_mixvm_cmd_entry_activate (GtkWidget *w, gpointer e)
{
  gchar *text;
  text = gtk_entry_get_text (GTK_ENTRY (w));
  mix_vm_cmd_dispatcher_dispatch_text (dis_data_.dispatcher, text);
  gtk_entry_set_text (GTK_ENTRY (w), "");
}