Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include <inttypes.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/uio.h>
  7. #include <unistd.h>
  8. #include <X11/Xlib.h>
  9. #include <X11/Xutil.h>
  10. #include <X11/XKBlib.h>
  11.  
  12. int main(int argc, char *argv[]) {
  13.     if (argc != 5) {
  14.         printf("usage: lfbviewx <pid> <address> <width> <height>\n");
  15.         exit(1);
  16.     }
  17.     int depth = 32;
  18.     int umka_pid = strtol(argv[1], NULL, 0);
  19.     uintptr_t umka_lfb_addr = strtol(argv[2], NULL, 0);
  20.     size_t lfb_width = strtoul(argv[3], NULL, 0);
  21.     size_t lfb_height = strtoul(argv[4], NULL, 0);
  22.  
  23.     Display *display = XOpenDisplay(NULL);
  24.     int screen_num = XDefaultScreen(display);
  25.     Window root = XDefaultRootWindow(display);
  26.  
  27.     XVisualInfo vis_info;
  28.     if(!XMatchVisualInfo(display, screen_num, depth, TrueColor, &vis_info)) {
  29.         fprintf(stderr, "ERR: %d-bit depth is not supported\n", depth);
  30.         exit(1);
  31.     }
  32.  
  33.     Visual *visual = vis_info.visual;
  34.     XSetWindowAttributes win_attr = {
  35.         .colormap = XCreateColormap(display, root, visual, AllocNone),
  36.         .background_pixel = 0,
  37.         .border_pixel = 0};
  38.     unsigned long win_mask = CWBackPixel | CWColormap | CWBorderPixel;
  39.     Window window = XCreateWindow(display, root, 0, 0, lfb_width, lfb_height, 0, depth, InputOutput, visual, win_mask, &win_attr);
  40.     GC gc = XCreateGC(display, window, 0, 0);
  41.  
  42.     uint32_t *lfb = (uint32_t*)malloc(lfb_width*lfb_height*sizeof(uint32_t));
  43.     XImage *image = XCreateImage(display, visual, depth, ZPixmap, 0, (char*)lfb, lfb_width, lfb_height, 32, 0);
  44.  
  45.     XSelectInput(display, window, ExposureMask | KeyPressMask);
  46.     XStoreName(display, window, "KolibriOS LFB Viewer for X");
  47.     XMapWindow(display, window);
  48.  
  49.  
  50.     struct iovec remote = {.iov_base = (void*)umka_lfb_addr,
  51.                            .iov_len = lfb_width*lfb_height*4};
  52.     struct iovec local = {.iov_base = lfb,
  53.                           .iov_len = lfb_width*lfb_height*4};
  54. /*
  55.     XEvent event;
  56.     while (true) {
  57.         XNextEvent(display, &event);
  58.         if (event.type == Expose) {
  59.             process_vm_readv(umka_pid, &local, 1, &remote, 1, 0);
  60.             XPutImage(display, window, gc, image, 0, 0, 0, 0, lfb_width, lfb_height);
  61.         } else if (event.type == KeyPress) {
  62.             int keysym = XkbKeycodeToKeysym(display, event. xkey.keycode, 0, 0);
  63.  
  64.             if (keysym == XK_Escape) break;
  65.             switch (keysym) {
  66.             case XK_Left:   {break;}
  67.             }
  68.         }
  69.     }
  70. */
  71.     XEvent event;
  72.     while (true) {
  73.         while (XCheckMaskEvent(display, (long)-1, &event)) { /* skip */ }
  74.         process_vm_readv(umka_pid, &local, 1, &remote, 1, 0);
  75.         XPutImage(display, window, gc, image, 0, 0, 0, 0, lfb_width, lfb_height);
  76.         sleep(1);
  77.     }
  78.  
  79.     XCloseDisplay(display);
  80.     return 0;
  81. }
  82.