#! /bin/sh # Output a system dependent set of variables, describing how to set the # run time search path of shared libraries in an executable. # # Copyright 1996-2007 Free Software Foundation, Inc. # Taken from GNU libtool, 2001 # Originally by Gordon Matzigkeit , 1996 # # This file is free software; the Free Software Foundation gives # unlimited permission to copy and/or distribute it, with or without # modifications, as long as this notice is preserved. # # The first argument passed to this file is the canonical host specification, # CPU_TYPE-MANUFACTURER-OPERATING_SYSTEM # or # CPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM # The environment variables CC, GCC, LDFLAGS, LD, with_gnu_ld # should be set by the caller. # # The set of defined variables is at the end of this script. # Known limitations: # - On IRIX 6.5 with CC="cc", the run time search patch must not be longer # than 256 bytes, otherwise the compiler driver will dump core. The only # known workaround is to choose shorter directory names for the build # directory and/or the installation directory. # All known linkers require a `.a' archive for static linking (except MSVC, # which needs '.lib'). libext=a shrext=.so host="$1" host_cpu=`echo "$host" | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\1/'` host_vendor=`echo "$host" | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\2/'` host_os=`echo "$host" | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\3/'` # Code taken from libtool.m4's _LT_CC_BASENAME. for cc_temp in $CC""; do case $cc_temp in compile | *[\\/]compile | ccache | *[\\/]ccache ) ;; distcc | *[\\/]distcc | purify | *[\\/]purify ) ;; \-*) ;; *) break;; esac done cc_basename=`echo "$cc_temp" | sed -e 's%^.*/%%'` # Code taken from libtool.m4's AC_LIBTOOL_PROG_COMPILER_PIC. wl= if test "$GCC" = yes; then wl='-Wl,' else case "$host_os" in aix*) wl='-Wl,' ;; darwin*) case $cc_basename in xlc*) wl='-Wl,' ;; esac ;; mingw* | cygwin* | pw32* | os2*) ;; hpux9* | hpux10* | hpux11*) wl='-Wl,' ;; irix5* | irix6* | nonstopux*) wl='-Wl,' ;; newsos6) ;; linux* | k*bsd*-gnu) case $cc_basename in icc* | ecc*) wl='-Wl,' ;; pgcc | pgf77 | pgf90) wl='-Wl,' ;; ccc*) wl='-Wl,' ;; como) wl='-lopt=' ;; *) case `$CC -V 2>&1 | sed 5q` in *Sun\ C*) wl='-Wl,' ;; esac ;; esac ;; osf3* | osf4* | osf5*) wl='-Wl,' ;; rdos*) ;; solaris*) wl='-Wl,' ;; sunos4*) wl='-Qoption ld ' ;; sysv4 | sysv4.2uw2* | sysv4.3*) wl='-Wl,' ;; sysv4*MP*) ;; sysv5* | unixware* | sco3.2v5* | sco5v6* | OpenUNIX*) wl='-Wl,' ;; unicos*) wl='-Wl,' ;; uts4*) ;; esac fi # Code taken from libtool.m4's AC_LIBTOOL_PROG_LD_SHLIBS. hardcode_libdir_flag_spec= hardcode_libdir_separator= hardcode_direct=no hardcode_minus_L=no case "$host_os" in cygwin* | mingw* | pw32*) # FIXME: the MSVC++ port hasn't been tested in a loooong time # When not using gcc, we currently assume that we are using # Microsoft Visual C++. if test "$GCC" != yes; then with_gnu_ld=no fi ;; interix*) # we just hope/assume this is gcc and not c89 (= MSVC++) with_gnu_ld=yes ;; openbsd*) with_gnu_ld=no ;; esac ld_shlibs=yes if test "$with_gnu_ld" = yes; then # Set some defaults for GNU ld with shared library support. These # are reset later if shared libraries are not supported. Putting them # here allows them to be overridden if necessary. # Unlike libtool, we use -rpath here, not --rpath, since the documented # option of GNU ld is called -rpath, not --rpath. hardcode_libdir_flag_spec='${wl}-rpath ${wl}$libdir' case "$host_os" in aix3* | aix4* | aix5*) # On AIX/PPC, the GNU linker is very broken if test "$host_cpu" != ia64; then ld_shlibs=no fi ;; amigaos*) hardcode_libdir_flag_spec='-L$libdir' hardcode_minus_L=yes # Samuel A. Falvo II reports # that the semantics of dynamic libraries on AmigaOS, at least up # to version 4, is to share data among multiple programs linked # with the same dynamic library. Since this doesn't match the # behavior of shared libraries on other platforms, we cannot use # them. ld_shlibs=no ;; beos*) if $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then : else ld_shlibs=no fi ;; cygwin* | mingw* | pw32*) # hardcode_libdir_flag_spec is actually meaningless, as there is # no search path for DLLs. hardcode_libdir_flag_spec='-L$libdir' if $LD --help 2>&1 | grep 'auto-import' > /dev/null; then : else ld_shlibs=no fi ;; interix[3-9]*) hardcode_direct=no hardcode_libdir_flag_spec='${wl}-rpath,$libdir' ;; gnu* | linux* | k*bsd*-gnu) if $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then : else ld_shlibs=no fi ;; netbsd*) ;; solaris*) if $LD -v 2>&1 | grep 'BFD 2\.8' > /dev/null; then ld_shlibs=no elif $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then : else ld_shlibs=no fi ;; sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX*) case `$LD -v 2>&1` in *\ [01].* | *\ 2.[0-9].* | *\ 2.1[0-5].*) ld_shlibs=no ;; *) if $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then hardcode_libdir_flag_spec='`test -z "$SCOABSPATH" && echo ${wl}-rpath,$libdir`' else ld_shlibs=no fi ;; esac ;; sunos4*) hardcode_direct=yes ;; *) if $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then : else ld_shlibs=no fi ;; esac if test "$ld_shlibs" = no; then hardcode_libdir_flag_spec= fi else case "$host_os" in aix3*) # Note: this linker hardcodes the directories in LIBPATH if there # are no directories specified by -L. hardcode_minus_L=yes if test "$GCC" = yes; then # Neither direct hardcoding nor static linking is supported with a # broken collect2. hardcode_direct=unsupported fi ;; aix4* | aix5*) if test "$host_cpu" = ia64; then # On IA64, the linker does run time linking by default, so we don't # have to do anything special. aix_use_runtimelinking=no else aix_use_runtimelinking=no # Test if we are trying to use run time linking or normal # AIX style linking. If -brtl is somewhere in LDFLAGS, we # need to do runtime linking. case $host_os in aix4.[23]|aix4.[23].*|aix5*) for ld_flag in $LDFLAGS; do if (test $ld_flag = "-brtl" || test $ld_flag = "-Wl,-brtl"); then aix_use_runtimelinking=yes break fi done ;; esac fi hardcode_direct=yes hardcode_libdir_separator=':' if test "$GCC" = yes; then case $host_os in aix4.[012]|aix4.[012].*) collect2name=`${CC} -print-prog-name=collect2` if test -f "$collect2name" && \ strings "$collect2name" | grep resolve_lib_name >/dev/null then # We have reworked collect2 : else # We have old collect2 hardcode_direct=unsupported hardcode_minus_L=yes hardcode_libdir_flag_spec='-L$libdir' hardcode_libdir_separator= fi ;; esac fi # Begin _LT_AC_SYS_LIBPATH_AIX. echo 'int main () { return 0; }' > conftest.c ${CC} ${LDFLAGS} conftest.c -o conftest aix_libpath=`dump -H conftest 2>/dev/null | sed -n -e '/Import File Strings/,/^$/ { /^0/ { s/^0 *\(.*\)$/\1/; p; } }'` if test -z "$aix_libpath"; then aix_libpath=`dump -HX64 conftest 2>/dev/null | sed -n -e '/Import File Strings/,/^$/ { /^0/ { s/^0 *\(.*\)$/\1/; p; } }'` fi if test -z "$aix_libpath"; then aix_libpath="/usr/lib:/lib" fi rm -f conftest.c conftest # End _LT_AC_SYS_LIBPATH_AIX. if test "$aix_use_runtimelinking" = yes; then hardcode_libdir_flag_spec='${wl}-blibpath:$libdir:'"$aix_libpath" else if test "$host_cpu" = ia64; then hardcode_libdir_flag_spec='${wl}-R $libdir:/usr/lib:/lib' else hardcode_libdir_flag_spec='${wl}-blibpath:$libdir:'"$aix_libpath" fi fi ;; amigaos*) hardcode_libdir_flag_spec='-L$libdir' hardcode_minus_L=yes # see comment about different semantics on the GNU ld section ld_shlibs=no ;; bsdi[45]*) ;; cygwin* | mingw* | pw32*) # When not using gcc, we currently assume that we are using # Microsoft Visual C++. # hardcode_libdir_flag_spec is actually meaningless, as there is # no search path for DLLs. hardcode_libdir_flag_spec=' ' libext=lib ;; darwin* | rhapsody*) hardcode_direct=no if test "$GCC" = yes ; then : else case $cc_basename in xlc*) ;; *) ld_shlibs=no ;; esac fi ;; dgux*) hardcode_libdir_flag_spec='-L$libdir' ;; freebsd1*) ld_shlibs=no ;; freebsd2.2*) hardcode_libdir_flag_spec='-R$libdir' hardcode_direct=yes ;; freebsd2*) hardcode_direct=yes hardcode_minus_L=yes ;; freebsd* | dragonfly*) hardcode_libdir_flag_spec='-R$libdir' hardcode_direct=yes ;; hpux9*) hardcode_libdir_flag_spec='${wl}+b ${wl}$libdir' hardcode_libdir_separator=: hardcode_direct=yes # hardcode_minus_L: Not really in the search PATH, # but as the default location of the library. hardcode_minus_L=yes ;; hpux10*) if test "$with_gnu_ld" = no; then hardcode_libdir_flag_spec='${wl}+b ${wl}$libdir' hardcode_libdir_separator=: hardcode_direct=yes # hardcode_minus_L: Not really in the search PATH, # but as the default location of the library. hardcode_minus_L=yes fi ;; hpux11*) if test "$with_gnu_ld" = no; then hardcode_libdir_flag_spec='${wl}+b ${wl}$libdir' hardcode_libdir_separator=: case $host_cpu in hppa*64*|ia64*) hardcode_direct=no ;; *) hardcode_direct=yes # hardcode_minus_L: Not really in the search PATH, # but as the default location of the library. hardcode_minus_L=yes ;; esac fi ;; irix5* | irix6* | nonstopux*) hardcode_libdir_flag_spec='${wl}-rpath ${wl}$libdir' hardcode_libdir_separator=: ;; netbsd*) hardcode_libdir_flag_spec='-R$libdir' hardcode_direct=yes ;; newsos6) hardcode_direct=yes hardcode_libdir_flag_spec='${wl}-rpath ${wl}$libdir' hardcode_libdir_separator=: ;; openbsd*) if test -f /usr/libexec/ld.so; then hardcode_direct=yes if test -z "`echo __ELF__ | $CC -E - | grep __ELF__`" || test "$host_os-$host_cpu" = "openbsd2.8-powerpc"; then hardcode_libdir_flag_spec='${wl}-rpath,$libdir' else case "$host_os" in openbsd[01].* | openbsd2.[0-7] | openbsd2.[0-7].*) hardcode_libdir_flag_spec='-R$libdir' ;; *) hardcode_libdir_flag_spec='${wl}-rpath,$libdir' ;; esac fi else ld_shlibs=no fi ;; os2*) hardcode_libdir_flag_spec='-L$libdir' hardcode_minus_L=yes ;; osf3*) hardcode_libdir_flag_spec='${wl}-rpath ${wl}$libdir' hardcode_libdir_separator=: ;; osf4* | osf5*) if test "$GCC" = yes; then hardcode_libdir_flag_spec='${wl}-rpath ${wl}$libdir' else # Both cc and cxx compiler support -rpath directly hardcode_libdir_flag_spec='-rpath $libdir' fi hardcode_libdir_separator=: ;; solaris*) hardcode_libdir_flag_spec='-R$libdir' ;; sunos4*) hardcode_libdir_flag_spec='-L$libdir' hardcode_direct=yes hardcode_minus_L=yes ;; sysv4) case $host_vendor in sni) hardcode_direct=yes # is this really true??? ;; siemens) hardcode_direct=no ;; motorola) hardcode_direct=no #Motorola manual says yes, but my tests say they lie ;; esac ;; sysv4.3*) ;; sysv4*MP*) if test -d /usr/nec; then ld_shlibs=yes fi ;; sysv4*uw2* | sysv5OpenUNIX* | sysv5UnixWare7.[01].[10]* | unixware7* | sco3.2v5.0.[024]*) ;; sysv5* | sco3.2v5* | sco5v6*) hardcode_libdir_flag_spec='`test -z "$SCOABSPATH" && echo ${wl}-R,$libdir`' hardcode_libdir_separator=':' ;; uts4*) hardcode_libdir_flag_spec='-L$libdir' ;; *) ld_shlibs=no ;; esac fi # Check dynamic linker characteristics # Code taken from libtool.m4's AC_LIBTOOL_SYS_DYNAMIC_LINKER. # Unlike libtool.m4, here we don't care about _all_ names of the library, but # only about the one the linker finds when passed -lNAME. This is the last # element of library_names_spec in libtool.m4, or possibly two of them if the # linker has special search rules. library_names_spec= # the last element of library_names_spec in libtool.m4 libname_spec='lib$name' case "$host_os" in aix3*) library_names_spec='$libname.a' ;; aix4* | aix5*) library_names_spec='$libname$shrext' ;; amigaos*) library_names_spec='$libname.a' ;; beos*) library_names_spec='$libname$shrext' ;; bsdi[45]*) library_names_spec='$libname$shrext' ;; cygwin* | mingw* | pw32*) shrext=.dll library_names_spec='$libname.dll.a $libname.lib' ;; darwin* | rhapsody*) shrext=.dylib library_names_spec='$libname$shrext' ;; dgux*) library_names_spec='$libname$shrext' ;; freebsd1*) ;; freebsd* | dragonfly*) case "$host_os" in freebsd[123]*) library_names_spec='$libname$shrext$versuffix' ;; *) library_names_spec='$libname$shrext' ;; esac ;; gnu*) library_names_spec='$libname$shrext' ;; hpux9* | hpux10* | hpux11*) case $host_cpu in ia64*) shrext=.so ;; hppa*64*) shrext=.sl ;; *) shrext=.sl ;; esac library_names_spec='$libname$shrext' ;; interix[3-9]*) library_names_spec='$libname$shrext' ;; irix5* | irix6* | nonstopux*) library_names_spec='$libname$shrext' case "$host_os" in irix5* | nonstopux*) libsuff= shlibsuff= ;; *) case $LD in *-32|*"-32 "|*-melf32bsmip|*"-melf32bsmip ") libsuff= shlibsuff= ;; *-n32|*"-n32 "|*-melf32bmipn32|*"-melf32bmipn32 ") libsuff=32 shlibsuff=N32 ;; *-64|*"-64 "|*-melf64bmip|*"-melf64bmip ") libsuff=64 shlibsuff=64 ;; *) libsuff= shlibsuff= ;; esac ;; esac ;; linux*oldld* | linux*aout* | linux*coff*) ;; linux* | k*bsd*-gnu) library_names_spec='$libname$shrext' ;; knetbsd*-gnu) library_names_spec='$libname$shrext' ;; netbsd*) library_names_spec='$libname$shrext' ;; newsos6) library_names_spec='$libname$shrext' ;; nto-qnx*) library_names_spec='$libname$shrext' ;; openbsd*) library_names_spec='$libname$shrext$versuffix' ;; os2*) libname_spec='$name' shrext=.dll library_names_spec='$libname.a' ;; osf3* | osf4* | osf5*) library_names_spec='$libname$shrext' ;; rdos*) ;; solaris*) library_names_spec='$libname$shrext' ;; sunos4*) library_names_spec='$libname$shrext$versuffix' ;; sysv4 | sysv4.3*) library_names_spec='$libname$shrext' ;; sysv4*MP*) library_names_spec='$libname$shrext' ;; sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX* | sysv4*uw2*) library_names_spec='$libname$shrext' ;; uts4*) library_names_spec='$libname$shrext' ;; esac sed_quote_subst='s/\(["`$\\]\)/\\\1/g' escaped_wl=`echo "X$wl" | sed -e 's/^X//' -e "$sed_quote_subst"` shlibext=`echo "$shrext" | sed -e 's,^\.,,'` escaped_libname_spec=`echo "X$libname_spec" | sed -e 's/^X//' -e "$sed_quote_subst"` escaped_library_names_spec=`echo "X$library_names_spec" | sed -e 's/^X//' -e "$sed_quote_subst"` escaped_hardcode_libdir_flag_spec=`echo "X$hardcode_libdir_flag_spec" | sed -e 's/^X//' -e "$sed_quote_subst"` LC_ALL=C sed -e 's/^\([a-zA-Z0-9_]*\)=/acl_cv_\1=/' <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
/* $Id: drv_BeckmannEgle.c,v 1.14 2005/01/18 06:30:22 reinelt Exp $
 *
 * driver for Beckmann+Egle "Mini Terminals" and "Compact Terminals"
 * Copyright (C) 2000 Michael Reinelt <reinelt@eunet.at>
 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 *
 * This file is part of LCD4Linux.
 *
 * LCD4Linux 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, or (at your option)
 * any later version.
 *
 * LCD4Linux 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 * $Log: drv_BeckmannEgle.c,v $
 * Revision 1.14  2005/01/18 06:30:22  reinelt
 * added (C) to all copyright statements
 *
 * Revision 1.13  2004/07/14 04:44:44  reinelt
 *
 * Beckmann+Egle fix
 * added smaple widget for the PPP plugin
 *
 * Revision 1.12  2004/06/29 04:49:30  reinelt
 *
 * B+E enhanced port detection
 *
 * Revision 1.11  2004/06/26 12:04:59  reinelt
 *
 * uh-oh... the last CVS log message messed up things a lot...
 *
 * Revision 1.10  2004/06/26 09:27:20  reinelt
 *
 * added '-W' to CFLAGS
 * changed all C++ comments to C ones
 * cleaned up a lot of signed/unsigned mistakes
 *
 * Revision 1.9  2004/06/26 06:12:15  reinelt
 *
 * support for Beckmann+Egle Compact Terminals
 * some mostly cosmetic changes in the MatrixOrbital and USBLCD driver
 * added debugging to the generic serial driver
 * fixed a bug in the generic text driver where icons could be drawn outside
 * the display bounds
 *
 * Revision 1.8  2004/06/20 10:09:54  reinelt
 *
 * 'const'ified the whole source
 *
 * Revision 1.7  2004/06/06 06:51:59  reinelt
 *
 * do not display end splash screen if quiet=1
 *
 * Revision 1.6  2004/06/05 06:41:39  reinelt
 *
 * chancged splash screen again
 *
 * Revision 1.5  2004/06/05 06:13:11  reinelt
 *
 * splash screen for all text-based display drivers
 *
 * Revision 1.4  2004/06/02 09:41:19  reinelt
 *
 * prepared support for startup splash screen
 *
 * Revision 1.3  2004/06/02 05:14:16  reinelt
 *
 * fixed models listing for Beckmann+Egle driver
 * some cosmetic changes
 *
 * Revision 1.2  2004/05/31 05:38:02  reinelt
 *
 * fixed possible bugs with user-defined chars (clear high bits)
 * thanks to Andy Baxter for debugging the MilfordInstruments driver!
 *
 * Revision 1.1  2004/05/28 14:36:10  reinelt
 *
 * added drv_BeckmannEgle.c (forgotten at first check in :-)
 *
 */

/* 
 *
 * exported fuctions:
 *
 * struct DRIVER drv_BeckmannEgle
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>

#include "debug.h"
#include "cfg.h"
#include "qprintf.h"
#include "plugin.h"
#include "widget.h"
#include "widget_text.h"
#include "widget_icon.h"
#include "widget_bar.h"
#include "drv.h"
#include "drv_generic_text.h"
#include "drv_generic_serial.h"


#define ESC "\033"

static char Name[]="Beckmann+Egle";


typedef struct {
  char *name;
  int rows;
  int cols;
  int protocol;
  int type;
} MODEL;


static MODEL Models[] = {

  /* MultiTerminals */
  { "MT16x1", 1, 16, 1,  0 },
  { "MT16x2", 2, 16, 1,  1 },
  { "MT16x4", 4, 16, 1,  2 },
  { "MT20x1", 1, 20, 1,  3 },
  { "MT20x2", 2, 20, 1,  4 },
  { "MT20x4", 4, 20, 1,  5 },
  { "MT24x1", 1, 24, 1,  6 },
  { "MT24x2", 2, 24, 1,  7 },
  { "MT32x1", 1, 32, 1,  8 },
  { "MT32x2", 2, 32, 1,  9 },
  { "MT40x1", 1, 40, 1, 10 },
  { "MT40x2", 2, 40, 1, 11 },
  { "MT40x4", 4, 40, 1, 12 },

  /* CompactTerminal */
  { "CT20x4", 4, 20, 2,  0 },

  { NULL,     0,  0, 0,  0 },
};


static int Model;
static int Protocol;



/****************************************/
/***  hardware dependant functions    ***/
/****************************************/

static void drv_BuE_clear (void)
{
  switch (Protocol) {
  case 1:
    drv_generic_serial_write (ESC "&#", 3); /* clear display */
    break;
  case 2:
    drv_generic_serial_write (ESC "LL", 3); /* clear display */
    break;
  }
}


static void drv_BuE_MT_write (const int row, const int col, const char *data, const int len)
{
  char cmd[] = ESC "[y;xH";

  cmd[2] = (char)row;
  cmd[4] = (char)col;

  drv_generic_serial_write (cmd, 6);
  drv_generic_serial_write (data, len);
}


static void drv_BuE_MT_defchar (const int ascii, const unsigned char *matrix)
{
  int  i;
  char cmd[22] = ESC "&T";         /* enter transparent mode */

  cmd[3] = '\0';                   /* write cmd */
  cmd[4] = 0x40|8*ascii;           /* write CGRAM */

  for (i = 0; i < 8; i++) {
    cmd[2*i+5] = '\1';             /* write data */
    cmd[2*i+6] = matrix[i] & 0x1f; /* character bitmap */
  }
  cmd[21] = '\377';                /* leave transparent mode */

  drv_generic_serial_write (cmd, 22);
}


static void drv_BuE_CT_write (const int row, const int col, const char *data, const int len)
{
  char cmd[] = ESC "LCzs\001";
  cmd[3] = (char)row + 1;
  cmd[4] = (char)col + 1;

  drv_generic_serial_write (cmd, 6);
  drv_generic_serial_write (data, len);

}


static void drv_BuE_CT_defchar (const int ascii, const unsigned char *matrix)
{
  int i;
  char cmd[13] = ESC "LZ";  /* set custom char */

  /* number of user-defined char (0..7) */
  cmd[3] = (char) ascii - CHAR0;

  /* ASCII code to replace */
  cmd[4] = (char) ascii;
  
  for (i = 0; i < 8; i++) {
    cmd[i+5] = matrix[i] & 0x1f;
  }

  drv_generic_serial_write (cmd, 13);
}


static int drv_BuE_CT_contrast (int contrast)
{
  static char Contrast = 7;
  char cmd[4] = ESC "LKn";

  /* -1 is used to query the current contrast */
  if (contrast == -1) return Contrast;

  if (contrast < 0 ) contrast = 0;
  if (contrast > 15) contrast = 15;
  Contrast = contrast;
  
  cmd[3] = Contrast;

  drv_generic_serial_write (cmd, 4);
  
  return Contrast;
}


static int drv_BuE_CT_backlight (int backlight)
{
  static char Backlight=0;
  char cmd[4] = ESC "LBn";

  /* -1 is used to query the current backlight */
  if (backlight == -1) return Backlight;

  if (backlight < 0) backlight = 0;
  if (backlight > 1) backlight = 1;
  Backlight = backlight;
  
  cmd[3] = Backlight;

  drv_generic_serial_write (cmd, 4);

  return Backlight;
}


static int drv_BuE_CT_gpo (int num, int val)
{
  static int GPO[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
  char cmd[4] = ESC "Pnx";
  
  if (num < 0) num = 0;
  if (num > 7) num = 7;

  /* -1 is used to query the current GPO */
  if (val == -1) return GPO[num];

  if (val < 0  ) val = 0;
  if (val > 255) val = 255;
  GPO[num] = val;
  
  cmd[2] = (char)num;
  cmd[3] = (char)val;
  
  drv_generic_serial_write (cmd, 4);
  
  return GPO[num];
}


static int drv_BuE_CT_gpi (int num)
{
  char cmd[4] = ESC "?Pn";
  char buffer[4];
  
  if (num < 0) num = 0;
  if (num > 7) num = 7;

  cmd[3] = (char)num;
  drv_generic_serial_write (cmd, 4);

  usleep (10000);

  if (drv_generic_serial_read (buffer, 4) != 4) {
    error ("%s: error reading port %d", Name, num);
    return -1;
  }
  
  return buffer[3];
}


static int drv_BuE_CT_adc (void)
{
  char buffer[4];

  drv_generic_serial_write (ESC "?A", 3);

  usleep (10000);
  
  if ((drv_generic_serial_read (buffer, 4) != 4) ||
      (buffer[0] != 'A') || 
      (buffer[1] != ':')
      ) {
    error ("%s: error reading ADC", Name);
    return -1;
  }
  
  /* 10 bit value: 8 bit high, 2 bit low */
  return 4 * (unsigned char) buffer[2] + (unsigned char) buffer[3];
}


static int drv_BuE_CT_pwm (int val)
{
  static int PWM = -1;
  char cmd[4] = ESC "Adm";
  
  /* -1 is used to query the current PWM */
  if (val == -1) return PWM;

  if (val <   0) val =   0;
  if (val > 255) val = 255;
  PWM = val;

  cmd[2] = (char) val;
  cmd[3] = val == 0 ? 1 : 2; 
  drv_generic_serial_write (cmd, 4);

  return val;
}


static int drv_BuE_MT_start (const char *section)
{
  char cmd[] = ESC "&sX";

  /* CSTOPB: 2 stop bits */
  if (drv_generic_serial_open(section, Name, CSTOPB) < 0) return -1;
  
  cmd[4] = Models[Model].type;
  drv_generic_serial_write (cmd,      4); /* select display type */
  drv_generic_serial_write (ESC "&D", 3); /* cursor off */
  
  return 0;
}


static int drv_BuE_CT_start (const char *section)
{
  char buffer[16];
  char *size;
  int i, len;
  
  if (drv_generic_serial_open(section, Name, 0) < 0) return -1;
  
#if 0
  /* restart terminal */
  drv_generic_serial_write (ESC "Kr", 3);
  usleep(10000);
#endif
  
  /* Fixme: the CT does not return a serial number in byte mode */
  /* set parameter mode 'decimal' */
  drv_generic_serial_write (ESC "KM\073", 4);
  
  /* read version */
  drv_generic_serial_write (ESC "?V", 3);
  usleep(100000);
  if ((len = drv_generic_serial_read (buffer, -sizeof(buffer))) > 0) {
    int v, r, s;
    if (sscanf(buffer, "V:%d.%d,%d;", &v, &r, &s) != 3) {
      error ("%s: error parsing display identification <%*s>", Name, len, buffer);
    } else {
      info ("%s: display identified as version %d.%d, S/N %d", Name, v, r, s);
    }
  }

  /* set parameter mode 'byte' */
  drv_generic_serial_write (ESC "KM\072", 4);

  /* the CT20x4 can control smaller displays, too */
  size = cfg_get(section, "Size", NULL);
  if (size != NULL && *size != '\0') {
    int r, c;
    char cmd[6] = ESC "LArc";
    if (sscanf(size, "%dx%d", &c, &r) != 2 || r < 1 || c < 1) {
      error ("%s: bad %s.Size '%s' from %s", Name, section, size, cfg_source());
      return -1;
    }
    info ("%s: display size: %d rows %d columns", Name, r, c); 
    /* set display size */
    cmd[3] = (char) r;
    cmd[4] = (char) c;
    drv_generic_serial_write (cmd, 5);
    DCOLS = c;
    DROWS = r;
  }
  
  /* set contrast */
  if (cfg_number(section, "Contrast", 7, 0, 15, &i) > 0) {
    drv_BuE_CT_contrast(i);
  }

  /* set backlight */
  if (cfg_number(section, "Backlight", 0, 0, 1, &i) > 0) {
    drv_BuE_CT_backlight(i);
  }


  /* identify modules */

  for (i = 0; i < 8; i++) {
    char cmd[5] = ESC "K?Pn";
    cmd[4] = (char)i;
    drv_generic_serial_write (cmd, 5); /* query I/O port */
    usleep(10000);
    if ((len = drv_generic_serial_read (buffer, 4)) == 4) {
      char *type = NULL;
      if (i == 0) {
	if( buffer[3] == 8) {
	  /* internal port */
	  type = "CT 20x4 internal port";
	} else {
	  error ("%s: internal error: port 0 type %d should be type 8", Name, buffer[3]);
	  continue;
	}
      } else {
	switch (buffer[3]) {
	case 1: /* Key Module */
	  type = "XM-KEY-2x4-LED";
	  break;
	case 8: /* I/O Module */
	  type = "XM-IO8-T";
	  break;
	case 9: /* I/O Module */
	  type = "XM-IO4-R";
	  break;
	case 15: /* nothing */
	  continue;
	default: /* unhandled */
	  type = NULL;
	  break;
	}
      }
      if (type != NULL) {
	info ("%s: Port %d: %s", Name, i, type);
      } else {
	error ("%s: internal error: port % unknown type %d", Name, i, cmd[3]);
      }
    } else {
      error ("%s: error fetching type of port %d", Name, i);
    }
  }

  return 0;
}


static int drv_BuE_start (const char *section)
{
  int i, ret;  
  char *model;
  
  model = cfg_get(section, "Model", NULL);
  if (model == NULL && *model == '\0') {
    error ("%s: no '%s.Model' entry from %s", Name, section, cfg_source());
    return -1;
  }
  
  for (i = 0; Models[i].name != NULL; i++) {
    if (strcasecmp(Models[i].name, model) == 0) break;
  }

  if (Models[i].name == NULL) {
    error ("%s: %s.Model '%s' is unknown from %s", Name, section, model, cfg_source());
    return -1;
  }

  Model = i;
  Protocol = Models[Model].protocol;

  info ("%s: using model '%s'", Name, Models[Model].name);
  
  /* initialize global variables */
  DROWS    = Models[Model].rows;
  DCOLS    = Models[Model].cols;
  
  ret = 0;
  switch (Protocol) {
  case 1:
    ret = drv_BuE_MT_start(section);
    break;
  case 2:
    ret = drv_BuE_CT_start(section);
    break;
  }
  if (ret != 0) {
    return ret;
  }
  
  drv_BuE_clear();

  return 0;
}


/****************************************/
/***            plugins               ***/
/****************************************/

static void plugin_contrast (RESULT *result, const int argc, RESULT *argv[])
{
  double contrast;
  
  switch (argc) {
  case 0:
    contrast = drv_BuE_CT_contrast(-1);
    SetResult(&result, R_NUMBER, &contrast); 
    break;
  case 1:
    contrast = drv_BuE_CT_contrast(R2N(argv[0]));
    SetResult(&result, R_NUMBER, &contrast); 
    break;
  default:
    error ("%s::contrast(): wrong number of parameters", Name);
    SetResult(&result, R_STRING, ""); 
  }
}


static void plugin_backlight (RESULT *result, const int argc, RESULT *argv[])
{
  double backlight;
  
  switch (argc) {
  case 0:
    backlight = drv_BuE_CT_backlight(-1);
    SetResult(&result, R_NUMBER, &backlight); 
    break;
  case 1:
    backlight = drv_BuE_CT_backlight(R2N(argv[0]));
    SetResult(&result, R_NUMBER, &backlight); 
    break;
  default:
    error ("%s::backlight(): wrong number of parameters", Name);
    SetResult(&result, R_STRING, ""); 
  }
}


static void plugin_gpo (RESULT *result, const int argc, RESULT *argv[])
{
  double gpo;
  
  switch (argc) {
  case 1:
    gpo = drv_BuE_CT_gpo(R2N(argv[0]), -1);
    SetResult(&result, R_NUMBER, &gpo); 
    break;
  case 2:
    gpo = drv_BuE_CT_gpo(R2N(argv[0]), R2N(argv[1]));
    SetResult(&result, R_NUMBER, &gpo); 
    break;
  default:
    error ("%s::gpo(): wrong number of parameters", Name);
    SetResult(&result, R_STRING, ""); 
  }
}


static void plugin_gpi (RESULT *result, RESULT *arg1)
{
  double gpi;
  
  gpi = drv_BuE_CT_gpi(R2N(arg1));
  SetResult(&result, R_NUMBER, &gpi); 
}


static void plugin_adc (RESULT *result)
{
  double adc;
  
  adc = drv_BuE_CT_adc();
  SetResult(&result, R_NUMBER, &adc); 
}


static void plugin_pwm (RESULT *result, const int argc, RESULT *argv[])
{
  double pwm;
  
  switch (argc) {
  case 0:
    pwm = drv_BuE_CT_pwm(-1);
    SetResult(&result, R_NUMBER, &pwm); 
    break;
  case 1:
    pwm = drv_BuE_CT_pwm(R2N(argv[0]));
    SetResult(&result, R_NUMBER, &pwm); 
    break;
  default:
    error ("%s::pwm(): wrong number of parameters", Name);
    SetResult(&result, R_STRING, ""); 
  }
}


/****************************************/
/***        widget callbacks          ***/
/****************************************/

/* using drv_generic_text_draw(W) */
/* using drv_generic_text_icon_draw(W) */
/* using drv_generic_text_bar_draw(W) */


/****************************************/
/***        exported functions        ***/
/****************************************/


/* list models */
int drv_BuE_list (void)
{
  int i;
  
  for (i = 0; Models[i].name != NULL; i++) {
    printf ("%s ", Models[i].name);
  }
  return 0;
}


/* initialize driver & display */
int drv_BuE_init (const char *section, const int quiet)
{
  WIDGET_CLASS wc;
  int ret;  
  
  /* start display */
  if ((ret=drv_BuE_start (section)) != 0) {
    return ret;
  }

  /* display preferences */
  XRES  = 5;     /* pixel width of one char  */
  YRES  = 8;     /* pixel height of one char  */
  CHARS = 8;     /* number of user-defineable characters */
  
  /* real worker functions */
  switch (Protocol) {
  case 1:
    CHAR0 = 0;     /* ASCII of first user-defineable char */
    GOTO_COST = 6; /* number of bytes a goto command requires */
    drv_generic_text_real_write   = drv_BuE_MT_write;
    drv_generic_text_real_defchar = drv_BuE_MT_defchar;
    break;
  case 2:
    CHAR0 = 128;   /* ASCII of first user-defineable char */
    GOTO_COST = 6; /* number of bytes a goto command requires */
    drv_generic_text_real_write   = drv_BuE_CT_write;
    drv_generic_text_real_defchar = drv_BuE_CT_defchar;
    break;
  }

  if (!quiet) {
    char buffer[40];
    qprintf(buffer, sizeof(buffer), "%s %s", Name, Models[Model].name);
    if (drv_generic_text_greet (buffer, "www.bue.com")) {
      sleep (3);
      drv_BuE_clear();
    }
  }

  /* initialize generic text driver */
  if ((ret=drv_generic_text_init(section, Name))!=0)
    return ret;

  /* initialize generic icon driver */
  if ((ret=drv_generic_text_icon_init())!=0)
    return ret;
  
  /* initialize generic bar driver */
  if ((ret=drv_generic_text_bar_init(0))!=0)
    return ret;
  
  /* add fixed chars to the bar driver */
  drv_generic_text_bar_add_segment (  0,  0,255, 32); /* ASCII  32 = blank */
  drv_generic_text_bar_add_segment (255,255,255,255); /* ASCII 255 = block */
  
  /* register text widget */
  wc=Widget_Text;
  wc.draw=drv_generic_text_draw;
  widget_register(&wc);
  
  /* register icon widget */
  wc=Widget_Icon;
  wc.draw=drv_generic_text_icon_draw;
  widget_register(&wc);
  
  /* register bar widget */
  wc=Widget_Bar;
  wc.draw=drv_generic_text_bar_draw;
  widget_register(&wc);
  
  /* register plugins */
  AddFunction ("LCD::contrast",  -1, plugin_contrast);
  AddFunction ("LCD::backlight", -1, plugin_backlight);
  AddFunction ("LCD::gpo",       -1, plugin_gpo);
  AddFunction ("LCD::gpi",        1, plugin_gpi);
  AddFunction ("LCD::adc",        0, plugin_adc);
  AddFunction ("LCD::pwm",       -1, plugin_pwm);

  return 0;
}


/* close driver & display */
int drv_BuE_quit (const int quiet) {

  info("%s: shutting down.", Name);

  drv_generic_text_quit();
  
  /* clear display  */
  drv_BuE_clear();

  /* say goodbye... */
  if (!quiet) {
    drv_generic_text_greet ("goodbye!", NULL);
  }
  
  drv_generic_serial_close();

  return (0);
}


DRIVER drv_BeckmannEgle = {
  name: Name,
  list: drv_BuE_list,
  init: drv_BuE_init,
  quit: drv_BuE_quit, 
};