Advertisement
iocoder

init.c

Apr 24th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.16 KB | None | 0 0
  1. /*
  2.  *        +----------------------------------------------------------+
  3.  *        | +------------------------------------------------------+ |
  4.  *        | |  Quafios Init Daemon.                                | |
  5.  *        | |  -> main() procedure.                                | |
  6.  *        | +------------------------------------------------------+ |
  7.  *        +----------------------------------------------------------+
  8.  *
  9.  * This file is part of Quafios 1.0.2 source code.
  10.  * Copyright (C) 2014  Mostafa Abd El-Aziz Mohamed.
  11.  *
  12.  * This program is free software: you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation, either version 3 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with Quafios.  If not, see <http://www.gnu.org/licenses/>.
  24.  *
  25.  * Visit http://www.quafios.com/ for contact information.
  26.  *
  27.  */
  28.  
  29. /* The init program :D
  30.  * This is my first out-of-the-kernel experience :D
  31.  */
  32.  
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <api/proc.h>
  36. #include <api/fs.h>
  37. #include <video/generic.h>
  38.  
  39. typedef struct {
  40.     char  id[2];
  41.     int   size;
  42.     short reserved1;
  43.     short reserved2;
  44.     int   offset;
  45. } __attribute__((packed)) bmp_file_header_t;
  46.  
  47. typedef struct {
  48.     int size;
  49.     int width;
  50.     int height;
  51.     short planes; /* number of color planes */
  52.     short depth; /* color depth */
  53.     int compression;
  54.     int imgsize;    /* size of the actual image data */
  55.     int horizontal; /* horizontal resolution */
  56.     int vertical;   /* vertical resolution */
  57.     int pallette;   /* number of colors in pallette */
  58.     int important;  /* important colors */
  59. } __attribute__((packed)) bmp_info_header_t;
  60.  
  61. FILE *vga, *bmp;
  62. int vga_width;
  63. int vga_height;
  64. unsigned char *tbuf = NULL;
  65. char font[256][16];
  66.  
  67. void draw_text(unsigned char *fbuf) {
  68.  
  69.     /* This is my first experience to write text on Quafios GUI! */
  70.     char *damn = "--Quafios Fractals 1.0.2--";
  71.     int i = 0, x = 50, y = 50, j = 0, k = 0;
  72.     int offset;
  73.     FILE *fnt;
  74.  
  75.     /* load the damn font */
  76.     fnt = fopen("/usr/share/font8x16.fon", "r");
  77.     fread(font, sizeof(font), 1, fnt);
  78.     fclose(fnt);
  79.  
  80.     /* draw characters on frame buffer */
  81.     while(damn[i]) {
  82.         for (j = 0; j < 8; j++)
  83.             for (k = 0; k < 16; k++) {
  84.                 if (font[damn[i]][k]&(1<<(7-j))) {
  85.                     fbuf[((y+k)*vga_width + (x+j))*3 + 0] = 0xFF;
  86.                     fbuf[((y+k)*vga_width + (x+j))*3 + 1] = 0xFF;
  87.                     fbuf[((y+k)*vga_width + (x+j))*3 + 2] = 0xFF;
  88.                 }
  89.             }
  90.         x += 9;
  91.         i++;
  92.     }
  93.  
  94. }
  95.  
  96. void parse_bmp(FILE *bmp, unsigned char *fbuf) {
  97.  
  98.     bmp_file_header_t header;
  99.     bmp_info_header_t info;
  100.     int i, j, k = 0;
  101.     unsigned char *rgb;
  102.  
  103.     printf("BMP Parser!\n");
  104.  
  105.     /* read bmp file header */
  106.     fread(&header, sizeof(header), 1, bmp);
  107.  
  108.     /* read bmp info header */
  109.     fread(&info, sizeof(info), 1, bmp);
  110.  
  111.     /* read pixel array */
  112.     rgb = malloc(info.imgsize);
  113.     fread(rgb, info.imgsize, 1, bmp);
  114.  
  115.     /* iterate on the colors */
  116.     for (i = info.height-1; i >= 0; i--) {
  117.         for(j = 0; j < info.width; j++) {
  118.             unsigned char r, g, b;
  119.             b = rgb[k++];
  120.             g = rgb[k++];
  121.             r = rgb[k++];
  122.             if (j < vga_width) {
  123.                 fbuf[(i*vga_width+j)*3+0] = b;
  124.                 fbuf[(i*vga_width+j)*3+1] = g;
  125.                 fbuf[(i*vga_width+j)*3+2] = r;
  126.             }
  127.         }
  128.         k+=2;
  129.     }
  130.  
  131. }
  132.  
  133. void update_screen(unsigned char *fbuf) {
  134.  
  135.     /* copy to tbuf */
  136.     int i;
  137.     for (i = 0; i < vga_width*vga_height*3; i++)
  138.         tbuf[i] = fbuf[i];
  139.  
  140.     /* draw program title */
  141.     draw_text(tbuf);
  142.  
  143.     /* write to VGA driver */
  144.     vga = fopen("/dev/vga", "r");
  145.     fwrite(tbuf, vga_width*vga_height*3, 1, vga);
  146.     fclose(vga);
  147.  
  148. }
  149.  
  150. void mandelbrot(unsigned char *fbuf) {
  151.  
  152.     /* plot Mandelbrot set, escape time algorithm */
  153.     int max_iteration = 1000; /* maximum amount of iterations */
  154.  
  155.     /* the c variable */
  156.     double *c_r, *c_i; /* real and imaginary parts of c */
  157.  
  158.     /* the z variable */
  159.     double *z_r, *z_i;
  160.  
  161.     /* temporary variable */
  162.     double tmp;
  163.  
  164.     /* counters: */
  165.     int i, j, k, l;
  166.  
  167.     /* color of the pixel */
  168.     int r, g, b;
  169.     int sum1, sum2;
  170.  
  171.     /* how does this work?
  172.      * The algorithm loops on every pixel on the screen.
  173.      * any pixel represents an imaginary number c...
  174.      * we want to know whether c is in Mandelbrot set or not.
  175.      * For c to be in mandelbrot set, Zn must remain bounded
  176.      * when (starting with Z0 = 0) applying the following
  177.      * iteration:
  178.      *     Z(n+1) = (Zn)^2 + c.
  179.      * C & Zn are complex numbers.
  180.      * The algorithm iterates until Zn becomes unbounded or
  181.      * maximum number of iterations reaches.
  182.      * The screen is scaled as follows:
  183.      * - The R-axis is from -2.5 to 1.
  184.      * - The I-axis is from -1 to 1.
  185.      */
  186.  
  187.     /* allocate the z & c arrays and initialize them */
  188.     z_r = malloc(vga_width*vga_height*sizeof(double));
  189.     z_i = malloc(vga_width*vga_height*sizeof(double));
  190.     c_i = malloc(vga_width*vga_height*sizeof(double));
  191.     c_r = malloc(vga_width*vga_height*sizeof(double));
  192.  
  193.     for (i = 0; i < vga_height; i++) {
  194.         for (j = 0; j < vga_width; j++) {
  195.             /* i represents y, j represents x */
  196.             l = i*vga_width + j;
  197.  
  198.             /* z is initially zero */
  199.             z_r[l] = z_i[l] = 0;
  200.  
  201.              /* calculate values of c_r and c_im */
  202.             c_r[l] = ((double)j/(double)vga_width )*((double)3.5)-(double)2.5;
  203.             c_i[l] = ((double)i/(double)vga_height)*((double)2)-(double)1;
  204.  
  205.             /* initialize pixel color to white */
  206.             fbuf[l*3+0] = 0xFF;
  207.             fbuf[l*3+1] = 0xFF;
  208.             fbuf[l*3+2] = 0xFF;
  209.         }
  210.     }
  211.  
  212.     /* write to vga */
  213.     update_screen(fbuf);
  214.  
  215.     /* iterations */
  216.     for (k = 0; k < max_iteration; k++) {
  217.         /* loop on all pixels */
  218.         for (i = 0; i < vga_height; i++) {
  219.             for (j = 0; j < vga_width; j++) {
  220.                 /* i represents y, j represents x */
  221.                 l = i*vga_width + j;
  222.  
  223.                 /* z is unbounded? we assume that z
  224.                  * is unbounded if |z| >= 2
  225.                  */
  226.                 if (z_r[l]*z_r[l] + z_i[l]*z_i[l] >= 2*2) {
  227.                     /* unbounded */
  228.                     if (fbuf[l*3+0] == 0xFF &&
  229.                         fbuf[l*3+0] == 0xFF &&
  230.                         fbuf[l*3+0] == 0xFF) {
  231.                         /* pick a color */
  232.                         if (k < 10) {
  233.                             fbuf[l*3+0] = k*10;
  234.                             fbuf[l*3+1] = 0;
  235.                         } else if (k < 100) {
  236.                             fbuf[l*3+0] = 100+(k-10);
  237.                             fbuf[l*3+1] = k*2;
  238.                         } else {
  239.                             fbuf[l*3+0] = 190+(k-200)/(800/50);
  240.                             fbuf[l*3+1] = 0;
  241.                         }
  242.                         fbuf[l*3+2] = 0;
  243.                     }
  244.                     continue;
  245.                 }
  246.  
  247.                 /* Znew = (Zold)^2 + C
  248.                  *      = z_r^2 + (2*z_r*z_i)j - (z_i)^2 + c_r + (c_i)j
  249.                  *      = z_r^2 - (z_i)^2 + c_r + (2*z_r*z_i + c_i)j
  250.                  */
  251.                 tmp = z_r[l]*z_r[l] - z_i[l]*z_i[l] + c_r[l];
  252.                 z_i[l] = 2*z_r[l]*z_i[l] + c_i[l];
  253.                 z_r[l] = tmp;
  254.  
  255.             }
  256.         }
  257.  
  258.         /* write to vga */
  259.         update_screen(fbuf);
  260.  
  261.     }
  262.  
  263.  
  264.  
  265.  
  266. }
  267.  
  268. int main() {
  269.  
  270.     int pid, status;
  271.     unsigned char *fbuf;
  272.     int i, j, fd;
  273.  
  274.     /* make vga device file */
  275.     mknod("/dev/vga", 0x2777, 12 /* devid for vga */);
  276.  
  277.     /* get vga resolution */
  278.     fd = open("/dev/vga", 0);
  279.     ioctl(fd, VGA_GET_WIDTH, &vga_width);
  280.     ioctl(fd, VGA_GET_HEIGHT, &vga_height);
  281.  
  282.     /* open bmp file */
  283.     bmp = fopen("/usr/share/bg.bmp", "r");
  284.  
  285.     /* allocate frame buffer */
  286.     fbuf = malloc(vga_width*vga_height*3);
  287.     tbuf = malloc(vga_width*vga_height*3);
  288.  
  289.     /* parse the bmp file */
  290.     /*parse_bmp(bmp, fbuf);*/
  291.  
  292.     /* draw Mandelbrot set */
  293.     mandelbrot(fbuf);
  294.  
  295.     /* write to vga */
  296.     fwrite(fbuf, vga_width*vga_height*3, 1, vga);
  297.  
  298.     /* done */
  299.     while(1);
  300.  
  301.     /* inform the user that init daemon has just started: */
  302.     printf("\n");
  303.     printf("Quafios init daemon is initalizing your system.\n");
  304.  
  305.     pid = fork();
  306.  
  307.     if (pid == 0) {
  308.  
  309.         /* run Quafios shell */
  310.         execve("/bin/sh", NULL, NULL);
  311.  
  312.     } else {
  313.  
  314.         /* sleep */
  315.         status;
  316.         waitpid(pid, &status);
  317.  
  318.         /* done */
  319.         printf("init: shell at /dev/console terminated.\n");
  320.  
  321.     }
  322.  
  323.     /* terminate. */
  324.     return 0;
  325.  
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement