Index: trunk/MagicSoft/Cosy/.cosyrc
===================================================================
--- trunk/MagicSoft/Cosy/.cosyrc	(revision 908)
+++ trunk/MagicSoft/Cosy/.cosyrc	(revision 909)
@@ -2,10 +2,12 @@
 # -85.0 , +310.0
 #
-MinAz[Deg]:   -30.0
-MaxAz[Deg]:  +375.0
+Az_MaxTime2ReachHome[s]: 100
+Az_Min[Deg]:   -30.0
+Az_Max[Deg]:  +375.0
 
 #
 # 
 #
-MinZd[Deg]:   -70.0
-MaxZd[Deg]:   +70.0
+Zd_MaxTime2ReachHome[s]: 100
+Zd_Min[Deg]:   -70.0
+Zd_Max[Deg]:   +70.0
Index: trunk/MagicSoft/Cosy/MCosy.cc
===================================================================
--- trunk/MagicSoft/Cosy/MCosy.cc	(revision 908)
+++ trunk/MagicSoft/Cosy/MCosy.cc	(revision 909)
@@ -14,8 +14,10 @@
 
 #include "macs.h"
-#include "timer.h"
+#include "base/timer.h"
 #include "shaftencoder.h"
 
-#include <sys/resource.h>  // PRIO_PROCESS
+#include "MGSkyPosition.h" // FIXME!!!!!!!!!!!!!!!!!
+
+//#include <sys/resource.h>  // PRIO_PROCESS
 
 typedef struct tm tm_t;
@@ -121,5 +123,4 @@
         min = dist;
     }
-    cout << "Shortest Zd: " << ret.Zd() << "  Az:" << ret.Az() << endl;
     return ret*(16384.0/360.0);
 }
@@ -347,5 +348,4 @@
         {
             lout << "Error 6004 happened" << endl;
-            SkipPendingSdos();
             break;
         }
@@ -393,5 +393,4 @@
         {
             lout << "Error 3006 happened" << endl;
-            SkipPendingSdos();
             break;
         }
@@ -525,4 +524,16 @@
 }
 
+void *MTGui::Thread()
+{
+    fCosy->GuiThread(this);
+    return NULL;
+}
+
+void *MTTalk::Thread()
+{
+    fCosy->TalkThread();
+    return NULL;
+}
+
 void MCosy::TalkThread()
 {
@@ -530,5 +541,7 @@
     // Start the Network
     //
-    Network::Start();
+    cout << "Reading configuration file..." << flush;
+    TEnv env(".cosyrc");
+    cout << "done." << endl;
 
     const int res = fMac3->GetVelRes();
@@ -538,8 +551,9 @@
     fMac3->SetDeceleration(res);
 
-    fMac3->SetSyncMode();
-
-    fMac1->SetHome(250000);
-    fMac2->SetHome(250000);
+    fMac3->StartPosSync();
+
+    cout << "Going Home..." << endl;
+    fMac1->SetHome(250000, env.GetValue("Az_MaxTime2ReachHome[s]", 100));
+    fMac2->SetHome(250000, env.GetValue("Zd_MaxTime2ReachHome[s]", 100));
     PostMsg(WM_PRESET, 0, 0);
     PostMsg(WM_WAIT,   0, 0);
@@ -551,15 +565,20 @@
     cout << "APOS: " << repos.Zd() << "re, " << repos.Az() << "re" << endl;
 
-    TEnv env(".cosyrc");
-
-    cout << Deg2AzRE(env.GetValue("MinAz[Deg]", -1.0)) << " < Az < "
-        << Deg2AzRE(env.GetValue("MaxAz[Deg]", +1.0)) << "RE" << endl;
-    cout << env.GetValue("MinAz[Deg]", -1.0) << " < Az < "
-        << env.GetValue("MaxAz[Deg]", +1.0) << kDEG << endl;
-    cout << Deg2ZdRE(env.GetValue("MinZd[Deg]", -1.0)) << "RE < Zd < "
-        << Deg2ZdRE(env.GetValue("MaxZd[Deg]", +1.0)) << "RE" << endl;
-
-    fMac1->SetNegEndswitch(Deg2AzRE(env.GetValue("MinAz[Deg]", -1.0)));
-    fMac1->SetPosEndswitch(Deg2AzRE(env.GetValue("MaxAz[Deg]", +1.0)));
+    /*
+     cout << Deg2AzRE(env.GetValue("MinAz[Deg]", -1.0)) << " < Az < "
+     << Deg2AzRE(env.GetValue("MaxAz[Deg]", +1.0)) << "RE" << endl;
+     cout << env.GetValue("MinAz[Deg]", -1.0) << " < Az < "
+     << env.GetValue("MaxAz[Deg]", +1.0) << kDEG << endl;
+     cout << Deg2ZdRE(env.GetValue("MinZd[Deg]", -1.0)) << "RE < Zd < "
+     << Deg2ZdRE(env.GetValue("MaxZd[Deg]", +1.0)) << "RE" << endl;
+     */
+
+    cout << "Setting up software endswitch..." << flush;
+    fMac1->SetNegEndswitch(Deg2AzRE(env.GetValue("Az_Min[Deg]", -1.0)));
+    fMac1->SetPosEndswitch(Deg2AzRE(env.GetValue("Az_Max[Deg]", +1.0)));
+
+    fMac2->SetNegEndswitch(Deg2ZdRE(env.GetValue("Zd_Min[Deg]", -1.0)));
+    fMac2->SetPosEndswitch(Deg2ZdRE(env.GetValue("Zd_Max[Deg]", +1.0)));
+    cout << "done." << endl;
 
 /*
@@ -567,5 +586,5 @@
     fMac2->SetPosEndswitch(Deg2ZdRE(env.GetValue("MaxZd[Deg]", +1.0)));
 */
-    fMac3->SetSyncMode();
+//    fMac3->StartVelSync();
 /*
     cout << "PostMsg(WM_PRESET)" << endl;
@@ -588,5 +607,4 @@
     // dest = AltAz(-46.0, 210);
     // SetPosition(dest);
-    setpriority(PRIO_PROCESS, 0, 10);
 
     Slalib sla;
@@ -675,49 +693,47 @@
 }
 
-void *MCosy::MapTalkThread(void *arg)
-{
-    pthread_detach(pthread_self());
-
-    MCosy *cosy = (MCosy*)arg;
-
-    cosy->TalkThread();
-
-    cosy->lout << "- Sending Thread done." << endl;
-
-    return NULL;
-}
-
-int MCosy::StopWaitingForSDO()
-{
-    return Break() || fMac1->HasError() || fMac2->HasError();
+void MCosy::GuiThread(MTGui *t)
+{
+    MGSkyPosition *disp = fWin->GetSkyDisplay();
+    while (!t->HasStopFlag())
+    {
+        usleep(100000); // 0.1s
+
+        fAlt1->DisplayVal();
+        fAlt2->DisplayVal();
+        fAz->DisplayVal();
+
+        // gSystem->ProcessEvents();
+
+        ZdAz ist = GetSePos()*(360.0/16384.0); // [se]
+        disp->DrawPosition(ist);
+    }
+    cout << "Not running anymore." << endl;
+}
+
+int MCosy::StopWaitingForSDO() const
+{
+    return Break() || HasError(); //fMac1->HasError() || fMac2->HasError();
 }
 
 void MCosy::Start()
 {
-    if (fTxThrd)
-    {
-        cout << "Error: tx thread already started." << endl;
-        return;
-    }
-
-    lout << "- Starting sending Thread." << endl;
-
-    fTxThrd = new pthread_t;
-    pthread_create(fTxThrd, NULL, MapTalkThread, this);
+    // Don't call this function twice!
+    Network::Start();
+
+    lout << "- Starting TX Thread." << endl;
+    fTTalk = new MTTalk(this);
+
+    lout << "- Starting GUI Thread." << endl;
+    fTGui = new MTGui(this);
 }
 
 void MCosy::Stop()
 {
-    if (!fTxThrd)
-        return;
-
-    pthread_cancel(*fTxThrd);
-
-    delete fTxThrd;
-    fTxThrd = NULL;
-
-    lout << "- Sending Thread stopped." << endl;
-
-    SkipPendingSdos();
+    delete fTGui;
+    lout << "- GUI Thread stopped." << endl;
+
+    delete fTTalk;
+    lout << "- TX Thread stopped." << endl;
 
     Network::Stop();
@@ -725,5 +741,5 @@
 
 MCosy::MCosy(const char *dev, const int baud, ostream &out)
-: Network(dev, baud, out), fTxThrd(NULL), fTracking(kFALSE)
+: Network(dev, baud, out), fTracking(kFALSE)
 {
     //
@@ -747,5 +763,5 @@
     SetNode(fAz);
 
-    MGCosy *fWin=new MGCosy(this, gClient->GetRoot(), 1, 1);
+    fWin=new MGCosy(this, gClient->GetRoot(), 1, 1);
 
     fAz->SetDisplay(fWin->GetLabel1());
@@ -756,9 +772,12 @@
 void MCosy::TerminateApp()
 {
-   gSystem->ExitLoop();
+    cout << "MCosy::TerminateApp()" << endl;
+    gApplication->Terminate(0);
 }
 
 MCosy::~MCosy()
 {
+    cout << "Deleting Nodes." << endl;
+
     delete fAz;
     delete fAlt2;
@@ -768,4 +787,8 @@
     delete fMac3;
 
+    cout << "Deleting MGCosy." << endl;
+
     delete fWin;
-}
+
+    cout << "MGCosy deleted." << endl;
+}
Index: trunk/MagicSoft/Cosy/MCosy.h
===================================================================
--- trunk/MagicSoft/Cosy/MCosy.h	(revision 908)
+++ trunk/MagicSoft/Cosy/MCosy.h	(revision 909)
@@ -5,7 +5,9 @@
 #include "msgqueue.h"
 #include "network.h"
+#include "MThread.h"
 
 #define kDEG ((char)0xb0)  // Linux 'ø'
 
+#define WM_WAIT      WM_NULL
 #define WM_PRESET    0x1000
 #define WM_POSITION  0x1001
@@ -13,12 +15,46 @@
 #define WM_STOP      0x1003
 #define WM_POLARIS   0x1004
-#define WM_WAIT      0x1005
 
 class ShaftEncoder;
 class Macs;
 class MGCosy;
+class MCosy;
+
+class MTGui : public MThread
+{
+private:
+    MCosy *fCosy;
+
+    void *Thread();
+
+public:
+    MTGui(MCosy *cosy) : MThread(false), fCosy(cosy)
+    {
+        SetPriority(20);
+        Start();
+    }
+};
+
+class MTTalk : public MThread
+{
+private:
+    MCosy *fCosy;
+
+    void *Thread();
+
+public:
+    MTTalk(MCosy *cosy) : MThread(false), fCosy(cosy)
+    {
+        SetPriority(10);
+        Detach();
+        Start();
+    }
+};
 
 class MCosy : public Network, public MsgQueue
 {
+    friend class MTGui;
+    friend class MTTalk;
+
 private:
     ShaftEncoder *fAlt1;
@@ -32,5 +68,6 @@
     MGCosy *fWin;
 
-    pthread_t *fTxThrd;
+    MTGui  *fTGui; 
+    MTTalk *fTTalk;
 
     ZdAz  fOffset;    // Tracking Offset between SE and calc-pos [re]
@@ -38,10 +75,10 @@
     int   fTracking;  // Flag for present tracking action
 
-    static void *MapTalkThread(void *arg);
-
+    ZdAz GetRePos();
     ZdAz GetSePos();     // [se]
-    ZdAz GetRePos();
 
     void TalkThread();
+    void GuiThread(MTGui *t);
+
     int  SetPosition(const ZdAz &dst);
 
@@ -50,5 +87,5 @@
     void TrackPosition(const RaDec &dst); // ra, dec [rad]
 
-    int StopWaitingForSDO();
+    int StopWaitingForSDO() const;
 
 public:
@@ -63,4 +100,5 @@
     static ZdAz CorrectTarget(const ZdAz &src, const ZdAz &dst);
 //    static ZdAz RaDec2ZdAz(const double mjd, const RaDec &pos, const RaDec &pm=RaDec(0,0));
+
 };
 
Index: trunk/MagicSoft/Cosy/Makefile
===================================================================
--- trunk/MagicSoft/Cosy/Makefile	(revision 908)
+++ trunk/MagicSoft/Cosy/Makefile	(revision 909)
@@ -11,22 +11,18 @@
 
 #
-#  ----->>>   settings for compiler
+#  please change all system depend values in the 
+#  config.mk.${OSTYPE} file 
+#
+#
+include Makefile.conf.$(OSTYPE)
+include Makefile.conf.general
+
 #
 
-CC     = gcc
-CXX    = g++
-AR     = ar -rc
-AS     = as
-LD     = ld
-RANLIB = ranlib
-
-#
-#  ----->>>   settings for compilation
-#
-
-PROGRAMS  = cosy starg
-
-ROOTFLAGS = `root-config --cflags`
-ROOTGLIBS = `root-config --glibs`
+PROGRAMS = cosy starg
+SOLIB    = 
+CINT     = M
+INCLUDES = -I. -Iincl -Ibase -Igui -Idevdrv -Icandrv -Ivideodev -Icatalog
+LIBS     = -lpng -lz -L/usr/X11R6/lib
 
 #
@@ -34,69 +30,27 @@
 #
 #    WARNING: the result (whether the linkage works or not) depends on the
-#             order of the libraries
+#             order of the libraries. It seems, that the most base library
+#             must be the last one
 #
-#
-#LIBS      = -L. -L/usr/lib -lstdc++ -lpthread 
-INCLUDES  = -I. -I/usr/X11R6/include -Icandrv -Idevdrv -Ibase -Igui -Iincl -Islalib -Ivideodev -Icatalog
-LIBS      = -L. -L/usr/X11R6/lib -lpthread -lpng -lz
-#INCLUDES  = -I. -Ibase -Igui -Islalib
-
-WARNING   = -Wall -fno-rtti -fno-exceptions 
-OPTIM     = -O2
-DEBUG 	  = -g 
-DEFINES	  = -DCPU=486 -DBUS_LITTLE_ENDIAN -DDEBUG=0 -DLINUX -DSHOW 
-
-CFLAGS    = $(OPTIM) $(DEBUG) $(WARNING) $(INCLUDES) $(ROOTFLAGS) $(DEFINES)
-CXXFLAGS  = $(CFLAGS)
-
-CINT     = M
 
 #
 #  ----->>>   mars libraries
 #
+SUBDIRS = \
+        candrv      \
+        catalog     \
+	devdrv      \
+        gui         \
+        videodev    \
+	base        \
+        slalib
+
+LIBRARIES = $(SUBDIRS:=.a)
 
 #------------------------------------------------------------------------------
-.SILENT:
 
 .SUFFIXES: .c .cc .h .o 
 
-SLASRC  = slalib/cldj.c   slalib/dranrm.c  slalib/mappa.c  slalib/aoppa.c \
-	  slalib/mapqk.c  slalib/aopqk.c   slalib/epj.c    slalib/evp.c   \
-	  slalib/dvn.c    slalib/prenut.c  slalib/geoc.c   slalib/refco.c \
-	  slalib/eqeqx.c  slalib/dcs2c.c   slalib/dvdv.c   slalib/dmxv.c  \
-	  slalib/dcc2s.c  slalib/refz.c    slalib/refro.c  slalib/prec.c  \
-	  slalib/nut.c    slalib/dmxm.c    slalib/nutc.c   slalib/range.c \
-          slalib/aoppat.c slalib/deuler.c  slalib/drange.c slalib/daf2r.c \
-          slalib/dh2e.c   slalib/gmst.c    slalib/de2h.c   slalib/altaz.c \
-          slalib/gmsta.c  slalib/dat.c     slalib/dtt.c    slalib/oapqk.c \
-          slalib/ampqk.c  slalib/dimxv.c   slalib/mapqkz.c slalib/dr2af.c \
-          slalib/dd2tf.c
-
-SLAOBJ  = $(SLASRC:.c=.o)
-
-SRCFILES = candrv/vmodican.cc \
-	   candrv/canopen.cc \
-	   candrv/network.cc \
-	   candrv/nodedrv.cc \
-           candrv/sdolist.cc \
-	   devdrv/shaftencoder.cc \
-	   devdrv/macs.cc \
-	   gui/MGCosy.cc \
-           gui/MGImage.cc \
-           gui/MGCoordinate.cc \
-           gui/MGCoordinates.cc \
-           base/timer.cc \
-           base/msgqueue.cc \
-           base/File.cc \
-           base/MStopwatch.cc \
-           catalog/SaoFile.cc \
-           catalog/StarCatalog.cc \
-           catalog/Slalib.cc \
-           videodev/Camera.cc \
-           videodev/Filter.cc \
-           videodev/Writer.cc \
-           MStarguider.cc \
-           MCosy.cc \
-           Starguider.cc
+SRCFILES = MCosy.cc Starguider.cc
 
 SRCS    = $(SRCFILES)
@@ -106,27 +60,58 @@
 ############################################################
 
-all: $(PROGRAMS)
+all: rmlib $(PROGRAMS) $(SOLIB)
 
-$(SLAOBJ):
-	echo 'Compiling $*.c'
-	$(CC) -c $(CFLAGS) -pedantic $*.c -o $*.o
+# Use $(CXX) -v ... for a more verbose output
+#
+# We could link mars.so instead of all libraries. This would need
+# some MBs less space on the HD. But this means, that the Shared
+# Library Path in your system must be set properly to be able to start
+# 'mars'
+#
+$(PROGRAMS): $(LIBRARIES) $(OBJS) $(HEADERS) $(PROGRAMS:=.o) 
+	@echo " Linking $@ ..." 
+	$(CXX) $(CXXFLAGS) $(LIBS) $@.o $(OBJS) $(MARS_LIB) $(ROOTGLIBS) -o $@
 
-#$(PROGRAMS): $(PROGRAMS:=.o) $(OBJS) $(SLAOBJ)
-#	@echo " Linking $@ ..." 
-#	$(CXX) $(CXXFLAGS) $@.o $(LIBS) $(ROOTGLIBS) -o $@
-$(PROGRAMS): $(SLAOBJ) $(OBJS) $(PROGRAMS:=.o) 
-	@echo " Linking $@ ..." 
-	$(CXX) $(CXXFLAGS) $(LIBS) $(SLAOBJ) $(OBJS) $(ROOTGLIBS) -o $@ $@.o
+$(SOLIB): $(LIBRARIES) $(OBJS) $(HEADERS) MCint.o
+	@echo " Linking $(SOLIB) ..."
+	$(CXX) -shared $(CXXFLAGS) $(SUBDIRS:=/*.o) $(ROOTGLIBS) -o $@
+	mv cosy.so lib
 
-#dox:
-#	@echo " Creating html documentation:"
-#	root -b -q dohtml.C
+$(LIBRARIES):
+	@echo " "  
+	@echo " Creating lib$@:"
+	(cd $*; make -f Makefile; cd ..; mv $*/$@ lib/lib$@) 
 
+dox:
+	@echo " Creating html documentation:"
+	root -b -q dohtml.C
 
 include Makefile.rules
 
-clean:	rmcint rmobjs rmcore rmbin rmbak
+clean:	rmlib rmcint rmobjs rmcore rmbin
 
-tar:	clean
+mrproper:	clean rmbak
+	@rm -f macros/*~
+	@rm -rf htmldoc
+	@echo "cd base"
+	@cd base; make mrproper; cd ..
+	@echo "cd .."
+	@echo "cd candrv"
+	@cd candrv; make mrproper; cd ..
+	@echo "cd .."
+	@echo "cd devdrv"
+	@cd devdrv; make mrproper; cd ..
+	@echo "cd .."
+	@echo "cd gui"
+	@cd gui; make mrproper; cd ..
+	@echo "cd .."
+	@echo "cd videodev"
+	@cd videodev; make mrproper; cd ..
+	@echo "cd .."
+	@echo "cd catalog"
+	@cd videodev; make mrproper; cd ..
+	@echo "cd .."
+
+tar:	mrproper
 	@echo "Making tar-file"
 	@tar -cvf ../cosy.tar *
Index: trunk/MagicSoft/Cosy/Makefile.conf.linux-gnu
===================================================================
--- trunk/MagicSoft/Cosy/Makefile.conf.linux-gnu	(revision 908)
+++ trunk/MagicSoft/Cosy/Makefile.conf.linux-gnu	(revision 909)
@@ -8,20 +8,25 @@
 # @maintitle
 
+# @code
+
 # compilers
 
-CC  = gcc
-CXX = g++
-F77 = f77
-AR  = ar -rc
+CC            = gcc
+CXX           = g++
+F77           = f77
+AR            = ar -rc
 #
 #  ----->>>   settings for compilation
 #
 
-OPTIM = -O2 -Wall -fno-rtti -fno-exceptions -fPIC
-DEBUG = -g 
+OPTIM    = -O2 -Wall -fno-rtti -fno-exceptions -fPIC -Wtraditional -Wpointer-arith -Wcast-align -Wwrite-strings -Wconversion -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Woverloaded-virtual
+DEBUG    = -g
+
+MARS_LIB  = -Llib $(SUBDIRS:%=-l%)
+INCLUDES  = -I. $(SUBDIRS:%=-I%)
 
 # uncomment this for quiet compilation
 
-#.SILENT:
+.SILENT:
 
 # @endcode
Index: trunk/MagicSoft/Cosy/Makefile.rules
===================================================================
--- trunk/MagicSoft/Cosy/Makefile.rules	(revision 908)
+++ trunk/MagicSoft/Cosy/Makefile.rules	(revision 909)
@@ -4,6 +4,6 @@
         -f Makefile.depend 2> kk.kk ; cat kk.kk
 
-$(LIB): $(OBJS) $(CINT)Cint.o
-	@echo " - Building Library $(LIB) ... "
+$(LIB): $(OBJS) $(HEADERS)
+	@echo " - Building Library lib$(LIB) ... "
 	$(AR) $(LIB) *.o
 
@@ -13,5 +13,5 @@
 
 	$(ROOTSYS)/bin/rootcint -f $(CINT)Cint.cc \
-	-c $(INCLUDES) $(HEADERS) $(CINT)Incl.h $(CINT)LinkDef.h 
+	-c $(INCLUDES) $(DEFINES) $(HEADERS) $(CINT)Incl.h $(CINT)LinkDef.h 
 
 .cxx.o:	
@@ -32,31 +32,25 @@
 rmcint:	
 	@echo "Removing cint-stuff..."
-	@rm -rf *Cint.*
+	@rm -f *Cint.*
+
+rmlib:	
+	@echo "Removing libraries..."
+	@rm -f lib/lib*.a  lib*.a
 
 rmobjs:	
 	@echo "Removing object files..."
 	@rm -f *.o
-	@rm -f base/*.o
-	@rm -f gui/*.o
-	@rm -f candrv/*.o
-	@rm -f devdrv/*.o
-	@rm -f slalib/*.o
 
 rmcore:	
 	@echo "Removing core files..."
-	@rm -rf core*
+	@rm -f core*
 
 rmbin:	
 	@echo "Removing binary files..."
-	@rm -rf $(PROGRAMS) so_locations
+	@rm -f $(PROGRAMS) so_locations
 
 rmbak:
 	@echo "Removing backup files..."
-	@rm -f *~        kk.kk        *.bak
-	@rm -f base/*~   base/kk.kk   base/*.bak
-	@rm -f gui/*~    gui/kk.kk    gui/*.bak
-	@rm -f candrv/*~ candrv/kk.kk candrv/*.bak
-	@rm -f devdrv/*~ devdrv/kk.kk devdrv/*.bak
-	@rm -f slalib/*~ slalib/kk.kk slalib/*.bak
+	@rm -f *~ kk.kk *.bak
 
 cflags: 
Index: trunk/MagicSoft/Cosy/Starguider.cc
===================================================================
--- trunk/MagicSoft/Cosy/Starguider.cc	(revision 908)
+++ trunk/MagicSoft/Cosy/Starguider.cc	(revision 909)
@@ -12,5 +12,5 @@
 #include "Filter.h"
 #include "Writer.h"
-#include "timer.h"
+#include "base/timer.h"
 
 enum {
@@ -293,5 +293,5 @@
 }
 
-void Starguider::Execute(const unsigned long n, byte *img, struct timeval *tm)
+void Starguider::ProcessFrame(const unsigned long n, byte *img, struct timeval *tm)
 {
 
Index: trunk/MagicSoft/Cosy/Starguider.h
===================================================================
--- trunk/MagicSoft/Cosy/Starguider.h	(revision 908)
+++ trunk/MagicSoft/Cosy/Starguider.h	(revision 909)
@@ -60,5 +60,5 @@
     // Execution of one frame - this function may be overloaded!
     //
-    void Execute(const unsigned long n, byte *img, struct timeval *tm);
+    void ProcessFrame(const unsigned long n, byte *img, struct timeval *tm);
 };
 
Index: trunk/MagicSoft/Cosy/base/MGList.h
===================================================================
--- trunk/MagicSoft/Cosy/base/MGList.h	(revision 908)
+++ trunk/MagicSoft/Cosy/base/MGList.h	(revision 909)
@@ -1,4 +1,6 @@
 #ifndef MGLIST_H
 #define MGLIST_H
+
+#include <iostream.h>
 
 #ifndef ROOT_TOrdCollection
@@ -11,9 +13,15 @@
     ~MGList()
     {
+        cout << "Deleting:" << flush;
+
         TIter Next(this);
 
         TObject *obj;
         while ((obj=Next()))
+        {
+            cout << " <" << ((TGObject*)obj)->GetName() << ">" << flush;
             delete (TGObject*)obj;
+        }
+        cout << endl;
     }
     void Add(TGObject *obj)
Index: trunk/MagicSoft/Cosy/base/msgqueue.cc
===================================================================
--- trunk/MagicSoft/Cosy/base/msgqueue.cc	(revision 908)
+++ trunk/MagicSoft/Cosy/base/msgqueue.cc	(revision 909)
@@ -73,4 +73,8 @@
     //
     // Set break state and wait until Proc() returned (break state deleted)
+    // This means, that a new command is invoked and (if forseen) the
+    // running command should stop execution.
+    //
+    // This is some kind of controlled user break without using signals
     //
     fBreak = 1;
Index: trunk/MagicSoft/Cosy/base/msgqueue.h
===================================================================
--- trunk/MagicSoft/Cosy/base/msgqueue.h	(revision 908)
+++ trunk/MagicSoft/Cosy/base/msgqueue.h	(revision 909)
@@ -32,5 +32,5 @@
     virtual ~MsgQueue();
 
-    int Break() { return fBreak; }
+    int Break() const { return fBreak; }
 
     virtual void *Proc(int msg, void *mp1);
Index: trunk/MagicSoft/Cosy/candrv/canopen.cc
===================================================================
--- trunk/MagicSoft/Cosy/candrv/canopen.cc	(revision 908)
+++ trunk/MagicSoft/Cosy/candrv/canopen.cc	(revision 909)
@@ -223,5 +223,5 @@
 }
 
-WORD_t CanOpen::CobId(BYTE_t node, BYTE_t fcode)
+WORD_t CanOpen::CobId(BYTE_t node, BYTE_t fcode) const
 {
     return (fcode<<7) | node&0x1f;
Index: trunk/MagicSoft/Cosy/candrv/network.cc
===================================================================
--- trunk/MagicSoft/Cosy/candrv/network.cc	(revision 908)
+++ trunk/MagicSoft/Cosy/candrv/network.cc	(revision 909)
@@ -6,12 +6,18 @@
 void Network::Start()
 {
-    StartReceiver();
+    lout << "- Starting network." << endl;
+
+    VmodIcan::Start();
     InitNodes();
+
+    lout << "- Network started." << endl;
 }
 
 void Network::Stop()
 {
+    lout << "- Stopping network." << endl;
+
     StopNodes();
-    StopReceiver();
+    VmodIcan::Stop();
 
     lout << "- Network stopped." << endl;
@@ -166,5 +172,5 @@
 }
 
-bool Network::HasError()
+bool Network::HasError() const
 {
     for (int i=0; i<32; i++)
Index: trunk/MagicSoft/Cosy/candrv/network.h
===================================================================
--- trunk/MagicSoft/Cosy/candrv/network.h	(revision 908)
+++ trunk/MagicSoft/Cosy/candrv/network.h	(revision 909)
@@ -31,5 +31,5 @@
     virtual void Stop();
 
-    bool HasError();
+    bool HasError() const;
 };
 
Index: trunk/MagicSoft/Cosy/candrv/nodedrv.cc
===================================================================
--- trunk/MagicSoft/Cosy/candrv/nodedrv.cc	(revision 908)
+++ trunk/MagicSoft/Cosy/candrv/nodedrv.cc	(revision 909)
@@ -116,10 +116,10 @@
 }
 
-void NodeDrv::WaitForSdo(WORD_t idx, BYTE_t subidx)
+void NodeDrv::WaitForSdo(WORD_t idx, BYTE_t subidx, WORD_t timeout)
 {
-    while (fNetwork->WaitingForSdo(fId, idx, subidx))
-        usleep(1);
+    fNetwork->WaitForSdo(fId, idx, subidx, timeout);
 }
 
+/*
 void NodeDrv::WaitForSdos()
 {
@@ -127,4 +127,5 @@
         usleep(1);
 }
+*/
 
 void NodeDrv::WaitForNextPdo1()
Index: trunk/MagicSoft/Cosy/candrv/nodedrv.h
===================================================================
--- trunk/MagicSoft/Cosy/candrv/nodedrv.h	(revision 908)
+++ trunk/MagicSoft/Cosy/candrv/nodedrv.h	(revision 909)
@@ -29,5 +29,5 @@
     virtual void StopDevice() = 0;
 
-    bool HasError()        { return fError; }
+    bool HasError()       { return fError; }
 
     virtual void HandleSDO(WORD_t idx, BYTE_t subidx, LWORD_t val, struct timeval *tv);
@@ -64,6 +64,6 @@
     void WaitForNextPdo4();
 
-    void WaitForSdos();
-    void WaitForSdo(WORD_t idx, BYTE_t subidx=0);
+    // void WaitForSdos();
+    void WaitForSdo(WORD_t idx, BYTE_t subidx=0, WORD_t timeout=500);
 
     void EnableCanMsg(BYTE_t fcode);
Index: trunk/MagicSoft/Cosy/candrv/sdolist.cc
===================================================================
--- trunk/MagicSoft/Cosy/candrv/sdolist.cc	(revision 908)
+++ trunk/MagicSoft/Cosy/candrv/sdolist.cc	(revision 909)
@@ -86,5 +86,5 @@
 }
 
-int PendingSDOList::IsPending()
+int PendingSDOList::IsPending() const
 {
     return (int)fFirst->Next;
Index: trunk/MagicSoft/Cosy/candrv/sdolist.h
===================================================================
--- trunk/MagicSoft/Cosy/candrv/sdolist.h	(revision 908)
+++ trunk/MagicSoft/Cosy/candrv/sdolist.h	(revision 909)
@@ -34,5 +34,5 @@
     void DelAll();
 
-    int IsPending();
+    int IsPending() const;
     int IsPending(BYTE_t node);
     int IsPending(BYTE_t node, WORD_t idx, BYTE_t subidx);
Index: trunk/MagicSoft/Cosy/candrv/vmodican.cc
===================================================================
--- trunk/MagicSoft/Cosy/candrv/vmodican.cc	(revision 908)
+++ trunk/MagicSoft/Cosy/candrv/vmodican.cc	(revision 909)
@@ -52,14 +52,7 @@
 }
 
-void *VmodIcan::ReceiveThread(void *data)
-{
-    VmodIcan *cal = (VmodIcan*)data;
-
-    cal->lout << "- Starting Receiver Loop." << endl;
-
-    //
-    // set higher priority to make sure that no messages are lost.
-    //
-    setpriority(PRIO_PROCESS, 0, -10);
+void *VmodIcan::Thread()
+{
+    lout << "- Starting Receiver Loop." << endl;
 
     while (1)
@@ -70,5 +63,5 @@
         //cout << "waiting..." << endl;
 
-        const int n = read(cal->fd, &c, 1); // sleep until message arrive
+        const int n = read(fd, &c, 1); // sleep until message arrive
 
         gettimeofday(&tv, NULL);
@@ -89,5 +82,5 @@
             FastMessage fmsg;
 
-            if (cal->ReceiveFast(&fmsg) < 0)
+            if (ReceiveFast(&fmsg) < 0)
                 return (void *)1;
 
@@ -105,14 +98,14 @@
             Message msg;
 
-            if (cal->Receive(&msg) < 0)
+            if (Receive(&msg) < 0)
                 return (void *)1;
 
             //cal->PrintMsg(&msg);
-            cal->HandleMessage(&msg, &tv);
+            HandleMessage(&msg, &tv);
 
             break;
         }
-
     }
+    return NULL;
 }
 
@@ -765,6 +758,12 @@
 }
 
-VmodIcan::VmodIcan(const char *dev, const int baud, ostream &out) : Log(out), fRxThrd(NULL)//: CanDriver(dev, baud)
-{
+VmodIcan::VmodIcan(const char *dev, const int baud, ostream &out) : Log(out), MThread(false)//: CanDriver(dev, baud)
+{
+    //
+    // Set priority of receiving thread and detach the receiving thread
+    //
+    SetPriority(-10);
+    Detach();
+
     Open(dev);                         // open module
     Reset();
@@ -782,5 +781,5 @@
 VmodIcan::~VmodIcan()
 {
-    StopReceiver();
+    Stop();
     DisableCanBusConnection();
     Close();
@@ -794,5 +793,5 @@
     SendCanFrame(cobid, msg);
 }
-
+/*
 void VmodIcan::StopReceiver()
 {
@@ -804,12 +803,14 @@
     delete fRxThrd;
     fRxThrd = NULL;
-
     lout << "- Receiver Thread stopped." << endl;
 }
+*/
+
+/*
 void VmodIcan::StartReceiver()
 {
-    //**************************************
-    //* create thread waiting for messages *
-    //**************************************
+    **************************************
+    * create thread waiting for messages *
+    **************************************
     if (fRxThrd)
     {
@@ -823,3 +824,4 @@
     pthread_create(fRxThrd, NULL, ReceiveThread, this);
 }
-
+*/
+
Index: trunk/MagicSoft/Cosy/candrv/vmodican.h
===================================================================
--- trunk/MagicSoft/Cosy/candrv/vmodican.h	(revision 908)
+++ trunk/MagicSoft/Cosy/candrv/vmodican.h	(revision 909)
@@ -9,13 +9,32 @@
 #include "gendef.h"
 
-class VmodIcan : public Log
+#include "MThread.h"
+/*
+class VmodIcanRX : public MThread
 {
 private:
+    VmodIcan *fModule;
+
+    void *Thread();
+
+public:
+    MTGui(VmodIcan *mod) : MThread(false), fModule(mod)
+    {
+        SetPriority(-10);
+        Detach();
+    }
+};
+*/
+class VmodIcan : public Log, protected MThread
+{
+    friend class VmodIcanRX;
+
+private:
     int fd; // file descriptor for can module
-    pthread_t *fRxThrd;
+    //    pthread_t *fRxThrd;
 
     int Ioctl(int msg, void *arg);
 
-    static void *ReceiveThread(void *data);
+    void *Thread();
 
     void HandleMessage(Message *msg, struct timeval *tv);
@@ -52,7 +71,7 @@
     virtual void TerminateApp() { exit(-1); }
 
-protected:
-    void StartReceiver();
-    void StopReceiver();
+//protected:
+//    void StartReceiver();
+//    void StopReceiver();
 
 public:
Index: trunk/MagicSoft/Cosy/cosy.cc
===================================================================
--- trunk/MagicSoft/Cosy/cosy.cc	(revision 908)
+++ trunk/MagicSoft/Cosy/cosy.cc	(revision 909)
@@ -8,14 +8,4 @@
 #include <TSystem.h>
 #include <TApplication.h>
-
-#include "MGCosy.h"
-
-#include "macs.h"
-#include "timer.h"
-#include "slalib.h"
-#include "slamac.h"
-#include "shaftencoder.h"
-
-#include <sys/resource.h>  // PRIO_PROCESS
 
 int main(int argc, char **argv)
@@ -32,5 +22,5 @@
     lout << "- Initialising Root environment." << endl;
     TROOT root("Cosy", "Magic Control System");
-    TApplication app ("App", &argc, argv);
+    TApplication app("App", &argc, argv);
 
     //
@@ -40,8 +30,10 @@
     cosy->Start();
 
-    lout << "- Starting mailoop." << endl;
-    app.Run();
+    lout << "- Starting mainloop." << endl;
+    app.Run(kTRUE);
 
+    lout << "- Stopping cosy." << endl;
     cosy->Stop();
+    lout << "- cosy stopped." << endl;
 
     delete cosy;
