summaryrefslogtreecommitdiffhomepage
path: root/mixgtk/mixgtk_device.c
blob: ea84c189f60f863ab4437cb2c30796d944fc8663 (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* -*-c-*- ---------------- mixgtk_device.c :
 * actual types for mixgtk devices
 * ------------------------------------------------------------------
 *  Last change: Time-stamp: <2001-05-08 00:58:34 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"

#define BIN_DEV_COL_ 5

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

/* a mixgtk device */
struct mixgtk_device_t
{
  mix_device_t device;
  GtkWidget *widget;
  gint pos;
  guint last_insert;
};

/* 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 (GTK_TEXT (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};
  static gchar BUFFER[BUFF_SIZE] = { 0 };
  static gchar *DEFTEXT[BIN_DEV_COL_] = { "0", "0", "0", "0", "0" };
    
  guint k, col, row;
  
  for (k = 0; k < SIZES_[dev->device.type]; k++)
    {
      mix_word_print_to_buffer (block[k], BUFFER);
      row = dev->last_insert / BIN_DEV_COL_;
      col = dev->last_insert % BIN_DEV_COL_;
      if (col == 0) gtk_clist_append (GTK_CLIST (dev->widget), DEFTEXT);
      gtk_clist_set_text (GTK_CLIST (dev->widget), row, col, BUFFER);
      dev->last_insert++;
    }
}

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 void
destroy_ (mix_device_t *dev)
{
  struct mixgtk_device_t *gtkdev = (struct mixgtk_device_t *)dev;
  gtk_notebook_remove_page (GTK_NOTEBOOK (container_), gtkdev->pos);
  gtk_widget_destroy (gtkdev->widget); 
  (DEF_DEV_VTABLE_->destroy) (dev);
  --last_pos_;
}

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

/* create the gui part of the device */
static void
mixgtk_device_construct_gui_ (struct mixgtk_device_t *dev)
{
  GtkWidget *label = gtk_label_new (DEF_NAMES_[dev->device.type]);

  g_assert (label);

  dev->pos = last_pos_++;
  if (MODES_[dev->device.type] == mix_dev_CHAR)
    {
      GtkWidget *box = gtk_hbox_new (0, 0);
      GtkWidget *scroll = NULL;
      GtkAdjustment *vadj = NULL;
      dev->widget = gtk_text_new (NULL, NULL);
      g_assert (box);
      g_assert (dev->widget);
      gtk_text_set_editable (GTK_TEXT (dev->widget), FALSE);
      vadj = GTK_TEXT (dev->widget)->vadj;
      scroll = gtk_vscrollbar_new (vadj);
      gtk_box_pack_start (GTK_BOX (box), dev->widget, TRUE, TRUE, 0);
      gtk_box_pack_start (GTK_BOX (box), scroll, FALSE, FALSE, 0);
      gtk_notebook_insert_page (container_, box, label, dev->pos);
      gtk_widget_show (box);
      gtk_widget_show (scroll);
    }
  else
    {
      gint k;
      GtkWidget *scroll = gtk_scrolled_window_new (NULL, NULL);
      g_assert (scroll);
      gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
				      GTK_POLICY_AUTOMATIC,
				      GTK_POLICY_AUTOMATIC);
      dev->widget = gtk_clist_new (BIN_DEV_COL_);
      g_assert (dev->widget);
      for (k =0; k < BIN_DEV_COL_; ++k)
	{
	  gtk_clist_set_column_width (GTK_CLIST (dev->widget), k, 100);
	  gtk_clist_set_column_auto_resize (GTK_CLIST (dev->widget), k, TRUE);
	}
      gtk_container_add (GTK_CONTAINER (scroll), dev->widget);
      gtk_notebook_insert_page (container_, scroll, label, dev->pos);
      gtk_widget_show (scroll);
    }
  
  gtk_widget_show (label);
  gtk_widget_show (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_;
  dev->last_insert = 0;
  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);