summaryrefslogtreecommitdiffhomepage
path: root/mixgtk/mixgtk_device.c
blob: 2a31afe0c629ff0fdc6f95d1488c32f98f80978e (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* -*-c-*- ---------------- mixgtk_device.c :
 * actual types for mixgtk devices
 * ------------------------------------------------------------------
 *  Last change: Time-stamp: <2001-05-06 23:34:51 jao>
 * ------------------------------------------------------------------
 * 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.
 *  
 */


#include <stdio.h>
#include <mixlib/xmix_device.h>
#include "mixgtk.h"
#include "mixgtk_device.h"

/* device container */
static GtkNotebook *container_ = NULL;
/* virtual machine */
static mix_vm_t *vm_ = NULL;

/* a mixgtk device */
struct mixgtk_device_t
{
  mix_device_t device;
  GtkText *widget;
  gint pos;
};

/* callbacks for output devices */
static void
write_char_ (struct mixgtk_device_t *dev, const mix_word_t *block)
{
  enum {MAX_BLOCK = 16, BUFF_SIZE = MAX_BLOCK * 5 + 2};
  static gchar BUFFER[BUFF_SIZE];
  guint k, j;

  for (k = 0; k < SIZES_[dev->device.type]; k++)
    for (j = 1; j < 6; j++)
      {
	mix_char_t ch = mix_word_get_byte (block[k], j);
	BUFFER[5 * k + j - 1] = mix_char_to_ascii (ch);
      }

  BUFFER[5 * k] = '\n';
  BUFFER[5 * k + 1] = '\0';
  
  gtk_text_insert (dev->widget, NULL, NULL, NULL, BUFFER, -1);
  
}

static void
write_bin_  (struct mixgtk_device_t *dev, const mix_word_t *block)
{
  enum {BUFF_SIZE = 17, STEP = 10};
  static gchar BUFFER[BUFF_SIZE] = { 0 };
  guint k;
  
  for (k = 0; k < SIZES_[dev->device.type]; k++)
    {
      mix_word_print_to_buffer (block[k], BUFFER);
      gtk_text_insert (dev->widget, NULL, NULL, NULL, BUFFER, -1);
      gtk_text_insert (dev->widget, NULL, NULL, NULL,
		       ((k+1)%STEP != 0) ?  " " : "\n", -1);
    }
}

static gboolean
write_ (mix_device_t *dev, const mix_word_t *block)
{
  struct mixgtk_device_t *gtkdev  = (struct mixgtk_device_t *) dev;
  
  if (dev->type != mix_dev_CONSOLE && !(DEF_DEV_VTABLE_->write)(dev, block))
    return FALSE;

  if (MODES_[dev->type] == mix_dev_CHAR) write_char_ (gtkdev, block);
  else write_bin_ (gtkdev, block);
  
  gtk_notebook_set_page (container_, gtkdev->pos);

  return TRUE;
}


static gboolean 
read_ (mix_device_t *dev, mix_word_t *block)
{
  struct mixgtk_device_t *gtkdev  = (struct mixgtk_device_t *) dev;

  if (dev->type != mix_dev_CONSOLE && !(DEF_DEV_VTABLE_->read)(dev, block))
    return FALSE;

  if (MODES_[dev->type] == mix_dev_CHAR) write_char_ (gtkdev, block);
  else write_bin_ (gtkdev, block);
  
  gtk_notebook_set_page (container_, gtkdev->pos);

  return TRUE;
}

static gboolean 
ioc_ (mix_device_t *dev, mix_short_t cmd)
{
  return (DEF_DEV_VTABLE_->ioc)(dev, cmd);
}

static gboolean
busy_ (const mix_device_t *dev) 
{
  return (DEF_DEV_VTABLE_->busy)(dev);
}

static mix_device_vtable_t MIXGTK_VTABLE_ = {write_, read_, ioc_, busy_};


/* create the gui part of the device */
static void
mixgtk_device_construct_gui_ (struct mixgtk_device_t *dev)
{
  static gint last_pos = 0;
  GtkWidget *label = gtk_label_new (DEF_NAMES_[dev->device.type]);
  GtkWidget *box = gtk_hbox_new (0, 0);
  GtkWidget *scroll = NULL;

  g_assert (label);
  g_assert (box);

  dev->widget = (GtkText *)gtk_text_new (NULL, NULL);
  g_assert (dev->widget);
  gtk_text_set_editable (dev->widget, FALSE);

  scroll = gtk_vscrollbar_new (dev->widget->vadj);
  gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (dev->widget),
		      TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (box), scroll, FALSE, FALSE, 0);
  dev->pos = last_pos++;
  gtk_notebook_insert_page (container_, box, label, dev->pos);
  gtk_widget_show (box);
  gtk_widget_show (label);
  gtk_widget_show (scroll);
  gtk_widget_show (GTK_WIDGET (dev->widget));
  gtk_widget_draw (GTK_WIDGET (container_), NULL);
}

/* create a new mixgtk device */
static mix_device_t *
mixgtk_device_new_ (mix_device_type_t type)
{
  struct mixgtk_device_t *dev = NULL;
  gchar *name = NULL;
  
  g_return_val_if_fail (type < mix_dev_INVALID, NULL);

  dev = g_new (struct mixgtk_device_t, 1);
  name = g_strconcat (g_get_home_dir(), "/",
		      MIXGTK_FILES_DIR, "/", DEF_NAMES_[type], NULL);
  construct_device_with_name_ (&dev->device, type, name);
  g_free (name);
  dev->device.vtable = &MIXGTK_VTABLE_;

  mixgtk_device_construct_gui_ (dev);

  return (mix_device_t *)dev;
}

/* init default devices */
gboolean
mixgtk_device_init (GtkNotebook *container, mix_vm_t *vm)
{
  static mix_device_type_t def_types[] = {
    mix_dev_CONSOLE, mix_dev_INVALID
  };
  
  gint k = 0;
				     
  g_return_val_if_fail (container != NULL, FALSE);
  g_return_val_if_fail (vm != NULL, FALSE);
  container_ = container;
  vm_ = vm;

  /* remove dummy page from container */
  gtk_notebook_remove_page (container_, 0);

  mix_vm_set_device_factory (vm, mixgtk_device_new_);
  
  /* connect default devices */
  while (def_types[k] != mix_dev_INVALID)
    {
      struct mix_device_t *dev = mixgtk_device_new_ (def_types[k]);
      if (dev != NULL)	mix_vm_connect_device (vm, dev);
      ++k;
    }

  /* set to first page */
  gtk_notebook_set_page (container_, 0);
  
  return TRUE;
}

/* connect a new (file-based) device */
gboolean
mixgtk_device_connect (mix_device_type_t type, const gchar *name);