1 #include "Uefi/UefiSpec.h"
2 #include "Protocol/UnixThunk.h"
3 #include "Protocol/SimpleTextIn.h"
4 #include "Protocol/UgaDraw.h"
5 #include "Protocol/UnixUgaIo.h"
9 #include <X11/extensions/XShm.h>
10 #include <X11/keysym.h>
18 struct uga_drv_shift_mask
28 EFI_UNIX_UGA_IO_PROTOCOL UgaIo
;
31 int screen
; /* values for window_size in main */
39 unsigned int line_bytes
;
40 unsigned int pixel_shift
;
41 unsigned char *image_data
;
43 struct uga_drv_shift_mask r
, g
, b
;
46 XShmSegmentInfo xshm_info
;
51 unsigned int key_count
;
52 EFI_INPUT_KEY keys
[NBR_KEYS
];
56 HandleEvents(UGA_IO_PRIVATE
*drv
);
59 fill_shift_mask (struct uga_drv_shift_mask
*sm
, unsigned long mask
)
63 while ((mask
& 1) == 0)
73 sm
->csize
= 8 - sm
->size
;
77 TryCreateShmImage(UGA_IO_PRIVATE
*drv
)
79 drv
->image
= XShmCreateImage (drv
->display
, drv
->visual
,
80 drv
->depth
, ZPixmap
, NULL
, &drv
->xshm_info
,
81 drv
->width
, drv
->height
);
82 if (drv
->image
== NULL
)
85 switch (drv
->image
->bitmap_unit
) {
97 drv
->xshm_info
.shmid
= shmget
98 (IPC_PRIVATE
, drv
->image
->bytes_per_line
* drv
->image
->height
,
100 if (drv
->xshm_info
.shmid
< 0)
102 XDestroyImage(drv
->image
);
106 drv
->image_data
= shmat (drv
->xshm_info
.shmid
, NULL
, 0);
109 shmctl (drv
->xshm_info
.shmid
, IPC_RMID
, NULL
);
110 XDestroyImage(drv
->image
);
113 /* Can this fail ? */
114 shmctl (drv
->xshm_info
.shmid
, IPC_RMID
, NULL
);
116 drv
->xshm_info
.shmaddr
= drv
->image_data
;
117 drv
->image
->data
= drv
->image_data
;
119 if (!XShmAttach (drv
->display
, &drv
->xshm_info
))
121 shmdt (drv
->image_data
);
122 XDestroyImage(drv
->image
);
130 UgaClose (EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
)
132 UGA_IO_PRIVATE
*drv
= (UGA_IO_PRIVATE
*)UgaIo
;
136 if (drv
->image
!= NULL
)
138 XDestroyImage(drv
->image
);
141 shmdt (drv
->image_data
);
143 drv
->image_data
= NULL
;
146 XDestroyWindow(drv
->display
, drv
->win
);
147 XCloseDisplay(drv
->display
);
154 UgaSize(EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
, UINT32 Width
, UINT32 Height
)
156 UGA_IO_PRIVATE
*drv
= (UGA_IO_PRIVATE
*)UgaIo
;
157 XSizeHints size_hints
;
159 /* Destroy current buffer if created. */
160 if (drv
->image
!= NULL
)
162 XDestroyImage(drv
->image
);
165 shmdt (drv
->image_data
);
167 drv
->image_data
= NULL
;
172 drv
->height
= Height
;
173 XResizeWindow (drv
->display
, drv
->win
, Width
, Height
);
175 /* Allocate image. */
176 if (XShmQueryExtension(drv
->display
) && TryCreateShmImage(drv
))
184 drv
->pixel_shift
= 2;
185 else if (drv
->depth
> 8)
186 drv
->pixel_shift
= 1;
188 drv
->pixel_shift
= 0;
190 drv
->image_data
= malloc((drv
->width
* drv
->height
) << drv
->pixel_shift
);
191 drv
->image
= XCreateImage (drv
->display
, drv
->visual
, drv
->depth
,
192 ZPixmap
, 0, drv
->image_data
,
193 drv
->width
, drv
->height
,
194 8 << drv
->pixel_shift
, 0);
196 drv
->line_bytes
= drv
->image
->bytes_per_line
;
197 fill_shift_mask (&drv
->r
, drv
->image
->red_mask
);
198 fill_shift_mask (&drv
->g
, drv
->image
->green_mask
);
199 fill_shift_mask (&drv
->b
, drv
->image
->blue_mask
);
202 size_hints
.flags
= PSize
| PMinSize
| PMaxSize
;
203 size_hints
.min_width
= size_hints
.max_width
= size_hints
.base_width
= Width
;
204 size_hints
.min_height
= size_hints
.max_height
= size_hints
.base_height
= Height
;
205 XSetWMNormalHints (drv
->display
, drv
->win
, &size_hints
);
207 XMapWindow (drv
->display
, drv
->win
);
213 handleKeyEvent(UGA_IO_PRIVATE
*drv
, XEvent
*ev
)
220 if (drv
->key_count
== NBR_KEYS
)
223 res
= XLookupString(&ev
->xkey
, str
, sizeof(str
), &keysym
, NULL
);
227 case XK_Home
: Key
.ScanCode
= SCAN_HOME
; break;
228 case XK_End
: Key
.ScanCode
= SCAN_END
; break;
229 case XK_Left
: Key
.ScanCode
= SCAN_LEFT
; break;
230 case XK_Right
: Key
.ScanCode
= SCAN_RIGHT
; break;
231 case XK_Up
: Key
.ScanCode
= SCAN_UP
; break;
232 case XK_Down
: Key
.ScanCode
= SCAN_DOWN
; break;
233 case XK_Delete
: Key
.ScanCode
= SCAN_DELETE
; break;
234 case XK_Insert
: Key
.ScanCode
= SCAN_INSERT
; break;
235 case XK_Page_Up
: Key
.ScanCode
= SCAN_PAGE_UP
; break;
236 case XK_Page_Down
: Key
.ScanCode
= SCAN_PAGE_DOWN
; break;
237 case XK_Escape
: Key
.ScanCode
= SCAN_ESC
; break;
239 case XK_F1
: Key
.ScanCode
= SCAN_F1
; break;
240 case XK_F2
: Key
.ScanCode
= SCAN_F2
; break;
241 case XK_F3
: Key
.ScanCode
= SCAN_F3
; break;
242 case XK_F4
: Key
.ScanCode
= SCAN_F4
; break;
243 case XK_F5
: Key
.ScanCode
= SCAN_F5
; break;
244 case XK_F6
: Key
.ScanCode
= SCAN_F6
; break;
245 case XK_F7
: Key
.ScanCode
= SCAN_F7
; break;
246 case XK_F8
: Key
.ScanCode
= SCAN_F8
; break;
247 case XK_F9
: Key
.ScanCode
= SCAN_F9
; break;
251 Key
.UnicodeChar
= str
[0];
257 drv
->keys
[drv
->key_wr
] = Key
;
258 drv
->key_wr
= (drv
->key_wr
+ 1) % NBR_KEYS
;
263 Redraw(UGA_IO_PRIVATE
*drv
, UINTN X
, UINTN Y
, UINTN Width
, UINTN Height
)
266 XShmPutImage (drv
->display
, drv
->win
, drv
->gc
, drv
->image
,
267 X
, Y
, X
, Y
, Width
, Height
, False
);
269 XPutImage (drv
->display
, drv
->win
, drv
->gc
, drv
->image
,
270 X
, Y
, X
, Y
, Width
, Height
);
274 HandleEvent(UGA_IO_PRIVATE
*drv
, XEvent
*ev
)
279 Redraw(drv
, ev
->xexpose
.x
, ev
->xexpose
.y
,
280 ev
->xexpose
.width
, ev
->xexpose
.height
);
283 Redraw(drv
, ev
->xgraphicsexpose
.x
, ev
->xgraphicsexpose
.y
,
284 ev
->xgraphicsexpose
.width
, ev
->xgraphicsexpose
.height
);
287 handleKeyEvent(drv
, ev
);
290 XRefreshKeyboardMapping(&ev
->xmapping
);
294 XCloseDisplay (drv
->display
);
305 HandleEvents(UGA_IO_PRIVATE
*drv
)
307 while (XPending(drv
->display
) != 0)
311 XNextEvent (drv
->display
, &ev
);
312 HandleEvent(drv
, &ev
);
318 UgaPixelToColor (UGA_IO_PRIVATE
*drv
, EFI_UGA_PIXEL pixel
)
320 return ((pixel
.Red
>> drv
->r
.csize
) << drv
->r
.shift
)
321 | ((pixel
.Green
>> drv
->g
.csize
) << drv
->g
.shift
)
322 | ((pixel
.Blue
>> drv
->b
.csize
) << drv
->b
.shift
);
327 UgaColorToPixel (UGA_IO_PRIVATE
*drv
, unsigned long val
)
331 /* FIXME: should round instead of truncate. */
332 res
.Red
= (val
>> drv
->r
.shift
) << drv
->r
.csize
;
333 res
.Green
= (val
>> drv
->g
.shift
) << drv
->g
.csize
;
334 res
.Blue
= (val
>> drv
->b
.shift
) << drv
->b
.csize
;
341 UgaCheckKey(EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
)
343 UGA_IO_PRIVATE
*drv
= (UGA_IO_PRIVATE
*)UgaIo
;
345 if (drv
->key_count
!= 0)
348 /* EFI is certainly polling. Be CPU-friendly. */
350 return EFI_NOT_READY
;
356 UgaGetKey(EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
, EFI_INPUT_KEY
*key
)
358 UGA_IO_PRIVATE
*drv
= (UGA_IO_PRIVATE
*)UgaIo
;
361 status
= UgaCheckKey(UgaIo
);
362 if (status
!= EFI_SUCCESS
)
365 *key
= drv
->keys
[drv
->key_rd
];
366 drv
->key_rd
= (drv
->key_rd
+ 1) % NBR_KEYS
;
372 UgaBlt(EFI_UNIX_UGA_IO_PROTOCOL
*UgaIo
,
373 IN EFI_UGA_PIXEL
*BltBuffer OPTIONAL
,
374 IN EFI_UGA_BLT_OPERATION BltOperation
,
377 IN UINTN DestinationX
,
378 IN UINTN DestinationY
,
381 IN UINTN Delta OPTIONAL
384 UGA_IO_PRIVATE
*Private
= (UGA_IO_PRIVATE
*)UgaIo
;
399 if (BltOperation
== EfiUgaVideoToBltBuffer
400 || BltOperation
== EfiUgaVideoToVideo
) {
404 if (SourceY
+ Height
> Private
->height
) {
405 return EFI_INVALID_PARAMETER
;
408 if (SourceX
+ Width
> Private
->width
) {
409 return EFI_INVALID_PARAMETER
;
413 if (BltOperation
== EfiUgaBltBufferToVideo
414 || BltOperation
== EfiUgaVideoToVideo
415 || BltOperation
== EfiUgaVideoFill
) {
417 // Destination is Video
419 if (DestinationY
+ Height
> Private
->height
) {
420 return EFI_INVALID_PARAMETER
;
423 if (DestinationX
+ Width
> Private
->width
) {
424 return EFI_INVALID_PARAMETER
;
428 switch (BltOperation
) {
429 case EfiUgaVideoToBltBuffer
:
431 Delta
-= Width
* sizeof (EFI_UGA_PIXEL
);
432 for (SrcY
= SourceY
; SrcY
< (Height
+ SourceY
); SrcY
++) {
433 for (SrcX
= SourceX
; SrcX
< (Width
+ SourceX
); SrcX
++) {
434 *Blt
++ = UgaColorToPixel(Private
,
435 XGetPixel(Private
->image
, SrcX
, SrcY
));
437 Blt
= (EFI_UGA_PIXEL
*) ((UINT8
*) Blt
+ Delta
);
440 case EfiUgaBltBufferToVideo
:
442 Delta
-= Width
* sizeof (EFI_UGA_PIXEL
);
443 for (DstY
= DestinationY
; DstY
< (Height
+ DestinationY
); DstY
++) {
444 for (DstX
= DestinationX
; DstX
< (Width
+ DestinationX
); DstX
++) {
445 XPutPixel(Private
->image
, DstX
, DstY
, UgaPixelToColor(Private
, *Blt
));
448 Blt
= (EFI_UGA_PIXEL
*) ((UINT8
*) Blt
+ Delta
);
451 case EfiUgaVideoToVideo
:
452 Dst
= Private
->image_data
+ (DestinationX
<< Private
->pixel_shift
)
453 + DestinationY
* Private
->line_bytes
;
454 Src
= Private
->image_data
+ (SourceX
<< Private
->pixel_shift
)
455 + SourceY
* Private
->line_bytes
;
456 Nbr
= Width
<< Private
->pixel_shift
;
457 if (DestinationY
< SourceY
) {
458 for (Index
= 0; Index
< Height
; Index
++) {
459 memcpy (Dst
, Src
, Nbr
);
460 Dst
+= Private
->line_bytes
;
461 Src
+= Private
->line_bytes
;
465 Dst
+= (Height
- 1) * Private
->line_bytes
;
466 Src
+= (Height
- 1) * Private
->line_bytes
;
467 for (Index
= 0; Index
< Height
; Index
++) {
469 // Source and Destination Y may be equal, therefore Dst and Src may
472 memmove (Dst
, Src
, Nbr
);
473 Dst
-= Private
->line_bytes
;
474 Src
-= Private
->line_bytes
;
478 case EfiUgaVideoFill
:
479 Color
= UgaPixelToColor(Private
, *BltBuffer
);
480 for (DstY
= DestinationY
; DstY
< (Height
+ DestinationY
); DstY
++) {
481 for (DstX
= DestinationX
; DstX
< (Width
+ DestinationX
); DstX
++) {
482 XPutPixel(Private
->image
, DstX
, DstY
, Color
);
487 return EFI_INVALID_PARAMETER
;
493 switch (BltOperation
) {
494 case EfiUgaVideoToVideo
:
495 XCopyArea(Private
->display
, Private
->win
, Private
->win
, Private
->gc
,
496 SourceX
, SourceY
, Width
, Height
, DestinationX
, DestinationY
);
500 XNextEvent (Private
->display
, &ev
);
501 HandleEvent(Private
, &ev
);
502 if (ev
.type
== NoExpose
|| ev
.type
== GraphicsExpose
)
506 case EfiUgaVideoFill
:
507 Color
= UgaPixelToColor(Private
, *BltBuffer
);
508 XSetForeground(Private
->display
, Private
->gc
, Color
);
509 XFillRectangle(Private
->display
, Private
->win
, Private
->gc
,
510 DestinationX
, DestinationY
, Width
, Height
);
512 case EfiUgaBltBufferToVideo
:
513 Redraw(Private
, DestinationX
, DestinationY
, Width
, Height
);
522 UgaCreate (EFI_UNIX_UGA_IO_PROTOCOL
**Uga
, CONST CHAR16
*Title
)
525 unsigned int border_width
= 0;
526 char *display_name
= NULL
;
529 drv
= (UGA_IO_PRIVATE
*)
530 malloc (sizeof (UGA_IO_PRIVATE
));
532 return EFI_OUT_OF_RESOURCES
;
534 drv
->UgaIo
.UgaClose
= UgaClose
;
535 drv
->UgaIo
.UgaSize
= UgaSize
;
536 drv
->UgaIo
.UgaCheckKey
= UgaCheckKey
;
537 drv
->UgaIo
.UgaGetKey
= UgaGetKey
;
538 drv
->UgaIo
.UgaBlt
= UgaBlt
;
543 drv
->display
= XOpenDisplay (display_name
);
544 if (drv
->display
== NULL
)
546 fprintf (stderr
, "uga: cannot connect to X server %s\n",
547 XDisplayName (display_name
));
549 return EFI_DEVICE_ERROR
;
551 drv
->screen
= DefaultScreen (drv
->display
);
552 drv
->visual
= DefaultVisual (drv
->display
, drv
->screen
);
553 drv
->win
= XCreateSimpleWindow
554 (drv
->display
, RootWindow (drv
->display
, drv
->screen
),
555 0, 0, 4, 4, border_width
,
556 BlackPixel (drv
->display
, drv
->screen
),
557 WhitePixel (drv
->display
, drv
->screen
));
559 drv
->depth
= DefaultDepth (drv
->display
, drv
->screen
);
561 /* Compute title len and convert to Ascii. */
562 for (title_len
= 0; Title
[title_len
] != 0; title_len
++)
565 char title
[title_len
+ 1];
567 for (i
= 0; i
< title_len
; i
++)
571 XStoreName (drv
->display
, drv
->win
, title
);
574 XSelectInput (drv
->display
, drv
->win
,
575 ExposureMask
| KeyPressMask
);
576 drv
->gc
= DefaultGC (drv
->display
, drv
->screen
);
578 *Uga
= (EFI_UNIX_UGA_IO_PROTOCOL
*)drv
;