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 <Ppi/StatusCode.h>
20 #include <Library/PeCoffLib.h>
21 #include <Library/BaseLib.h>
22 #include <Library/BaseMemoryLib.h>
23 #include <Library/PrintLib.h>
24 #include <Library/PcdLib.h>
25 #include <Library/DebugLib.h>
27 extern void msSleep (unsigned long Milliseconds
);
31 struct uga_drv_shift_mask
41 EFI_UNIX_UGA_IO_PROTOCOL UgaIo
;
44 int screen
; /* values for window_size in main */
52 unsigned int line_bytes
;
53 unsigned int pixel_shift
;
54 unsigned char *image_data
;
56 struct uga_drv_shift_mask r
, g
, b
;
59 XShmSegmentInfo xshm_info
;
64 unsigned int key_count
;
65 EFI_INPUT_KEY keys
[NBR_KEYS
];
69 HandleEvents(UGA_IO_PRIVATE
*drv
);
72 fill_shift_mask (struct uga_drv_shift_mask
*sm
, unsigned long mask
)
76 while ((mask
& 1) == 0)
86 sm
->csize
= 8 - sm
->size
;
90 TryCreateShmImage(UGA_IO_PRIVATE
*drv
)
92 drv
->image
= XShmCreateImage (drv
->display
, drv
->visual
,
93 drv
->depth
, ZPixmap
, NULL
, &drv
->xshm_info
,
94 drv
->width
, drv
->height
);
95 if (drv
->image
== NULL
)
98 switch (drv
->image
->bitmap_unit
) {
100 drv
->pixel_shift
= 2;
103 drv
->pixel_shift
= 1;
106 drv
->pixel_shift
= 0;
110 drv
->xshm_info
.shmid
= shmget
111 (IPC_PRIVATE
, drv
->image
->bytes_per_line
* drv
->image
->height
,
113 if (drv
->xshm_info
.shmid
< 0)
115 XDestroyImage(drv
->image
);
119 drv
->image_data
= shmat (drv
->xshm_info
.shmid
, NULL
, 0);
122 shmctl (drv
->xshm_info
.shmid
, IPC_RMID
, NULL
);
123 XDestroyImage(drv
->image
);
126 /* Can this fail ? */
127 shmctl (drv
->xshm_info
.shmid
, IPC_RMID
, NULL
);
129 drv
->xshm_info
.shmaddr
= (char*)drv
->image_data
;
130 drv
->image
->data
= (char*)drv
->image_data
;
132 if (!XShmAttach (drv
->display
, &drv
->xshm_info
))
134 shmdt (drv
->image_data
);
135 XDestroyImage(drv
->image
);
142 UgaClose (EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
)
144 UGA_IO_PRIVATE
*drv
= (UGA_IO_PRIVATE
*)UgaIo
;
148 if (drv
->image
!= NULL
)
150 XDestroyImage(drv
->image
);
153 shmdt (drv
->image_data
);
155 drv
->image_data
= NULL
;
158 XDestroyWindow(drv
->display
, drv
->win
);
159 XCloseDisplay(drv
->display
);
165 UgaSize(EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
, UINT32 Width
, UINT32 Height
)
167 UGA_IO_PRIVATE
*drv
= (UGA_IO_PRIVATE
*)UgaIo
;
168 XSizeHints size_hints
;
170 /* Destroy current buffer if created. */
171 if (drv
->image
!= NULL
)
173 XDestroyImage(drv
->image
);
176 shmdt (drv
->image_data
);
178 drv
->image_data
= NULL
;
183 drv
->height
= Height
;
184 XResizeWindow (drv
->display
, drv
->win
, Width
, Height
);
186 /* Allocate image. */
187 if (XShmQueryExtension(drv
->display
) && TryCreateShmImage(drv
)) {
192 drv
->pixel_shift
= 2;
193 else if (drv
->depth
> 8)
194 drv
->pixel_shift
= 1;
196 drv
->pixel_shift
= 0;
198 drv
->image_data
= malloc((drv
->width
* drv
->height
) << drv
->pixel_shift
);
199 drv
->image
= XCreateImage (drv
->display
, drv
->visual
, drv
->depth
,
200 ZPixmap
, 0, (char *)drv
->image_data
,
201 drv
->width
, drv
->height
,
202 8 << drv
->pixel_shift
, 0);
204 drv
->line_bytes
= drv
->image
->bytes_per_line
;
205 fill_shift_mask (&drv
->r
, drv
->image
->red_mask
);
206 fill_shift_mask (&drv
->g
, drv
->image
->green_mask
);
207 fill_shift_mask (&drv
->b
, drv
->image
->blue_mask
);
210 size_hints
.flags
= PSize
| PMinSize
| PMaxSize
;
211 size_hints
.min_width
= size_hints
.max_width
= size_hints
.base_width
= Width
;
212 size_hints
.min_height
= size_hints
.max_height
= size_hints
.base_height
= Height
;
213 XSetWMNormalHints (drv
->display
, drv
->win
, &size_hints
);
215 XMapWindow (drv
->display
, drv
->win
);
221 handleKeyEvent(UGA_IO_PRIVATE
*drv
, XEvent
*ev
)
228 if (drv
->key_count
== NBR_KEYS
)
231 res
= XLookupString(&ev
->xkey
, str
, sizeof(str
), &keysym
, NULL
);
235 case XK_Home
: Key
.ScanCode
= SCAN_HOME
; break;
236 case XK_End
: Key
.ScanCode
= SCAN_END
; break;
237 case XK_Left
: Key
.ScanCode
= SCAN_LEFT
; break;
238 case XK_Right
: Key
.ScanCode
= SCAN_RIGHT
; break;
239 case XK_Up
: Key
.ScanCode
= SCAN_UP
; break;
240 case XK_Down
: Key
.ScanCode
= SCAN_DOWN
; break;
241 case XK_Delete
: Key
.ScanCode
= SCAN_DELETE
; break;
242 case XK_Insert
: Key
.ScanCode
= SCAN_INSERT
; break;
243 case XK_Page_Up
: Key
.ScanCode
= SCAN_PAGE_UP
; break;
244 case XK_Page_Down
: Key
.ScanCode
= SCAN_PAGE_DOWN
; break;
245 case XK_Escape
: Key
.ScanCode
= SCAN_ESC
; break;
247 case XK_F1
: Key
.ScanCode
= SCAN_F1
; break;
248 case XK_F2
: Key
.ScanCode
= SCAN_F2
; break;
249 case XK_F3
: Key
.ScanCode
= SCAN_F3
; break;
250 case XK_F4
: Key
.ScanCode
= SCAN_F4
; break;
251 case XK_F5
: Key
.ScanCode
= SCAN_F5
; break;
252 case XK_F6
: Key
.ScanCode
= SCAN_F6
; break;
253 case XK_F7
: Key
.ScanCode
= SCAN_F7
; break;
254 case XK_F8
: Key
.ScanCode
= SCAN_F8
; break;
255 case XK_F9
: Key
.ScanCode
= SCAN_F9
; break;
259 Key
.UnicodeChar
= str
[0];
265 drv
->keys
[drv
->key_wr
] = Key
;
266 drv
->key_wr
= (drv
->key_wr
+ 1) % NBR_KEYS
;
271 Redraw(UGA_IO_PRIVATE
*drv
, UINTN X
, UINTN Y
, UINTN Width
, UINTN Height
)
274 XShmPutImage (drv
->display
, drv
->win
, drv
->gc
, drv
->image
,
275 X
, Y
, X
, Y
, Width
, Height
, False
);
277 XPutImage (drv
->display
, drv
->win
, drv
->gc
, drv
->image
,
278 X
, Y
, X
, Y
, Width
, Height
);
279 XFlush(drv
->display
);
283 HandleEvent(UGA_IO_PRIVATE
*drv
, XEvent
*ev
)
288 Redraw(drv
, ev
->xexpose
.x
, ev
->xexpose
.y
,
289 ev
->xexpose
.width
, ev
->xexpose
.height
);
292 Redraw(drv
, ev
->xgraphicsexpose
.x
, ev
->xgraphicsexpose
.y
,
293 ev
->xgraphicsexpose
.width
, ev
->xgraphicsexpose
.height
);
296 handleKeyEvent(drv
, ev
);
299 XRefreshKeyboardMapping(&ev
->xmapping
);
303 XCloseDisplay (drv
->display
);
314 HandleEvents(UGA_IO_PRIVATE
*drv
)
316 while (XPending(drv
->display
) != 0)
320 XNextEvent (drv
->display
, &ev
);
321 HandleEvent(drv
, &ev
);
326 UgaPixelToColor (UGA_IO_PRIVATE
*drv
, EFI_UGA_PIXEL pixel
)
328 return ((pixel
.Red
>> drv
->r
.csize
) << drv
->r
.shift
)
329 | ((pixel
.Green
>> drv
->g
.csize
) << drv
->g
.shift
)
330 | ((pixel
.Blue
>> drv
->b
.csize
) << drv
->b
.shift
);
334 UgaColorToPixel (UGA_IO_PRIVATE
*drv
, unsigned long val
)
338 memset (&res
, 0, sizeof (EFI_UGA_PIXEL
));
339 /* FIXME: should round instead of truncate. */
340 res
.Red
= (val
>> drv
->r
.shift
) << drv
->r
.csize
;
341 res
.Green
= (val
>> drv
->g
.shift
) << drv
->g
.csize
;
342 res
.Blue
= (val
>> drv
->b
.shift
) << drv
->b
.csize
;
348 UgaCheckKey(EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
)
350 UGA_IO_PRIVATE
*drv
= (UGA_IO_PRIVATE
*)UgaIo
;
352 if (drv
->key_count
!= 0)
355 /* EFI is certainly polling. Be CPU-friendly. */
357 return EFI_NOT_READY
;
362 UgaGetKey(EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
, EFI_INPUT_KEY
*key
)
364 UGA_IO_PRIVATE
*drv
= (UGA_IO_PRIVATE
*)UgaIo
;
367 status
= UgaCheckKey(UgaIo
);
368 if (status
!= EFI_SUCCESS
)
371 *key
= drv
->keys
[drv
->key_rd
];
372 drv
->key_rd
= (drv
->key_rd
+ 1) % NBR_KEYS
;
378 UgaBlt(EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
,
379 IN EFI_UGA_PIXEL
*BltBuffer OPTIONAL
,
380 IN EFI_UGA_BLT_OPERATION BltOperation
,
383 IN UINTN DestinationX
,
384 IN UINTN DestinationY
,
387 IN UINTN Delta OPTIONAL
390 UGA_IO_PRIVATE
*Private
= (UGA_IO_PRIVATE
*)UgaIo
;
405 if (BltOperation
== EfiUgaVideoToBltBuffer
406 || BltOperation
== EfiUgaVideoToVideo
) {
410 if (SourceY
+ Height
> Private
->height
) {
411 return EFI_INVALID_PARAMETER
;
414 if (SourceX
+ Width
> Private
->width
) {
415 return EFI_INVALID_PARAMETER
;
419 if (BltOperation
== EfiUgaBltBufferToVideo
420 || BltOperation
== EfiUgaVideoToVideo
421 || BltOperation
== EfiUgaVideoFill
) {
423 // Destination is Video
425 if (DestinationY
+ Height
> Private
->height
) {
426 return EFI_INVALID_PARAMETER
;
429 if (DestinationX
+ Width
> Private
->width
) {
430 return EFI_INVALID_PARAMETER
;
434 switch (BltOperation
) {
435 case EfiUgaVideoToBltBuffer
:
436 Blt
= (EFI_UGA_PIXEL
*)((UINT8
*)BltBuffer
+ (DestinationY
* Delta
) + DestinationX
* sizeof (EFI_UGA_PIXEL
));
437 Delta
-= Width
* sizeof (EFI_UGA_PIXEL
);
438 for (SrcY
= SourceY
; SrcY
< (Height
+ SourceY
); SrcY
++) {
439 for (SrcX
= SourceX
; SrcX
< (Width
+ SourceX
); SrcX
++) {
440 *Blt
++ = UgaColorToPixel(Private
,
441 XGetPixel(Private
->image
, SrcX
, SrcY
));
443 Blt
= (EFI_UGA_PIXEL
*) ((UINT8
*) Blt
+ Delta
);
446 case EfiUgaBltBufferToVideo
:
447 Blt
= (EFI_UGA_PIXEL
*)((UINT8
*)BltBuffer
+ (SourceY
* Delta
) + SourceX
* sizeof (EFI_UGA_PIXEL
));
448 Delta
-= Width
* sizeof (EFI_UGA_PIXEL
);
449 for (DstY
= DestinationY
; DstY
< (Height
+ DestinationY
); DstY
++) {
450 for (DstX
= DestinationX
; DstX
< (Width
+ DestinationX
); DstX
++) {
451 XPutPixel(Private
->image
, DstX
, DstY
, UgaPixelToColor(Private
, *Blt
));
454 Blt
= (EFI_UGA_PIXEL
*) ((UINT8
*) Blt
+ Delta
);
457 case EfiUgaVideoToVideo
:
458 Dst
= Private
->image_data
+ (DestinationX
<< Private
->pixel_shift
)
459 + DestinationY
* Private
->line_bytes
;
460 Src
= Private
->image_data
+ (SourceX
<< Private
->pixel_shift
)
461 + SourceY
* Private
->line_bytes
;
462 Nbr
= Width
<< Private
->pixel_shift
;
463 if (DestinationY
< SourceY
) {
464 for (Index
= 0; Index
< Height
; Index
++) {
465 memcpy (Dst
, Src
, Nbr
);
466 Dst
+= Private
->line_bytes
;
467 Src
+= Private
->line_bytes
;
471 Dst
+= (Height
- 1) * Private
->line_bytes
;
472 Src
+= (Height
- 1) * Private
->line_bytes
;
473 for (Index
= 0; Index
< Height
; Index
++) {
475 // Source and Destination Y may be equal, therefore Dst and Src may
478 memmove (Dst
, Src
, Nbr
);
479 Dst
-= Private
->line_bytes
;
480 Src
-= Private
->line_bytes
;
484 case EfiUgaVideoFill
:
485 Color
= UgaPixelToColor(Private
, *BltBuffer
);
486 for (DstY
= DestinationY
; DstY
< (Height
+ DestinationY
); DstY
++) {
487 for (DstX
= DestinationX
; DstX
< (Width
+ DestinationX
); DstX
++) {
488 XPutPixel(Private
->image
, DstX
, DstY
, Color
);
493 return EFI_INVALID_PARAMETER
;
499 switch (BltOperation
) {
500 case EfiUgaVideoToVideo
:
501 XCopyArea(Private
->display
, Private
->win
, Private
->win
, Private
->gc
,
502 SourceX
, SourceY
, Width
, Height
, DestinationX
, DestinationY
);
506 XNextEvent (Private
->display
, &ev
);
507 HandleEvent(Private
, &ev
);
508 if (ev
.type
== NoExpose
|| ev
.type
== GraphicsExpose
)
512 case EfiUgaVideoFill
:
513 Color
= UgaPixelToColor(Private
, *BltBuffer
);
514 XSetForeground(Private
->display
, Private
->gc
, Color
);
515 XFillRectangle(Private
->display
, Private
->win
, Private
->gc
,
516 DestinationX
, DestinationY
, Width
, Height
);
517 XFlush(Private
->display
);
519 case EfiUgaBltBufferToVideo
:
520 Redraw(Private
, DestinationX
, DestinationY
, Width
, Height
);
529 UgaCreate (EFI_UNIX_UGA_IO_PROTOCOL
**Uga
, CONST CHAR16
*Title
)
532 unsigned int border_width
= 0;
533 char *display_name
= NULL
;
536 drv
= (UGA_IO_PRIVATE
*)
537 calloc (1, sizeof (UGA_IO_PRIVATE
));
539 return EFI_OUT_OF_RESOURCES
;
541 drv
->UgaIo
.UgaClose
= UgaClose
;
542 drv
->UgaIo
.UgaSize
= UgaSize
;
543 drv
->UgaIo
.UgaCheckKey
= UgaCheckKey
;
544 drv
->UgaIo
.UgaGetKey
= UgaGetKey
;
545 drv
->UgaIo
.UgaBlt
= UgaBlt
;
550 drv
->display
= XOpenDisplay (display_name
);
551 if (drv
->display
== NULL
)
553 fprintf (stderr
, "uga: cannot connect to X server %s\n",
554 XDisplayName (display_name
));
556 return EFI_DEVICE_ERROR
;
558 drv
->screen
= DefaultScreen (drv
->display
);
559 drv
->visual
= DefaultVisual (drv
->display
, drv
->screen
);
560 drv
->win
= XCreateSimpleWindow
561 (drv
->display
, RootWindow (drv
->display
, drv
->screen
),
562 0, 0, 4, 4, border_width
,
563 WhitePixel (drv
->display
, drv
->screen
),
564 BlackPixel (drv
->display
, drv
->screen
));
566 drv
->depth
= DefaultDepth (drv
->display
, drv
->screen
);
568 /* Compute title len and convert to Ascii. */
569 for (title_len
= 0; Title
[title_len
] != 0; title_len
++)
572 char title
[title_len
+ 1];
574 for (i
= 0; i
< title_len
; i
++)
578 XStoreName (drv
->display
, drv
->win
, title
);
581 XSelectInput (drv
->display
, drv
->win
,
582 ExposureMask
| KeyPressMask
);
583 drv
->gc
= DefaultGC (drv
->display
, drv
->screen
);
585 *Uga
= (EFI_UNIX_UGA_IO_PROTOCOL
*)drv
;