gtkterm /gtkterm-0.99.6/src/macros.c

Language C Lines 534
MD5 Hash 3a567735e6e398c790c4b58fccf4d10d Estimated Cost $10,784 (why?)
Repository git://pkgs.fedoraproject.org/gtkterm View Raw File
  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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/***********************************************************************/
/* macros.c                                                            */
/* --------                                                            */
/*           GTKTerm Software                                          */
/*                      (c) Julien Schmitt                             */
/*                      julien@jls-info.com                            */
/*                                                                     */
/* ------------------------------------------------------------------- */
/*                                                                     */
/*   Purpose                                                           */
/*      Functions for the management of the macros                     */
/*                                                                     */
/*   ChangeLog                                                         */
/*      - 0.99.2 : Internationalization                                */
/*      - 0.99.0 : file creation by Julien                             */
/*                                                                     */
/***********************************************************************/

#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "gettext.h"
#include "widgets.h"
#include "macros.h"

enum
  {
    COLUMN_SHORTCUT,
    COLUMN_ACTION,
    NUM_COLUMNS
  };

macro_t *macros = NULL;
static GtkWidget *window = NULL;

macro_t *get_shortcuts(gint *size)
{
  gint i = 0;
  
  if(macros != NULL)
    {
      while(macros[i].shortcut != NULL)
	i++;
    }
  *size = i;
  return macros;
}


static void shortcut_callback(gpointer *number)
{ 
  gchar *string;
  gchar *str;
  gint i, length;
  guchar a;
  guint val_read;
  
  string = macros[(long)number].action;
  length = strlen(string);
  
  for(i = 0; i < length; i++)
    {
      if(string[i] == '\\')
        {
	  if(g_unichar_isdigit((gunichar)string[i + 1]))
	    {
	      if((string[i + 1] == '0') && (string[i + 2] != 0))
		{
		  if(g_unichar_isxdigit((gunichar)string[i + 3]))
		    {
		      str = &string[i + 2];
		      i += 3;
		    }
		  else
		    {
		      str = &string[i + 1];
		      if(g_unichar_isxdigit((gunichar)string[i + 2]))
			i += 2;
		      else
			i++;
		    }
		}
	      else
		{
		  str = &string[i + 1];
		  if(g_unichar_isxdigit((gunichar)string[i + 2]))
		    i += 2;
		  else
		    i++;
		}
	      if(sscanf(str, "%02X", &val_read) == 1)
		a = (guchar)val_read;
	      else
		a = '\\';
	    }
	  else
	    {
	      switch(string[i + 1])
		{
		case 'a':
		  a = '\a';
		  break;
		case 'b':
		  a = '\b';
		  break;
		case 't':
		  a = '\t';
		  break;
		case 'n':
		  a = '\n';
		  break;
		case 'v':
		  a = '\v';
		  break;
		case 'f':
		  a = '\f';
		  break;
		case 'r':
		  a = '\r';
		  break;
		case '\\':
		  a = '\\';
		  break;
		default:
		  a = '\\';
		  i--;
		  break;
		}
	      i++;
	    }
	  send_serial(&a, 1);
	}
      else
	{
	  send_serial(&string[i], 1);
	}
    }

  str = g_strdup_printf(_("Macro \"%s\" sent !"), macros[(long)number].shortcut);
  Put_temp_message(str, 800);
  g_free(str);
}

void create_shortcuts(macro_t *macro, gint size)
{
  macros = g_malloc((size + 1) * sizeof(macro_t));
  if(macros != NULL)
    {
      memcpy(macros, macro, size * sizeof(macro_t));
      macros[size].shortcut = NULL;
      macros[size].action = NULL;
    }
  else
    perror("malloc");
}

void add_shortcuts(void)
{
  long i = 0;
  guint acc_key;
  GdkModifierType mod;

  if(macros == NULL)
    return;

  while(macros[i].shortcut != NULL)
    {
      macros[i].closure = g_cclosure_new_swap(G_CALLBACK(shortcut_callback), (gpointer)i, NULL);
      gtk_accelerator_parse(macros[i].shortcut, &acc_key, &mod);
      if(acc_key != 0)
	gtk_accel_group_connect(shortcuts, acc_key, mod, GTK_ACCEL_MASK, macros[i].closure);
      i++;
    }
}

static void macros_destroy(void)
{
  gint i = 0;

  if(macros == NULL)
    return;

  while(macros[i].shortcut != NULL)
    {
      g_free(macros[i].shortcut);
      g_free(macros[i].action);
      /*
      g_closure_unref(macros[i].closure);
      */
      i++;
    }
  g_free(macros);
  macros = NULL;
}

void remove_shortcuts(void)
{
  gint i = 0;

  if(macros == NULL)
    return;
  
  while(macros[i].shortcut != NULL)
    {
      gtk_accel_group_disconnect(shortcuts, macros[i].closure);
      i++;
    }

  macros_destroy();
}

static GtkTreeModel *create_model(void)
{
  gint i = 0;
  GtkListStore *store;
  GtkTreeIter iter;

  /* create list store */
  store = gtk_list_store_new (NUM_COLUMNS,
			      G_TYPE_STRING,
			      G_TYPE_STRING,
			      G_TYPE_BOOLEAN,
			      G_TYPE_BOOLEAN);

  /* add data to the list store */
  if(macros != NULL)
    {
      while(1)
	{
	  if(macros[i].shortcut == NULL)
	    break;
	  gtk_list_store_append (store, &iter);
	  gtk_list_store_set (store, &iter,
			      COLUMN_SHORTCUT, macros[i].shortcut,
			      COLUMN_ACTION, macros[i].action,
			      -1);
	  i++;
	}
    }

  return GTK_TREE_MODEL(store);
}

static gboolean
shortcut_edited (GtkCellRendererText *cell,
		 const gchar         *path_string,
		 const gchar         *new_text,
		 gpointer             data)
{
  GtkTreeModel *model = (GtkTreeModel *)data;
  GtkTreePath *path = gtk_tree_path_new_from_string (path_string);
  GtkTreeIter iter;

  gtk_tree_model_get_iter(model, &iter, path); 
  
  gtk_list_store_set(GTK_LIST_STORE(model), &iter, COLUMN_ACTION, new_text, -1);
  gtk_tree_path_free (path);
 
  return TRUE;
}

static void
add_columns (GtkTreeView *treeview)
{
  GtkCellRenderer *renderer;
  GtkTreeViewColumn *column;
  GtkTreeModel *model = gtk_tree_view_get_model (treeview);

  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes (_("Shortcut"),
						     renderer,
						     "text",
						     COLUMN_SHORTCUT,
						     NULL);
  gtk_tree_view_column_set_sort_column_id (column, COLUMN_SHORTCUT);
  gtk_tree_view_append_column (treeview, column);

  renderer = gtk_cell_renderer_text_new ();
  g_signal_connect (renderer, "edited", G_CALLBACK(shortcut_edited), model);
  column = gtk_tree_view_column_new_with_attributes ("Action", renderer, "text", COLUMN_ACTION, NULL);
  g_object_set(G_OBJECT(renderer), "editable", TRUE, NULL);
  gtk_tree_view_column_set_sort_column_id (column, COLUMN_ACTION);
  gtk_tree_view_append_column (treeview, column);
}

static gint Add_shortcut(GtkWidget *button, gpointer pointer)
{
  GtkTreeIter iter;
  GtkTreeModel *model = (GtkTreeModel *)pointer;
  

  gtk_list_store_append(GTK_LIST_STORE(model), &iter);

  gtk_list_store_set(GTK_LIST_STORE(model), &iter, COLUMN_SHORTCUT, "None", -1);

  return FALSE;
}

static gboolean Delete_shortcut(GtkWidget *button, gpointer pointer)
{
  GtkTreeIter iter;
  GtkTreeView *treeview = (GtkTreeView *)pointer;
  GtkTreeModel *model = gtk_tree_view_get_model (treeview);
  GtkTreeSelection *selection = gtk_tree_view_get_selection (treeview);

  if (gtk_tree_selection_get_selected (selection, NULL, &iter))
    {
      gint i;
      GtkTreePath *path;

      path = gtk_tree_model_get_path(model, &iter);
      i = gtk_tree_path_get_indices(path)[0];
      gtk_list_store_remove (GTK_LIST_STORE (model), &iter);

      gtk_tree_path_free (path);
    }

  return FALSE;
}

static gboolean Save_shortcuts(GtkWidget *button, gpointer pointer)
{
  GtkTreeIter iter;
  GtkTreeView *treeview = (GtkTreeView *)pointer;
  GtkTreeModel *model = gtk_tree_view_get_model (treeview);
  gint i = 0;

  remove_shortcuts();

  if(gtk_tree_model_get_iter_first(model, &iter))
    {
      do
	{
	  i++;
	} while(gtk_tree_model_iter_next(model, &iter));

      gtk_tree_model_get_iter_first(model, &iter);

      macros = g_malloc((i + 1) * sizeof(macro_t));
      i = 0;
      if(macros != NULL)
	{
	  do
	    {
	      gtk_tree_model_get(model, &iter, COLUMN_SHORTCUT, &(macros[i].shortcut), \
				 COLUMN_ACTION, &(macros[i].action), \
				 -1);
	      i++;
	    } while(gtk_tree_model_iter_next(model, &iter));

	  macros[i].shortcut = NULL;
	  macros[i].action = NULL;
	}
    }

  add_shortcuts();

  return FALSE;
}

static gboolean key_pressed(GtkWidget *window, GdkEventKey *key, gpointer pointer)
{
  GtkTreeIter iter;
  GtkTreeView *treeview = (GtkTreeView *)pointer;
  GtkTreeModel *model = gtk_tree_view_get_model (treeview);
  GtkTreeSelection *selection = gtk_tree_view_get_selection (treeview);
  gchar *str = NULL;

  switch(key->keyval)
    {
    case GDK_Shift_L:
    case GDK_Shift_R:
    case GDK_Control_L:
    case GDK_Control_R:
    case GDK_Caps_Lock:
    case GDK_Shift_Lock:
    case GDK_Meta_L:
    case GDK_Meta_R:
    case GDK_Alt_L:
    case GDK_Alt_R:
    case GDK_Super_L:
    case GDK_Super_R:
    case GDK_Hyper_L:
    case GDK_Hyper_R:
    case GDK_Mode_switch:
      return FALSE;
    default:
      break;
    }

  if(gtk_tree_selection_get_selected(selection, NULL, &iter))
    {
      gint i;
      GtkTreePath *path;
      
      path = gtk_tree_model_get_path(model, &iter);
      i = gtk_tree_path_get_indices(path)[0];
      str = gtk_accelerator_name(key->keyval, key->state & ~GDK_MOD2_MASK);
      gtk_list_store_set(GTK_LIST_STORE (model), &iter, COLUMN_SHORTCUT, str, -1);

      gtk_tree_path_free(path);
      g_free(str);

      g_signal_handlers_disconnect_by_func(window, G_CALLBACK(key_pressed), pointer);
    }
  return FALSE;
}


static gboolean Capture_shortcut(GtkWidget *button, gpointer pointer)
{
  g_signal_connect_after(window, "key_press_event", G_CALLBACK(key_pressed), pointer);

  return FALSE;
}

static gboolean Help_screen(GtkWidget *button, gpointer pointer)
{
  GtkWidget *Dialogue, *Label, *Bouton, *Frame;

  Dialogue = gtk_dialog_new();
  gtk_window_set_title(GTK_WINDOW(Dialogue), _("Help on macros"));
  Bouton = gtk_button_new_from_stock (GTK_STOCK_OK);
  gtk_signal_connect_object(GTK_OBJECT(Bouton), "clicked", (GtkSignalFunc)gtk_widget_destroy, GTK_OBJECT(Dialogue));
  gtk_signal_connect(GTK_OBJECT(Dialogue), "destroy", (GtkSignalFunc)gtk_widget_destroy, NULL);
  gtk_signal_connect(GTK_OBJECT(Dialogue), "delete_event", (GtkSignalFunc)gtk_widget_destroy, NULL);
  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(Dialogue)->action_area), Bouton, TRUE, TRUE, 0);

  Label = gtk_label_new(_("The \"action\" field of a macro is the data to be sent on the port. Text can be entered, but also special chars, like \\n, \\t, \\r, etc. You can also enter hexadecimal data preceded by a '\\'. The hexadecimal data should not begin with a letter (eg. use \\0FF and not \\FF)\nExamples :\n\t\"Hello\\n\" sends \"Hello\" followed by a Line Feed\n\t\"Hello\\0A\" does the same thing but the LF is entered in hexadecimal"));
  gtk_label_set_line_wrap(GTK_LABEL (Label), TRUE);
  gtk_label_set_selectable(GTK_LABEL(Label), TRUE);
  gtk_misc_set_padding(GTK_MISC(Label), 10, 10);

  Frame = gtk_frame_new(NULL);
  gtk_container_set_border_width(GTK_CONTAINER(Frame), 5);
  gtk_box_pack_start(GTK_BOX(GTK_DIALOG(Dialogue)->vbox), Frame, FALSE, FALSE, 0);
  gtk_container_add(GTK_CONTAINER(Frame), Label);

  gtk_widget_show_all(Dialogue);

  return FALSE;
}


gint Config_macros(GtkWidget *wid, guint param)
{
  GtkWidget *vbox, *hbox;
  GtkWidget *sw;
  GtkTreeModel *model;
  GtkWidget *treeview;
  GtkWidget *button;
  GtkWidget *separator;

  /* create window, etc */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), _("Configure Macros"));
  
  g_signal_connect (window, "destroy",
		    G_CALLBACK (gtk_widget_destroyed), &window);
  gtk_container_set_border_width (GTK_CONTAINER (window), 8);

  vbox = gtk_vbox_new (FALSE, 8);
  gtk_container_add (GTK_CONTAINER (window), vbox);
  
  sw = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
				       GTK_SHADOW_ETCHED_IN);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
				  GTK_POLICY_NEVER,
				  GTK_POLICY_AUTOMATIC);
  gtk_box_pack_start (GTK_BOX (vbox), sw, TRUE, TRUE, 0);

  /* create tree model */
  model = create_model ();
  
  /* create tree view */
  treeview = gtk_tree_view_new_with_model (model);
  gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (treeview), TRUE);
  gtk_tree_view_set_search_column (GTK_TREE_VIEW (treeview),
				   COLUMN_SHORTCUT);
  
  g_object_unref (model);
  
  gtk_container_add (GTK_CONTAINER (sw), treeview);
  
  /* add columns to the tree view */
  add_columns (GTK_TREE_VIEW (treeview));

  hbox = gtk_hbox_new (TRUE, 4);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  
  button = gtk_button_new_with_mnemonic (_("_Add"));
  g_signal_connect(button, "clicked", G_CALLBACK(Add_shortcut), (gpointer)model);
  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);

  button = gtk_button_new_with_mnemonic (_("_Delete"));
  g_signal_connect(button, "clicked", G_CALLBACK(Delete_shortcut), (gpointer)treeview);
  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
  
  button = gtk_button_new_with_mnemonic (_("_Capture Shortcut"));
  g_signal_connect(button, "clicked", G_CALLBACK(Capture_shortcut), (gpointer)treeview);
  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);

  separator = gtk_hseparator_new();
  gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, TRUE, 0);

  hbox = gtk_hbox_new (TRUE, 4);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
  
  button = gtk_button_new_from_stock (GTK_STOCK_HELP);
  g_signal_connect(button, "clicked", G_CALLBACK(Help_screen), NULL);
  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);

  button = gtk_button_new_from_stock (GTK_STOCK_OK);
  g_signal_connect(button, "clicked", G_CALLBACK(Save_shortcuts), (gpointer)treeview);
  g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), (gpointer)window);
  gtk_box_pack_end (GTK_BOX (hbox), button, TRUE, TRUE, 0);

  button = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
  g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), (gpointer)window);
  gtk_box_pack_end (GTK_BOX (hbox), button, TRUE, TRUE, 0);

  gtk_window_set_default_size (GTK_WINDOW(window), 300, 400);
  
  gtk_widget_show_all(window);

  return FALSE;
}
Back to Top