8 #include "Protocol/UnixThunk.h"
9 #include "Protocol/SimpleTextIn.h"
10 #include "Protocol/UgaDraw.h"
11 #include "Protocol/UnixUgaIo.h"
13 #include <X11/Xutil.h>
15 #include <X11/extensions/XShm.h>
16 #include <X11/keysym.h>
18 #include <Guid/PeiPeCoffLoader.h>
19 #include <Ppi/StatusCode.h>
21 #include <Library/PeCoffLib.h>
22 #include <Library/BaseLib.h>
23 #include <Library/BaseMemoryLib.h>
24 #include <Library/PrintLib.h>
25 #include <Library/PcdLib.h>
26 #include <Library/DebugLib.h>
28 extern void msSleep (unsigned long Milliseconds
);
32 struct uga_drv_shift_mask
42 EFI_UNIX_UGA_IO_PROTOCOL UgaIo
;
45 int screen
; /* values for window_size in main */
53 unsigned int line_bytes
;
54 unsigned int pixel_shift
;
55 unsigned char *image_data
;
57 struct uga_drv_shift_mask r
, g
, b
;
60 XShmSegmentInfo xshm_info
;
65 unsigned int key_count
;
66 EFI_INPUT_KEY keys
[NBR_KEYS
];
70 HandleEvents(UGA_IO_PRIVATE
*drv
);
73 fill_shift_mask (struct uga_drv_shift_mask
*sm
, unsigned long mask
)
77 while ((mask
& 1) == 0)
87 sm
->csize
= 8 - sm
->size
;
91 TryCreateShmImage(UGA_IO_PRIVATE
*drv
)
93 drv
->image
= XShmCreateImage (drv
->display
, drv
->visual
,
94 drv
->depth
, ZPixmap
, NULL
, &drv
->xshm_info
,
95 drv
->width
, drv
->height
);
96 if (drv
->image
== NULL
)
99 switch (drv
->image
->bitmap_unit
) {
101 drv
->pixel_shift
= 2;
104 drv
->pixel_shift
= 1;
107 drv
->pixel_shift
= 0;
111 drv
->xshm_info
.shmid
= shmget
112 (IPC_PRIVATE
, drv
->image
->bytes_per_line
* drv
->image
->height
,
114 if (drv
->xshm_info
.shmid
< 0)
116 XDestroyImage(drv
->image
);
120 drv
->image_data
= shmat (drv
->xshm_info
.shmid
, NULL
, 0);
123 shmctl (drv
->xshm_info
.shmid
, IPC_RMID
, NULL
);
124 XDestroyImage(drv
->image
);
127 /* Can this fail ? */
128 shmctl (drv
->xshm_info
.shmid
, IPC_RMID
, NULL
);
130 drv
->xshm_info
.shmaddr
= (char*)drv
->image_data
;
131 drv
->image
->data
= (char*)drv
->image_data
;
133 if (!XShmAttach (drv
->display
, &drv
->xshm_info
))
135 shmdt (drv
->image_data
);
136 XDestroyImage(drv
->image
);
144 UgaClose (EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
)
146 UGA_IO_PRIVATE
*drv
= (UGA_IO_PRIVATE
*)UgaIo
;
150 if (drv
->image
!= NULL
)
152 XDestroyImage(drv
->image
);
155 shmdt (drv
->image_data
);
157 drv
->image_data
= NULL
;
160 XDestroyWindow(drv
->display
, drv
->win
);
161 XCloseDisplay(drv
->display
);
168 UgaSize(EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
, UINT32 Width
, UINT32 Height
)
170 UGA_IO_PRIVATE
*drv
= (UGA_IO_PRIVATE
*)UgaIo
;
171 XSizeHints size_hints
;
173 /* Destroy current buffer if created. */
174 if (drv
->image
!= NULL
)
176 XDestroyImage(drv
->image
);
179 shmdt (drv
->image_data
);
181 drv
->image_data
= NULL
;
186 drv
->height
= Height
;
187 XResizeWindow (drv
->display
, drv
->win
, Width
, Height
);
189 /* Allocate image. */
190 if (XShmQueryExtension(drv
->display
) && TryCreateShmImage(drv
)) {
195 drv
->pixel_shift
= 2;
196 else if (drv
->depth
> 8)
197 drv
->pixel_shift
= 1;
199 drv
->pixel_shift
= 0;
201 drv
->image_data
= malloc((drv
->width
* drv
->height
) << drv
->pixel_shift
);
202 drv
->image
= XCreateImage (drv
->display
, drv
->visual
, drv
->depth
,
203 ZPixmap
, 0, (char *)drv
->image_data
,
204 drv
->width
, drv
->height
,
205 8 << drv
->pixel_shift
, 0);
207 drv
->line_bytes
= drv
->image
->bytes_per_line
;
208 fill_shift_mask (&drv
->r
, drv
->image
->red_mask
);
209 fill_shift_mask (&drv
->g
, drv
->image
->green_mask
);
210 fill_shift_mask (&drv
->b
, drv
->image
->blue_mask
);
213 size_hints
.flags
= PSize
| PMinSize
| PMaxSize
;
214 size_hints
.min_width
= size_hints
.max_width
= size_hints
.base_width
= Width
;
215 size_hints
.min_height
= size_hints
.max_height
= size_hints
.base_height
= Height
;
216 XSetWMNormalHints (drv
->display
, drv
->win
, &size_hints
);
218 XMapWindow (drv
->display
, drv
->win
);
224 handleKeyEvent(UGA_IO_PRIVATE
*drv
, XEvent
*ev
)
231 if (drv
->key_count
== NBR_KEYS
)
234 res
= XLookupString(&ev
->xkey
, str
, sizeof(str
), &keysym
, NULL
);
238 case XK_Home
: Key
.ScanCode
= SCAN_HOME
; break;
239 case XK_End
: Key
.ScanCode
= SCAN_END
; break;
240 case XK_Left
: Key
.ScanCode
= SCAN_LEFT
; break;
241 case XK_Right
: Key
.ScanCode
= SCAN_RIGHT
; break;
242 case XK_Up
: Key
.ScanCode
= SCAN_UP
; break;
243 case XK_Down
: Key
.ScanCode
= SCAN_DOWN
; break;
244 case XK_Delete
: Key
.ScanCode
= SCAN_DELETE
; break;
245 case XK_Insert
: Key
.ScanCode
= SCAN_INSERT
; break;
246 case XK_Page_Up
: Key
.ScanCode
= SCAN_PAGE_UP
; break;
247 case XK_Page_Down
: Key
.ScanCode
= SCAN_PAGE_DOWN
; break;
248 case XK_Escape
: Key
.ScanCode
= SCAN_ESC
; break;
250 case XK_F1
: Key
.ScanCode
= SCAN_F1
; break;
251 case XK_F2
: Key
.ScanCode
= SCAN_F2
; break;
252 case XK_F3
: Key
.ScanCode
= SCAN_F3
; break;
253 case XK_F4
: Key
.ScanCode
= SCAN_F4
; break;
254 case XK_F5
: Key
.ScanCode
= SCAN_F5
; break;
255 case XK_F6
: Key
.ScanCode
= SCAN_F6
; break;
256 case XK_F7
: Key
.ScanCode
= SCAN_F7
; break;
257 case XK_F8
: Key
.ScanCode
= SCAN_F8
; break;
258 case XK_F9
: Key
.ScanCode
= SCAN_F9
; break;
262 Key
.UnicodeChar
= str
[0];
268 drv
->keys
[drv
->key_wr
] = Key
;
269 drv
->key_wr
= (drv
->key_wr
+ 1) % NBR_KEYS
;
274 Redraw(UGA_IO_PRIVATE
*drv
, UINTN X
, UINTN Y
, UINTN Width
, UINTN Height
)
277 XShmPutImage (drv
->display
, drv
->win
, drv
->gc
, drv
->image
,
278 X
, Y
, X
, Y
, Width
, Height
, False
);
280 XPutImage (drv
->display
, drv
->win
, drv
->gc
, drv
->image
,
281 X
, Y
, X
, Y
, Width
, Height
);
282 XFlush(drv
->display
);
286 HandleEvent(UGA_IO_PRIVATE
*drv
, XEvent
*ev
)
291 Redraw(drv
, ev
->xexpose
.x
, ev
->xexpose
.y
,
292 ev
->xexpose
.width
, ev
->xexpose
.height
);
295 Redraw(drv
, ev
->xgraphicsexpose
.x
, ev
->xgraphicsexpose
.y
,
296 ev
->xgraphicsexpose
.width
, ev
->xgraphicsexpose
.height
);
299 handleKeyEvent(drv
, ev
);
302 XRefreshKeyboardMapping(&ev
->xmapping
);
306 XCloseDisplay (drv
->display
);
317 HandleEvents(UGA_IO_PRIVATE
*drv
)
319 while (XPending(drv
->display
) != 0)
323 XNextEvent (drv
->display
, &ev
);
324 HandleEvent(drv
, &ev
);
330 UgaPixelToColor (UGA_IO_PRIVATE
*drv
, EFI_UGA_PIXEL pixel
)
332 return ((pixel
.Red
>> drv
->r
.csize
) << drv
->r
.shift
)
333 | ((pixel
.Green
>> drv
->g
.csize
) << drv
->g
.shift
)
334 | ((pixel
.Blue
>> drv
->b
.csize
) << drv
->b
.shift
);
339 UgaColorToPixel (UGA_IO_PRIVATE
*drv
, unsigned long val
)
343 memset (&res
, 0, sizeof (EFI_UGA_PIXEL
));
344 /* FIXME: should round instead of truncate. */
345 res
.Red
= (val
>> drv
->r
.shift
) << drv
->r
.csize
;
346 res
.Green
= (val
>> drv
->g
.shift
) << drv
->g
.csize
;
347 res
.Blue
= (val
>> drv
->b
.shift
) << drv
->b
.csize
;
354 UgaCheckKey(EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
)
356 UGA_IO_PRIVATE
*drv
= (UGA_IO_PRIVATE
*)UgaIo
;
358 if (drv
->key_count
!= 0)
361 /* EFI is certainly polling. Be CPU-friendly. */
363 return EFI_NOT_READY
;
369 UgaGetKey(EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
, EFI_INPUT_KEY
*key
)
371 UGA_IO_PRIVATE
*drv
= (UGA_IO_PRIVATE
*)UgaIo
;
374 status
= UgaCheckKey(UgaIo
);
375 if (status
!= EFI_SUCCESS
)
378 *key
= drv
->keys
[drv
->key_rd
];
379 drv
->key_rd
= (drv
->key_rd
+ 1) % NBR_KEYS
;
385 UgaBlt(EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
,
386 IN EFI_UGA_PIXEL
*BltBuffer OPTIONAL
,
387 IN EFI_UGA_BLT_OPERATION BltOperation
,
390 IN UINTN DestinationX
,
391 IN UINTN DestinationY
,
394 IN UINTN Delta OPTIONAL
397 UGA_IO_PRIVATE
*Private
= (UGA_IO_PRIVATE
*)UgaIo
;
412 if (BltOperation
== EfiUgaVideoToBltBuffer
413 || BltOperation
== EfiUgaVideoToVideo
) {
417 if (SourceY
+ Height
> Private
->height
) {
418 return EFI_INVALID_PARAMETER
;
421 if (SourceX
+ Width
> Private
->width
) {
422 return EFI_INVALID_PARAMETER
;
426 if (BltOperation
== EfiUgaBltBufferToVideo
427 || BltOperation
== EfiUgaVideoToVideo
428 || BltOperation
== EfiUgaVideoFill
) {
430 // Destination is Video
432 if (DestinationY
+ Height
> Private
->height
) {
433 return EFI_INVALID_PARAMETER
;
436 if (DestinationX
+ Width
> Private
->width
) {
437 return EFI_INVALID_PARAMETER
;
441 switch (BltOperation
) {
442 case EfiUgaVideoToBltBuffer
:
443 Blt
= (EFI_UGA_PIXEL
*)((UINT8
*)BltBuffer
+ (DestinationY
* Delta
) + DestinationX
* sizeof (EFI_UGA_PIXEL
));
444 Delta
-= Width
* sizeof (EFI_UGA_PIXEL
);
445 for (SrcY
= SourceY
; SrcY
< (Height
+ SourceY
); SrcY
++) {
446 for (SrcX
= SourceX
; SrcX
< (Width
+ SourceX
); SrcX
++) {
447 *Blt
++ = UgaColorToPixel(Private
,
448 XGetPixel(Private
->image
, SrcX
, SrcY
));
450 Blt
= (EFI_UGA_PIXEL
*) ((UINT8
*) Blt
+ Delta
);
453 case EfiUgaBltBufferToVideo
:
454 Blt
= (EFI_UGA_PIXEL
*)((UINT8
*)BltBuffer
+ (SourceY
* Delta
) + SourceX
* sizeof (EFI_UGA_PIXEL
));
455 Delta
-= Width
* sizeof (EFI_UGA_PIXEL
);
456 for (DstY
= DestinationY
; DstY
< (Height
+ DestinationY
); DstY
++) {
457 for (DstX
= DestinationX
; DstX
< (Width
+ DestinationX
); DstX
++) {
458 XPutPixel(Private
->image
, DstX
, DstY
, UgaPixelToColor(Private
, *Blt
));
461 Blt
= (EFI_UGA_PIXEL
*) ((UINT8
*) Blt
+ Delta
);
464 case EfiUgaVideoToVideo
:
465 Dst
= Private
->image_data
+ (DestinationX
<< Private
->pixel_shift
)
466 + DestinationY
* Private
->line_bytes
;
467 Src
= Private
->image_data
+ (SourceX
<< Private
->pixel_shift
)
468 + SourceY
* Private
->line_bytes
;
469 Nbr
= Width
<< Private
->pixel_shift
;
470 if (DestinationY
< SourceY
) {
471 for (Index
= 0; Index
< Height
; Index
++) {
472 memcpy (Dst
, Src
, Nbr
);
473 Dst
+= Private
->line_bytes
;
474 Src
+= Private
->line_bytes
;
478 Dst
+= (Height
- 1) * Private
->line_bytes
;
479 Src
+= (Height
- 1) * Private
->line_bytes
;
480 for (Index
= 0; Index
< Height
; Index
++) {
482 // Source and Destination Y may be equal, therefore Dst and Src may
485 memmove (Dst
, Src
, Nbr
);
486 Dst
-= Private
->line_bytes
;
487 Src
-= Private
->line_bytes
;
491 case EfiUgaVideoFill
:
492 Color
= UgaPixelToColor(Private
, *BltBuffer
);
493 for (DstY
= DestinationY
; DstY
< (Height
+ DestinationY
); DstY
++) {
494 for (DstX
= DestinationX
; DstX
< (Width
+ DestinationX
); DstX
++) {
495 XPutPixel(Private
->image
, DstX
, DstY
, Color
);
500 return EFI_INVALID_PARAMETER
;
506 switch (BltOperation
) {
507 case EfiUgaVideoToVideo
:
508 XCopyArea(Private
->display
, Private
->win
, Private
->win
, Private
->gc
,
509 SourceX
, SourceY
, Width
, Height
, DestinationX
, DestinationY
);
513 XNextEvent (Private
->display
, &ev
);
514 HandleEvent(Private
, &ev
);
515 if (ev
.type
== NoExpose
|| ev
.type
== GraphicsExpose
)
519 case EfiUgaVideoFill
:
520 Color
= UgaPixelToColor(Private
, *BltBuffer
);
521 XSetForeground(Private
->display
, Private
->gc
, Color
);
522 XFillRectangle(Private
->display
, Private
->win
, Private
->gc
,
523 DestinationX
, DestinationY
, Width
, Height
);
524 XFlush(Private
->display
);
526 case EfiUgaBltBufferToVideo
:
527 Redraw(Private
, DestinationX
, DestinationY
, Width
, Height
);
536 UgaCreate (EFI_UNIX_UGA_IO_PROTOCOL
**Uga
, CONST CHAR16
*Title
)
539 unsigned int border_width
= 0;
540 char *display_name
= NULL
;
543 drv
= (UGA_IO_PRIVATE
*)
544 calloc (1, sizeof (UGA_IO_PRIVATE
));
546 return EFI_OUT_OF_RESOURCES
;
548 drv
->UgaIo
.UgaClose
= UgaClose
;
549 drv
->UgaIo
.UgaSize
= UgaSize
;
550 drv
->UgaIo
.UgaCheckKey
= UgaCheckKey
;
551 drv
->UgaIo
.UgaGetKey
= UgaGetKey
;
552 drv
->UgaIo
.UgaBlt
= UgaBlt
;
557 drv
->display
= XOpenDisplay (display_name
);
558 if (drv
->display
== NULL
)
560 fprintf (stderr
, "uga: cannot connect to X server %s\n",
561 XDisplayName (display_name
));
563 return EFI_DEVICE_ERROR
;
565 drv
->screen
= DefaultScreen (drv
->display
);
566 drv
->visual
= DefaultVisual (drv
->display
, drv
->screen
);
567 drv
->win
= XCreateSimpleWindow
568 (drv
->display
, RootWindow (drv
->display
, drv
->screen
),
569 0, 0, 4, 4, border_width
,
570 WhitePixel (drv
->display
, drv
->screen
),
571 BlackPixel (drv
->display
, drv
->screen
));
573 drv
->depth
= DefaultDepth (drv
->display
, drv
->screen
);
575 /* Compute title len and convert to Ascii. */
576 for (title_len
= 0; Title
[title_len
] != 0; title_len
++)
579 char title
[title_len
+ 1];
581 for (i
= 0; i
< title_len
; i
++)
585 XStoreName (drv
->display
, drv
->win
, title
);
588 XSelectInput (drv
->display
, drv
->win
,
589 ExposureMask
| KeyPressMask
);
590 drv
->gc
= DefaultGC (drv
->display
, drv
->screen
);
592 *Uga
= (EFI_UNIX_UGA_IO_PROTOCOL
*)drv
;