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 / main.c < prev    next >
C/C++ Source or Header  |  1997-11-17  |  9KB  |  339 lines

  1. #include "enlightenment.h"
  2.  
  3. /* this just cleans up after dead child processes.... */
  4. void Reap() {
  5.     int status;
  6.  
  7.     while (wait3(&status, WNOHANG, 0)>0);
  8. }
  9.  
  10. void err(char *s) {
  11.     fprintf(stderr,s);
  12. }
  13.  
  14. void X_Connect(void)
  15. {
  16.    XWindowAttributes xwa;
  17.    
  18.    disp=XOpenDisplay(NULL); /* open the display connection */
  19.    if (disp==NULL) /* if we couldn't open the connection.. error */
  20.      {
  21.     fprintf(stderr,"Enlightenment: cannot connect to Display \n");
  22.     EExit(1); /* bad error.. we have to exit */
  23.      }
  24.    /* for recoverable X errors, runt his routine */
  25.    XSetErrorHandler((XErrorHandler)handleError);
  26.    /* for fatal X errors.. runthis routine */
  27.    XSetIOErrorHandler((XIOErrorHandler)handleIOError);
  28.    /* now get some imperative info about the display */
  29.    screen=DefaultScreen(disp); /* the screen number */
  30.    root=DefaultRootWindow(disp); /* the root window id */
  31.    visual=DefaultVisual(disp,screen); /* the visual type */
  32.    depth=DefaultDepth(disp,screen); /* the depth of the screen in bpp */
  33.    scr_width=DisplayWidth(disp,screen); /* the width of the screen */
  34.    scr_height=DisplayHeight(disp,screen); /* height of the screen */
  35.    XSelectInput(disp,root,SubstructureNotifyMask|ButtonPressMask|
  36.         ButtonReleaseMask|EnterWindowMask|LeaveWindowMask|
  37.         ButtonMotionMask|PropertyChangeMask|SubstructureRedirectMask|
  38.         KeyPressMask|KeyReleaseMask|PointerMotionMask);
  39.    if (!debug_mode) UnmapClients(1);
  40.    /* set up an apporpriate event mask */
  41.    if (XGetWindowAttributes(disp,root,&xwa))
  42.      {
  43.     if (xwa.colormap) root_cmap=xwa.colormap;
  44.     else root_cmap=0;
  45.      }
  46.    else root_cmap=0;
  47. }
  48.  
  49. Image *LoadImage(ImlibData *d, char *file, ImColor *icl)
  50. {
  51.    Image *im;
  52.    char ss[4096];
  53.    unsigned char *ptr;
  54.    int x,y;
  55.    int a,b,c;
  56.    int load_err;
  57.    
  58.    im=NULL;
  59.    load_err=0;
  60.    ss[0]=0;
  61.    if (!file) return NULL;
  62.    if (!d) return NULL;
  63.    if (file[0]!='/') 
  64.      {
  65.          sprintf(ss,".enlightenment/%s",file);
  66.          if (!exists(ss))
  67.            {
  68.           sprintf(ss,"%s/.enlightenment/%s",getenv("HOME"),file);
  69.           if (!exists(ss))
  70.             {
  71.           if (Theme_Path[0])
  72.             sprintf(ss,"%s/%s",Theme_Path,file);
  73.           else
  74.             strcpy(ss,file);
  75.           if (!exists(ss))
  76.             {
  77.                Alert("Allrighty then...\nThat damned %s file does not exits!\nYou're going to be in for a nasty day!\n",file);
  78.                load_err=1;
  79.             }
  80.            }
  81.       }
  82.      }
  83.    else 
  84.      strcpy(ss,file);
  85.    if (!load_err) im=ImlibLoadImage(d,ss,icl);
  86.    if ((!im)&&(!load_err))
  87.      Alert("Holy Carumba! Geez oh crikey!\nI couldn't for the life of me load %s\nI'll probably run screaming off in a fit of rage now...\n",ss);
  88.    if (!im)
  89.      {
  90.     
  91.     im=malloc(sizeof(Image));
  92.     im->rgb_width=50;
  93.     im->rgb_height=50;
  94.     im->rgb_data=malloc(3*im->rgb_width*im->rgb_height);
  95.     im->width=0;
  96.     im->height=0;
  97.     im->shape_color.r=-1;
  98.     im->shape_color.g=-1;
  99.     im->shape_color.b=-1;
  100.     im->pixmap=0;
  101.     im->shape_mask=0;
  102.     ptr=im->rgb_data;
  103.     a=rand()%256;
  104.     b=rand()%256;
  105.     c=rand()%256;
  106.     for (y=0;y<im->rgb_height;y++)
  107.       {
  108.          for (x=0;x<im->rgb_width;x++)
  109.            {
  110.           ptr[0]=rand()%a;
  111.           ptr[1]=rand()%b;
  112.           ptr[2]=rand()%c;
  113.           ptr+=3;
  114.            }
  115.       }
  116.      }
  117.    return im;
  118. }
  119.  
  120. void main(int argc, char **argv) {
  121.    char s[1024];
  122.    XEvent event;
  123.    listhead *l;
  124.    struct sigaction sa;
  125.    int i,j;
  126.    unsigned char buf[16];
  127.    FILE *f;
  128.  
  129.    restart=0;
  130.    imd=NULL;
  131.    timer_mode=0;
  132.    srand(time(NULL));
  133.    nodel=0;
  134.    debug_mode=0;
  135.    Theme_Tar_Ball[0]=0;
  136.    Theme_Name[0]=0;
  137.    Theme_Path[0]=0;
  138.    states=0;
  139.    statefile[0]=0;
  140.    AlreadyWinListed=0;
  141.    
  142.    sa.sa_handler = Reap;
  143.    sa.sa_flags = 0;
  144.    sigemptyset (&sa.sa_mask);
  145.    sigaction(SIGCHLD, &sa, (struct sigaction *)0);
  146.    /* when child processes die.. clean up after them */
  147.    sa.sa_handler = SIG_IGN;
  148.    sigemptyset(&sa.sa_mask);
  149.    sigaction(SIGALRM, &sa, (struct sigaction *)0);
  150.    /* no timer iterrupt signal yet */
  151.    sa.sa_handler = Segfault;
  152.    sigemptyset (&sa.sa_mask);
  153.    sigaction(SIGSEGV, &sa, (struct sigaction *)0);
  154.    /* Segmentation Fault.... capture it*/
  155.    sa.sa_handler = Hup;
  156.    sigemptyset (&sa.sa_mask);
  157.    sigaction(SIGHUP, &sa, (struct sigaction *)0);
  158.    /* Hup signal sent... respond to it */
  159.    sa.sa_handler = Quit;
  160.    sigemptyset (&sa.sa_mask);
  161.    sigaction(SIGQUIT, &sa, (struct sigaction *)0);
  162.    /* Quit signal... */
  163.    sa.sa_handler = Bus;
  164.    sigemptyset (&sa.sa_mask);
  165.    sigaction(SIGBUS, &sa, (struct sigaction *)0);
  166.    /* Bus Error... Wohooooo! */
  167.    sa.sa_handler = Illegal;
  168.    sigemptyset (&sa.sa_mask);
  169.    sigaction(SIGILL, &sa, (struct sigaction *)0);
  170.    /* Illegal instruction.. ??? Geez.. how did we get this one! */
  171.    sa.sa_handler = Abort;
  172.    sigemptyset (&sa.sa_mask);
  173.    sigaction(SIGABRT, &sa, (struct sigaction *)0);
  174.    /* Abort signal recieved */
  175.    sa.sa_handler = FloatError;
  176.    sigemptyset (&sa.sa_mask);
  177.    sigaction(SIGFPE, &sa, (struct sigaction *)0);
  178.    /* Floating Point Error */
  179.    sa.sa_handler = Terminate;
  180.    sigemptyset (&sa.sa_mask);
  181.    sigaction(SIGTERM, &sa, (struct sigaction *)0);
  182.    /* Terminate Signal......... */
  183.    sa.sa_handler = User1;
  184.    sigemptyset (&sa.sa_mask);
  185.    sigaction(SIGUSR1, &sa, (struct sigaction *)0);
  186.    /* User Signal 1 */
  187.    sa.sa_handler = User2;
  188.    sigemptyset (&sa.sa_mask);
  189.    sigaction(SIGUSR2, &sa, (struct sigaction *)0);
  190.    /* User signal 2 */
  191.    for (j=1;j<argc;j++)
  192.      {
  193.     printf("%s\n",argv[j]);
  194.     if (!strcmp("-debug",argv[j])) debug_mode=1;
  195.     else if (!strcmp("-restart",argv[j])) restart=1;
  196.     else if ((!strcmp("-theme",argv[j]))&&((argc-j)>1)) 
  197.       {
  198.          strcpy(Theme_Tar_Ball,argv[j+1]);
  199.          strcpy(Theme_Name,Theme_Tar_Ball);
  200.          j++;
  201.       }
  202.     else if ((!strcmp("-statefile",argv[j]))&&((argc-j)>1))
  203.       {
  204.          states=1;
  205.          strcpy(statefile,argv[j+1]);
  206.          j++;
  207.       }
  208.      }
  209.    if (!Theme_Tar_Ball[0])
  210.      {
  211.     strcpy(Theme_Tar_Ball,"DEFAULT");
  212.     strcpy(Theme_Name,"DEFAULT");
  213.      }
  214.    if (Theme_Tar_Ball[0])
  215.      {
  216.     i=getpid();
  217.     if (!exists(Theme_Tar_Ball))
  218.       {
  219.          sprintf(s,"%s%s",THEMES_DIR,Theme_Tar_Ball);
  220.          strcpy(Theme_Tar_Ball,s);
  221.       }
  222.     if (exists(Theme_Tar_Ball))
  223.       {
  224.          if (isfile(Theme_Tar_Ball))
  225.            {
  226.           sprintf(Theme_Path,"/tmp/enlightenment_theme_%i",i);
  227.           md(Theme_Path);
  228.           f=fopen(Theme_Tar_Ball,"r");
  229.           if (f)
  230.             {
  231.                fread(&buf[0],1,1,f);
  232.                fread(&buf[1],1,1,f);
  233.                fclose (f);
  234.                if ((buf[0]==31)&&(buf[1]==139))
  235.              {
  236.                 /*gzipped tarball*/
  237.                 /*sprintf(s,"gzip -d -c < %s | tar -xf - -C %s",Theme_Tar_Ball,Theme_Path);*/
  238.                 sprintf(s,"gzip -d -c < %s | (cd %s ; tar -xf -)",Theme_Tar_Ball,Theme_Path);
  239.              }
  240.                else
  241.              {
  242.                 /*vanilla tarball*/
  243.                 /*sprintf(s,"tar -xf - -C %s < %s",Theme_Path,Theme_Tar_Ball);*/
  244.                 sprintf(s,"(cd %s ; tar -xf %s",Theme_Path,Theme_Tar_Ball);
  245.              }
  246.                Do_Exec(s);
  247.                wait(&i);
  248.             }
  249.            }
  250.          else if (isdir(Theme_Tar_Ball))
  251.            {
  252.           nodel=1;
  253.           strcpy(Theme_Path,Theme_Tar_Ball);
  254.           Theme_Tar_Ball[0]=0;
  255.            }
  256.          else
  257.            {
  258.           Theme_Path[0]=0;
  259.           Theme_Tar_Ball[0]=0;
  260.            }
  261.       }
  262.     else
  263.       {
  264.          Theme_Path[0]=0;
  265.          Theme_Tar_Ball[0]=0;
  266.       }
  267.      }
  268.    argv1=malloc(strlen(argv[0])+1);
  269.    strcpy(argv1, argv[0]);
  270.    
  271.    X_Connect(); /* connect to the display nominated */
  272.    imd=ImlibInit(disp); /* Inititalise Imlib..... */
  273.    if (!imd) /* uh oh.. problems... imlib had a bad day... */
  274.      { 
  275.     fprintf(stderr,"FATAL.... cannot initialise Imlib!!! Outa here!\n");
  276.     Alert("FATAL ERROR!\n Cannot initialise Imlib!\n Quitting.\n");
  277.     EExit(1);
  278.      }
  279.    depth=imd->x.depth; 
  280.    visual=imd->x.visual;
  281.    l=ListInit(); /* initialise the window list */
  282.    WM_STATE=XInternAtom(disp,"WM_STATE", False);
  283.    global_l=l;
  284.    InitConfig();
  285.    LoadConfig("MAIN");
  286.    Kill_StatusWin();
  287.    newicon.ewin=NULL;
  288.    HighlightDeskInfo(0,1);
  289.    SetRoot();
  290.    MapDeskInfo();
  291.    MapClients(l);
  292. /*   if (restart) ConformAllEwinToState();*/
  293.    restart=0;
  294.    while(1) /* loop continuously and reacting to events */
  295.      {
  296.     if (global_killewin)
  297.       {
  298.          ListDelWinID(l,global_killewin->frame_win);
  299.          global_killewin=NULL;
  300.       }
  301.     if ((ifb.on)&&(!raisewin)&&(!newicon.ewin))
  302.       {
  303.          struct sigaction sa;
  304.          struct itimerval tv1,tv2;
  305.          
  306.          if (timer_mode < TIMER_INFOBOX) {
  307.            for (getitimer(ITIMER_REAL, &tv1);
  308.             tv1.it_value.tv_sec != 0 || tv1.it_value.tv_usec != 0
  309.               || tv1.it_interval.tv_sec != 0 
  310.               || tv1.it_interval.tv_usec != 0;
  311.             getitimer(ITIMER_REAL, &tv1));
  312.          }
  313.          timer_mode = TIMER_INFOBOX;
  314.          sa.sa_handler = InfoBox;
  315.          sa.sa_flags = 0;
  316.          sigemptyset (&sa.sa_mask);
  317.          tv1.it_value.tv_sec=(unsigned long)(ifb.timeout*1000 / 1000000);
  318.          tv1.it_value.tv_usec=(long)(ifb.timeout*1000 % 1000000);
  319.          tv2.it_value.tv_sec=(unsigned long)(ifb.timeout*1000 / 1000000);
  320.          tv2.it_value.tv_usec=(long)(ifb.timeout*1000 % 1000000);
  321.          tv1.it_interval.tv_sec=0;
  322.          tv1.it_interval.tv_usec=0;
  323.          tv2.it_interval.tv_sec=0;
  324.          tv2.it_interval.tv_usec=0;
  325.          setitimer(ITIMER_REAL,&tv1,&tv2);
  326.          sigaction(SIGALRM,&sa,(struct sigaction *)0);
  327.       }
  328.     ifb.nodo=0;
  329.     XNextEvent(disp,&event); /* Get the next event to the display */
  330.     ifb.nodo=1;
  331.     XGrabServer(disp); /* grab the server.. dont let clients play around */
  332.     handleEvent(&event,l);
  333.     XSync(disp,0x00);
  334.     XUngrabServer(disp);
  335.      }
  336.    /* EExit(0); */
  337. }
  338.  
  339.