home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Enlightenment / enl_BETA-0.13.src.tar.gz / enl_BETA-0.13.src.tar / enl-0.13 / states.c < prev    next >
C/C++ Source or Header  |  1997-11-18  |  8KB  |  398 lines

  1. #include "enlightenment.h"
  2.  
  3. void WriteAllWindowStates() {
  4.     char s[FILENAME_MAX];
  5.     struct list *ptr;
  6.     int i;
  7.  
  8.     if (restart) {
  9.         strcpy(s,statefile);
  10.         strcpy(statefile,".E-restart_states");
  11.     }
  12.     DeleteWindowStates();
  13.     ptr=global_l->first;
  14.     i=0;
  15.     while (ptr) {
  16.         AppendWindowState(ptr->win);
  17.         ptr=ptr->next;
  18.     }
  19.     if (restart) 
  20.         strcpy(statefile,s);
  21. }
  22.  
  23. void DeleteWindowStates() {
  24.     char s[FILENAME_MAX];
  25.     char *home;
  26.  
  27.     if (!statefile[0]) 
  28.         strcpy(statefile,".E-window_states");
  29.     home=getenv("HOME");
  30.     sprintf(s,"%s/.enlightenment",home);
  31.     if (!exists(s)) 
  32.         md(s);
  33.     else if (statefile[0]=='/') 
  34.         strcpy (s,statefile);
  35.     else 
  36.         sprintf(s,"%s/.enlightenment/%s",home,statefile);
  37.     if (exists(s)) 
  38.         rm(s);
  39. }
  40.  
  41. void AppendWindowState(EWin *ewin) {
  42.     char s[FILENAME_MAX];
  43.     char ss[FILENAME_MAX];
  44.     char *home;
  45.     FILE *f;
  46.     int argc;
  47.     char **argv;
  48.     int i;
  49.     XClassHint xch;
  50.     int num;
  51.  
  52.     if (!statefile[0]) 
  53.         strcpy(statefile,".E-window_states");
  54.     home=getenv("HOME");
  55.     sprintf(s,"%s/.enlightenment",home);
  56.     if (!exists(s)) 
  57.         md(s);
  58.     if (restart) 
  59.         sprintf(s,"%s/.enlightenment/.E-restart_states",home);
  60.     else if (statefile[0]=='/') 
  61.         strcpy (s,statefile);
  62.     else 
  63.         sprintf(s,"%s/.enlightenment/%s",home,statefile);
  64.     num=0;
  65.     if (exists(s)) {
  66.         f=fopen(s,"r");
  67.         while (fgets(ss,sizeof(ss),f)) 
  68.             num++;
  69.         if (num>7) 
  70.             num/=8;
  71.         fclose (f);
  72.     }
  73.     f=fopen(s,"a");
  74.     ewin->state_entry=num;
  75.     if (!f) 
  76.         return;
  77. /* format on a line:
  78.  * Shell_Command OR - (char)
  79.  * Title  OR -        (char)
  80.  * Resource_Name OR - (char)
  81.  * Window_ID          (int)
  82.  * X Y                (int) (int)
  83.  * Width Height       (int) (int)
  84.  * Desktop_Number     (int)
  85.  * Iconified_Sate     (int: 0/1)
  86.  */
  87.     argv=NULL;
  88.     argc=0;
  89.     if (XGetCommand(disp,ewin->client_win,&argv,&argc)) {
  90.         for (i=0;i<argc;i++) 
  91.             if (argv[i]) 
  92.                 fprintf(f,"%s ",argv[i]);
  93.         if (argc!=0) 
  94.             fprintf(f,"\n");
  95.         else 
  96.             fprintf(f,"-\n");
  97.         if ((argv!=NULL)&&(argc>0)) 
  98.             XFreeStringList(argv);
  99.     } else
  100.         fprintf(f,"-\n");
  101.     fprintf(f,"%s\n",ewin->title);
  102.     xch.res_name=NULL;
  103.     xch.res_class=NULL;
  104.     if (XGetClassHint(disp,ewin->client_win,&xch)) {
  105.         if (xch.res_name) 
  106.             fprintf(f,"%s\n",xch.res_name);
  107.         else 
  108.             fprintf(f,"-\n");
  109.         if (xch.res_name) 
  110.             XFree(xch.res_name);
  111.         if (xch.res_class) 
  112.             XFree(xch.res_class);
  113.     } else
  114.         fprintf(f,"-\n");
  115.     fprintf(f,"%i\n",(int)ewin->client_win);
  116.     fprintf(f,"%i %i\n",ewin->frame_x,ewin->frame_y);
  117.     fprintf(f,"%i %i\n",ewin->client_width,ewin->client_height);
  118.     fprintf(f,"%i\n",ewin->desk);
  119.     if (ewin->state&ICONIFIED) 
  120.         fprintf(f,"1\n");
  121.     else 
  122.         fprintf(f,"0\n");
  123.     fclose(f);
  124. }
  125.  
  126. void ConformAllEwinToState() {
  127.     struct list *ptr;
  128.     int i;
  129.  
  130.     ptr=global_l->first;
  131.     i=0;
  132.     while (ptr) {
  133.         ConformEwinToState(ptr->win);
  134.         ptr=ptr->next;
  135.     }
  136. }
  137.  
  138. int StateEntryUsed(int num) {
  139.     struct list *ptr;
  140.     int i;
  141.  
  142.     if (restart) 
  143.         return 0;
  144.  
  145.     ptr=global_l->first;
  146.     i=0;
  147.     while (ptr) {
  148.         if (ptr->win->state_entry==num) 
  149.             return 1;
  150.         ptr=ptr->next;
  151.     }
  152.     return 0;
  153. }
  154.  
  155. void DeleteEwinState(EWin *ewin) {
  156.     char s[FILENAME_MAX];
  157.     char s1[FILENAME_MAX];
  158.     char ss[FILENAME_MAX];
  159.     char *home;
  160.     FILE *f;
  161.     FILE *ff;
  162.     int i;
  163.  
  164.     if (ewin->state_entry<0) 
  165.         return;
  166.     if (!statefile[0]) 
  167.         strcpy(statefile,".E-window_states");
  168.     home=getenv("HOME");
  169.     sprintf(s,"%s/.enlightenment",home);
  170.     if (!exists(s)) 
  171.         md(s);
  172.     sprintf(s1,"%s/.enlightenment/%s",home,statefile);
  173.     f=fopen(s1,"r");
  174.     sprintf(ss,"/tmp/enl_tmp_%i",getpid());
  175.     ff=fopen(ss,"w");
  176.     if (!f) 
  177.         return;
  178.     if (!ff) 
  179.         return;
  180.     for (i=0;i<(8*(ewin->state_entry));i++) {
  181.         if (!fgets(s,sizeof(s),f)) {
  182.             fclose(f);
  183.             fclose (ff);
  184.             return;
  185.         }
  186.         fputs(s,ff);
  187.     }
  188.     for (i=0;i<8;i++) {
  189.         if (!fgets(s,sizeof(s),f)) {
  190.             fclose(f);
  191.             fclose (ff);
  192.             cp(ss,s1);
  193.             rm(ss);
  194.             return;
  195.         }
  196.     }
  197.     while(fgets(s,sizeof(s),f))
  198.         fputs(s,ff);
  199.     fclose (ff);
  200.     fclose(f);
  201.     cp(ss,s1);
  202.     rm(ss);
  203. }
  204.  
  205. void ModifyEwinState(EWin *ewin) {
  206.     char s[FILENAME_MAX];
  207.     char s1[FILENAME_MAX];
  208.     char ss[FILENAME_MAX];
  209.     char sss[FILENAME_MAX];
  210.     char *home;
  211.     FILE *f;
  212.     FILE *ff;
  213.     int i;
  214.  
  215.     if (ewin->state_entry<0) {
  216.         AppendWindowState(ewin);
  217.         return;
  218.     }
  219.     if (!statefile[0]) 
  220.         strcpy(statefile,".E-window_states");
  221.     home=getenv("HOME");
  222.     sprintf(s,"%s/.enlightenment",home);
  223.     if (!exists(s)) 
  224.         md(s);
  225.     if (statefile[0]=='/') 
  226.         strcpy(s1,statefile);
  227.     else 
  228.         sprintf(s1,"%s/.enlightenment/%s",home,statefile);
  229.     sprintf(ss,"/tmp/enl_tmp_%i",getpid());
  230.     f=fopen(s1,"r");
  231.     ff=fopen(ss,"w");
  232.     if (!ff) 
  233.         return;
  234.     if (f) {
  235.         for (i=0;i<(8*ewin->state_entry);i++) {
  236.             if (!fgets(s,sizeof(s),f)) {
  237.                 fclose(f);
  238.                 i=ewin->state_entry*8+1;
  239.                 f=NULL;
  240.             } else
  241.                 fputs(s,ff);
  242.         }
  243.     }
  244.     fclose (ff);
  245.     strcpy(sss,statefile);
  246.     strcpy(statefile,ss);
  247.     AppendWindowState(ewin);   
  248.     strcpy(statefile,sss);
  249.     ff=fopen(ss,"a");
  250.     for (i=0;i<8;i++) {
  251.         if (!fgets(s,sizeof(s),f)) {
  252.             fclose(f);
  253.             i=8;
  254.             f=NULL;
  255.         }
  256.     }
  257.     if (f) 
  258.         while(fgets(s,sizeof(s),f)) 
  259.             fputs(s,ff);
  260.     fclose (ff);
  261.     if (f) 
  262.         fclose(f);
  263.     cp(ss,s1);
  264.     rm(ss);
  265. }
  266.  
  267. void ConformEwinToState(EWin *ewin) {
  268.     char s[FILENAME_MAX];
  269.     char cmd[4096];
  270.     char name[4096];
  271.     char s_title[4096];
  272.     char s_cmd[4096];
  273.     char s_name[4096];
  274.     int s_winid;
  275.     int s_x,s_y;
  276.     int s_width,s_height;
  277.     int s_desk;
  278.     int s_iconified;
  279.     char *home;
  280.     FILE *f;
  281.     int argc;
  282.     char **argv;
  283.     int i;
  284.     XClassHint xch;
  285.     unsigned char match;
  286.     int num;
  287.  
  288.     if (!statefile[0]) 
  289.         strcpy(statefile,".E-window_states");
  290.     home=getenv("HOME");
  291.     sprintf(s,"%s/.enlightenment",home);
  292.     if (!exists(s)) 
  293.         md(s);
  294.     if (restart) 
  295.         sprintf(s,"%s/.enlightenment/.E-restart_states",home);
  296.     else if (statefile[0]=='/') 
  297.         strcpy (s,statefile);
  298.     else 
  299.         sprintf(s,"%s/.enlightenment/%s",home,statefile);
  300.     f=fopen(s,"r");
  301.     if (!f) 
  302.         return;
  303.     num=0;
  304.     argv=NULL;
  305.     argc=0;
  306.     cmd[0]=0;
  307.     name[0]=0;
  308.     if (XGetCommand(disp,ewin->client_win,&argv,&argc)) {
  309.         for (i=0;i<argc;i++) 
  310.             if (argv[i]) 
  311.                 sprintf(cmd,"%s ",argv[i]);
  312.             else 
  313.                 sprintf(cmd,"-");
  314.         if ((argv!=NULL)&&(argc>0)) 
  315.             XFreeStringList(argv);
  316.     } else
  317.         sprintf(cmd,"-");
  318.     xch.res_name=NULL;
  319.     xch.res_class=NULL;
  320.     if (XGetClassHint(disp,ewin->client_win,&xch)) {
  321.         if (xch.res_name) 
  322.             sprintf(name,"%s",xch.res_name);
  323.         else 
  324.             sprintf(name,"-");
  325.         if (xch.res_name) 
  326.             XFree(xch.res_name);
  327.         if (xch.res_class) 
  328.             XFree(xch.res_class);
  329.     } else
  330.         sprintf(name,"-");
  331.     while (1) {
  332.         for (i=0;i<8;i++) {
  333.             if (!fgets(s,sizeof(s),f)) {
  334.                 fclose(f);
  335.                 return;
  336.             }
  337.             s[strlen(s)-1]=0;
  338.             if (i==0) 
  339.                 strcpy(s_cmd,s);
  340.             else if (i==1) 
  341.                 strcpy(s_title,s);
  342.             else if (i==2) 
  343.                 strcpy(s_name,s);
  344.             else if (i==3) 
  345.                 sscanf(s,"%i",&s_winid);
  346.             else if (i==4) 
  347.                 sscanf(s,"%i %i",&s_x,&s_y);
  348.             else if (i==5) 
  349.                 sscanf(s,"%i %i",&s_width,&s_height);
  350.             else if (i==6) 
  351.                 sscanf(s,"%i",&s_desk);
  352.             else if (i==7) 
  353.                 sscanf(s,"%i",&s_iconified);
  354.         }
  355.         match=0;
  356.         if (restart) {
  357.             if (ewin->client_win==(Window)s_winid) 
  358.                 match=1;
  359.         } else {
  360.             if ((s_cmd[0]!='-')&&(cmd[0]!='-')) {
  361.                 if (!strcmp(s_cmd,cmd)) 
  362.                     match=1;
  363.             } else {
  364.                 if ((s_name[0]!='-')&&(name[0]!='-')) {
  365.                     if (!strcmp(s_name,name)) 
  366.                         match=1;
  367.                 } else {
  368.                     if (!strcmp(s_title,ewin->title)) 
  369.                         match=1;
  370.                 }
  371.             }
  372.         }
  373.         if ((match)&&(!StateEntryUsed(num))) {
  374.             if (!restart) 
  375.                 ewin->state_entry=num;
  376.             XSync(disp,False);
  377.             ModifyEWin(ewin,s_x,s_y,s_width,s_height);
  378.             if (s_iconified) {
  379.                 Do_IconifyWin(ewin);
  380.                 while (newicon.ewin) 
  381.                     usleep(100000); /* wait for iconification */
  382.                 ewin->desk=s_desk;
  383.             } else {
  384.                 if (s_desk>=0) 
  385.                     MoveEwinToDesk(ewin,s_desk);
  386.                 else 
  387.                     ewin->desk=s_desk;
  388.             }
  389.             MimickEwin(ewin);
  390.             XSync(disp,False);
  391.             fclose(f);
  392.             return;
  393.         }
  394.         num++;
  395.     }
  396. /*     fclose(f); */
  397. }
  398.