OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [MW_0_8_9PRE7/] [mw/] [src/] [nanox/] [clientfb.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 673 markom
/*
2
 * Microwindows direct client-side framebuffer mapping routines
3
 *
4
 * Copyright (c) 2001 by Greg Haerr <greg@censoft.com>
5
 */
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <unistd.h>
9
#include <fcntl.h>
10
#include <sys/ioctl.h>
11
#include <sys/mman.h>
12
#include <asm/page.h>           /* For definition of PAGE_SIZE */
13
#include <linux/fb.h>
14
#include "nano-X.h"
15
 
16
/* globals: assumes use of non-shared libnano-X.a for now*/
17
static int              frame_fd;       /* client side framebuffer fd*/
18
static unsigned char *  frame_map;      /* client side framebuffer mmap'd addr*/
19
static int              frame_len;      /* client side framebuffer length*/
20
static unsigned char *  physpixels;     /* start address of pixels*/
21
static GR_SCREEN_INFO   sinfo;
22
 
23
/* map framebuffer address into client memory*/
24
unsigned char *
25
GrOpenClientFramebuffer(void)
26
{
27
        int     frame_offset;
28
        char *  fbdev;
29
        struct fb_fix_screeninfo finfo;
30
 
31
        /* if already open, return fb address*/
32
        if (physpixels)
33
                return physpixels;
34
 
35
        /*
36
         * For now, we'll just check whether or not Microwindows
37
         * is running its framebuffer driver to determine whether
38
         * to allow direct client-side framebuffer mapping.  In
39
         * the future, we could allow direct mapping for Microwindows
40
         * running on top of X, and finding the address of the
41
         * window within the Microwindows X window.
42
         */
43
        GrGetScreenInfo(&sinfo);
44
        if (!sinfo.fbdriver)
45
                return NULL;
46
 
47
        /*
48
         * Try to open the framebuffer directly.
49
         */
50
        if (!(fbdev = getenv("FRAMEBUFFER")))
51
                fbdev = "/dev/fb0";
52
        frame_fd = open(fbdev, O_RDWR);
53
        if (frame_fd < 0) {
54
                printf("Can't open framebuffer device\n");
55
                return NULL;
56
        }
57
 
58
        /* Get the type of video hardware */
59
        if (ioctl(frame_fd, FBIOGET_FSCREENINFO, &finfo) < 0 ) {
60
                printf("Couldn't get fb hardware info\n");
61
                goto err;
62
        }
63
 
64
        // FIXME remove when mwin returns fb or X
65
        switch (finfo.visual) {
66
                case FB_VISUAL_TRUECOLOR:
67
                case FB_VISUAL_PSEUDOCOLOR:
68
                case FB_VISUAL_STATIC_PSEUDOCOLOR:
69
                case FB_VISUAL_DIRECTCOLOR:
70
                        break;
71
                default:
72
                        printf("Unsupported fb color map\n");
73
                        goto err;
74
        }
75
 
76
        /* Memory map the device, compensating for buggy PPC mmap() */
77
        frame_offset = (((long)finfo.smem_start) -
78
                (((long)finfo.smem_start)&~(PAGE_SIZE-1)));
79
        frame_len = finfo.smem_len + frame_offset;
80
        frame_map = (unsigned char *)mmap(NULL, frame_len, PROT_READ|PROT_WRITE,
81
                MAP_SHARED, frame_fd, 0);
82
        if (frame_map == (unsigned char *)-1) {
83
                printf("Unable to memory map the video hardware\n");
84
                frame_map = NULL;
85
                goto err;
86
        }
87
        physpixels = frame_map + frame_offset;
88
        return physpixels;
89
 
90
err:
91
        close(frame_fd);
92
        return NULL;
93
}
94
 
95
void
96
GrCloseClientFramebuffer(void)
97
{
98
        if (frame_fd >= 0) {
99
                if (frame_map) {
100
                        munmap(frame_map, frame_len);
101
                        frame_map = NULL;
102
                        physpixels = NULL;
103
                }
104
                close(frame_fd);
105
                frame_fd = -1;
106
 
107
                /* reset sinfo struct*/
108
                sinfo.cols = 0;
109
        }
110
}
111
 
112
/*
113
 * Return client-side mapped framebuffer info for
114
 * passed window.  If not running framebuffer, the
115
 * physpixel and winpixel members will be NULL, and
116
 * everything else correct.
117
 */
118
void
119
GrGetWindowFBInfo(GR_WINDOW_ID wid, GR_WINDOW_FB_INFO *fbinfo)
120
{
121
        int                     physoffset;
122
        GR_WINDOW_INFO          info;
123
        static int              last_portrait = -1;
124
 
125
        /* re-get screen info on auto-portrait switch*/
126
        if (sinfo.cols == 0 || last_portrait != sinfo.portrait)
127
                GrGetScreenInfo(&sinfo);
128
        last_portrait = sinfo.portrait;
129
 
130
        /* must get window position anew each time*/
131
        GrGetWindowInfo(wid, &info);
132
 
133
        fbinfo->bpp = sinfo.bpp;
134
        fbinfo->bytespp = (sinfo.bpp+7)/8;
135
        fbinfo->pixtype = sinfo.pixtype;
136
        fbinfo->x = info.x;
137
        fbinfo->y = info.y;
138
        fbinfo->portrait_mode = sinfo.portrait;
139
 
140
        switch (fbinfo->portrait_mode) {
141
        case MWPORTRAIT_RIGHT:
142
        case MWPORTRAIT_LEFT:
143
                /*
144
                 * We reverse coords since Microwindows reports
145
                 * back the virtual xres/yres, and we want
146
                 * the physical xres/yres.
147
                 */
148
                // FIXME return xres and xvirtres in SCREENINFO?
149
                fbinfo->xres = sinfo.rows;      /* reverse coords*/
150
                fbinfo->yres = sinfo.cols;
151
                break;
152
        default:
153
                fbinfo->xres = sinfo.cols;
154
                fbinfo->yres = sinfo.rows;
155
                break;
156
        }
157
        fbinfo->xvirtres = sinfo.cols;
158
        fbinfo->yvirtres = sinfo.rows;
159
        fbinfo->pitch = fbinfo->xres * fbinfo->bytespp;
160
 
161
        /* fill in memory mapped addresses*/
162
        fbinfo->physpixels = physpixels;
163
 
164
        /* winpixels only valid for non-portrait modes*/
165
        physoffset = info.y*fbinfo->pitch + info.x*fbinfo->bytespp;
166
        fbinfo->winpixels = physpixels? (physpixels + physoffset): NULL;
167
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.