Filter:   InfoImg
download _new_snd-test.rb
Language: Ruby
LOC: 724
Project Info
Snd
Server: SourceForge
Type: cvs
SourceForge\s\snd\snd\cvs‑snd\
   _new_snd-test.rb
   _sndlib.h
   aclocal.m4
   analog-filter.scm
   audinfo.c
   audio.c
   autosave.scm
   bess.rb
   bess.scm
   bess1.rb
   bess1.scm
   bird.rb
   bird.scm
   clm-ins.rb
   clm-ins.scm
   clm-strings.h
   clm.c
   clm.h
   clm23.scm
   clm2xen.c
   clm2xen.h
   cmn-glyphs.lisp
   config.guess
   config.h.in
   config.rpath
   config.sub
   configure.ac
   debug.scm
   dlocsig.rb
   draw.rb
   draw.scm
   dsp.rb
   dsp.scm
   edit-menu.scm
   edit123.scm
   effects-utils.scm
   effects.rb
   env.rb
   env.scm
   enved.scm
   eval-c.scm
   event.scm
   examp.rb
   examp.scm
   extensions.rb
   extensions.scm
   fade.scm
   fft-menu.scm
   fix-optargs.scm
   fmv.scm
   freeverb.rb
   freeverb.scm
   gettext.h
   gl.c
   grani.scm
   gtk-effects.scm
   gtk-popup.scm
   gui.scm
   headers.c
   help-snd-fm.pd
   help-snd.pd
   HISTORY.Snd
   hooks.rb
   hooks.scm
   index.rb
   index.scm
   inf-snd.el
   io.c
   jcrev.scm
   ladspa-help.scm
   ladspa.scm
   makefile.in
   makefile.no-gettext
   maraca.rb
   maraca.scm
   marks-menu.scm
   marks.rb
   marks.scm
   maxf.rb
   maxf.scm
   midi.c
   misc.scm
   mix-menu.scm
   mix.rb
   mix.scm
   mixer.scm
   moog.scm
   musglyphs.rb
   musglyphs.scm
   nb.rb
   nb.scm
   new-backgrounds.scm
   new-buttons.scm
   new-effects.scm
   new-icons.scm
   noise.rb
   noise.scm
   old-mac-audio.c
   old-snd.spec
   oo.scm
   oscope.scm
   panic.scm
   pd-add.scm
   pd-any.scm
   pd-fm.scm
   pd-global.scm
   pd-inout.scm
   pd-local.scm
   pd-mozilla.scm
   pd-send_receive.scm
   peak-env.scm
   piano.rb
   piano.scm
   play.rb
   play.scm
   poly.rb
   poly.scm
   popup.rb
   popup.scm
   prc95.rb
   prc95.scm
   pvoc.rb
   pvoc.scm
   README.Snd
   rgb.rb
   rgb.scm
   rmsgain.scm
   rt-compiler.scm
   rt-engine.scm
   rt-examples.scm
   rt.tex
   rtio.rb
   rtio.scm
   rubber.rb
   rubber.scm
   saw.c
   singer.rb
   singer.scm
   snd-0.h
   snd-1.h
   snd-axis.c
   snd-chn.c
   snd-completion.c
   snd-dac.c
   snd-data.c
   snd-draw.c
   snd-edits.c
   snd-env.c
   snd-error.c
   snd-fft.c
   snd-file.c
   snd-file.h
   snd-find.c
   snd-g0.h
   snd-g1.h
   snd-gchn.c
   snd-gdraw.c
   snd-gdrop.c
   snd-genv.c
   snd-gfft.c
   snd-gfile.c
   snd-gfind.c
   snd-ghelp.c
   snd-gl.scm
   snd-glistener.c
   snd-gmain.c
   snd-gmenu.c
   snd-gmix.c
   snd-gprefs.c
   snd-gprint.c
   snd-grec.c
   snd-gregion.c
   snd-gsnd.c
   snd-gtk.scm
   snd-gutils.c
   snd-gxbitmaps.c
   snd-gxcolormaps.c
   snd-gxen.c
   snd-gxutils.c
   snd-help.c
   snd-hobbit.scm
   snd-io.c
   snd-kbd.c
   snd-ladspa.c
   snd-listener.c
   snd-main.c
   snd-marks.c
   snd-menu.c
   snd-menu.h
   snd-mix.c
   snd-motif.scm
   snd-nogui.c
   snd-nogui0.h
   snd-nogui1.h
   snd-prefs.c
   snd-print.c
   snd-rec.c
   snd-rec.h
   snd-region.c
   snd-run.c
   snd-select.c
   snd-sig.c
   snd-snd.c
   snd-strings.h
   snd-trans.c
   snd-utils.c
   snd-x0.h
   snd-x1.h
   snd-xchn.c
   snd-xdraw.c
   snd-xdrop.c
   snd-xen.c
   snd-xenv.c
   snd-xfft.c
   snd-xfile.c
   snd-xfind.c
   snd-xhelp.c
   snd-xlistener.c
   snd-xm.rb
   snd-xmain.c
   snd-xmenu.c
   snd-xmix.c
   snd-xprefs.c
   snd-xprint.c
   snd-xrec.c
   snd-xref.c
   snd-xregion.c
   snd-xsnd.c
   snd-xutils.c
   snd-xxen.c
   snd.1
   Snd.ad
   snd.c
   Snd.gtkrc
   snd.h
   snd.spec
   snd_conffile.scm
   snd_pd_external.c
   snd_pd_external.h
   snd4.scm
   snd5.scm
   snd6.scm
   snd7.scm
   sndctrl.c
   sndinfo.c
   sndlib-strings.h
   sndlib.h.in
   sndlib2xen.c
   sndlib2xen.h
   sndplay.c
   sndrecord.c
   sndwarp.scm
   sound.c
   special-menu.scm
   spectr.rb
   spectr.scm
   strad.rb
   strad.scm
   TODO.Snd
   track-colors.scm
   v.rb
   v.scm
   vct.c
   vct.h
   ws.rb
   ws.scm
   xen.c
   xen.h
   xg-x11.h
   xm-enved.rb
   xm-enved.scm
   zip.rb
   zip.scm

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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
# snd-test.rb: Snd Ruby code and tests -*- snd-ruby -*-
#
#  test 00: constants
#  test 01: defaults
#  test 02: headers
#  test 03: variables
#  test 04: sndlib
#  test 05: simple overall checks
#  test 06: vcts
#  test 07: colors
#  test 08: clm
#  test 09: mix
#  test 10: marks
#  test 11: dialogs
#  test 12: extensions
#  test 13: menus, edit lists, hooks, etc
#  test 14: all together now
#  test 15: chan-local vars
#  test 16: regularized funcs
#  test 17: dialogs and graphics
#  test 18: enved
#  test 19: save and restore
#  test 20: transforms
#  test 21: new stuff
#  test 23: with-sound
#  test 24: user-interface
#  test 28: errors
#  test all done

# snd -noinit -load snd-test.rb         # all tests
# snd -noinit -load snd-test.rb 3 7 20  # test 3, 7, 20

require "examp"
require "ws"
require "hooks"
require "extensions"
require "mix"
require "marks"
require "pvoc"
require "bird"
require "v"
require "poly"
require "rubber"
include Rubber
unless provided? :snd_nogui
  require "rgb"
  provided?(:snd_motif) and (not provided?(:xm)) and require("libxm")
  provided?(:snd_gtk)   and (not provided?(:xg)) and require("libxg")
  require "snd-xm"
  include Snd_XM
  require "popup"
  require "xm-enved"
  require "draw"
  require "musglyphs"
  require "effects"
end

$my_snd_error_hook = nil
$original_save_dir = (save_dir or "/zap/snd")
$original_temp_dir = (temp_dir or "/zap/tmp")
$original_prompt = listener_prompt
$sample_reader_tests = 300
$default_file_buffer_size = 65536
$home_dir = ENV["HOME"]

$audio_amp_zero = 0.0
$with_backtrace = false
$sf_dir = "/home/bil/sf1/"
$with_exit = true
$full_test = true

$with_big_file = false
$bigger_snd = "/home/bil/zap/sounds/bigger.snd"

$all_args = false

$test00 = true
$test01 = true
$test02 = true
$test03 = true
$test04 = true
$test05 = true
$test06 = true
$test07 = true
$test08 = true
$test09 = true
$test10 = true
$test11 = true
$test12 = true
$test13 = true
$test14 = true
$test15 = true
$test16 = true
$test17 = true
$test18 = true
$test19 = true
$test20 = true
$test21 = true
$test22 = true
$test23 = true
$test24 = true
$test25 = true
$test26 = true
$test27 = true
$test28 = true

reset_all_hooks

# global variables may be overridden in `pwd`/.sndtestrc or ~/.sndtestrc
load_init_file(".sndtestrc")

# to prepare view_files_infos in snd-xfile.c
# (save_state calls save_view_files_dialogs() in snd-xfile.c)
Snd.add_sound_path(Dir.pwd)

def main_test
  test00
  test01
  test02
  test03
  test04
  test05
  test06
  test07
  test08
  test09
  test10
  test11
  test12
  test13
  test14
  test15
  test16
  test17
  test18
  test19
  test20
  test21
  # test 22: run skipped
  test23
  test24
  # test25
  # test26
  # test27
  test28
end

def snd_info(*args)
  args[0] = String(args[0])
  str = format(*args)
  if provided? :snd_nogui
    clm_print("# %s\n", str)
  else
    clm_print("\n# %s", str)
    $stderr.printf("# %s\n", str)
  end
  nil
end

if $with_backtrace
  def snd_display(*args)
    snd_info(*args)
    $stderr.printf("%s\n", verbose_message_string(true, "# "))
    nil
  end
else
  alias snd_display snd_info
end

# $snd_error_hook
def snd_display_error(*args)
  snd_info(*args)
  $stderr.printf("%s\n", verbose_message_string(true, "# "))
  nil
end

# command line args: the last arg(s) may be zero or many test numbers
# snd -noinit -load snd-test.rb 0 17 18 28
if script_arg.positive?
  if script_args[script_arg - 1] !~ /^-l.*/
    Snd.display("script_args[%d]: %s %s", script_arg - 1, script_args[script_arg - 1], script_args)
  end
  if script_args[script_arg] !~ /snd-test/
    Snd.display("script_args[%d]: %s %s", script_arg, script_args[script_arg], script_args)
  end
  args = script_args[script_arg..-1].map do |arg|
    Snd.catch(:all, nil) do Integer(arg) end.first
  end.compact
  set_script_arg(script_args.length - 1)
  if args.length.positive?
    29.times do |i| eval(format("$test%02d = false", i)) end
    args.each do |arg|
      if arg.between?(0, 28)
        eval(format("$test%02d = true", arg))
        $with_exit = true
        $full_test = false
      end
    end
  end
end

def fneq_err(f1, f2, err = 0.001)
  (Float(f1) - Float(f2)).abs > err
end

def fneq(a, b)
  fneq_err(a, b, 0.001)
end

def ffneq(a, b)
  fneq_err(a, b, 0.01)
end

def fffneq(a, b)
  fneq_err(a, b, 0.1)
end

# returns a vct or false
# obj: Vct, Array, Vec, Poly
def any2vct(obj)
  obj.respond_to?(:to_vct) and obj.to_vct
end

# compares Arrays and Vcts
def vequal_err(val0, val1, err = 0.001)
  (v0 = any2vct(val0)) and (v1 = any2vct(val1)) and v0.subtract(v1).peak <= err
end

def vequal(v0, v1)
  vequal_err(v0, v1, 0.001)
end

def vvequal(v0, v1)
  vequal_err(v0, v1, 0.00002)
end

def vfequal(v0, v1)
  vequal_err(v0, v1, 0.01)
end

def vffequal(v0, v1)
  vequal_err(v0, v1, 0.1)
end

def cneq(a, b)
  if complex?(a) and complex?(b)
    fneq(a.real, b.real) or fneq(a.image, b.image)
  else
    true
  end
end

def rs(val)
  random(1.0) < val
end

def log0(try_it = true)
  try_it ? log(0) : 0.0
end

def list_p(obj)
  array?(obj) and (not obj.empty?)
end

def any_arity(obj)
  case obj
  when Proc, Method
    obj.arity
  when String, Symbol
    method(obj).arity
  else
    0
  end
end

def arity_ok(func, args)
  if integer?(rargs = Snd.catch do any_arity(func) end.first)
    if rargs >= 0
      rargs == args
    else
      rargs.abs <= args
    end
  else
    false
  end
end

def set_arity_ok(func, args)
  arity_ok("set_#{func}", args)
end

# let proc $snd_error_hook("sndtestrc") untouched
# perhaps defined in .sndtestrc
def reset_almost_all_hooks
  reset_all_hooks
  if proc? $my_snd_error_hook then $snd_error_hook.add_hook!("sndtestrc", &$my_snd_error_hook) end
end

def dismiss_all_dialogs
  if provided? :xm or provided? :xg
    dialog_widgets.each do |dialog|
      if array?(dialog)
        if symbol?(dialog.car)
          if provided? :snd_motif
            if RXtIsManaged(dialog) then RXtUnmanageChild(dialog) end
          elsif provided? :snd_gtk
              Rgtk_widget_hide(dialog)
          end
        else
          dialog.each do |d|
            if symbol?(d.car)
              if provided? :snd_motif
                if RXtIsManaged(d) then RXtUnmanageChild(d) end
              elsif provided? :snd_gtk
                Rgtk_widget_hide(d)
              end
            end
          end
        end
      end
    end
  end
end

set_mus_file_buffer_size($default_file_buffer_size)
set_with_background_processes(false)
set_show_backtrace(true)

make_proc_with_setter(:mus_audio_playback_amp,
                      lambda do
                        vals = make_vct(32)
                        mus_audio_mixer_read(Mus_audio_default, Mus_audio_amp, 0, vals)
                        ch0_amp = vals[0]
                        mus_audio_mixer_read(Mus_audio_default, Mus_audio_amp, 1, vals)
                        ch1_amp = vals[0]
                        [ch0_amp, ch1_amp]
                      end,
                      lambda do |val|
                        vals = make_vct(32)
                        vals[0] = val
                        mus_audio_mixer_write(Mus_audio_default, Mus_audio_amp, 0, vals)
                        mus_audio_mixer_write(Mus_audio_default, Mus_audio_amp, 1, vals)
                        val
                      end)

$orig_audio_amp = mus_audio_playback_amp.first
set_mus_audio_playback_amp($audio_amp_zero)

def make_color_with_catch(c1, c2, c3)
  make_color(c1, c2, c3)
rescue
  make_color(1, 0, 0)
end

def file_copy(f1, f2)
  File.exists?(f1) and system(format("cp %s %s", f1, f2))
end

def delete_file(file)
  File.owned?(file) and File.unlink(file)
end

def delete_files(*files)
  files.each do |f| delete_file(f) end
end

def with_file(file, verbose = true, &body)
  if File.exists?(full_name = $sf_dir + file)
    body.call(full_name)
  else
    if verbose
      snd_info("%s missing?", full_name)
    end
  end
end

def with_gc_disabled
  GC.disable
  ret = yield
  GC.enable
  ret
end

def snd_error_test
  Snd_error_tags.each do |tag|
    if (res = Snd.catch(tag) do Snd.throw(tag, "snd-test") end).first != tag
      snd_display("Snd.catch (throwing 1): %s -> %s", tag.inspect, res.inspect)
    end
    if (res = Snd.catch(tag) do Snd.raise(tag, "snd-test") end).first != tag
      snd_display("Snd.catch (raising 1): %s -> %s", tag.inspect, res.inspect)
    end
    if (res = Snd.catch(tag, :okay) do Snd.throw(tag, "snd-test") end).first != :okay
      snd_display("Snd.catch (throwing 2): %s -> %s", tag.inspect, res.inspect)
    end
    if (res = Snd.catch(tag, :okay) do Snd.raise(tag, "snd-test") end).first != :okay
      snd_display("Snd.catch (raising 2): %s -> %s", tag.inspect, res.inspect)
    end
  end
end
snd_error_test if $full_test

show_listener
set_window_x(600)
set_window_y(10)
file_copy(ENV["HOME"] + "/.snd", ENV["HOME"] + "/dot-snd")

def cwd
  Dir.pwd + "/"
end

class Snd_test_time
  def initialize
    @real_time = Time.now
    @process_time = process_times
    @real = @utime = @stime = 0.0
  end
  attr_reader :real, :utime, :stime
  
  def inspect
    format("#<%s: real: %8.3f, utime: %8.3f, stime: %8.3f>", self.class, @real, @utime, @stime)
  end
  
  def start
    @real_time = Time.now
    @process_time = process_times
  end
  
  def stop
    @real = Time.now - @real_time
    cur_time = process_times
    @utime = cur_time.utime - @process_time.utime
    @stime = cur_time.stime - @process_time.stime
  end
  
  def display(msg)
    str = if msg
            " (" + msg + ")"
          else
            ""
          end
    snd_info("real: %1.3f, utime: %1.3f, stime: %1.3f%s", @real, @utime, @stime, str) 
  end
  
  def run(msg, &body)
    start
    body.call
    stop
    if msg then display(msg) end
    [self.real, self.utime, self.stime]
  end
end

def with_time(msg = nil, &body)
  Snd_test_time.new.run(msg, &body)
end

def finish_snd_test
  $overall_start_time.stop
  Snd.regions.apply(:forget_region)
  Snd.tracks.apply(:free_track)
  set_view_files_sort(0)
  clear_sincs
  stop_playing
  reset_almost_all_hooks
  snd_info("all done!")
  snd_info
  unless $timings.empty?
    $timings.each do |tst| snd_info("test %2d  %s", tst.first, tst.last.inspect) end
  end
  snd_info("total    %s\n", $overall_start_time.inspect)
  show_listener
  save_listener("test.output")
  show_listener
  dot_snd = ENV["HOME"] + "/dot-snd"
  File.exists?(dot_snd) and File.rename(dot_snd, ENV["HOME"] + "/.snd")
  fs = 0
  [$original_save_dir, $original_temp_dir, "/tmp"].each do |path|
    if File.exists?(path)
      fs += Dir[path + "/snd_*"].length
      Dir[path + "/snd_*"].each do |f| delete_file(f) end
    end
  end
  snd_display("%s temporary file%s deleted", fs.zero? ? "no" : fs, fs.between?(0, 1) ? "" : "s")
  mus_sound_prune
  ["aaa.eps",
   "envs.save",
   "fmv.snd",
   "fmv.wav",
   "fmv0.snd",
   "fmv1.snd",
   "fmv2.snd",
   "fmv3.snd",
   "fmv4.reverb",
   "fmv4.snd",
   "hiho.marks",
   "hiho.snd",
   "hiho.snd",
   "hiho.tmp",
   "hiho.wave",
   "ho",
   "new.snd",
   "oboe.marks",
   "obtest.snd.stereo",
   "snd.eps",
   "test-1.snd",
   "test-2.snd",
   "test-macros.scm",
   "test.aiff",
   "test.data",
   "test.rev",
   "test.reverb",
   "test.snd",
   "test.snd.snd",
   "test.wav",
   "test.xpm",
   "test2.snd",
   "test3.snd",
   "tmp.snd",
   "with-mix.snd",
   "1",
   "gtk-errors",
   "accelmap"].each do |file|
    delete_file(file)
  end
  ["mus10.snd.snd",
   "ieee-text-16.snd.snd",
   "trumps22.adp.snd",
   "oki.wav.snd",
   "nasahal.avi.snd",
   "hcom-16.snd.snd",
   "ce-c3.w02.snd",
   "oboe.g723_24.snd",
   "oboe.g723_40.snd",
   "oboe.g721.snd",
   "wood.sds.snd",
   "o2_dvi.wave.snd",
   "nist-shortpack.wav.snd"].each do |file|
    with_file(file, false) do delete_file(file) end
  end
  set_mus_audio_playback_amp($orig_audio_amp)
end

# map_chan* procedure
$init_channel = lambda do |y| 1.0 end
$timings = Array.new(0)

make_hook("$before_test_hook", 1, "snd-test") do |n|
  $timings.push([n, Snd_test_time.new])
  snd_info("test %d", n)
  set_show_backtrace(false)
end

make_hook("$after_test_hook", 1, "snd-test") do |n|
  $timings.last.last.stop
  if sounds
    snd_info("test %d: open sounds: %s", n, short_file_name(true))
    Snd.sounds.apply(:close_sound)
  end
  dismiss_all_dialogs
  snd_info("test %d done\n#", n)
end

unless hook? $before_test_hook
  snd_display("$before_test_hook not a hook: %s?", $before_test_hook.inspect)
end
unless hook? $after_test_hook
  snd_display("$after_test_hook not a hook: %s?", $after_test_hook.inspect)
end

snd_info("===  Snd version: %s", snd_version)
snd_info("=== Ruby version: %s (%s) [%s]", RUBY_VERSION, RUBY_RELEASE_DATE, RUBY_PLATFORM)
snd_info
snd_info("%s\n#", Time.now.localtime.strftime("%a %d-%b-%Y %H:%M %Z"))
$overall_start_time = Snd_test_time.new

module Test_event
  # see event.scm
  def key_event(widget, key, state)
    e = RXEvent(RKeyPress)
    dpy = RXtDisplay(widget)
    window = RXtWindow(widget)
    Rset_type(e, RKeyPress)
    Rset_window(e, window)
    Rset_display(e, dpy)
    Rset_root(e, RRootWindow(dpy, RDefaultScreen(dpy)))
    Rset_x(e, 0)
    Rset_y(e, 0)
    Rset_x_root(e, 0)
    Rset_y_root(e, 0)
    Rset_keycode(e, RXKeysymToKeycode(dpy, [:KeySym, key]))
    Rset_state(e, state)
    Rset_time(e, [:Time, RCurrentTime])
    Rset_same_screen(e, true)
    Rset_subwindow(e, [:Window, RNone])
    err = RXSendEvent(dpy, window, false, RKeyPressMask, e)
    if err.nonzero?
      Rset_type(e, RKeyRelease)
      Rset_time(e, [:Time, RCurrentTime])
      err = RXSendEvent(dpy, window, false, RKeyReleaseMask, e)
    end
    if err.zero? then snd_display("[key-event error] ", err) end
    err
  end

  def key_event_with_mouse(widget, key, state, x, y)
    e = RXEvent(RKeyPress)
    dpy = RXtDisplay(widget)
    window = RXtWindow(widget)
    Rset_type(e, RKeyPress)
    Rset_window(e, window)
    Rset_display(e, dpy)
    Rset_root(e, RRootWindow(dpy, RDefaultScreen(dpy)))
    Rset_x(e, x)
    Rset_y(e, y)
    Rset_x_root(e, x)
    Rset_y_root(e, y)
    Rset_keycode(e, RXKeysymToKeycode(dpy, [:KeySym, key]))
    Rset_state(e, state)
    Rset_time(e, [:Time, RCurrentTime])
    Rset_same_screen(e, true)
    Rset_subwindow(e, [:Window, RNone])
    err = RXSendEvent(dpy, window, false, RKeyPressMask, e)
    if err.nonzero?
      Rset_type(e, RKeyRelease)
      Rset_time(e, [:Time, RCurrentTime])
      err = RXSendEvent(dpy, window, false, RKeyReleaseMask, e)
    end
    if err.zero? then snd_display("[key-event error] ", err) end
    err
  end

  def resize_event(widget, width, height)
    e = RXEvent(RResizeRequest)
    dpy = RXtDisplay(widget)
    window = RXtWindow(widget)
    Rset_window(e, window)
    Rset_display(e, dpy)
    Rset_width(e, width)
    Rset_height(e, height)
    RXSendEvent(dpy, window, false, RResizeRedirectMask, e)
  end

  def enter_event(widget)
    e = RXEvent(REnterNotify)
    dpy = RXtDisplay(widget)
    window = RXtWindow(widget)
    Rset_window(e, window)
    Rset_display(e, dpy)
    RXSendEvent(dpy, window, false, REnterWindowMask, e)
  end

  def leave_event(widget)
    e = RXEvent(RLeaveNotify)
    dpy = RXtDisplay(widget)
    window = RXtWindow(widget)
    Rset_window(e, window)
    Rset_display(e, dpy)
    RXSendEvent(dpy, window, false, RLeaveWindowMask, e)
  end

  def expose_event(widget, x, y, width, height)
    e = RXEvent(RExpose)
    dpy = RXtDisplay(widget)
    window = RXtWindow(widget)
    Rset_window(e, window)
    Rset_display(e, dpy)
    Rset_x(e, x)
    Rset_y(e, y)
    Rset_width(e, width)
    Rset_height(e, height)
    Rset_count(e, 0)
    RXSendEvent(dpy, window, false, RExposureMask, e)
  end
  
  def click_event(widget, button, state, x, y)
    e = RXEvent(RButtonPress)
    dpy = RXtDisplay(widget)
    window = RXtWindow(widget)
    Rset_type(e, RButtonPress)
    Rset_window(e, window)
    Rset_display(e, dpy)
    Rset_root(e, RRootWindow(dpy, RDefaultScreen(dpy)))
    Rset_x(e, x)
    Rset_y(e, y)
    Rset_x_root(e, 0)
    Rset_y_root(e, 0)
    Rset_state(e, state)
    Rset_button(e, button)
    Rset_time(e, [:Time, RCurrentTime])
    Rset_same_screen(e, true)
    Rset_subwindow(e, [:Window, RNone])
    err = RXSendEvent(dpy, window, false, RButtonPressMask, e)
    if err.nonzero?
      Rset_type(e, RButtonRelease)
      Rset_time(e, [:Time, RCurrentTime])
      err = RXSendEvent(dpy, window, false, RButtonReleaseMask, e)
    end
    if err.zero? then snd_display("[click-event error] ", err) end
    err
  end
  
  def drag_event(widget, button, state, x0, y0, x1, y1)
    e = RXEvent(RButtonPress)
    e1 = RXEvent(RMotionNotify)
    dpy = RXtDisplay(widget)
    window = RXtWindow(widget)
    Rset_type(e, RButtonPress)
    Rset_window(e, window)
    Rset_display(e, dpy)
    Rset_root(e, RRootWindow(dpy, RDefaultScreen(dpy)))
    Rset_x(e, x0)
    Rset_y(e, y0)
    Rset_x_root(e, 0)
    Rset_y_root(e, 0)
    Rset_state(e, state)
    Rset_button(e, button)
    Rset_time(e, [:Time, RCurrentTime])
    Rset_same_screen(e, true)
    Rset_subwindow(e, [:Window, RNone])
    err = RXSendEvent(dpy, window, false, RButtonPressMask, e)
    if err.nonzero?
      Rset_window(e1, window)
      Rset_display(e1, dpy)
      Rset_root(e1, RRootWindow(dpy, RDefaultScreen(dpy)))
      # Rset_x(e1, x1)
      # Rset_y(e1, y1)
      Rset_x_root(e1, x0)
      Rset_y_root(e1, y0)
      Rset_state(e1, state)
      Rset_time(e1, [:Time, RCurrentTime + 300])
      Rset_same_screen(e1, true)
      Rset_subwindow(e1, [:Window, RNone])
      Rset_is_hint(e1, RNotifyNormal)
      den = if (x1 - x0).abs > 10 or (y1 - y0).abs > 10
              10
            else
              2
            end
      xdiff = ((x1 - x0) / den.to_f).floor
      ydiff = ((y1 - y0) / den.to_f).floor
      xn = x0 + xdiff
      yn = y0 + ydiff
      den.times do
        Rset_x(e1, xn)
        Rset_y(e1, yn)
        RXSendEvent(dpy, window, false, RButtonMotionMask, e1)
        xn += xdiff
        yn += ydiff
      end
      Rset_type(e, RButtonRelease)
      Rset_time(e, [:Time, RCurrentTime + 500])
      Rset_x(e, x1)
      Rset_y(e, y1)
      RXSendEvent(dpy, window, false, RButtonReleaseMask, e)
    end
  end

  def select_item(wid, pos)
    if RXmIsList(wid)
      RXmListSelectPos(wid, pos + 1, true)
    else
      snd_display("is not a list!", RXtName(wid))
    end
  end

  def click_button(button, value = false, bits = false)
    if RWidget?(button)
      if RXtIsSensitive(button)
        if RXmIsPushButton(button) or RXmIsPushButtonGadget(button)
          if RXtHasCallbacks(button, RXmNactivateCallback) == RXtCallbackHasSome
            RXtCallCallbacks(button, RXmNactivateCallback,
                             let(RXmPushButtonCallbackStruct()) do |but|
                               Rset_click_count(but, 0)
                               Rset_event(but, let(RXEvent(RButtonPress)) do |e|
                                            Rset_state(e, (bits or 0))
                                            e
                                          end)
                               but
                             end)
          else
            snd_display("pushbutton %s has no active callbacks", RXtName(button))
          end
        else
          if RXmIsToggleButton(button) or RXmIsToggleButtonGadget(button)
            if RXtHasCallbacks(button, RXmNvalueChangedCallback) == RXtCallbackHasSome
              RXtCallCallbacks(button, RXmNvalueChangedCallback,
                               let(RXmToggleButtonCallbackStruct()) do |tgl|
                                 Rset_set(tgl, value)
                                 Rset_event(tgl, let(RXEvent(RButtonPress)) do |e|
                                              Rset_state(e, (bits or 0))
                                              e
                                            end)
                                 tgl
                               end)
            else
              snd_display("togglebutton %s has no valueChanged callbacks", RXtName(button))
            end
          else
            if RXmIsArrowButton(button)
              if RXtHasCallbacks(button, RXmNactivateCallback) == RXtCallbackHasSome
                RXtCallCallbacks(button, RXmNactivateCallback,
                                 let(RXmArrowButtonCallbackStruct()) do |arr|
                                   Rset_click_count(arr, 0)
                                   Rset_event(arr, let(RXEvent(RButtonPress)) do |e|
                                                Rset_state(e, (bits or 0))
                                                e
                                              end)
                                   arr
                                 end)
              else
                snd_display("arrowbutton %s has no active callbacks", RXtName(button))
              end
            else
              snd_display("%s (%s) is not a push or toggle button",
                          RXtName(button), RXtName(RXtParent(button)))
            end
          end
        end
      else
        snd_display("%s is not sensitive", RXtName(button))
      end
    else
      snd_display("%s is not a widget", button)
    end
  end

  def resize_pane(wid, height)
    RXtUnmanageChild(wid)
    RXtVaSetValues(wid,
                   [RXmNpaneMinimum, (height > 5 ? (height - 5) : 0),
                     RXmNpaneMaximum, height + 5])
    RXtManageChild(wid)
    RXtVaSetValues(wid, [RXmNpaneMinimum, 5, RXmNpaneMaximum, 1000])
  end
  
  def force_event
    app = main_widgets.car
    msk = RXtIMXEvent | RXtIMAlternateInput
    until (RXtAppPending(app) & msk).zero?
      RXtDispatchEvent(RXtAppNextEvent(app))
    end
  end

  def take_keyboard_focus(wid)
    i