#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/Xmu/WinUtil.h>
#include <X11/cursorfont.h>

void xwinreturnstring(char *thestring){

        	wVariant *myvariant;

		myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );

           	//-- create a string
           	wVariantFromChar( myvariant, thestring ,4,4 );

    		//-- return it to wxBasic
    		wVariantMove( wStackPushNothing(), myvariant );

   		//-- cleanup
    		wVariantDeref( myvariant );
    		wFree(myvariant);
}

void wBuiltin_xwin_getenv(){

    char *arg0;
    wVariant *myarg;
    myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg = wStackPopString();
    arg0 = wVariantToChar( myarg, W_FALSE, 0 ); 


    char ret[4097];

    sprintf(ret , "%s" , getenv(arg0));

    free(arg0);
    wVariantDeref( myarg );
    xwinreturnstring(ret);
}

void wBuiltin_xwin_getstringcontent(){
// this is crap
/*
	wVariant *myarg;
	//myarg = wStackPop();
myarg = *wStackPopObjectPtr;

        	wVariant *myvariant;

		myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );

           	//-- create a string
//           	wVariantFromChar( myvariant, myarg ,4,4 );
// wStringCopyData( myvariant, myarg );
//myvariant->value.string = myarg->value.string;

	wVariantFromChar( myvariant, myarg->value.string ,4,4 );

    //wVariantClone( myvariant, myarg );
    		//-- return it to wxBasic
    		wVariantMove( wStackPushNothing(), myvariant );

   		//-- cleanup
    		wVariantDeref( myvariant );
    		wFree(myvariant);
*/
}

void wBuiltin_xwin_cd()
{

    char *arg1;
    wVariant *myarg;
    myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg = wStackPopString();
    arg1 = wVariantToChar( myarg, W_FALSE, 0 ); 

    chdir(arg1);

    wVariantDeref( myarg );
    free(arg1);
}
void wBuiltin_xwin_getdir()
{


    char *arg0;
    wVariant *myarg;
    myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg = wStackPopString();
    arg0 = wVariantToChar( myarg, W_FALSE, 0 ); 



    char result[4097];
    char buf[4097];
    int len;

    if ((len = readlink(arg0, buf, sizeof(buf)-1)) != -1){
        buf[len] = '\0';
        sprintf (result , "%s", buf);
    }else{
        sprintf (result , "%s", arg0);
    }

    char thedir[4097];
    int laenge;
    int laenge2;


    strcpy( thedir , "" );
    char *rest;

    if ( strstr(result, "/" ) != NULL ){
      //printf("ok\n");
      laenge = strlen( result );
      rest = strrchr( result , '/' );
      laenge2 =  strlen( rest );

      //printf("ok %d - %d\n" , laenge,laenge2);
      if( laenge2 > 1 ){
          strncat ( thedir , result , laenge - laenge2 );
      }else{
          strncat ( thedir , result , laenge );
      }
    }else{
        strncat ( thedir , "." , 1 );
   }


    wVariantDeref( myarg );

    char *here;
    long size;
    char *ptr;
    char ret[4097];

    sprintf(ret , "%s" , thedir);

    size = pathconf(".", _PC_PATH_MAX);

    if ((here = (char *)malloc((size_t)size)) != NULL)
    ptr = getcwd(here, (size_t)size);


    //printf("-script-- %s\n" , thedir);
    //printf("-hier-- %s\n" , here);

    if(! strcmp(thedir , here)){
        //printf("-gleich-\n");
    }else{

      if(! strcmp(thedir , ".")){
          sprintf(ret , "%s" , here);
          //printf("-script ist . -\n");
      }
    }

    //   sprintf(arg0 , "%s" , ret);
    //printf("-%d--- %s\n" ,size , ret);


    free(ptr);
    free(arg0);



    xwinreturnstring(ret);


return;
}

static int 
ignore_error (Display *d, XErrorEvent *e)
{
  return 0;
}

void wBuiltin_xwin_usleep()
{
    int microseconds;
   // sscanf(argv[1], "%d", &microseconds);
microseconds = wStackPopNumber();
//    printf("%d\n" , microseconds);

    usleep(microseconds);

}
void wBuiltin_xwin_exec()
{

	char    *text;
	wVariant *variant;
	
	variant = wStackPopString();

//this one would exit current program and continue new one
//	execlp(   "sh" , "sh" , "-c",  wStringChar( variant ) );


//--- this one would clone the current process
//int pid = vfork();
//      execl("/bin/sh", "sh", "-c",wStringChar( variant ) , 0);

#ifdef __WXWIN__
#else

   int status, pid, w;
   register int (*istat)(), (*qstat)();
   if ((pid = vfork()) == 0) {
      signal(SIGINT, SIG_DFL);
      signal(SIGQUIT, SIG_DFL);
      signal(SIGHUP, SIG_DFL);
      execl("/bin/sh", "sh", "-c", wStringChar( variant ), 0);
      _exit(127);
   }
   // istat = signal(SIGINT, SIG_IGN);
   // qstat = signal(SIGQUIT, SIG_IGN);
   while ((w = wait(&status)) != pid && w != -1)
      ;
   if (w == -1)
      status = -256;
//   signal(SIGINT, istat);
//   signal(SIGQUIT, qstat);

signal(SIGINT , signal(SIGINT, SIG_IGN));
signal(SIGQUIT , signal(SIGQUIT, SIG_IGN));

//   return(status);



	wVariantDeref( variant );    

wStackPushNumber( status/256 );
#endif

}
void wBuiltin_xwin_execnowait()
{

	char    *text;
	wVariant *variant;
	
	variant = wStackPopString();

//this one would exit current program and continue new one
//	execlp(   "sh" , "sh" , "-c",  wStringChar( variant ) );


//--- this one would clone the current process
//int pid = vfork();
//      execl("/bin/sh", "sh", "-c",wStringChar( variant ) , 0);

/*
   int status, pid, w;
   register int (*istat)(), (*qstat)();
   if ((pid = vfork()) == 0) {
      signal(SIGINT, SIG_DFL);
      signal(SIGQUIT, SIG_DFL);
      signal(SIGHUP, SIG_DFL);
      execl("/bin/sh", "sh", "-c", wStringChar( variant ), 0);
      _exit(127);
   }
   istat = signal(SIGINT, SIG_IGN);
   qstat = signal(SIGQUIT, SIG_IGN);
 //  while ((w = wait(&status)) != pid && w != -1)
 //     ;
   if (w == -1)
      status = -256;
   signal(SIGINT, istat);
   signal(SIGQUIT, qstat);
//   return(status);



	wVariantDeref( variant );    

wStackPushNumber( status/256 );

*/
}


void wBuiltin_xwin_system2()
{

	wVariant *variant;
	variant = (wVariant *)wMalloc( sizeof( wVariant ) );
	variant = wStackPopString();


    char mystring[1000];
    wVariant *mylist;
    wVariant *myvariant;
    wVariant *mykey;
    int c=0;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

       wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );

char newstring[1024];


sprintf( newstring , "test" );
//printf("%s", bufline);
        //-- create a key

        sprintf(mystring,"%d" ,c );
        wVariantFromChar( mykey, mystring ,3,3 );


       //-- create a string
       wVariantFromChar( myvariant, newstring ,4,4 );
      // wVariantFromChar( myvariant, bufline ,4,4 );

        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  

       c++;

    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );


    return;
}

void wBuiltin_xwin_system()
{

	wVariant *variant;
	variant = (wVariant *)wMalloc( sizeof( wVariant ) );
	variant = wStackPopString();
  	char bufline[10240];

        char mystring[1000];
        wVariant *mylist;
        wVariant *myvariant;
        wVariant *mykey;
        int c=0;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

        wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );


        FILE *fp;
        int status;

        fp = popen(wStringChar( variant ), "r");
        if (fp == NULL){
          return;
        }

        while (!feof(fp)){
 
           fgets(bufline,10240,fp);
           //-- create a key
           sprintf( mystring,"%d" ,  c );
           wVariantFromChar( mykey, mystring ,3,3 );
           //-- create a string
           wVariantFromChar( myvariant, bufline ,4,4 );

           //-- append it to the list
           wTableInsert(mylist , mykey, myvariant );  

           c++;

        }
        status = pclose(fp);
        if (c>0){
          wTableRemove(mylist , mykey ); 
        }
    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );
           sprintf( mystring,"%d" ,  status/256 );
           wVariantFromChar( mykey, mystring ,3,3 );
    wVariantMove( wStackPushNothing(), mykey );

    //-- cleanup    
    wVariantDeref( myvariant );
    wFree(myvariant);
    wVariantDeref( mylist );
    wFree(mylist);
    wVariantDeref( mykey );
    wFree(mykey);

    return;
}
void wBuiltin_xwin_searchpid()
{

	char *parg1;

	wVariant *variant;
	variant = (wVariant *)wMalloc( sizeof( wVariant ) );
	variant = wStackPopString();
  	char bufline[1024];

        char mystring[1000];
        wVariant *mylist;
        wVariant *myvariant;
        wVariant *mykey;
        int c=0;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

        wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );


	parg1 = wVariantToChar( variant, W_FALSE, 0 ); 


        FILE *fp;
        int status;


 fp = popen("", "r");

//        fp = popen("ps -ax", "r");
        if (fp == NULL){
          return;
        }


        while (!feof(fp)){
 
           fgets(bufline,1024,fp);

if (strstr(bufline , parg1) != NULL){

           //-- create a key
           sprintf( mystring,"%d" ,  c );
           wVariantFromChar( mykey, mystring ,3,3 );
           //-- create a string
           wVariantFromChar( myvariant, bufline ,4,4 );

           //-- append it to the list
           wTableInsert(mylist , mykey, myvariant );  

           c++;
}
        }
        status = pclose(fp);
        if (c>0){
       //   wTableRemove(mylist , mykey ); 
        }
    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );
           sprintf( mystring,"%d" ,  status/256 );
           wVariantFromChar( mykey, mystring ,3,3 );
    wVariantMove( wStackPushNothing(), mykey );

    //-- cleanup    
    wVariantDeref( myvariant );
    wFree(myvariant);
    wVariantDeref( mylist );
    wFree(mylist);
    wVariantDeref( mykey );
    wFree(mykey);

    return;
}
void wBuiltin_xwin_createsimplewindow()
{

    char mystring[1000];
    wVariant *mylist;
    wVariant *myvariant;
    wVariant *mykey;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

       wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );
     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

     Display *dpy;  
 

     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }


     int scr = DefaultScreen (dpy);
//int win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy),  0, 0, 640, 480, 0, 0, 0 );

int win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy),  0, 0, 1, 1, 0, 0, 0 );


//XSetWindowAttributes attr; 
//attr.override_redirect=True;
//int win=XCreateWindow(dpy,DefaultRootWindow(dpy),0,0,100,100,0,CopyFromParent,InputOutput,CopyFromParent,CWBackingStore|CWOverrideRedirect|CWEventMask|CWBorderPixel|CWSaveUnder|CWColormap|CWBackPixel,&attr);
            Atom WM_HINTS;

XUnmapWindow(dpy,win);

            XSetWindowAttributes attributes;
#define MWM_HINTS_DECORATIONS   2
            /* First try to set MWM hints */
            WM_HINTS = XInternAtom(dpy, "_MOTIF_WM_HINTS", True);
            if ( WM_HINTS != None ) {
//#               define  MWM_HINTS_DECORATIONS   (1L << 1)
                struct {
                    int flags;
                    int functions;
                    int decorations;
                    int inputMode;
                    int unknown;
                } MWMHints = { MWM_HINTS_DECORATIONS, 0, 0, 0, 0 };

                XChangeProperty(dpy, win, WM_HINTS, WM_HINTS, 32,
                                PropModeReplace, (unsigned char *)&MWMHints,
                                sizeof(MWMHints)/4);
            }
XMapWindow(dpy, win);
XLowerWindow(dpy, win);
XMoveWindow(dpy, win,20000,10000);
//XMoveWindow(dpy, win,0,0);

        //-- create a key
        sprintf( mystring, "0" );
        wVariantFromChar( mykey, mystring ,3,3 );

       //-- create a string
             sprintf( mystring, "%x" , win );
             wVariantFromChar( myvariant, mystring ,4,4 );

        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  
    XFlush(dpy);
  //  XCloseDisplay(dpy);

    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );

   //-- cleanup

    wVariantDeref( myvariant );
    wFree(myvariant);
    wVariantDeref( mylist );
    wFree(mylist);

    wVariantDeref( mykey );
    wFree(mykey);

}

void wBuiltin_xwin_listwindows()
{

    char mystring[1000];
    wVariant *mylist;
    wVariant *myvariant;
    wVariant *mykey;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

       wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );

     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

     Display *dpy;  
     int windowid;
     XWindowAttributes windowattr;


     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }


     int scr = DefaultScreen (dpy);
     Window w  = RootWindow (dpy, scr);
     Window       root_return, parent_return;
     Window      *children = NULL;
     Window   root = RootWindow (dpy, scr);
     Window  client;
     //Atom            winNormalAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
     unsigned int nchildren, i;

    if (!XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren)){
	/* XQueryTree failed. */
	if (children)
	    XFree ((void *)children);
	//return winNormalAtom;
        return;
    }

    char *win_name;
    int c=0;
    for (i = 0;i < nchildren;i++){

	client = XmuClientWindow (dpy, children[i]);
if(client){
        if (client != children[i] ){
          XGetWindowAttributes(dpy,client, &windowattr);
          if(windowattr.map_state == 2){
             XFetchName(dpy,client , &win_name);
             //printf("%x %s\n" , client , win_name);

        //-- create a key
        sprintf( mystring, "%d" , c );
        wVariantFromChar( mykey, mystring ,3,3 );

       //-- create a string
if (win_name){
             sprintf( mystring, "%x %s" , client , win_name );
}else{
             sprintf( mystring, "%x " , client  );
}
             wVariantFromChar( myvariant, mystring ,4,4 );
        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  
free(win_name);
c++;
          }
}
        }
    }
    if (children)
	XFree ((void *)children);

//free(win_name);
if( 1==2 ){
}
    XCloseDisplay(dpy);



    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );

    //-- cleanup

    wVariantDeref( myvariant );
    wFree(myvariant);
    wVariantDeref( mylist );
    wFree(mylist);

    wVariantDeref( mykey );
    wFree(mykey);



//printf("---------------ok-----------------\n");

    return;
}

void wBuiltin_xwin_listicons()
{

    char mystring[1000];
    wVariant *mylist;
    wVariant *myvariant;
    wVariant *mykey;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

       wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );

     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

     Display *dpy;  
     int windowid;
     XWindowAttributes windowattr;


     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }
     int scr = DefaultScreen (dpy);

     Window w  = RootWindow (dpy, scr);
     Window       root_return, parent_return;
     Window      *children = NULL;
     Window   root = RootWindow (dpy, scr);
     Window  client;
  //   Atom            winNormalAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
     unsigned int nchildren, i;
     Atom         type;

    if (!XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren)){
	/* XQueryTree failed. */
	if (children)
	    XFree ((void *)children);
	//return winNormalAtom;
        return;
    }



    int ret;
    int mycount = 0;
    Atom xa_prop_name;
    Atom xa_ret_type;
     int ret_format;
    unsigned long ret_nitems;
    unsigned long ret_bytes_after;
    unsigned char *ret_prop;
  

  Atom _WIN_WORKSPACE = XInternAtom(dpy,  "_WIN_WORKSPACE",  False);
  Atom _NET_WM_DESKTOP = XInternAtom(dpy,  "_NET_WM_DESKTOP",  False);

    char *win_name;
    for (i = 0;i < nchildren;i++){

	client = XmuClientWindow (dpy, children[i]);
        if (client != children[i] ){
          XGetWindowAttributes(dpy,client, &windowattr);
          if(windowattr.map_state == 0){
             XFetchName(dpy,client , &win_name);
             //printf("%x %s\n" , client , win_name);


if(  XGetWindowProperty(dpy, client, _WIN_WORKSPACE , 0, 1024 ,  False , 
                         XA_CARDINAL, &xa_ret_type, &ret_format,     
                         &ret_nitems, &ret_bytes_after, &ret_prop) != Success ){
             ret = XGetWindowProperty(dpy, client, _NET_WM_DESKTOP , 0, 1024 ,  False , 
                         XA_CARDINAL, &xa_ret_type, &ret_format,     
                         &ret_nitems, &ret_bytes_after, &ret_prop);
}


//printf("++++++++++%d\n" , *ret_prop);

        //-- create a key
        sprintf( mystring, "%d" , mycount  );
        wVariantFromChar( mykey, mystring ,3,3 );

       //-- create a string
             sprintf( mystring, "%x %d %s" , client , *ret_prop , win_name );
             wVariantFromChar( myvariant, mystring ,4,4 );

        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  
free(win_name);
free(ret_prop);
mycount++;
          }
        }
    }
    if (children)
	XFree ((void *)children);


    XCloseDisplay(dpy);

    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );

    //-- cleanup

    wVariantDeref( myvariant );
    wFree(myvariant);
    wVariantDeref( mylist );
    wFree(mylist);

    wVariantDeref( mykey );
    wFree(mykey);

//printf("---------------ok-----------------\n");

    return;
}
void wBuiltin_xwin_listiconsofworkspace()
{
    char *arg1;
    wVariant *myarg;

    myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg = wStackPopString();
    arg1 = wVariantToChar( myarg, W_FALSE, 0 ); 

    int workspace = atof(arg1);     
    //int windowworkspace =0;

    char mystring[1000];
    wVariant *mylist;
    wVariant *myvariant;
    wVariant *mykey;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

       wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );

     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

     Display *dpy;  
     int windowid;
     XWindowAttributes windowattr;


     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }
     int scr = DefaultScreen (dpy);

     Window w  = RootWindow (dpy, scr);
     Window       root_return, parent_return;
     Window      *children = NULL;
     Window   root = RootWindow (dpy, scr);
     Window  client;
   //  Atom            winNormalAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
     unsigned int nchildren, i;
     Atom         type;

    if (!XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren)){
	/* XQueryTree failed. */
	if (children)
	    XFree ((void *)children);
	//return winNormalAtom;
        return;
    }



    int ret;
    int mycount = 0;
    Atom xa_prop_name;
    Atom xa_ret_type;
     int ret_format;
    unsigned long ret_nitems;
    unsigned long ret_bytes_after;
    unsigned char *ret_prop;
  

  Atom _WIN_WORKSPACE = XInternAtom(dpy,  "_WIN_WORKSPACE",  False);
  Atom _NET_WM_DESKTOP = XInternAtom(dpy,  "_NET_WM_DESKTOP",  False);

    char *win_name;
    for (i = 0;i < nchildren;i++){

	client = XmuClientWindow (dpy, children[i]);
        if (client != children[i] ){
          XGetWindowAttributes(dpy,client, &windowattr);
          if(windowattr.map_state == 0){
             XFetchName(dpy,client , &win_name);
             //printf("%x %s\n" , client , win_name);


if(  XGetWindowProperty(dpy, client, _WIN_WORKSPACE , 0, 1024 ,  False , 
                         XA_CARDINAL, &xa_ret_type, &ret_format,     
                         &ret_nitems, &ret_bytes_after, &ret_prop) != Success ){
             ret = XGetWindowProperty(dpy, client, _NET_WM_DESKTOP , 0, 1024 ,  False , 
                         XA_CARDINAL, &xa_ret_type, &ret_format,     
                         &ret_nitems, &ret_bytes_after, &ret_prop);

//windowworkspace = *ret_prop; 
}

//printf("+++%d\n" , workspace);
//printf("++++++++++%d\n" , *ret_prop);

if( workspace ==  *ret_prop){
         //printf("ok %d\n" , mycount);
        //-- create a key
        sprintf( mystring, "%d" , mycount  );
        wVariantFromChar( mykey, mystring ,3,3 );

       //-- create a string
             sprintf( mystring, "%x %s" , client , win_name );
             wVariantFromChar( myvariant, mystring ,4,4 );

        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  
mycount++;
}
free(win_name);
free(ret_prop);
          }
        }
    }
    if (children)
	XFree ((void *)children);


    XCloseDisplay(dpy);

    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );

    //-- cleanup
    // wVariantDeref( myarg );
     //wFree(myarg);
     free(arg1);

    wVariantDeref( myvariant );
    wFree(myvariant);
    wVariantDeref( mylist );
    wFree(mylist);

    wVariantDeref( mykey );
    wFree(mykey);

//printf("---------------ok-----------------\n");

    return;
}
//---------------------------------------------------------------------------------------------------------------------
void wBuiltin_xwin_listiconids()
{

    char mystring[1000];
    wVariant *mylist;
    wVariant *myvariant;
    wVariant *mykey;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

       wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );

     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

     Display *dpy;  
     int windowid;
     XWindowAttributes windowattr;


     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }
     int scr = DefaultScreen (dpy);

     Window w  = RootWindow (dpy, scr);
     Window       root_return, parent_return;
     Window      *children = NULL;
     Window   root = RootWindow (dpy, scr);
     Window  client;
  //   Atom            winNormalAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
     unsigned int nchildren, i;
     Atom         type;

    if (!XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren)){
	/* XQueryTree failed. */
	if (children)
	    XFree ((void *)children);
	//return winNormalAtom;
        return;
    }



    int ret;
    int mycount = 0;
    Atom xa_prop_name;
    Atom xa_ret_type;
     int ret_format;
    unsigned long ret_nitems;
    unsigned long ret_bytes_after;
    unsigned char *ret_prop;
  

  Atom _WIN_WORKSPACE = XInternAtom(dpy,  "_WIN_WORKSPACE",  False);
  Atom _NET_WM_DESKTOP = XInternAtom(dpy,  "_NET_WM_DESKTOP",  False);

    //char *win_name;
    for (i = 0;i < nchildren;i++){

	client = XmuClientWindow (dpy, children[i]);
        if (client != children[i] ){
          XGetWindowAttributes(dpy,client, &windowattr);
          if(windowattr.map_state == 0){
          //   XFetchName(dpy,client , &win_name);
             //printf("%x %s\n" , client , win_name);


if(  XGetWindowProperty(dpy, client, _WIN_WORKSPACE , 0, 1024 ,  False , 
                         XA_CARDINAL, &xa_ret_type, &ret_format,     
                         &ret_nitems, &ret_bytes_after, &ret_prop) != Success ){
             ret = XGetWindowProperty(dpy, client, _NET_WM_DESKTOP , 0, 1024 ,  False , 
                         XA_CARDINAL, &xa_ret_type, &ret_format,     
                         &ret_nitems, &ret_bytes_after, &ret_prop);
}


//printf("++++++++++%d\n" , *ret_prop);

        //-- create a key
        sprintf( mystring, "%d" , mycount  );
        wVariantFromChar( mykey, mystring ,3,3 );

       //-- create a string
             sprintf( mystring, "%x %d" , client , *ret_prop);
             wVariantFromChar( myvariant, mystring ,4,4 );

        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  
//free(win_name);
free(ret_prop);
mycount++;
          }
        }
    }
    if (children)
	XFree ((void *)children);


    XCloseDisplay(dpy);

    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );

    //-- cleanup

    wVariantDeref( myvariant );
    wFree(myvariant);
    wVariantDeref( mylist );
    wFree(mylist);

    wVariantDeref( mykey );
    wFree(mykey);

//printf("---------------ok-----------------\n");

    return;
}
//---------------------------------------------------------------------------------------------------------------------
void wBuiltin_xwin_listiconidsofworkspace()
{
    char *arg1;
    wVariant *myarg;

    myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg = wStackPopString();
    arg1 = wVariantToChar( myarg, W_FALSE, 0 ); 

    int workspace = atof(arg1);     
    //int windowworkspace =0;

    char mystring[1000];
    wVariant *mylist;
    wVariant *myvariant;
    wVariant *mykey;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

       wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );

     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

     Display *dpy;  
     int windowid;
     XWindowAttributes windowattr;


     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }
     int scr = DefaultScreen (dpy);

     Window w  = RootWindow (dpy, scr);
     Window       root_return, parent_return;
     Window      *children = NULL;
     Window   root = RootWindow (dpy, scr);
     Window  client;
   //  Atom            winNormalAtom = XInternAtom (dpy, "_NET_WM_WINDOW_TYPE_NORMAL", False);
     unsigned int nchildren, i;
     Atom         type;

    if (!XQueryTree (dpy, root, &root_return, &parent_return, &children, &nchildren)){
	/* XQueryTree failed. */
	if (children)
	    XFree ((void *)children);
	//return winNormalAtom;
        return;
    }



    int ret;
    int mycount = 0;
    Atom xa_prop_name;
    Atom xa_ret_type;
     int ret_format;
    unsigned long ret_nitems;
    unsigned long ret_bytes_after;
    unsigned char *ret_prop;
  

  Atom _WIN_WORKSPACE = XInternAtom(dpy,  "_WIN_WORKSPACE",  False);
  Atom _NET_WM_DESKTOP = XInternAtom(dpy,  "_NET_WM_DESKTOP",  False);

  //  char *win_name;
    for (i = 0;i < nchildren;i++){

	client = XmuClientWindow (dpy, children[i]);
        if (client != children[i] ){
          XGetWindowAttributes(dpy,client, &windowattr);
          if(windowattr.map_state == 0){
            // XFetchName(dpy,client , &win_name);
             //printf("%x %s\n" , client , win_name);


if(  XGetWindowProperty(dpy, client, _WIN_WORKSPACE , 0, 1024 ,  False , 
                         XA_CARDINAL, &xa_ret_type, &ret_format,     
                         &ret_nitems, &ret_bytes_after, &ret_prop) != Success ){
             ret = XGetWindowProperty(dpy, client, _NET_WM_DESKTOP , 0, 1024 ,  False , 
                         XA_CARDINAL, &xa_ret_type, &ret_format,     
                         &ret_nitems, &ret_bytes_after, &ret_prop);

//windowworkspace = *ret_prop; 
}

//printf("+++%d\n" , workspace);
//printf("++++++++++%d\n" , *ret_prop);

if( workspace ==  *ret_prop){
         //printf("ok %d\n" , mycount);
        //-- create a key
        sprintf( mystring, "%d" , mycount  );
        wVariantFromChar( mykey, mystring ,3,3 );

       //-- create a string
             sprintf( mystring, "%x" , client );
             wVariantFromChar( myvariant, mystring ,4,4 );

        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  
mycount++;
}
//free(win_name);
free(ret_prop);
          }
        }
    }
    if (children)
	XFree ((void *)children);


    XCloseDisplay(dpy);

    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );

    //-- cleanup
    // wVariantDeref( myarg );
     //wFree(myarg);
     free(arg1);

    wVariantDeref( myvariant );
    wFree(myvariant);
    wVariantDeref( mylist );
    wFree(mylist);

    wVariantDeref( mykey );
    wFree(mykey);

//printf("---------------ok-----------------\n");

    return;
}
//---------------------------------------------------------------------------------------------------------------------
void wBuiltin_xwin_info()
{

    char mystring[1000];
    wVariant *mylist;
    wVariant *myvariant;
    wVariant *mykey;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

       wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );


     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

    char *arg1;

    wVariant *myarg;

    myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg = wStackPopString();
 

   arg1 = wVariantToChar( myarg, W_FALSE, 0 ); 

    Display *dpy;  
    int windowid = atof(arg1);   
  

     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }
     
     XWindowAttributes windowattr;
     XGetWindowAttributes(dpy, windowid, &windowattr);

     //--- absolute coordinates  
     Window junkwin;
     int rx,ry;

     XTranslateCoordinates (dpy, windowid, windowattr.root, 
				-windowattr.border_width,
				-windowattr.border_width,
				&rx, &ry, &junkwin);

//      printf( "%d %d %d %d %d\n" , rx , ry , windowattr.width , windowattr.height, windowattr.map_state);

        //-- create a key
        sprintf( mystring, "0" );
        wVariantFromChar( mykey, mystring ,3,3 );
       //-- create a string
             sprintf( mystring, "%d" , rx );

             wVariantFromChar( myvariant, mystring ,4,4 );
        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  

        //-- create a key
        sprintf( mystring, "1" );
        wVariantFromChar( mykey, mystring ,3,3 );
       //-- create a string
             sprintf( mystring, "%d" , ry );
             wVariantFromChar( myvariant, mystring ,4,4 );
        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );
  
        //-- create a key
        sprintf( mystring, "2" );
        wVariantFromChar( mykey, mystring ,3,3 );
       //-- create a string
             sprintf( mystring, "%d" , windowattr.width );
             wVariantFromChar( myvariant, mystring ,4,4 );
        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  

        //-- create a key
        sprintf( mystring, "3" );
        wVariantFromChar( mykey, mystring ,3,3 );
       //-- create a string
             sprintf( mystring, "%d" , windowattr.height );
             wVariantFromChar( myvariant, mystring ,4,4 );
        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  

        //-- create a key
        sprintf( mystring, "4" );
        wVariantFromChar( mykey, mystring ,3,3 );
       //-- create a string
             sprintf( mystring, "%d" , windowattr.map_state );
             wVariantFromChar( myvariant, mystring ,4,4 );
        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  

//   wWrapDerefArgs();
//wStackPushChar(arg1,0);
 



    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );


    //-- cleanup    
    wVariantDeref( myvariant );
    wFree(myvariant);
    wVariantDeref( mylist );
    wFree(mylist);
    wVariantDeref( mykey );
    wFree(mykey);

 //   wVariantDeref( myarg );
    //wFree(myarg);
    free(arg1);
    return;
}
//---------------------------------------------------------------------------------------------------------------------
void wBuiltin_xwin_classinfo()
{

    char mystring[1000];
    wVariant *mylist;
    wVariant *myvariant;
    wVariant *mykey;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

       wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );


     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

    char *arg1;

    wVariant *myarg;

    myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg = wStackPopString();
 

   arg1 = wVariantToChar( myarg, W_FALSE, 0 ); 

    Display *dpy;  
    int windowid = atof(arg1);   
  

     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }
     
    int ret;

    Atom xa_prop_name;
    Atom xa_ret_type;
     int ret_format;
    unsigned long ret_nitems;
    unsigned long ret_bytes_after;
    unsigned char *ret_prop;
  
        //-- create a key
        sprintf( mystring, "" );
        wVariantFromChar( mykey, mystring ,3,3 );

if(  XGetWindowProperty(dpy, windowid, XInternAtom(dpy,  "WM_CLASS",  False) , 0, 1024 ,  False , 
                         XInternAtom (dpy, "STRING", False), &xa_ret_type, &ret_format,     
                         &ret_nitems, &ret_bytes_after, &ret_prop) != Success ){


       //-- create a string
             sprintf( mystring, ""  );

}else{
//int i;
//   for(i = 0; i < ret_nitems; i++){
//     printf(" data [ %d ] : %x \n", i, ret_prop[i]); 
//   }

if(ret_prop != NULL){

             sprintf( mystring, "%s" , ret_prop);
             free(ret_prop);
}
}

             wVariantFromChar( myvariant, mystring ,4,4 );
        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  



    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );


    //-- cleanup    
    wVariantDeref( myvariant );
    wFree(myvariant);
    wVariantDeref( mylist );
    wFree(mylist);
    wVariantDeref( mykey );
    wFree(mykey);

  //  wVariantDeref( myarg );
    //wFree(myarg);
    free(arg1);
    return;
}


void wBuiltin_xwin_iconify()
{
    char *arg1;
    wVariant *myarg;


     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

    myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg = wStackPopString();
    arg1 = wVariantToChar( myarg, W_FALSE, 0 ); 

     //  printf( "--%s\n" , arg1);

    Display *dpy;  
    int windowid;

    windowid = atof(arg1);       

     //  printf( "%x\n" , windowid);


     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }
     int screen=DefaultScreen(dpy);
     XIconifyWindow( dpy, windowid ,screen );

     XFlush(dpy);
     XCloseDisplay(dpy);

     //-- cleanup
     wVariantDeref( myarg );
     //wFree(myarg);
     free(arg1);
     return;
}

void wBuiltin_xwin_toggle_fullscreen()
{
    char *arg1;
    wVariant *myarg;


     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

    myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg = wStackPopString();
    arg1 = wVariantToChar( myarg, W_FALSE, 0 ); 

     //  printf( "--%s\n" , arg1);

    Display *dpy;  
    int windowid;

    windowid = atof(arg1);       

     //  printf( "%x\n" , windowid);


     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }


  XEvent     report;
  Atom       _net_fs, _net_state;
  XEvent     msg;

  _net_state = XInternAtom(dpy, "_NET_WM_STATE", False);
  _net_fs = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);

  printf("fullscreen\n");
  msg.xclient.type = ClientMessage;
  msg.xclient.message_type = _net_state;
  msg.xclient.display = dpy;
  msg.xclient.window = windowid;
  msg.xclient.format = 32;
  msg.xclient.data.l[0] = 2; // toggle
  msg.xclient.data.l[1] = _net_fs;
  msg.xclient.data.l[2] = 0l;
  msg.xclient.data.l[3] = 0l;
  msg.xclient.data.l[4] = 0l;
  XSendEvent(dpy, RootWindow(dpy, 0), False,
	     SubstructureNotifyMask | SubstructureRedirectMask, &msg);
  XFlush(dpy);

     XCloseDisplay(dpy);

     //-- cleanup
     wVariantDeref( myarg );
     //wFree(myarg);
     free(arg1);
     return;
}

void wBuiltin_xwin_activate()
{


     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

    char *arg1;
    wVariant *myarg;

    myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg = wStackPopString();
    arg1 = wVariantToChar( myarg, W_FALSE, 0 ); 

     //  printf( "--%s\n" , arg1);

    Display *dpy;  
    int windowid;

    windowid = atof(arg1);       

     //  printf( "%x\n" , windowid);


     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }



     XMapWindow(dpy,windowid);
     XRaiseWindow(dpy,windowid);
     XFlush(dpy);
     XCloseDisplay(dpy);

     //-- cleanup
     wVariantDeref( myarg );
     //wFree(myarg);
    free(arg1);
        return;
}


void wBuiltin_xwin_move()
{


     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

     // window-ID , x , y
    char *arg1;
    char *arg2;
    char *arg3;
    wVariant *myarg1;
    wVariant *myarg2;
    wVariant *myarg3;

    myarg3 = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg3 = wStackPopString();
    arg3 = wVariantToChar( myarg3, W_FALSE, 0 ); 
    myarg2 = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg2 = wStackPopString();
    arg2 = wVariantToChar( myarg2, W_FALSE, 0 ); 
    myarg1 = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg1 = wStackPopString();
    arg1 = wVariantToChar( myarg1, W_FALSE, 0 ); 

    //   printf( "--%s\n" , arg1);

    Display *dpy;  

    int windowid = atof(arg1);       
    int x = atoi(arg2);
    int y = atoi(arg3);

     //  printf( "%x\n" , windowid);

     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }


     XMoveWindow( dpy, windowid, x, y );
     XFlush(dpy);
     XCloseDisplay(dpy);

     //-- cleanup
     wVariantDeref( myarg1 );
     wVariantDeref( myarg2 );
     wVariantDeref( myarg3 );

     //wFree(myarg);
    free(arg1);
    free(arg2);
    free(arg3);

    return;
}

void wBuiltin_xwin_resize()
{


     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

     // window-ID , x , y
    char *arg1;
    char *arg2;
    char *arg3;
    wVariant *myarg1;
    wVariant *myarg2;
    wVariant *myarg3;

    myarg3 = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg3 = wStackPopString();
    arg3 = wVariantToChar( myarg3, W_FALSE, 0 ); 
    myarg2 = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg2 = wStackPopString();
    arg2 = wVariantToChar( myarg2, W_FALSE, 0 ); 
     myarg1 = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg1 = wStackPopString();
    arg1 = wVariantToChar( myarg1, W_FALSE, 0 ); 

    Display *dpy;  

    int windowid = atof(arg1);       
    int x = atoi(arg2);
    int y = atoi(arg3);

     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }

     XResizeWindow( dpy, windowid, x, y );
     XFlush(dpy);
     XCloseDisplay(dpy);

     //-- cleanup
     wVariantDeref( myarg1 );
     wVariantDeref( myarg2 );
     wVariantDeref( myarg3 );

     //wFree(myarg);
    free(arg1);
    free(arg2);
    free(arg3);

    return;
}

void wBuiltin_xwin_select()
{


     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

    char mystring[1000];
    wVariant *mylist;
    wVariant *myvariant;
    wVariant *mykey;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

       wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );

     Display *dpy;  
     int windowid;
     XWindowAttributes windowattr;


     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }

//  Code taken from dsimple.c

  int screen = DefaultScreen (dpy);
  int status;
  Cursor cursor;
  XEvent event;
  Window target_win = None, root = RootWindow(dpy,screen);
  int buttons = 0;

  /* Make the target cursor */
  cursor = XCreateFontCursor(dpy, XC_crosshair);

  /* Grab the pointer using target cursor, letting it room all over */
  status = XGrabPointer(dpy, root, False,
			ButtonPressMask|ButtonReleaseMask, GrabModeSync,
			GrabModeAsync, root, cursor, CurrentTime);
  if (status != GrabSuccess) printf("Can't grab the mouse.\n");

  /* Let the user select a window... */
  while ((target_win == None) || (buttons != 0)) {
    /* allow one more event */
    XAllowEvents(dpy, SyncPointer, CurrentTime);
    XWindowEvent(dpy, root, ButtonPressMask|ButtonReleaseMask, &event);
    switch (event.type) {
    case ButtonPress:
      if (target_win == None) {
	target_win = event.xbutton.subwindow; /* window selected */
	if (target_win == None) target_win = root;
      }
      buttons++;
      break;
    case ButtonRelease:
      if (buttons > 0) /* there may have been some down before we started */
	buttons--;
       break;
    }
  } 

  XUngrabPointer(dpy, CurrentTime);      /* Done with pointer */

	     // Window root;
	      int dummyi;
	      unsigned int dummy;

	      if (XGetGeometry (dpy, target_win, &root, &dummyi, &dummyi,
				&dummy, &dummy, &dummy, &dummy) &&
		  target_win != root)
	        target_win = XmuClientWindow (dpy, target_win);

        //-- create a key
        sprintf( mystring, "0" );
        wVariantFromChar( mykey, mystring ,3,3 );
       //-- create a string
             sprintf( mystring, "%x" , target_win );

             wVariantFromChar( myvariant, mystring ,4,4 );
        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  


    XCloseDisplay(dpy);



    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );

    //-- cleanup

    wVariantDeref( myvariant );
    wFree(myvariant);
    wVariantDeref( mylist );
    wFree(mylist);

    wVariantDeref( mykey );
    wFree(mykey);


}


void wBuiltin_xwin_multiselect()
{


     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

    char mystring[1000];
    wVariant *mylist;
    wVariant *myvariant;
    wVariant *mykey;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

       wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );

     Display *dpy;  
     int windowid;
     XWindowAttributes windowattr;


     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }

//  Code taken from dsimple.c

  int screen = DefaultScreen (dpy);
  int status;
  Cursor cursor;
  XEvent event;
  Window target_win = None, root = RootWindow(dpy,screen);
  int buttons = 0;

  int dummyi;
  unsigned int dummy;

  int stop = 1;
  int counter = 0;



  /* Make the target cursor */
  cursor = XCreateFontCursor(dpy, XC_crosshair);

  /* Grab the pointer using target cursor, letting it room all over */
  status = XGrabPointer(dpy, root, False,
			ButtonPressMask|ButtonReleaseMask, GrabModeSync,
			GrabModeAsync, root, cursor, CurrentTime);
  if (status != GrabSuccess) printf("Can't grab the mouse.\n");

  /* Let the user select a window... */
  while ( stop==1 ) {
    /* allow one more event */
    XAllowEvents(dpy, SyncPointer, CurrentTime);
    XWindowEvent(dpy, root, ButtonPressMask|ButtonReleaseMask, &event);
    switch (event.type) {
    case ButtonPress:                         
       if(event.xbutton.button==1) {

				
         if (target_win == None) {
	   target_win = event.xbutton.subwindow; /* window selected */
	   if (target_win == None) target_win = root;


	      if (XGetGeometry (dpy, target_win, &root, &dummyi, &dummyi,
				&dummy, &dummy, &dummy, &dummy) &&
		  target_win != root)
	        target_win = XmuClientWindow (dpy, target_win);

 	       //-- create a key
  	       sprintf( mystring, "%d" , counter );
 	       wVariantFromChar( mykey, mystring ,3,3 );
 	       //-- create a string
               sprintf( mystring, "%x" , target_win );
               wVariantFromChar( myvariant, mystring ,4,4 );
              //-- append it to the list
  	      wTableInsert(mylist , mykey, myvariant );  

             target_win = None;
 	     counter++;
         }

      }
      if(event.xbutton.button==3) {
          stop=0;
		
      }
         buttons++;
       //  break;
    case ButtonRelease:
      if (buttons > 0) /* there may have been some down before we started */
	buttons--;
      // break;
    }
  



}
  XUngrabPointer(dpy, CurrentTime);      /* Done with pointer */


    XCloseDisplay(dpy);



    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );

    //-- cleanup

    wVariantDeref( myvariant );
    wFree(myvariant);
    wVariantDeref( mylist );
    wFree(mylist);

    wVariantDeref( mykey );
    wFree(mykey);


}


void wBuiltin_xwin_select_1()
{


     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

    char mystring[1000];
    wVariant *mylist;
    wVariant *myvariant;
    wVariant *mykey;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

       wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );

     Display *dpy;  
     int windowid;
     XWindowAttributes windowattr;


     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }

//  Code taken from dsimple.c

  int screen = DefaultScreen (dpy);
  int status;
  Cursor cursor;
  XEvent event;
  Window target_win = None, root = RootWindow(dpy,screen);
  int buttons = 0;

  int dummyi;
  unsigned int dummy;

  int stop = 1;


  /* Make the target cursor */
  cursor = XCreateFontCursor(dpy, XC_crosshair);

  /* Grab the pointer using target cursor, letting it room all over */
  status = XGrabPointer(dpy, root, False,
			ButtonPressMask|ButtonReleaseMask, GrabModeSync,
			GrabModeAsync, root, cursor, CurrentTime);
  if (status != GrabSuccess) printf("Can't grab the mouse.\n");

  /* Let the user select a window... */
  while ( stop==1 ) {
    /* allow one more event */
    XAllowEvents(dpy, SyncPointer, CurrentTime);
    XWindowEvent(dpy, root, ButtonPressMask|ButtonReleaseMask, &event);
    switch (event.type) {
    case ButtonPress:                         
       if(event.xbutton.button==1) {

				
         if (target_win == None) {
	   target_win = event.xbutton.subwindow; /* window selected */
	   if (target_win == None) target_win = root;


	      if (XGetGeometry (dpy, target_win, &root, &dummyi, &dummyi,
				&dummy, &dummy, &dummy, &dummy) &&
		  target_win != root)
	        target_win = XmuClientWindow (dpy, target_win);

 	       //-- create a key
  	       sprintf( mystring, "0");
 	       wVariantFromChar( mykey, mystring ,3,3 );
 	       //-- create a string
               sprintf( mystring, "%x" , target_win );
               wVariantFromChar( myvariant, mystring ,4,4 );
              //-- append it to the list
  	      wTableInsert(mylist , mykey, myvariant );  


          stop=0;
         }

      }
      if(event.xbutton.button==3) {

 	       //-- create a key
  	       sprintf( mystring, "0");
 	       wVariantFromChar( mykey, mystring ,3,3 );
 	       //-- create a string
               sprintf( mystring, "END"  );
               wVariantFromChar( myvariant, mystring ,4,4 );
              //-- append it to the list
  	      wTableInsert(mylist , mykey, myvariant );  
          stop=0;
		
      }
         buttons++;
       //  break;
    case ButtonRelease:
      if (buttons > 0) /* there may have been some down before we started */
	buttons--;
      // break;
    }
  



}
  XUngrabPointer(dpy, CurrentTime);      /* Done with pointer */


    XCloseDisplay(dpy);



    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );

    //-- cleanup

    wVariantDeref( myvariant );
    wFree(myvariant);
    wVariantDeref( mylist );
    wFree(mylist);

    wVariantDeref( mykey );
    wFree(mykey);


}


void wBuiltin_xwin_getmouseposition()
{


     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

    char mystring[1000];
    wVariant *mylist;
    wVariant *myvariant;
    wVariant *mykey;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

       wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );


    Display *dpy;  
  

     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }
     
int screen=DefaultScreen(dpy);


  Window w1, w2;
  int x1, y1, x2, y2, m;

  XQueryPointer (dpy, DefaultRootWindow(dpy), &w1, &w2, &x1, &y1, &x2, &y2, &m);

    XCloseDisplay(dpy);



        //-- create a key
        sprintf( mystring, "0" );
        wVariantFromChar( mykey, mystring ,3,3 );
       //-- create a string
             sprintf( mystring, "%d" ,  x1);

             wVariantFromChar( myvariant, mystring ,4,4 );
        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  

        //-- create a key
        sprintf( mystring, "1" );
        wVariantFromChar( mykey, mystring ,3,3 );
       //-- create a string
             sprintf( mystring, "%d" , y1 );
             wVariantFromChar( myvariant, mystring ,4,4 );
        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );
  


    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );


    //-- cleanup    
    wVariantDeref( myvariant );
    wFree(myvariant);
    wVariantDeref( mylist );
    wFree(mylist);
    wVariantDeref( mykey );
    wFree(mykey);


    return;
}



void wBuiltin_xwin_screensize()
{


     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

    char mystring[1000];
    wVariant *mylist;
    wVariant *myvariant;
    wVariant *mykey;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

       wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );


    Display *dpy;  
  

     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }
     
int screen=DefaultScreen(dpy);
int screenWidth=0;
int screenHeight=0;
screenWidth=XDisplayWidth(dpy,screen);
screenHeight=XDisplayHeight(dpy,screen);

    XCloseDisplay(dpy);


        //-- create a key
        sprintf( mystring, "0" );
        wVariantFromChar( mykey, mystring ,3,3 );
       //-- create a string
             sprintf( mystring, "%d" ,  screenWidth);

             wVariantFromChar( myvariant, mystring ,4,4 );
        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  

        //-- create a key
        sprintf( mystring, "1" );
        wVariantFromChar( mykey, mystring ,3,3 );
       //-- create a string
             sprintf( mystring, "%d" , screenHeight );
             wVariantFromChar( myvariant, mystring ,4,4 );
        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );
  


    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );


    //-- cleanup    
    wVariantDeref( myvariant );
    wFree(myvariant);
    wVariantDeref( mylist );
    wFree(mylist);
    wVariantDeref( mykey );
    wFree(mykey);


    return;
}
void wBuiltin_xwin_getworkspace()
{

     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

    char mystring[1000];
    wVariant *mylist;
    wVariant *myvariant;
    wVariant *mykey;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

       wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );


    Display *dpy;  
  

     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }

    int ret;

    Atom xa_prop_name;
    Atom xa_ret_type;
     int ret_format;
    unsigned long ret_nitems;
    unsigned long ret_bytes_after;
    unsigned char *ret_prop;
  

  Atom _WIN_WORKSPACE = XInternAtom(dpy,  "_WIN_WORKSPACE",  False);
  Atom _NET_WM_DESKTOP = XInternAtom(dpy,  "_NET_WM_DESKTOP",  False);

        //-- create a key
        sprintf( mystring, "0" );
        wVariantFromChar( mykey, mystring ,3,3 );

       //-- create a string
             sprintf( mystring, "0"  );
             wVariantFromChar( myvariant, mystring ,4,4 );

        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  

     
     int screen=DefaultScreen(dpy);     
     Window client  = RootWindow (dpy, screen);


if(  XGetWindowProperty(dpy, client, _WIN_WORKSPACE , 0, 1024 ,  False , 
                         XA_CARDINAL, &xa_ret_type, &ret_format,     
                         &ret_nitems, &ret_bytes_after, &ret_prop) != Success ){
             ret = XGetWindowProperty(dpy, client, _NET_WM_DESKTOP , 0, 1024 ,  False , 
                         XA_CARDINAL, &xa_ret_type, &ret_format,     
                         &ret_nitems, &ret_bytes_after, &ret_prop);
}else{
             ret = XGetWindowProperty(dpy, client, _WIN_WORKSPACE , 0, 1024 ,  False , 
                         XA_CARDINAL, &xa_ret_type, &ret_format,     
                         &ret_nitems, &ret_bytes_after, &ret_prop);

}

if(ret == Success  && ret_prop){

//printf("++++++++++%d\n" , *ret_prop);

        //-- create a key
        sprintf( mystring, "0"  );
        wVariantFromChar( mykey, mystring ,3,3 );

       //-- create a string
             sprintf( mystring, "%d" , *ret_prop );
             wVariantFromChar( myvariant, mystring ,4,4 );

        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  

       free(ret_prop);
}
    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );

    XCloseDisplay(dpy);

    //-- cleanup    
    wVariantDeref( myvariant );
    wFree(myvariant);
    wVariantDeref( mylist );
    wFree(mylist);
    wVariantDeref( mykey );
    wFree(mykey);


    return;
}
void wBuiltin_xwin_getworkspaces()
{

     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

    char mystring[1000];
    wVariant *mylist;
    wVariant *myvariant;
    wVariant *mykey;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

       wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );


    Display *dpy;  
  

     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }

    int ret;

    Atom xa_prop_name;
    Atom xa_ret_type;
     int ret_format;
    unsigned long ret_nitems;
    unsigned long ret_bytes_after;
    unsigned char *ret_prop;
  

  Atom _WIN_WORKSPACE_COUNT = XInternAtom(dpy,  "_WIN_WORKSPACE_COUNT",  False);
  Atom _NET_NUMBER_OF_DESKTOPS = XInternAtom(dpy,  "_NET_NUMBER_OF_DESKTOPS",  False); 


     
     int screen=DefaultScreen(dpy);     
     Window client  = RootWindow (dpy, screen);

        //-- create a key
        sprintf( mystring, "0" );
        wVariantFromChar( mykey, mystring ,3,3 );

       //-- create a string
             sprintf( mystring, "0"  );
             wVariantFromChar( myvariant, mystring ,4,4 );

        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  



if(  XGetWindowProperty(dpy, client, _WIN_WORKSPACE_COUNT , 0, 1024 ,  False , 
                         XA_CARDINAL, &xa_ret_type, &ret_format,     
                         &ret_nitems, &ret_bytes_after, &ret_prop) != Success ){
             ret = XGetWindowProperty(dpy, client, _NET_NUMBER_OF_DESKTOPS , 0, 1024 ,  False , 
                         XA_CARDINAL, &xa_ret_type, &ret_format,     
                         &ret_nitems, &ret_bytes_after, &ret_prop);
}else{
             ret = XGetWindowProperty(dpy, client, _WIN_WORKSPACE_COUNT , 0, 1024 ,  False , 
                         XA_CARDINAL, &xa_ret_type, &ret_format,     
                         &ret_nitems, &ret_bytes_after, &ret_prop);

}

if(ret == Success  && ret_prop){



//printf("++++++++++%d\n" , *ret_prop);

        //-- create a key
        sprintf( mystring, "0"  );
        wVariantFromChar( mykey, mystring ,3,3 );

       //-- create a string
             sprintf( mystring, "%d" , *ret_prop );
             wVariantFromChar( myvariant, mystring ,4,4 );

        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  

       free(ret_prop);

}



    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );


    //-- cleanup    
    wVariantDeref( myvariant );
    wFree(myvariant);
    wVariantDeref( mylist );
    wFree(mylist);
    wVariantDeref( mykey );
    wFree(mykey);


    return;
}


void wBuiltin_xwin_setworkspace()
{
    char *arg1;
    wVariant *myarg;


     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

    myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg = wStackPopString();
    arg1 = wVariantToChar( myarg, W_FALSE, 0 ); 

     //  printf( "--%s\n" , arg1);

    Display *dpy;  
    int workspace;

    workspace = atof(arg1);       

     //  printf( "%x\n" , windowid);


     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }


  XEvent     report;
  XEvent     msg;

  printf("set workspace %d\n" , workspace);
  msg.xclient.type = ClientMessage;
  msg.xclient.message_type = XInternAtom(dpy, "_WIN_WORKSPACE", False);
  msg.xclient.display = dpy;
  msg.xclient.format = 32;
  msg.xclient.data.l[0] = workspace;
  msg.xclient.data.l[1] = 0l;
  msg.xclient.data.l[2] = 0l;
  msg.xclient.data.l[3] = 0l;
  msg.xclient.data.l[4] = 0l;
  XSendEvent(dpy, RootWindow(dpy, 0), False,
	     SubstructureNotifyMask | SubstructureRedirectMask, &msg);
  XFlush(dpy);

     XCloseDisplay(dpy);

     //-- cleanup
     wVariantDeref( myarg );
     //wFree(myarg);
     free(arg1);
     return;
}

void wBuiltin_xwin_movetoworkspace(){
    char *arg1;
    wVariant *myarg;
    char *arg2;
    wVariant *myarg2;

     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);


    myarg2 = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg2 = wStackPopString();
    arg2 = wVariantToChar( myarg2, W_FALSE, 0 ); 

    myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg = wStackPopString();
    arg1 = wVariantToChar( myarg, W_FALSE, 0 ); 

     //  printf( "--%s\n" , arg1);

    Display *dpy;  
    int windowid;
    windowid = atof(arg1);       
    int workspace;
    workspace = atof(arg2);
   
     //  printf( "%x\n" , windowid);


     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }


    XChangeProperty(dpy, windowid, XInternAtom(dpy, "_WIN_WORKSPACE", False), XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&workspace, 1);
    XChangeProperty(dpy, windowid, XInternAtom(dpy, "_NET_WM_DESKTOP", False), XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&workspace, 1);


  XFlush(dpy);

     XCloseDisplay(dpy);

     //-- cleanup
     wVariantDeref( myarg );
     //wFree(myarg);
     free(arg1);
     wVariantDeref( myarg2 );
     //wFree(myarg2);
     free(arg2);
     return;

}

void wBuiltin_xwin_titleandborder(){
    char *arg1;
    wVariant *myarg;

     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

    myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg = wStackPopString();
    arg1 = wVariantToChar( myarg, W_FALSE, 0 ); 

     //  printf( "--%s\n" , arg1);

    Display *dpy;  
    int window;
    window = atof(arg1);       
   
     //  printf( "%x\n" , windowid);


     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }

            Atom WM_HINTS;

XUnmapWindow(dpy,window);

            XSetWindowAttributes attributes;
#define MWM_HINTS_DECORATIONS   2
            /* First try to set MWM hints */
            WM_HINTS = XInternAtom(dpy, "_MOTIF_WM_HINTS", True);
            if ( WM_HINTS != None ) {
//#               define  MWM_HINTS_DECORATIONS   (1L << 1)
                struct {
                    int flags;
                    int functions;
                    int decorations;
                    int inputMode;
                    int unknown;
                } MWMHints = { MWM_HINTS_DECORATIONS, 1, 1, 1, 1 };

                XChangeProperty(dpy, window, WM_HINTS, WM_HINTS, 32,
                                PropModeReplace, (unsigned char *)&MWMHints,
                                sizeof(MWMHints)/4);
            }
            /* Now try to set KWM hints */
            WM_HINTS = XInternAtom(dpy, "KWM_WIN_DECORATION", True);
            if ( WM_HINTS != None ) {
                long KWMHints = 1;

                XChangeProperty(dpy, window, WM_HINTS, WM_HINTS, 32,
                                PropModeReplace, (unsigned char *)&KWMHints,
                                sizeof(KWMHints)/4);
            }
#if 0       /* The Butcher way of removing window decorations -- not polite */
            attributes.override_redirect = True;
            XChangeWindowAttributes(dpy, iwindow,
                                    CWOverrideRedirect, &attributes);
#endif
 

XMapWindow(dpy,window);

  XFlush(dpy);

     XCloseDisplay(dpy);

     //-- cleanup
     wVariantDeref( myarg );
     //wFree(myarg);
     free(arg1);

     return;
}


void wBuiltin_xwin_hastitleandborder(){
    char *arg1;
    wVariant *myarg;

     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

    myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg = wStackPopString();
    arg1 = wVariantToChar( myarg, W_FALSE, 0 ); 

     //  printf( "--%s\n" , arg1);
    char mystring[1000];
    wVariant *mylist;
    wVariant *myvariant;
    wVariant *mykey;

	myvariant = (wVariant *)wMalloc( sizeof( wVariant ) );
	mylist = (wVariant *)wMalloc( sizeof( wVariant ) );

       wTableNew(mylist);
	mykey = (wVariant *)wMalloc( sizeof( wVariant ) );

    Display *dpy;  
    int window;
    window = atof(arg1);       
   
     //  printf( "%x\n" , windowid);


     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }  

  int * data;
  unsigned long nitems, bytesafter;
  Atom actual_type;
  int actual_format;
 

XGetWindowProperty(dpy, window, XInternAtom(dpy, "_MOTIF_WM_HINTS", False), 0, 5,
                             0,XInternAtom(dpy, "_MOTIF_WM_HINTS", False) ,
                             &actual_type, &actual_format,
                             &nitems, &bytesafter,
                             (unsigned char **) &data);
 
int i;
int result=-2;
   for(i = 0; i < nitems; i++){

  //   printf(" data [ %d ] : %x \n", i, data[i]);
       result = result + data[i];
   
   
   }

  XFlush(dpy);

     XCloseDisplay(dpy);
 
       //-- create a key
        sprintf( mystring, "0"  );
        wVariantFromChar( mykey, mystring ,3,3 );

       //-- create a string
             sprintf( mystring, "%d" , result );
             wVariantFromChar( myvariant, mystring ,4,4 );

        //-- append it to the list
       wTableInsert(mylist , mykey, myvariant );  

       free(data);

    //-- return the wList to wxBasic
    wVariantMove( wStackPushNothing(), mylist );


    //-- cleanup    
    wVariantDeref( myvariant );
    wFree(myvariant);
    wVariantDeref( mylist );
    wFree(mylist);
    wVariantDeref( mykey );
    wFree(mykey);


    return;
}

void wBuiltin_xwin_notitlenoborder(){
    char *arg1;
    wVariant *myarg;

     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);

    myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg = wStackPopString();
    arg1 = wVariantToChar( myarg, W_FALSE, 0 ); 

     //  printf( "--%s\n" , arg1);

    Display *dpy;  
    int window;
    window = atof(arg1);       
   
     //  printf( "%x\n" , windowid);


     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }

            Atom WM_HINTS;

XUnmapWindow(dpy,window);

            XSetWindowAttributes attributes;
#define MWM_HINTS_DECORATIONS   2
            /* First try to set MWM hints */
            WM_HINTS = XInternAtom(dpy, "_MOTIF_WM_HINTS", True);
            if ( WM_HINTS != None ) {
//#               define  MWM_HINTS_DECORATIONS   (1L << 1)
                struct {
                    int flags;
                    int functions;
                    int decorations;
                    int inputMode;
                    int unknown;
                } MWMHints = { MWM_HINTS_DECORATIONS, 0, 0, 0, 0 };

                XChangeProperty(dpy, window, WM_HINTS, WM_HINTS, 32,
                                PropModeReplace, (unsigned char *)&MWMHints,
                                sizeof(MWMHints)/4);
            }
            /* Now try to set KWM hints */
            WM_HINTS = XInternAtom(dpy, "KWM_WIN_DECORATION", True);
            if ( WM_HINTS != None ) {
                long KWMHints = 0;

                XChangeProperty(dpy, window, WM_HINTS, WM_HINTS, 32,
                                PropModeReplace, (unsigned char *)&KWMHints,
                                sizeof(KWMHints)/4);
            }
#if 0       /* The Butcher way of removing window decorations -- not polite */
            attributes.override_redirect = True;
            XChangeWindowAttributes(dpy, iwindow,
                                    CWOverrideRedirect, &attributes);
#endif
 

XMapWindow(dpy,window);

  XFlush(dpy);

     XCloseDisplay(dpy);

     //-- cleanup
     wVariantDeref( myarg );
     //wFree(myarg);
     free(arg1);

     return;

}
void wBuiltin_xwin_setlayer(){
    char *arg1;
    wVariant *myarg;
    char *arg2;
    wVariant *myarg2;

     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);


    myarg2 = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg2 = wStackPopString();
    arg2 = wVariantToChar( myarg2, W_FALSE, 0 ); 

    myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg = wStackPopString();
    arg1 = wVariantToChar( myarg, W_FALSE, 0 ); 

     //  printf( "--%s\n" , arg1);

    Display *dpy;  
    int windowid;
    windowid = atof(arg1);       
    int layer;
    layer = atof(arg2);
   
     //  printf( "%x\n" , windowid);


     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }

#define WIN_LAYER_DESKTOP                0
#define WIN_LAYER_BELOW                  2
#define WIN_LAYER_NORMAL                 4
#define WIN_LAYER_ONTOP                  6
#define WIN_LAYER_DOCK                   8
#define WIN_LAYER_ABOVE_DOCK             10
#define WIN_LAYER_MENU                   12

    XClientMessageEvent  xev;
    memset(&xev, 0, sizeof(xev));

    xev.type = ClientMessage;
    xev.window = windowid;
    xev.message_type = XInternAtom(dpy, "_WIN_LAYER", False);
    xev.format = 32;
    switch (layer)
      {
      case -1: 
        xev.data.l[0] = WIN_LAYER_BELOW;
        break;
      case  0:
        xev.data.l[0] = WIN_LAYER_NORMAL;
        break;
      case  1:
        xev.data.l[0] = WIN_LAYER_ABOVE_DOCK;
        break;
      default:
        xev.data.l[0] = WIN_LAYER_NORMAL;
        break;
      }
    XSendEvent(dpy,DefaultRootWindow(dpy),False,
               SubstructureNotifyMask,(XEvent*)&xev);
    if (layer)
      XRaiseWindow(dpy,windowid);


  XFlush(dpy);

     XCloseDisplay(dpy);

     //-- cleanup
     wVariantDeref( myarg );
     //wFree(myarg);
     free(arg1);
     wVariantDeref( myarg2 );
     //wFree(myarg2);
     free(arg2);
     return;

}
void wBuiltin_xwin_setwmstruts(){
    char *arg1;
    wVariant *myarg;
    char *arg2;
    wVariant *myarg2;
    char *arg3;
    wVariant *myarg3;
    char *arg4;
    wVariant *myarg4;
    char *arg5;
    wVariant *myarg5;





     // avoid badwindow-errors
     int (*myold_handler) (Display *, XErrorEvent *);
     myold_handler= XSetErrorHandler (ignore_error);


    myarg5 = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg5 = wStackPopString();
    arg5 = wVariantToChar( myarg5, W_FALSE, 0 ); 

    myarg4 = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg4 = wStackPopString();
    arg4 = wVariantToChar( myarg4, W_FALSE, 0 ); 

    myarg3 = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg3 = wStackPopString();
    arg3 = wVariantToChar( myarg3, W_FALSE, 0 ); 


    myarg2 = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg2 = wStackPopString();
    arg2 = wVariantToChar( myarg2, W_FALSE, 0 ); 

    myarg = (wVariant *)wMalloc( sizeof( wVariant ) );
    myarg = wStackPopString();
    arg1 = wVariantToChar( myarg, W_FALSE, 0 ); 

     //  printf( "--%s\n" , arg1);

    Display *dpy;  
    int windowid;
 //   windowid = atof(arg1);       
int w= atof(arg1);  

   int left = atof(arg2);
   int right = atof(arg3);  
   int top = atof(arg4);
   int bottom = atof(arg5);

     //  printf( "%x\n" , windowid);
     // printf( "%d\n" , top);

     dpy = XOpenDisplay(NULL);
     if (dpy == NULL){
         fprintf(stderr, "Can't open display\n");
         return;
     }
     int scr = DefaultScreen (dpy);
//   int w  = RootWindow (dpy, scr);



    unsigned long data[12] = { 0 };
    int i = 0;
        data[i] = left;
        data[4 + i*2] = 0;
        data[5 + i*2] = 0;
    i = 1;
        data[i] = right;
        data[4 + i*2] = 0;
        data[5 + i*2] = 0;
    i = 2;
        data[i] = top;
        data[4 + i*2] = 0;
        data[5 + i*2] = 0;
    i = 3;
        data[i] = bottom;
        data[4 + i*2] = 0;
        data[5 + i*2] = 0;

    XDeleteProperty( dpy, w, XInternAtom(dpy, "_NET_WM_STRUT", False) );
    XDeleteProperty( dpy, w, XInternAtom(dpy, "_NET_WM_STRUT_PARTIAL", False) );

    /* if wm supports STRUT_PARTIAL it will ignore STRUT */
    XChangeProperty(dpy, w, XInternAtom(dpy, "_NET_WM_STRUT_PARTIAL", False), 
          XA_CARDINAL, 32, PropModeReplace,  (unsigned char *) data, 12);
    /* old spec, for wms that do not support STRUT_PARTIAL */
    XChangeProperty(dpy, w, XInternAtom(dpy, "_NET_WM_STRUT", False), 
          XA_CARDINAL, 32, PropModeReplace,  (unsigned char *) data, 4); 


  XFlush(dpy);

     XCloseDisplay(dpy);

     //-- cleanup
     wVariantDeref( myarg );
     wVariantDeref( myarg2 );
     wVariantDeref( myarg3 );
     wVariantDeref( myarg4 );
     wVariantDeref( myarg5 );

     //wFree(myarg);
    free(arg1);
    free(arg2);
    free(arg3);
    free(arg4);
    free(arg5);

}
