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
);
143 UgaClose (EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
)
145 UGA_IO_PRIVATE
*drv
= (UGA_IO_PRIVATE
*)UgaIo
;
149 if (drv
->image
!= NULL
)
151 XDestroyImage(drv
->image
);
154 shmdt (drv
->image_data
);
156 drv
->image_data
= NULL
;
159 XDestroyWindow(drv
->display
, drv
->win
);
160 XCloseDisplay(drv
->display
);
166 UgaSize(EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
, UINT32 Width
, UINT32 Height
)
168 UGA_IO_PRIVATE
*drv
= (UGA_IO_PRIVATE
*)UgaIo
;
169 XSizeHints size_hints
;
171 /* Destroy current buffer if created. */
172 if (drv
->image
!= NULL
)
174 XDestroyImage(drv
->image
);
177 shmdt (drv
->image_data
);
179 drv
->image_data
= NULL
;
184 drv
->height
= Height
;
185 XResizeWindow (drv
->display
, drv
->win
, Width
, Height
);
187 /* Allocate image. */
188 if (XShmQueryExtension(drv
->display
) && TryCreateShmImage(drv
)) {
193 drv
->pixel_shift
= 2;
194 else if (drv
->depth
> 8)
195 drv
->pixel_shift
= 1;
197 drv
->pixel_shift
= 0;
199 drv
->image_data
= malloc((drv
->width
* drv
->height
) << drv
->pixel_shift
);
200 drv
->image
= XCreateImage (drv
->display
, drv
->visual
, drv
->depth
,
201 ZPixmap
, 0, (char *)drv
->image_data
,
202 drv
->width
, drv
->height
,
203 8 << drv
->pixel_shift
, 0);
205 drv
->line_bytes
= drv
->image
->bytes_per_line
;
206 fill_shift_mask (&drv
->r
, drv
->image
->red_mask
);
207 fill_shift_mask (&drv
->g
, drv
->image
->green_mask
);
208 fill_shift_mask (&drv
->b
, drv
->image
->blue_mask
);
211 size_hints
.flags
= PSize
| PMinSize
| PMaxSize
;
212 size_hints
.min_width
= size_hints
.max_width
= size_hints
.base_width
= Width
;
213 size_hints
.min_height
= size_hints
.max_height
= size_hints
.base_height
= Height
;
214 XSetWMNormalHints (drv
->display
, drv
->win
, &size_hints
);
216 XMapWindow (drv
->display
, drv
->win
);
222 handleKeyEvent(UGA_IO_PRIVATE
*drv
, XEvent
*ev
)
229 if (drv
->key_count
== NBR_KEYS
)
232 res
= XLookupString(&ev
->xkey
, str
, sizeof(str
), &keysym
, NULL
);
236 case XK_Home
: Key
.ScanCode
= SCAN_HOME
; break;
237 case XK_End
: Key
.ScanCode
= SCAN_END
; break;
238 case XK_Left
: Key
.ScanCode
= SCAN_LEFT
; break;
239 case XK_Right
: Key
.ScanCode
= SCAN_RIGHT
; break;
240 case XK_Up
: Key
.ScanCode
= SCAN_UP
; break;
241 case XK_Down
: Key
.ScanCode
= SCAN_DOWN
; break;
242 case XK_Delete
: Key
.ScanCode
= SCAN_DELETE
; break;
243 case XK_Insert
: Key
.ScanCode
= SCAN_INSERT
; break;
244 case XK_Page_Up
: Key
.ScanCode
= SCAN_PAGE_UP
; break;
245 case XK_Page_Down
: Key
.ScanCode
= SCAN_PAGE_DOWN
; break;
246 case XK_Escape
: Key
.ScanCode
= SCAN_ESC
; break;
248 case XK_F1
: Key
.ScanCode
= SCAN_F1
; break;
249 case XK_F2
: Key
.ScanCode
= SCAN_F2
; break;
250 case XK_F3
: Key
.ScanCode
= SCAN_F3
; break;
251 case XK_F4
: Key
.ScanCode
= SCAN_F4
; break;
252 case XK_F5
: Key
.ScanCode
= SCAN_F5
; break;
253 case XK_F6
: Key
.ScanCode
= SCAN_F6
; break;
254 case XK_F7
: Key
.ScanCode
= SCAN_F7
; break;
255 case XK_F8
: Key
.ScanCode
= SCAN_F8
; break;
256 case XK_F9
: Key
.ScanCode
= SCAN_F9
; break;
260 Key
.UnicodeChar
= str
[0];
266 drv
->keys
[drv
->key_wr
] = Key
;
267 drv
->key_wr
= (drv
->key_wr
+ 1) % NBR_KEYS
;
272 Redraw(UGA_IO_PRIVATE
*drv
, UINTN X
, UINTN Y
, UINTN Width
, UINTN Height
)
275 XShmPutImage (drv
->display
, drv
->win
, drv
->gc
, drv
->image
,
276 X
, Y
, X
, Y
, Width
, Height
, False
);
278 XPutImage (drv
->display
, drv
->win
, drv
->gc
, drv
->image
,
279 X
, Y
, X
, Y
, Width
, Height
);
280 XFlush(drv
->display
);
284 HandleEvent(UGA_IO_PRIVATE
*drv
, XEvent
*ev
)
289 Redraw(drv
, ev
->xexpose
.x
, ev
->xexpose
.y
,
290 ev
->xexpose
.width
, ev
->xexpose
.height
);
293 Redraw(drv
, ev
->xgraphicsexpose
.x
, ev
->xgraphicsexpose
.y
,
294 ev
->xgraphicsexpose
.width
, ev
->xgraphicsexpose
.height
);
297 handleKeyEvent(drv
, ev
);
300 XRefreshKeyboardMapping(&ev
->xmapping
);
304 XCloseDisplay (drv
->display
);
315 HandleEvents(UGA_IO_PRIVATE
*drv
)
317 while (XPending(drv
->display
) != 0)
321 XNextEvent (drv
->display
, &ev
);
322 HandleEvent(drv
, &ev
);
327 UgaPixelToColor (UGA_IO_PRIVATE
*drv
, EFI_UGA_PIXEL pixel
)
329 return ((pixel
.Red
>> drv
->r
.csize
) << drv
->r
.shift
)
330 | ((pixel
.Green
>> drv
->g
.csize
) << drv
->g
.shift
)
331 | ((pixel
.Blue
>> drv
->b
.csize
) << drv
->b
.shift
);
335 UgaColorToPixel (UGA_IO_PRIVATE
*drv
, unsigned long val
)
339 memset (&res
, 0, sizeof (EFI_UGA_PIXEL
));
340 /* FIXME: should round instead of truncate. */
341 res
.Red
= (val
>> drv
->r
.shift
) << drv
->r
.csize
;
342 res
.Green
= (val
>> drv
->g
.shift
) << drv
->g
.csize
;
343 res
.Blue
= (val
>> drv
->b
.shift
) << drv
->b
.csize
;
349 UgaCheckKey(EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
)
351 UGA_IO_PRIVATE
*drv
= (UGA_IO_PRIVATE
*)UgaIo
;
353 if (drv
->key_count
!= 0)
356 /* EFI is certainly polling. Be CPU-friendly. */
358 return EFI_NOT_READY
;
363 UgaGetKey(EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
, EFI_INPUT_KEY
*key
)
365 UGA_IO_PRIVATE
*drv
= (UGA_IO_PRIVATE
*)UgaIo
;
368 status
= UgaCheckKey(UgaIo
);
369 if (status
!= EFI_SUCCESS
)
372 *key
= drv
->keys
[drv
->key_rd
];
373 drv
->key_rd
= (drv
->key_rd
+ 1) % NBR_KEYS
;
379 UgaBlt(EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
,
380 IN EFI_UGA_PIXEL
*BltBuffer OPTIONAL
,
381 IN EFI_UGA_BLT_OPERATION BltOperation
,
384 IN UINTN DestinationX
,
385 IN UINTN DestinationY
,
388 IN UINTN Delta OPTIONAL
391 UGA_IO_PRIVATE
*Private
= (UGA_IO_PRIVATE
*)UgaIo
;
406 if (BltOperation
== EfiUgaVideoToBltBuffer
407 || BltOperation
== EfiUgaVideoToVideo
) {
411 if (SourceY
+ Height
> Private
->height
) {
412 return EFI_INVALID_PARAMETER
;
415 if (SourceX
+ Width
> Private
->width
) {
416 return EFI_INVALID_PARAMETER
;
420 if (BltOperation
== EfiUgaBltBufferToVideo
421 || BltOperation
== EfiUgaVideoToVideo
422 || BltOperation
== EfiUgaVideoFill
) {
424 // Destination is Video
426 if (DestinationY
+ Height
> Private
->height
) {
427 return EFI_INVALID_PARAMETER
;
430 if (DestinationX
+ Width
> Private
->width
) {
431 return EFI_INVALID_PARAMETER
;
435 switch (BltOperation
) {
436 case EfiUgaVideoToBltBuffer
:
437 Blt
= (EFI_UGA_PIXEL
*)((UINT8
*)BltBuffer
+ (DestinationY
* Delta
) + DestinationX
* sizeof (EFI_UGA_PIXEL
));
438 Delta
-= Width
* sizeof (EFI_UGA_PIXEL
);
439 for (SrcY
= SourceY
; SrcY
< (Height
+ SourceY
); SrcY
++) {
440 for (SrcX
= SourceX
; SrcX
< (Width
+ SourceX
); SrcX
++) {
441 *Blt
++ = UgaColorToPixel(Private
,
442 XGetPixel(Private
->image
, SrcX
, SrcY
));
444 Blt
= (EFI_UGA_PIXEL
*) ((UINT8
*) Blt
+ Delta
);
447 case EfiUgaBltBufferToVideo
:
448 Blt
= (EFI_UGA_PIXEL
*)((UINT8
*)BltBuffer
+ (SourceY
* Delta
) + SourceX
* sizeof (EFI_UGA_PIXEL
));
449 Delta
-= Width
* sizeof (EFI_UGA_PIXEL
);
450 for (DstY
= DestinationY
; DstY
< (Height
+ DestinationY
); DstY
++) {
451 for (DstX
= DestinationX
; DstX
< (Width
+ DestinationX
); DstX
++) {
452 XPutPixel(Private
->image
, DstX
, DstY
, UgaPixelToColor(Private
, *Blt
));
455 Blt
= (EFI_UGA_PIXEL
*) ((UINT8
*) Blt
+ Delta
);
458 case EfiUgaVideoToVideo
:
459 Dst
= Private
->image_data
+ (DestinationX
<< Private
->pixel_shift
)
460 + DestinationY
* Private
->line_bytes
;
461 Src
= Private
->image_data
+ (SourceX
<< Private
->pixel_shift
)
462 + SourceY
* Private
->line_bytes
;
463 Nbr
= Width
<< Private
->pixel_shift
;
464 if (DestinationY
< SourceY
) {
465 for (Index
= 0; Index
< Height
; Index
++) {
466 memcpy (Dst
, Src
, Nbr
);
467 Dst
+= Private
->line_bytes
;
468 Src
+= Private
->line_bytes
;
472 Dst
+= (Height
- 1) * Private
->line_bytes
;
473 Src
+= (Height
- 1) * Private
->line_bytes
;
474 for (Index
= 0; Index
< Height
; Index
++) {
476 // Source and Destination Y may be equal, therefore Dst and Src may
479 memmove (Dst
, Src
, Nbr
);
480 Dst
-= Private
->line_bytes
;
481 Src
-= Private
->line_bytes
;
485 case EfiUgaVideoFill
:
486 Color
= UgaPixelToColor(Private
, *BltBuffer
);
487 for (DstY
= DestinationY
; DstY
< (Height
+ DestinationY
); DstY
++) {
488 for (DstX
= DestinationX
; DstX
< (Width
+ DestinationX
); DstX
++) {
489 XPutPixel(Private
->image
, DstX
, DstY
, Color
);
494 return EFI_INVALID_PARAMETER
;
500 switch (BltOperation
) {
501 case EfiUgaVideoToVideo
:
502 XCopyArea(Private
->display
, Private
->win
, Private
->win
, Private
->gc
,
503 SourceX
, SourceY
, Width
, Height
, DestinationX
, DestinationY
);
507 XNextEvent (Private
->display
, &ev
);
508 HandleEvent(Private
, &ev
);
509 if (ev
.type
== NoExpose
|| ev
.type
== GraphicsExpose
)
513 case EfiUgaVideoFill
:
514 Color
= UgaPixelToColor(Private
, *BltBuffer
);
515 XSetForeground(Private
->display
, Private
->gc
, Color
);
516 XFillRectangle(Private
->display
, Private
->win
, Private
->gc
,
517 DestinationX
, DestinationY
, Width
, Height
);
518 XFlush(Private
->display
);
520 case EfiUgaBltBufferToVideo
:
521 Redraw(Private
, DestinationX
, DestinationY
, Width
, Height
);
530 UgaCreate (EFI_UNIX_UGA_IO_PROTOCOL
**Uga
, CONST CHAR16
*Title
)
533 unsigned int border_width
= 0;
534 char *display_name
= NULL
;
537 drv
= (UGA_IO_PRIVATE
*)
538 calloc (1, sizeof (UGA_IO_PRIVATE
));
540 return EFI_OUT_OF_RESOURCES
;
542 drv
->UgaIo
.UgaClose
= UgaClose
;
543 drv
->UgaIo
.UgaSize
= UgaSize
;
544 drv
->UgaIo
.UgaCheckKey
= UgaCheckKey
;
545 drv
->UgaIo
.UgaGetKey
= UgaGetKey
;
546 drv
->UgaIo
.UgaBlt
= UgaBlt
;
551 drv
->display
= XOpenDisplay (display_name
);
552 if (drv
->display
== NULL
)
554 fprintf (stderr
, "uga: cannot connect to X server %s\n",
555 XDisplayName (display_name
));
557 return EFI_DEVICE_ERROR
;
559 drv
->screen
= DefaultScreen (drv
->display
);
560 drv
->visual
= DefaultVisual (drv
->display
, drv
->screen
);
561 drv
->win
= XCreateSimpleWindow
562 (drv
->display
, RootWindow (drv
->display
, drv
->screen
),
563 0, 0, 4, 4, border_width
,
564 WhitePixel (drv
->display
, drv
->screen
),
565 BlackPixel (drv
->display
, drv
->screen
));
567 drv
->depth
= DefaultDepth (drv
->display
, drv
->screen
);
569 /* Compute title len and convert to Ascii. */
570 for (title_len
= 0; Title
[title_len
] != 0; title_len
++)
573 char title
[title_len
+ 1];
575 for (i
= 0; i
< title_len
; i
++)
579 XStoreName (drv
->display
, drv
->win
, title
);
582 XSelectInput (drv
->display
, drv
->win
,
583 ExposureMask
| KeyPressMask
);
584 drv
->gc
= DefaultGC (drv
->display
, drv
->screen
);
586 *Uga
= (EFI_UNIX_UGA_IO_PROTOCOL
*)drv
;