Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * +----------------------------------------------------------+
- * | +------------------------------------------------------+ |
- * | | Quafios Init Daemon. | |
- * | | -> main() procedure. | |
- * | +------------------------------------------------------+ |
- * +----------------------------------------------------------+
- *
- * This file is part of Quafios 1.0.2 source code.
- * Copyright (C) 2014 Mostafa Abd El-Aziz Mohamed.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Quafios. If not, see <http://www.gnu.org/licenses/>.
- *
- * Visit http://www.quafios.com/ for contact information.
- *
- */
- /* The init program :D
- * This is my first out-of-the-kernel experience :D
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <api/proc.h>
- #include <api/fs.h>
- #include <video/generic.h>
- typedef struct {
- char id[2];
- int size;
- short reserved1;
- short reserved2;
- int offset;
- } __attribute__((packed)) bmp_file_header_t;
- typedef struct {
- int size;
- int width;
- int height;
- short planes; /* number of color planes */
- short depth; /* color depth */
- int compression;
- int imgsize; /* size of the actual image data */
- int horizontal; /* horizontal resolution */
- int vertical; /* vertical resolution */
- int pallette; /* number of colors in pallette */
- int important; /* important colors */
- } __attribute__((packed)) bmp_info_header_t;
- FILE *vga, *bmp;
- int vga_width;
- int vga_height;
- unsigned char *tbuf = NULL;
- char font[256][16];
- void draw_text(unsigned char *fbuf) {
- /* This is my first experience to write text on Quafios GUI! */
- char *damn = "--Quafios Fractals 1.0.2--";
- int i = 0, x = 50, y = 50, j = 0, k = 0;
- int offset;
- FILE *fnt;
- /* load the damn font */
- fnt = fopen("/usr/share/font8x16.fon", "r");
- fread(font, sizeof(font), 1, fnt);
- fclose(fnt);
- /* draw characters on frame buffer */
- while(damn[i]) {
- for (j = 0; j < 8; j++)
- for (k = 0; k < 16; k++) {
- if (font[damn[i]][k]&(1<<(7-j))) {
- fbuf[((y+k)*vga_width + (x+j))*3 + 0] = 0xFF;
- fbuf[((y+k)*vga_width + (x+j))*3 + 1] = 0xFF;
- fbuf[((y+k)*vga_width + (x+j))*3 + 2] = 0xFF;
- }
- }
- x += 9;
- i++;
- }
- }
- void parse_bmp(FILE *bmp, unsigned char *fbuf) {
- bmp_file_header_t header;
- bmp_info_header_t info;
- int i, j, k = 0;
- unsigned char *rgb;
- printf("BMP Parser!\n");
- /* read bmp file header */
- fread(&header, sizeof(header), 1, bmp);
- /* read bmp info header */
- fread(&info, sizeof(info), 1, bmp);
- /* read pixel array */
- rgb = malloc(info.imgsize);
- fread(rgb, info.imgsize, 1, bmp);
- /* iterate on the colors */
- for (i = info.height-1; i >= 0; i--) {
- for(j = 0; j < info.width; j++) {
- unsigned char r, g, b;
- b = rgb[k++];
- g = rgb[k++];
- r = rgb[k++];
- if (j < vga_width) {
- fbuf[(i*vga_width+j)*3+0] = b;
- fbuf[(i*vga_width+j)*3+1] = g;
- fbuf[(i*vga_width+j)*3+2] = r;
- }
- }
- k+=2;
- }
- }
- void update_screen(unsigned char *fbuf) {
- /* copy to tbuf */
- int i;
- for (i = 0; i < vga_width*vga_height*3; i++)
- tbuf[i] = fbuf[i];
- /* draw program title */
- draw_text(tbuf);
- /* write to VGA driver */
- vga = fopen("/dev/vga", "r");
- fwrite(tbuf, vga_width*vga_height*3, 1, vga);
- fclose(vga);
- }
- void mandelbrot(unsigned char *fbuf) {
- /* plot Mandelbrot set, escape time algorithm */
- int max_iteration = 1000; /* maximum amount of iterations */
- /* the c variable */
- double *c_r, *c_i; /* real and imaginary parts of c */
- /* the z variable */
- double *z_r, *z_i;
- /* temporary variable */
- double tmp;
- /* counters: */
- int i, j, k, l;
- /* color of the pixel */
- int r, g, b;
- int sum1, sum2;
- /* how does this work?
- * The algorithm loops on every pixel on the screen.
- * any pixel represents an imaginary number c...
- * we want to know whether c is in Mandelbrot set or not.
- * For c to be in mandelbrot set, Zn must remain bounded
- * when (starting with Z0 = 0) applying the following
- * iteration:
- * Z(n+1) = (Zn)^2 + c.
- * C & Zn are complex numbers.
- * The algorithm iterates until Zn becomes unbounded or
- * maximum number of iterations reaches.
- * The screen is scaled as follows:
- * - The R-axis is from -2.5 to 1.
- * - The I-axis is from -1 to 1.
- */
- /* allocate the z & c arrays and initialize them */
- z_r = malloc(vga_width*vga_height*sizeof(double));
- z_i = malloc(vga_width*vga_height*sizeof(double));
- c_i = malloc(vga_width*vga_height*sizeof(double));
- c_r = malloc(vga_width*vga_height*sizeof(double));
- for (i = 0; i < vga_height; i++) {
- for (j = 0; j < vga_width; j++) {
- /* i represents y, j represents x */
- l = i*vga_width + j;
- /* z is initially zero */
- z_r[l] = z_i[l] = 0;
- /* calculate values of c_r and c_im */
- c_r[l] = ((double)j/(double)vga_width )*((double)3.5)-(double)2.5;
- c_i[l] = ((double)i/(double)vga_height)*((double)2)-(double)1;
- /* initialize pixel color to white */
- fbuf[l*3+0] = 0xFF;
- fbuf[l*3+1] = 0xFF;
- fbuf[l*3+2] = 0xFF;
- }
- }
- /* write to vga */
- update_screen(fbuf);
- /* iterations */
- for (k = 0; k < max_iteration; k++) {
- /* loop on all pixels */
- for (i = 0; i < vga_height; i++) {
- for (j = 0; j < vga_width; j++) {
- /* i represents y, j represents x */
- l = i*vga_width + j;
- /* z is unbounded? we assume that z
- * is unbounded if |z| >= 2
- */
- if (z_r[l]*z_r[l] + z_i[l]*z_i[l] >= 2*2) {
- /* unbounded */
- if (fbuf[l*3+0] == 0xFF &&
- fbuf[l*3+0] == 0xFF &&
- fbuf[l*3+0] == 0xFF) {
- /* pick a color */
- if (k < 10) {
- fbuf[l*3+0] = k*10;
- fbuf[l*3+1] = 0;
- } else if (k < 100) {
- fbuf[l*3+0] = 100+(k-10);
- fbuf[l*3+1] = k*2;
- } else {
- fbuf[l*3+0] = 190+(k-200)/(800/50);
- fbuf[l*3+1] = 0;
- }
- fbuf[l*3+2] = 0;
- }
- continue;
- }
- /* Znew = (Zold)^2 + C
- * = z_r^2 + (2*z_r*z_i)j - (z_i)^2 + c_r + (c_i)j
- * = z_r^2 - (z_i)^2 + c_r + (2*z_r*z_i + c_i)j
- */
- tmp = z_r[l]*z_r[l] - z_i[l]*z_i[l] + c_r[l];
- z_i[l] = 2*z_r[l]*z_i[l] + c_i[l];
- z_r[l] = tmp;
- }
- }
- /* write to vga */
- update_screen(fbuf);
- }
- }
- int main() {
- int pid, status;
- unsigned char *fbuf;
- int i, j, fd;
- /* make vga device file */
- mknod("/dev/vga", 0x2777, 12 /* devid for vga */);
- /* get vga resolution */
- fd = open("/dev/vga", 0);
- ioctl(fd, VGA_GET_WIDTH, &vga_width);
- ioctl(fd, VGA_GET_HEIGHT, &vga_height);
- /* open bmp file */
- bmp = fopen("/usr/share/bg.bmp", "r");
- /* allocate frame buffer */
- fbuf = malloc(vga_width*vga_height*3);
- tbuf = malloc(vga_width*vga_height*3);
- /* parse the bmp file */
- /*parse_bmp(bmp, fbuf);*/
- /* draw Mandelbrot set */
- mandelbrot(fbuf);
- /* write to vga */
- fwrite(fbuf, vga_width*vga_height*3, 1, vga);
- /* done */
- while(1);
- /* inform the user that init daemon has just started: */
- printf("\n");
- printf("Quafios init daemon is initalizing your system.\n");
- pid = fork();
- if (pid == 0) {
- /* run Quafios shell */
- execve("/bin/sh", NULL, NULL);
- } else {
- /* sleep */
- status;
- waitpid(pid, &status);
- /* done */
- printf("init: shell at /dev/console terminated.\n");
- }
- /* terminate. */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement