Re: gui de xrandr

Bernard Siaud alias Troumad <[email protected]>
Newsgroups gmane.linux.mandrake.expert.french
Message-ID <[email protected]>
Une version complètée de mon petit programme :)
Ok jipe : ce n'est pas du bash ! Chacun ses facilités ;)

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


struct mode
{
 char nom[16];
 struct mode * suiv;
};


struct ecran
{
 char nom[16];
 char mode_ac[16];
 char mode_op[16];
 unsigned short x,y;
 struct mode * mode;
 struct ecran * suiv;
};

int main()
{
 FILE * sortie;
 char ch[512],nom[16];
 unsigned short i,j;
 unsigned short xv,yv,xp,yp;
 unsigned short x,y;
 struct ecran * debut = NULL, *actuelle;
 struct mode * mode;
 
 system("/usr/bin/xrandr > /tmp/xrandr.sortie");
 
 sortie=fopen("/tmp/xrandr.sortie","r");
 
 while( fgets(ch,127,sortie)!=NULL)
 {
  /* printf("%hu => ",ch[0]);
  printf("%s",ch); */
  if (ch[0]!=' ') // Pas les modes
  {
   sscanf(ch,"%s",nom);
   if (strcmp(nom,"Screen")!=0) // Pas la ligne de présentation de la carte
   {
    if (debut==NULL)
    {
     actuelle=(struct ecran *)malloc(sizeof(struct ecran));
     debut=actuelle;
    }
    else
    {
     actuelle->suiv=(struct ecran *)malloc(sizeof(struct ecran));
     actuelle=actuelle->suiv;
    }
    actuelle->suiv=NULL;
    actuelle->x=0;
    strncpy(actuelle->nom,nom,15);
    actuelle->mode_ac[0]=0;
    actuelle->mode_op[0]=0;
   
   }
  }
  else // Les modes
  {
   if (actuelle->mode==NULL)
   {
    mode=(struct mode *)malloc(sizeof(struct mode));
    actuelle->mode=mode;
   }
   else
   {
    mode->suiv=(struct mode *)malloc(sizeof(struct mode));
    mode=mode->suiv;
   }
   mode->suiv=NULL;
  
   for(i=0;ch[i]==' ';i++);
   sscanf(ch+i,"%s",mode->nom);
   if (strstr(ch+i,"+")!=NULL)
   {
    strncpy(actuelle->mode_op,mode->nom,15);
    sscanf(mode->nom,"%hux%hu",&actuelle->x,&actuelle->y);
   }
   if (strstr(ch+i,"*")!=NULL)
   {
    strncpy(actuelle->mode_ac,mode->nom,15);
    sscanf(mode->nom,"%hux%hu",&actuelle->x,&actuelle->y);
   }
  }
 }
 
 printf("Configurations possibles pour les écrans\n");
 actuelle=debut;
 do
 {
  if (actuelle->x!=0)
  {
   i=0;
   printf ("\n Liste des modes pour l'écran %s\n",actuelle->nom);
   mode=actuelle->mode;
   do
   {
    i++;
    printf("  %hu) %s %s 
%s\n",i,mode->nom,(strcmp(actuelle->mode_ac,mode->nom)==0)?", mode 
actuel":" ",(strcmp(actuelle->mode_op,mode->nom)==0)?", mode optimal":" ");
    if (strcmp(actuelle->mode_ac,mode->nom)==0) j=i;
    mode=mode->suiv;
   }
   while (mode!=NULL);
  
   do
   {
    printf("Quel choix voulez-vous ? de 0 à %hu (0 pour conserver le 
mode actuel) : ",i);
    scanf("%hu",&x);
   }
   while (x>i);
   if (x==0) x=j;

   i=0;
   mode=actuelle->mode;
   do
   {
    i++;
    if (i!=x) mode=mode->suiv;
   }
   while (i!=x);
  
   if (strcmp(mode->nom,actuelle->mode_ac)!=0)
   {
    sprintf(ch,"xrandr --output %s --mode %s",actuelle->nom,mode->nom);
    system(ch);
    strcpy(actuelle->mode_ac,mode->nom);
   }
   sscanf(actuelle->mode_ac,"%hux%hu",&actuelle->x,&actuelle->y);
  }
  actuelle=actuelle->suiv;
 }
 while (actuelle != NULL);
 
 
 
 printf("\n\nChoix de vos écrans\n");
 actuelle=debut;
 i=0;
 do
 {
  if (actuelle->x!=0)
  {
   i++;
   printf ("%hu) sortie %s mode %s\n",i,actuelle->nom,actuelle->mode);
  }
  actuelle=actuelle->suiv;
 }
 while (actuelle != NULL);
 
 if (i>1)
 {
  printf("Choix de l'écran du vidéo projecteur de 0 à %hu (0 pour aucun) 
: ",i);
  scanf("%hu",&j);
 
  while (j>i)
  {
   printf("Mauvais choix : %hu !\nChoix de l'écran du vidéo projecteur 
de 0 à %hu (0 pour aucun) : ",j,i);
   scanf("%hu",&j);
  }
  if (j!=0)
  {
   if (i>2)
   {
    printf("Choix de l'écran du portable (de 1 à %hu, mais pas %hu)",i,j);
    scanf("%hu",&x);
    while (x>i || x==0 || x==j)
    {
     printf("Mauvais choix : %hu !\nChoix de l'écran du vidéo projecteur 
(de 1 à %hu pas %hu)",x,i,j);
    }
   }
   else
   {
    if (j==1) x=2;
    else x=1;
   }
  
   actuelle=debut;
   i=0;
   do
   {
    /* printf("Retour\n"); */
    /* printf("%hu %s\n",i,actuelle->nom); */
    if (actuelle->x!=0)
    {
     i++;
     //printf("i=%hu j=%hu et x=%hu\n",i,j,x);
     if (i==j) // vidéoprojecteur
     {
      xv=actuelle->x;
      yv=actuelle->y;
     }
     else if (i==x) // écran
     {
      xp=actuelle->x;
      yp=actuelle->y;
      strcpy(nom,actuelle->nom);
     }
    }
    actuelle=actuelle->suiv;
   }
   while (actuelle!=NULL);
  
   // printf("Fin\n");
   sprintf(ch,"xrandr  --output %s --pos 0x0 --panning 0x0 --scale 1x1 
--transform 1,0,0,0,1,0,0,0,1 --output LVDS1 --pos 0x0 --panning 0x0 
--scale 1x1   --transform 1,0,0,0,1,0,0,0,1",nom);
   //printf("%s\n",ch);
   system(ch);
   sprintf(ch,"xrandr --output %s --scale 1x1",nom); /* Je ne comprends 
pas pourquoi je dois mettre cette ligne ! Bug de xrandr ? */
   //printf("%s\n",ch);
   system(ch);
   sprintf(ch,"xrandr --output %s --scale 
%fx%f",nom,(float)xv/xp,(float)yv/yp);
   printf("%s\n",ch);
   system(ch);
   //printf("%s par xp=%hu, yp=%hu, xv=%hu et yv=%hu\n",nom,xp,yp,xv,yv);
  }
  else
   printf("Aucun vidéo projecteur de sélectionné\n");
 }
 else
  printf("Je n'ai trouvé qu'une seule sortie écran\n");
 
 
}
message-footer.txt (text/plain, 150 B)
=================================
http://www.mandriva.com/linux/

Services & cert. hw on http://store.mandriva.com/
=================================
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.