/*************************************************************************************************************************************/
/*                                                                                                                                   */
/*        T E S T   D U   C R E A T E - W I N D O W   T R U E - C O L O R   S U R   ' SYSTEME_APC_LinuxDebian_GCC '  :               */
/*                                                                                                                                   */
/*                                                                                                                                   */
/*        Author of '$xtc/Xwindow.43$c' :                                                                                            */
/*                                                                                                                                   */
/*                    Jean-Francois Colonna (LACTAMME, 20021226094011).                                                              */
/*                                                                                                                                   */
/*                                                                                                                                   */
/*************************************************************************************************************************************/

#include  "INCLUDES.01.I"
                                        /* Introduit le 20051116100551...                                                            */

extern    void      *malloc();

#define   NC        1
#define   dimX      512
#define   dimY      512
#define   Xmin      0
#define   Xmax      (dimX-1)
#define   Ymin      0
#define   Ymax      (dimY-1)
#define   NBITOC    8
#define   pEVENT(x)                                                                                                                     \
                    case x:                                                                                                             \
                         {                                                                                                              \
                         printf("\n x");                                                                                                \
                         break;                                                                                                         \
                         }
#define   EVENEMENT(x)                                                                                                                  \
                    {                                                                                                                   \
                    printf("\n evenement attendu=%d",x);                                                                                \
                    evenement.type = LASTEvent;                                                                                         \
                                        /* Ainsi, il n'y a pas encore d'evenement                                                    */ \
                    while (evenement.type != x)                                                                                         \
                         {                                                                                                              \
                         XNextEvent(display,&evenement);                                                                                \
                         printf("   -arrive=%d",evenement.type);                                                                        \
                         }                                                                                                              \
                    }

#include  <fcntl.h>
#include  <sys/signal.h>
#include  <X11/Xlib.h>
#include  <X11/X.h>
#include  <X11/Xutil.h>

extern    Display   *XOpenDisplay();
extern    Window    XCreateWindow();
extern    int       XSelectInput();
extern    int       XMapWindow();
extern    int       XUnmapWindow();
extern    int       XDestroyWindow();
extern    Status    XAllocColor();
extern    int       XPutImage();
extern    XImage    *XCreateImage();
extern    GC        XCreateGC();
main(argc,argv)
int       argc;
char      *argv[];
     {
     int            Xargc=0;
                                        /* Pour simuler argc                                                                         */
     char           **Xargv;
                                        /* Pour simuler argv                                                                         */

     unsigned long  niveau_du_bord;
     unsigned long  niveau_du_fond;
     unsigned long  largeur_du_bord=9;
     int            profondeur;
     int            NOPP=3;
                                        /* Nombre d'octets par point.                                                                */

     char           *nom_du_display;
     Display        *display;
     int            ecran;
     Window         fenetre_de_base;
                                        /* Root window                                                                               */
     Window         fenetre_a_ouvrir;

     XEvent         evenement;

     XSetWindowAttributes     attributs;
     XSizeHints               hints;

     Visual         visual;
     Visual         *visual_courant;
     XVisualInfo    visual_recherche;
     XVisualInfo    *liste_des_visuals;
     int            nombre_de_visuals;

     Colormap       palette;
     XColor         entree_palette[NC];
     int            niveau;
     unsigned long  couleurs[NC];
     unsigned long  Cmasque[1];
     int            substitution[COULEURS];
     int            sc=NOIR;

     GC             contexte_graphique;
     XGCValues      valeurs_du_contexte_graphique;

     unsigned  char *image;
#define   IMAGE(x,y,n)                                                                                                                  \
                    (*(image + ((((y-Ymin)*dimX) + (x-Xmin))*NOPP + n)))                                                                \
                                        /* Acces a un point de l'image au format externe.                                            */
     int            cx,cy;
                                        /* Coordonnees de l'image                                                                    */
     int            offx=0,offy=0;
                                        /* Coordonnee sous-image dans l'image                                                        */
     int            dx=0,dy=0;
                                        /* Position de la sous-image dans la fenetre                                                 */
     XImage         *Simage;

/*************************************************************************************************************************************/
/*                                                                                                                                   */
/*        O U V E R T U R E   D U   D I S P L A Y                                                                                    */
/*                                                                                                                                   */
/*************************************************************************************************************************************/
     image = malloc(NOPP*dimY*dimX);

     nom_du_display = getenv("DISPLAY");
     display = XOpenDisplay(nom_du_display);
     ecran = DefaultScreen(display);

     if   (ecran > ScreenCount(display)-1)
          {
          printf("\n probleme de nombre d'ecrans");
          }
     else
          {
          }

     if   (display == 0)
          {
          printf("\n erreur d'open");
          exit(-1);
          }
     else
          {
          }

     if   (dimX > DisplayWidth(display,ecran))
          {
          printf("\n image trop large");
          }
     else
          {
          }

     if   (dimY > DisplayHeight(display,ecran))
          {
          printf("\n image trop haute");
          }
     else
          {
          }

     fenetre_de_base= RootWindow(display,ecran);
     niveau_du_fond = BlackPixel(display,ecran);
     niveau_du_bord = WhitePixel(display,ecran);
     profondeur = NOPP*NBITOC;

     if   (profondeur > DefaultDepth(display,ecran))
          {
          printf("\n la fenetre a ouvrir ne peut avoir plus de couleurs que la fenetre 'parent'");
          }
     else
          {
          }

     printf("\n niveau_du_fond=%x",niveau_du_fond);
     printf("\n niveau_du_bord=%x",niveau_du_bord);
     printf("\n profondeur=%d",profondeur);
/*************************************************************************************************************************************/
/*                                                                                                                                   */
/*        D E F I N I T I O N   D U   V I S U A L                                                                                    */
/*                                                                                                                                   */
/*************************************************************************************************************************************/
     visual_recherche.screen = ecran;
     visual_recherche.class = TrueColor;
     visual_recherche.depth = profondeur;

     liste_des_visuals = XGetVisualInfo(display
                                       ,VisualScreenMask | VisualClassMask | VisualDepthMask
                                       ,&visual_recherche
                                       ,&nombre_de_visuals
                                        );
     if   (nombre_de_visuals == 0)
          {
          printf("\n pas de visuals \n");
          }
     else
          {
          printf("\n %d visuals \n",nombre_de_visuals);
          }

     visual_courant=liste_des_visuals->visual;

     printf("\n ID=%x",visual_courant->visualid);
     printf("\n red_mask=%x",visual_courant->red_mask);
     printf("\n green_mask=%x",visual_courant->green_mask);
     printf("\n blue_mask=%x",visual_courant->blue_mask);
     printf("\n bits_per_rgb=%x",visual_courant->bits_per_rgb);
     printf("\n map_entries=%x",visual_courant->map_entries);
     printf("\n");

     if   (visual_courant->class != TrueColor)
          {
          printf("\n on n'est pas en vraies couleurs");
          }
     else
          {
          }
/*************************************************************************************************************************************/
/*                                                                                                                                   */
/*        O U V E R T U R E   D E   L A   F E N E T R E                                                                              */
/*                                                                                                                                   */
/*************************************************************************************************************************************/
     hints.x = 400;
     hints.y = 400;
     hints.width = dimX;
     hints.height= dimY;
     hints.flags = PPosition | PSize;

     attributs.backing_store = WhenMapped;

#define   MASKE                                                                                                                         \
                    ExposureMask | VisibilityChangeMask | PropertyChangeMask | StructureNotifyMask | ColormapChangeMask

     attributs.event_mask = MASKE;
     attributs.override_redirect = True;
                                        /* Doit etre present, car sinon, le cadre clignotte                                          */

#define   ATTRIB                                                                                                                        \
                    CWBackingStore | CWEventMask | CWOverrideRedirect

     fenetre_a_ouvrir = XCreateWindow(display
                                     ,fenetre_de_base
                                     ,hints.x,hints.y
                                     ,hints.width,hints.height
                                     ,largeur_du_bord
                                     ,profondeur
                                     ,InputOutput
                                     ,visual_courant
                                     ,ATTRIB,&attributs
                                      );

     printf("\n ouverture\n");

     XSelectInput(display,fenetre_a_ouvrir,MASKE);
     XSetStandardProperties(display,fenetre_a_ouvrir,"NOM-FENETRE","NOM-ICONE",None,Xargv,Xargc,&hints);
     EVENEMENT(PropertyNotify);
     XSetWindowBackground(display,fenetre_a_ouvrir,niveau_du_fond);
     EVENEMENT(PropertyNotify);
     XSetWindowBorder(display,fenetre_a_ouvrir,niveau_du_bord-1);
     EVENEMENT(PropertyNotify);
/*************************************************************************************************************************************/
/*                                                                                                                                   */
/*        A F F I C H A G E   D E   L A   F E N E T R E                                                                              */
/*                                                                                                                                   */
/*************************************************************************************************************************************/
     XMapWindow(display,fenetre_a_ouvrir);
     EVENEMENT(MapNotify);
     EVENEMENT(VisibilityNotify);
     EVENEMENT(Expose);
/*************************************************************************************************************************************/
/*                                                                                                                                   */
/*        C R E A T I O N   D E   L A   S T R U C T U R E   I M A G E  :                                                             */
/*                                                                                                                                   */
/*************************************************************************************************************************************/
     valeurs_du_contexte_graphique.function = GXcopy;
                                        /* Afin de forcer les pixels dans la fenetre                                                 */

     contexte_graphique = XCreateGC(display,fenetre_a_ouvrir,GCFunction|GCBackground|GCForeground,&valeurs_du_contexte_graphique);

     fprintf(stderr,"\n display=%d",display);
     fprintf(stderr,"\n visual_courant=%d",visual_courant);
     fprintf(stderr,"\n profondeur=%d",profondeur);
     fprintf(stderr,"\n ZPixmap=%d",ZPixmap);
     fprintf(stderr,"\n image=%ld",image);
     fprintf(stderr,"\n dimX=%d",dimX);
     fprintf(stderr,"\n dimY=%d",dimY);
     fprintf(stderr,"\n nbitoc=%d",NBITOC);
     fprintf(stderr,"\n NOPP_dimX=%d",NOPP*dimX);
     fprintf(stderr,"\n");
                                        /* Editions introduites le 20170619140529 pour voir la difference entre 'vaugirard' (OK...)  */
                                        /* et 'porte-brancion' ("Segmentation fault" lors du 'XCreateImage(...)') sur '$CMAP28'...   */

     Simage = XCreateImage(display,visual_courant,profondeur,ZPixmap,0,image,dimX,dimY,NBITOC,NOPP*dimX);

     Simage->bits_per_pixel=profondeur;

/*************************************************************************************************************************************/
/*                                                                                                                                   */
/*        G E N E R A T I O N   D E   L ' I M A G E                                                                                  */
/*                                                                                                                                   */
/*************************************************************************************************************************************/
     for  (cy=Ymin ; cy<=Ymax ; cy++)
          {
          for  (cx=Xmin ; cx<=Xmax ; cx++)
               {
               int  rouge=(int)((((float)cx)/((float)dimX))*((float)BLANC));
               int  verte=(int)((((float)cy)/((float)dimX))*((float)BLANC));
               int  bleue=(int)((((float)(cx+cy))/((float)dimX))*((float)BLANC));

               if   ((cy%20) == 0)
                    {
                    rouge=verte=bleue=BLANC;
                    }
               else
                    {
                    }

               if   ((cx%40) == 0)
                    {
                    rouge=verte=bleue=BLANC;
                    }
               else
                    {
                    }

               XPutPixel(Simage,cx,cy,(((rouge*COULEURS) + verte)*COULEURS + bleue));
               }
          }
/*************************************************************************************************************************************/
/*                                                                                                                                   */
/*        E N V O I   D E   L ' I M A G E                                                                                            */
/*                                                                                                                                   */
/*************************************************************************************************************************************/
     fprintf(stderr,"\n Simage=%d",Simage);
     fprintf(stderr,"\n display=%d",display);
     fprintf(stderr,"\n visual_courant=%d",visual_courant);
     fprintf(stderr,"\n width=%d",Simage->width);
     fprintf(stderr,"\n xoffset=%d",Simage->xoffset);
     fprintf(stderr,"\n format=%d",Simage->format);
     fprintf(stderr,"\n data=%d",Simage->data);
     fprintf(stderr,"\n byte_order=%d",Simage->byte_order);
     fprintf(stderr,"\n bitmap_unit=%d",Simage->bitmap_unit);
     fprintf(stderr,"\n bitmap_bit_order=%d",Simage->bitmap_bit_order);
     fprintf(stderr,"\n bitmap_pad=%d",Simage->bitmap_pad);
     fprintf(stderr,"\n depth=%d",Simage->depth);
     fprintf(stderr,"\n bytes_per_line=%d",Simage->bytes_per_line);
     fprintf(stderr,"\n bits_per_pixel=%d",Simage->bits_per_pixel);
     fprintf(stderr,"\n red_mask=%x",Simage->red_mask);
     fprintf(stderr,"\n green_mask=%x",Simage->green_mask);
     fprintf(stderr,"\n blue_mask=%x",Simage->blue_mask);

     XPutImage(display,fenetre_a_ouvrir,contexte_graphique,Simage,offx,offy,dx,dy,dimX,dimY);
     sleep(4);
     XDestroyImage(Simage);
/*************************************************************************************************************************************/
/*                                                                                                                                   */
/*        F E R M E T U R E   D E   L A   F E N E T R E                                                                              */
/*                                                                                                                                   */
/*************************************************************************************************************************************/
     XUnmapWindow(display,fenetre_a_ouvrir);
     EVENEMENT(UnmapNotify);
     XDestroyWindow(display,fenetre_a_ouvrir);
     EVENEMENT(DestroyNotify);
     XSync(display,True);
     XCloseDisplay(display);
     }



Copyright © Jean-François Colonna, 2021-2021.
Copyright © CMAP (Centre de Mathématiques APpliquées) UMR CNRS 7641 / Ecole Polytechnique, 2021-2021.