source: mainline/fb/fb.c@ 04a73cdf

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 04a73cdf was 04a73cdf, checked in by Jakub Jermar <jakub@…>, 20 years ago

Sync with kernel.
Add ipc_wait_for_call_timeout() and ipc_trywait_for_call().
Modify ipc_wait_for_call() to be unconditional.

  • Property mode set to 100644
File size: 16.4 KB
Line 
1/*
2 * Copyright (C) 2006 Jakub Vana
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <stdio.h>
30#include <ddi.h>
31#include <task.h>
32#include <stdlib.h>
33#include <ddi.h>
34#include <sysinfo.h>
35#include <align.h>
36#include <as.h>
37#include <ipc/fb.h>
38
39
40#include <ipc/ipc.h>
41#include <ipc/services.h>
42#include <unistd.h>
43#include <stdlib.h>
44#include <ipc/ns.h>
45
46#include <kernel/errno.h>
47
48
49#include "fb.h"
50
51
52
53#define pl /*printf("FB:L:%d\n",(int)__LINE__);*/
54
55#define EFB (-1)
56
57extern int __DONT_OPEN_STDIO__=1;
58
59#define DEFAULT_BGCOLOR 0x000080
60#define DEFAULT_FGCOLOR 0xffff00
61#define DEFAULT_LOGOCOLOR 0x0000ff
62
63#define MAIN_BGCOLOR 0x404000
64#define MAIN_FGCOLOR 0x000000
65#define MAIN_LOGOCOLOR 0x404000
66
67#define SPACING (2)
68
69#define H_NO_VFBS 3
70#define V_NO_VFBS 3
71
72
73static void fb_putchar(int item,char ch);
74int create_window(int item,unsigned int x, unsigned int y,unsigned int x_size, unsigned int y_size,
75 unsigned int BGCOLOR,unsigned int FGCOLOR,unsigned int LOGOCOLOR);
76void fb_init(int item,__address addr, unsigned int x, unsigned int y, unsigned int bpp, unsigned int scan,
77 unsigned int BGCOLOR,unsigned int FGCOLOR,unsigned int LOGOCOLOR);
78
79
80unsigned int mod_col(unsigned int col,int mod);
81
82
83
84
85
86int main(int argc, char *argv[])
87{
88
89 __address fb_ph_addr;
90 unsigned int fb_width;
91 unsigned int fb_height;
92 unsigned int fb_bpp;
93 unsigned int fb_scanline;
94 __address fb_addr;
95 int a=0;
96
97
98 if(!sysinfo_value("fb")) return -1;
99
100 fb_ph_addr=sysinfo_value("fb.address.physical");
101 fb_width=sysinfo_value("fb.width");
102 fb_height=sysinfo_value("fb.height");
103 fb_bpp=sysinfo_value("fb.bpp");
104 fb_scanline=sysinfo_value("fb.scanline");
105
106 fb_addr=ALIGN_UP(((__address)set_maxheapsize(USER_ADDRESS_SPACE_SIZE_ARCH>>1)),PAGE_SIZE);
107
108
109
110 map_physmem(task_get_id(),(void *)((__address)fb_ph_addr),(void *)fb_addr,
111 (fb_scanline*fb_height+PAGE_SIZE-1)>>PAGE_WIDTH,1);
112
113 fb_init(0,fb_addr, fb_width, fb_height, fb_bpp, fb_scanline,
114 MAIN_BGCOLOR,MAIN_FGCOLOR,MAIN_LOGOCOLOR);
115
116 fb_putchar(0,'\n');
117 fb_putchar(0,' ');
118
119
120 {
121 int i,j;
122
123 for(i=0;i<H_NO_VFBS;i++)
124 for(j=0;j<V_NO_VFBS;j++)
125 {
126
127 int w=create_window(0,(fb_width/H_NO_VFBS)*i+SPACING,
128 (fb_height/V_NO_VFBS)*j+SPACING,(fb_width/H_NO_VFBS)-2*SPACING ,
129 (fb_height/V_NO_VFBS)-2*SPACING,mod_col(DEFAULT_BGCOLOR,/*i+j*H_NO_VFBS*/0),
130 mod_col(DEFAULT_FGCOLOR,/*i+j*H_NO_VFBS*/0),
131 mod_col(DEFAULT_LOGOCOLOR,/*i+j*H_NO_VFBS)*/0));
132
133 if(w==EFB) return -1;
134
135 {
136 char text[]="Hello, World from\nHelenOS Framebuffer driver\non Virtual Framebuffer\nVFB ";
137 int i;
138 for(i=0;text[i];i++) fb_putchar(w,text[i]);
139 fb_putchar(w,w+'0');
140 fb_putchar(w,'\n');
141 }
142 }
143 }
144
145
146 ipc_call_t call;
147 ipc_callid_t callid;
148 char connected = 0;
149 int res;
150 int c;
151 ipcarg_t phonead;
152
153 ipcarg_t retval, arg1, arg2;
154
155
156
157 if ((res = ipc_connect_to_me(PHONE_NS, SERVICE_VIDEO, 0, &phonead)) != 0)
158 {
159 return -1;
160 };
161
162
163 while (1) {
164 static int vfb_no=1;
165
166 callid = ipc_wait_for_call(&call);
167 // printf("%s:Call phone=%lX..", NAME, call.in_phone_hash);
168 switch (IPC_GET_METHOD(call)&((1<<METHOD_WIDTH)-1)) {
169 case IPC_M_PHONE_HUNGUP:
170// fb_putchar(4,((a++)&15)+'A');
171
172 retval = 0;
173 break;
174 case IPC_M_CONNECT_ME_TO:
175 retval = 0;
176// fb_putchar(1,((a++)&15)+'A');
177 break;
178 case FB_GET_VFB:
179 retval = 0;
180 arg1 = vfb_no++;
181// fb_putchar(2,((a++)&15)+'A');
182
183 break;
184
185 case FB_PUTCHAR:
186 retval = 0;
187 fb_putchar(IPC_GET_ARG1(call),IPC_GET_ARG2(call));
188// fb_putchar(2,((a++)&15)+'A');
189 break;
190
191 default:
192 retval = ENOENT;
193// fb_putchar(3,((a++)&15)+'A');
194 break;
195 }
196
197 if (! (callid & IPC_CALLID_NOTIFICATION)) {
198 ipc_answer_fast(callid, retval, arg1, arg2);
199 }
200 }
201
202 return 0;
203}
204/*
205 * Copyright (C) 2006 Ondrej Palkovsky
206 * All rights reserved.
207 *
208 * Redistribution and use in source and binary forms, with or without
209 * modification, are permitted provided that the following conditions
210 * are met:
211 *
212 * - Redistributions of source code must retain the above copyright
213 * notice, this list of conditions and the following disclaimer.
214 * - Redistributions in binary form must reproduce the above copyright
215 * notice, this list of conditions and the following disclaimer in the
216 * documentation and/or other materials provided with the distribution.
217 * - The name of the author may not be used to endorse or promote products
218 * derived from this software without specific prior written permission.
219 *
220 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
221 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
222 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
223 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
224 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
225 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
226 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
227 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
228 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
229 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
230 */
231
232#include "font-8x16.h"
233#include <string.h>
234
235#include "helenos.xbm"
236
237
238
239
240
241
242#define GRAPHICS_ITEMS 1024
243
244
245/***************************************************************/
246/* Pixel specific fuctions */
247
248typedef void (*putpixel_fn_t)(int item,unsigned int x, unsigned int y, int color);
249typedef int (*getpixel_fn_t)(int item,unsigned int x, unsigned int y);
250
251typedef struct framebuffer_descriptor
252{
253 __u8 *fbaddress ;
254
255 unsigned int xres ;
256 unsigned int yres ;
257 unsigned int scanline ;
258 unsigned int pixelbytes ;
259
260 unsigned int position ;
261 unsigned int columns ;
262 unsigned int rows ;
263
264 unsigned int BGCOLOR;
265 unsigned int FGCOLOR;
266 unsigned int LOGOCOLOR;
267
268 putpixel_fn_t putpixel;
269 getpixel_fn_t getpixel;
270
271}framebuffer_descriptor_t;
272
273void * graphics_items[GRAPHICS_ITEMS+1]={NULL};
274
275#define FB(__a__,__b__) (((framebuffer_descriptor_t*)(graphics_items[__a__]))->__b__)
276
277#define COL_WIDTH 8
278#define ROW_BYTES (FB(item,scanline) * FONT_SCANLINES)
279#define RED(x, bits) ((x >> (16 + 8 - bits)) & ((1 << bits) - 1))
280#define GREEN(x, bits) ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
281#define BLUE(x, bits) ((x >> (8 - bits)) & ((1 << bits) - 1))
282
283#define POINTPOS(x, y) (y * FB(item,scanline) + x * FB(item,pixelbytes))
284
285
286
287
288/** Put pixel - 24-bit depth, 1 free byte */
289static void putpixel_4byte(int item,unsigned int x, unsigned int y, int color)
290{
291 *((__u32 *)(FB(item,fbaddress) + POINTPOS(x, y))) = color;
292}
293
294/** Return pixel color - 24-bit depth, 1 free byte */
295static int getpixel_4byte(int item,unsigned int x, unsigned int y)
296{
297 return *((__u32 *)(FB(item,fbaddress) + POINTPOS(x, y))) & 0xffffff;
298}
299
300/** Put pixel - 24-bit depth */
301static void putpixel_3byte(int item,unsigned int x, unsigned int y, int color)
302{
303 unsigned int startbyte = POINTPOS(x, y);
304
305#if (defined(BIG_ENDIAN) || defined(FB_BIG_ENDIAN))
306 FB(item,fbaddress)[startbyte] = RED(color, 8);
307 FB(item,fbaddress)[startbyte + 1] = GREEN(color, 8);
308 FB(item,fbaddress)[startbyte + 2] = BLUE(color, 8);
309#else
310 FB(item,fbaddress)[startbyte + 2] = RED(color, 8);
311 FB(item,fbaddress)[startbyte + 1] = GREEN(color, 8);
312 FB(item,fbaddress)[startbyte + 0] = BLUE(color, 8);
313#endif
314
315
316}
317
318/** Return pixel color - 24-bit depth */
319static int getpixel_3byte(int item,unsigned int x, unsigned int y)
320{
321 unsigned int startbyte = POINTPOS(x, y);
322
323
324
325#if (defined(BIG_ENDIAN) || defined(FB_BIG_ENDIAN))
326 return FB(item,fbaddress)[startbyte] << 16 | FB(item,fbaddress)[startbyte + 1] << 8 | FB(item,fbaddress)[startbyte + 2];
327#else
328 return FB(item,fbaddress)[startbyte + 2] << 16 | FB(item,fbaddress)[startbyte + 1] << 8 | FB(item,fbaddress)[startbyte + 0];
329#endif
330
331
332}
333
334/** Put pixel - 16-bit depth (5:6:5) */
335static void putpixel_2byte(int item,unsigned int x, unsigned int y, int color)
336{
337 /* 5-bit, 6-bits, 5-bits */
338 *((__u16 *)(FB(item,fbaddress) + POINTPOS(x, y))) = RED(color, 5) << 11 | GREEN(color, 6) << 5 | BLUE(color, 5);
339}
340
341/** Return pixel color - 16-bit depth (5:6:5) */
342static int getpixel_2byte(int item,unsigned int x, unsigned int y)
343{
344 int color = *((__u16 *)(FB(item,fbaddress) + POINTPOS(x, y)));
345 return (((color >> 11) & 0x1f) << (16 + 3)) | (((color >> 5) & 0x3f) << (8 + 2)) | ((color & 0x1f) << 3);
346}
347
348/** Put pixel - 8-bit depth (3:2:3) */
349static void putpixel_1byte(int item,unsigned int x, unsigned int y, int color)
350{
351 FB(item,fbaddress)[POINTPOS(x, y)] = RED(color, 3) << 5 | GREEN(color, 2) << 3 | BLUE(color, 3);
352}
353
354/** Return pixel color - 8-bit depth (3:2:3) */
355static int getpixel_1byte(int item,unsigned int x, unsigned int y)
356{
357 int color = FB(item,fbaddress)[POINTPOS(x, y)];
358 return (((color >> 5) & 0x7) << (16 + 5)) | (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
359}
360
361/** Fill line with color BGCOLOR */
362static void clear_line(int item,unsigned int y)
363{
364 unsigned int x;
365 for (x = 0; x < FB(item,xres); x++)
366 FB(item,putpixel)(item,x, y, FB(item,BGCOLOR));
367}
368
369
370/** Fill screen with background color */
371static void clear_screen(int item)
372{
373 unsigned int y;
374 for (y = 0; y < FB(item,yres); y++)
375 {
376 clear_line(item,y); pl
377 }
378}
379
380
381/** Scroll screen one row up */
382static void scroll_screen(int item)
383{
384 unsigned int i;
385 unsigned int j;
386
387 for(j=0;j<FB(item,yres)-FONT_SCANLINES;j++)
388 memcpy((void *) FB(item,fbaddress)+j*FB(item,scanline),
389 (void *) &FB(item,fbaddress)[ROW_BYTES+j*FB(item,scanline)], FB(item,pixelbytes)*FB(item,xres));
390
391 //memcpy((void *) FB(item,fbaddress), (void *) &FB(item,fbaddress)[ROW_BYTES], FB(item,scanline) * FB(item,yres) - ROW_BYTES);
392
393 /* Clear last row */
394 for (i = 0; i < FONT_SCANLINES; i++)
395 clear_line(item,(FB(item,rows) - 1) * FONT_SCANLINES + i);
396}
397
398
399static void invert_pixel(int item,unsigned int x, unsigned int y)
400{
401 FB(item,putpixel)(item, x, y, ~FB(item,getpixel)(item, x, y));
402}
403
404
405/** Draw one line of glyph at a given position */
406static void draw_glyph_line(int item,unsigned int glline, unsigned int x, unsigned int y)
407{
408 unsigned int i;
409
410 for (i = 0; i < 8; i++)
411 if (glline & (1 << (7 - i))) {
412 FB(item,putpixel)(item,x + i, y, FB(item,FGCOLOR));
413 } else
414 FB(item,putpixel)(item,x + i, y, FB(item,BGCOLOR));
415}
416
417/***************************************************************/
418/* Character-console functions */
419
420/** Draw character at given position */
421static void draw_glyph(int item,__u8 glyph, unsigned int col, unsigned int row)
422{
423 unsigned int y;
424
425 for (y = 0; y < FONT_SCANLINES; y++)
426 draw_glyph_line(item ,fb_font[glyph * FONT_SCANLINES + y], col * COL_WIDTH, row * FONT_SCANLINES + y);
427}
428
429/** Invert character at given position */
430static void invert_char(int item,unsigned int col, unsigned int row)
431{
432 unsigned int x;
433 unsigned int y;
434
435 for (x = 0; x < COL_WIDTH; x++)
436 for (y = 0; y < FONT_SCANLINES; y++)
437 invert_pixel(item,col * COL_WIDTH + x, row * FONT_SCANLINES + y);
438}
439
440/** Draw character at default position */
441static void draw_char(int item,char chr)
442{
443 draw_glyph(item ,chr, FB(item,position) % FB(item,columns), FB(item,position) / FB(item,columns));
444}
445
446static void draw_logo(int item,unsigned int startx, unsigned int starty)
447{
448 unsigned int x;
449 unsigned int y;
450 unsigned int byte;
451 unsigned int rowbytes;
452
453 rowbytes = (helenos_width - 1) / 8 + 1;
454
455 for (y = 0; y < helenos_height; y++)
456 for (x = 0; x < helenos_width; x++) {
457 byte = helenos_bits[rowbytes * y + x / 8];
458 byte >>= x % 8;
459 if (byte & 1)
460 FB(item,putpixel)(item,startx + x, starty + y, FB(item,LOGOCOLOR));
461 }
462}
463
464/***************************************************************/
465/* Stdout specific functions */
466
467static void invert_cursor(int item)
468{
469 invert_char(item,FB(item,position) % FB(item,columns), FB(item,position) / FB(item,columns));
470}
471
472/** Print character to screen
473 *
474 * Emulate basic terminal commands
475 */
476static void fb_putchar(int item,char ch)
477{
478
479 switch (ch) {
480 case '\n':
481 invert_cursor(item);
482 FB(item,position) += FB(item,columns);
483 FB(item,position) -= FB(item,position) % FB(item,columns);
484 break;
485 case '\r':
486 invert_cursor(item);
487 FB(item,position) -= FB(item,position) % FB(item,columns);
488 break;
489 case '\b':
490 invert_cursor(item);
491 if (FB(item,position) % FB(item,columns))
492 FB(item,position)--;
493 break;
494 case '\t':
495 invert_cursor(item);
496 do {
497 draw_char(item,' ');
498 FB(item,position)++;
499 } while (FB(item,position) % 8);
500 break;
501 default:
502 draw_char(item,ch);
503 FB(item,position)++;
504 }
505
506 if (FB(item,position) >= FB(item,columns) * FB(item,rows)) {
507 FB(item,position) -= FB(item,columns);
508 scroll_screen(item);
509 }
510
511 invert_cursor(item);
512
513}
514
515
516/** Initialize framebuffer as a chardev output device
517 *
518 * @param addr Address of theframebuffer
519 * @param x Screen width in pixels
520 * @param y Screen height in pixels
521 * @param bpp Bits per pixel (8, 16, 24, 32)
522 * @param scan Bytes per one scanline
523 *
524 */
525void fb_init(int item,__address addr, unsigned int x, unsigned int y, unsigned int bpp, unsigned int scan,
526 unsigned int BGCOLOR,unsigned int FGCOLOR,unsigned int LOGOCOLOR)
527{
528
529 if( (graphics_items[item]=malloc(sizeof(framebuffer_descriptor_t))) ==NULL)
530 {
531 return;
532 }
533
534 switch (bpp) {
535 case 8:
536 FB(item,putpixel) = putpixel_1byte;
537 FB(item,getpixel) = getpixel_1byte;
538 FB(item,pixelbytes) = 1;
539 break;
540 case 16:
541 FB(item,putpixel) = putpixel_2byte;
542 FB(item,getpixel) = getpixel_2byte;
543 FB(item,pixelbytes) = 2;
544 break;
545 case 24:
546 FB(item,putpixel) = putpixel_3byte;
547 FB(item,getpixel) = getpixel_3byte;
548 FB(item,pixelbytes) = 3;
549 break;
550 case 32:
551 FB(item,putpixel) = putpixel_4byte;
552 FB(item,getpixel) = getpixel_4byte;
553 FB(item,pixelbytes) = 4;
554 break;
555 }
556
557
558 FB(item,fbaddress) = (unsigned char *) addr; pl
559 FB(item,xres) = x; pl
560 FB(item,yres) = y; pl
561 FB(item,scanline) = scan; pl
562
563
564 FB(item,rows) = y / FONT_SCANLINES; pl
565 FB(item,columns) = x / COL_WIDTH; pl
566
567 FB(item,BGCOLOR)=BGCOLOR;
568 FB(item,FGCOLOR)=FGCOLOR;
569 FB(item,LOGOCOLOR)=LOGOCOLOR;
570
571
572 clear_screen(item); pl
573 draw_logo(item,FB(item,xres) - helenos_width, 0); pl
574 invert_cursor(item); pl
575
576}
577
578
579static int get_free_item()
580{
581 int item;
582 for(item=0;graphics_items[item]!=NULL;item++);
583 return (item==GRAPHICS_ITEMS)?EFB:item;
584}
585
586unsigned int mod_col(unsigned int col,int mod)
587{
588 if(mod & 1) col^=0xff;
589 if(mod & 2) col^=0xff00;
590 if(mod & 4) col^=0xff0000;
591 return col;
592}
593
594
595int create_window(int item,unsigned int x, unsigned int y,unsigned int x_size, unsigned int y_size,
596 unsigned int BGCOLOR,unsigned int FGCOLOR,unsigned int LOGOCOLOR)
597{
598 int item_new;
599
600 if(EFB==(item_new=get_free_item()))return EFB;
601
602
603 if( (graphics_items[item_new]=malloc(sizeof(framebuffer_descriptor_t))) ==NULL)
604 {
605 return EFB;
606 }
607
608
609
610 FB(item_new,putpixel) = FB(item,putpixel);
611 FB(item_new,getpixel) = FB(item,getpixel);
612 FB(item_new,pixelbytes) = FB(item,pixelbytes);
613
614 FB(item_new,fbaddress) = FB(item,fbaddress) + POINTPOS(x, y) ;
615 FB(item_new,xres) = x_size;
616 FB(item_new,yres) = y_size;
617 FB(item_new,scanline) = FB(item,scanline);
618
619
620 FB(item_new,rows) = y_size / FONT_SCANLINES;
621 FB(item_new,columns) = x_size / COL_WIDTH;
622
623 FB(item_new,BGCOLOR)=BGCOLOR;
624 FB(item_new,FGCOLOR)=FGCOLOR;
625 FB(item_new,LOGOCOLOR)=LOGOCOLOR;
626
627
628 clear_screen(item_new);
629 draw_logo(item_new,FB(item_new,xres) - helenos_width, 0);
630 invert_cursor(item_new);
631
632 return item_new;
633}
634
635
636
Note: See TracBrowser for help on using the repository browser.