All pastes #628657 Raw Edit

Untitled

public text v1 · immutable
·published 2007-07-21 14:24 UTC
rendered paste body
/* 
 * konfetti
 */

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

#include <ft2build.h>
#include FT_FREETYPE_H

typedef struct
{
    unsigned int width;
    unsigned int height;
} tDimension;

#define MY_BASE 'a'
#define MY_REQUEST 1
#define MY_LAYER_GET_DIMENSION_NR 1

#define IOCTL_LAYER_GET_DIMENSION  _IOR(MY_BASE, MY_REQUEST, MY_LAYER_GET_DIMENSION_NR)


long int screenWidth = 720;
long int screenHeight = 576;
long int bytes_per_pixel = 2;

long int screensize = 0;
char *fbp = 0;

/* ARGB1555 COLOR */
void draw_pixel(long int x,long int y, int rot, int gruen, int blue, int alpha)
{
	long int bytes_per_line = (screenWidth) * bytes_per_pixel;

	long int position = ((y /*+ y_offset*/) * bytes_per_line) + ((x /*+ x_offset*/) * (bytes_per_pixel));
        /* ??? */

	position -= (265 * bytes_per_pixel);
	position += (21 * 720 * bytes_per_pixel);
       //position -= (730 * bytes_per_pixel);
	//position += (34 *screenWidth * bytes_per_pixel);
                    
	unsigned short int col = alpha << 16 | rot << 10 | gruen << 5 | blue;

	(*(unsigned short int*) (fbp + position)) = col;
}

void draw_horiz_line(int x1, int y1, int breite, int rot, int gruen, int blau, int alpha)
{
   int vLoop;

   for (vLoop = x1; vLoop < x1 + breite; vLoop++)
      draw_pixel(vLoop, y1, rot, gruen, blau, alpha);

}

void draw_verti_line(int x1, int y1, int hoehe, int rot, int gruen, int blau, int alpha)
{
   int vLoop;

   for (vLoop = y1; vLoop < y1 + hoehe; vLoop++)
      draw_pixel(x1, vLoop, rot, gruen, blau, alpha);

}

void draw_rectangle(int x1, int y1, int breite, int hoehe, int rot, int gruen, int blau, int alpha)
{
    draw_horiz_line(x1, y1, breite, rot, gruen, blau, alpha);
    draw_horiz_line(x1, y1 + hoehe, breite, rot, gruen, blau, alpha);

    draw_verti_line(x1, y1, hoehe, rot, gruen, blau, alpha);
    draw_verti_line(x1 + breite, y1, hoehe, rot, gruen, blau, alpha);
}


void draw_filled_rectangle(int x1, int y1, int breite, int hoehe, int rot, int gruen, int blau, int alpha)
{
   int vLoop;	

   for (vLoop = y1; vLoop < y1 + hoehe; vLoop++)
      draw_horiz_line(x1, vLoop, breite, rot, gruen, blau, alpha);

}

int main()
{
    int stlayer_fd = 0;
    int x = 0, y = 0, error, pen_x, pen_y, n;
    char text[17] = "framebuffer test";
    FT_Library  library;
    FT_Face     face;
    FT_GlyphSlot  slot = face->glyph;

    // Open device
    stlayer_fd = open("/dev/stapi/stlayer_ioctl", O_RDWR);
    if (!stlayer_fd) 
    {
       printf("Error: cannot open stlayer_ioctl device.\n");
       exit(1);
    }
    printf("The stlayer_ioctl device was opened successfully.\n");

    error = FT_Init_FreeType(&library);
    if (error)
    {
       printf("Error: cannot initialize freetype library.\n");
       exit(1);
    }


    error = FT_New_Face(library, "/tmp/arial.ttf", 0, &face);
    if (error == FT_Err_Unknown_File_Format)
    {
       printf("Error: unknown freetype-font file format.\n");
       exit(1);
    }
    else if (error)
    {
       printf("Error: loading freetype-font file failed.\n");
       exit(1);
    }

    error = FT_Set_Char_Size(
            face,    /* handle to face object           */
            0,       /* char_width in 1/64th of points  */
            16*64,   /* char_height in 1/64th of points */
            300,     /* horizontal device resolution    */
            300 );   /* vertical device resolution      */
    if (error)
    {
       printf("Error: setting freetype char-size failed.\n");
       exit(1);
    }


    pen_x = 300;
    pen_y = 200;

    for ( n = 0; n < 16; n++ )
    {
        FT_UInt  glyph_index;
 
       /* retrieve glyph index from character code */
       glyph_index = FT_Get_Char_Index(face, text[n]);

       /* load glyph image into the slot (erase previous one) */
       error = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT );
       if ( error )
         continue;  /* ignore errors */

       /* convert to an anti-aliased bitmap */
       error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_LCD);
       if ( error )
        continue;

       /* now, draw to our target surface */
       /*my_draw_bitmap(&slot->bitmap,
                     pen_x + slot->bitmap_left,
                     pen_y - slot->bitmap_top);*/

       /* increment pen position */
       pen_x += slot->advance.x >> 6;
       pen_y += slot->advance.y >> 6; /* not useful for now */
    }



    screenWidth = 720;
    screenHeight = 576;

    // aus strace gefiltert
    screensize = 833535;

    // Map the device to userspace memory - SCART
    fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, stlayer_fd, 0xa6c8a000);

    printf("The device was mapped to memory successfully at %.8x.\n", fbp);

    for (y = 0; y < screenHeight - 100; y++)
       for (x = 369; x < 370; x++)
       {
          draw_pixel(10,y, 0, 0, 255, 0); /* blau */
	  draw_pixel(x,y, 0, 255, 0, 0); /* gruen */
	  draw_pixel(x+20,y, 255, 0 , 0, 0); /* rot */
       }

    draw_rectangle(100 , 100, 200, 50, 255, 0 , 255, 0);
    draw_filled_rectangle(screenWidth / 2 , screenHeight / 2, 200, 100, 0, 128, 255, 0);

    munmap(fbp, screensize);
    close(stlayer_fd);
    return 0;
}