Advertisement
Guest User

Untitled

a guest
Sep 28th, 2013
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <assert.h>
  3.  
  4. #include <wayland-client.h>
  5.  
  6. #include <sys/mman.h>
  7. #include <fcntl.h>
  8.  
  9.  
  10. struct display {
  11.     struct wl_display *display;
  12.     struct wl_compositor *compositor;
  13.     struct wl_registry *registry;
  14.     struct wl_shm *shm;
  15.     struct wl_shell *shell;
  16.     struct
  17.     {
  18.         int32_t x, y, width, height;
  19.     } screen_allocation;
  20. };
  21.  
  22. struct monbuffer {
  23.     struct wl_buffer *buffer;
  24.     void *shm_data;
  25.     int busy;
  26. };
  27.  
  28. struct window {
  29.     struct display *display;
  30.     struct wl_surface *surface;
  31.     struct wl_shell_surface *shell_surface;
  32.     struct monbuffer buffers[2];
  33.     struct wl_callback *callback;
  34.     int width;
  35.     int height;
  36. };
  37.  
  38.  
  39.  
  40. static void
  41. registry_handle_global (void *data, struct wl_registry *registry, uint32_t id, const char *interface, uint32_t version)
  42. {
  43.     struct display *d = data;
  44.  
  45.     if (strcmp(interface, "wl_compositor") == 0) {
  46.         d->compositor = wl_registry_bind (registry, id, &wl_compositor_interface, 1);
  47.     } else if (strcmp(interface, "wl_shell") == 0) {
  48.         d->shell = wl_registry_bind (registry, id, &wl_shell_interface, 1);
  49.     } else if (strcmp(interface, "wl_shm") == 0) {
  50.         d->shm = wl_registry_bind (registry, id, &wl_shm_interface, 1);
  51.     }
  52. }
  53.  
  54. static void
  55. registry_handle_global_remove (void *data, struct wl_registry *registry, uint32_t name)
  56. {
  57. }
  58.  
  59. static const struct wl_registry_listener registry_listener = {
  60.     registry_handle_global,
  61.     registry_handle_global_remove
  62. };
  63.  
  64.  
  65. static void
  66. handle_ping(void *data, struct wl_shell_surface *shell_surface, uint32_t serial)
  67. {
  68.     wl_shell_surface_pong (shell_surface, serial);
  69. }
  70.  
  71. static const struct wl_shell_surface_listener shell_surface_listener = {
  72.     handle_ping
  73. };
  74.  
  75.  
  76. static void
  77. buffer_release(void *data, struct wl_buffer *buffer)
  78. {
  79.     struct monbuffer *monbuf = data;
  80.     monbuf->busy = 0;
  81. }
  82.  
  83. static const struct wl_buffer_listener buffer_listener = {
  84.     buffer_release
  85. };
  86.  
  87.  
  88. static const struct wl_callback_listener frame_listener;
  89.  
  90. static void
  91. redraw(void *data, struct wl_callback *callback, uint32_t time)
  92. {
  93.     struct window *window = data;
  94.  
  95.     struct monbuffer *buffer;
  96.     if (!window->buffers[0].busy)
  97.         buffer = &window->buffers[0];
  98.     else if (!window->buffers[0].busy)
  99.         buffer = &window->buffers[1];
  100.     else
  101.         return NULL;
  102.  
  103.     if (!buffer->busy)
  104.     {
  105.         const char fd_name[] = "/tmp/tarnyko-shm.swp";
  106.         int fd = open (fd_name, O_CREAT | O_RDWR | O_TRUNC);
  107.         if (fd < 0)
  108.         {
  109.             printf ("Impossible de créer un fichier tampon !\n");
  110.             exit (-1);
  111.         }
  112.         long flags = fcntl (fd, F_GETFD);
  113.         if (fcntl (fd, F_SETFD, flags | FD_CLOEXEC) < 0)
  114.         {
  115.             printf ("Pas de CLOEXEC !\n");
  116.             exit (-1);
  117.         }
  118.         if (ftruncate (fd, 320*240*4) < 0)
  119.         {
  120.             printf ("Impossible d'allouer l'espace dans le fichier tampon !\n");
  121.             exit (-1);
  122.         }
  123.  
  124.         void *fd_contenu;
  125.         fd_contenu = mmap (0, 320*240*4, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  126.         if (fd_contenu == MAP_FAILED)
  127.         {
  128.             printf ("Impossible d'allouer de l'espace dans le fichier tampon !\n");
  129.             close (fd);
  130.             exit (-1);
  131.         }
  132.  
  133.         int result = 0;
  134.         struct wl_shm_pool *pool;
  135.         pool = wl_shm_create_pool(window->display->shm, fd, 320*240*4);
  136.         buffer->buffer = wl_shm_pool_create_buffer (pool, 0, 320, 240, 320*4, WL_SHM_FORMAT_XRGB8888);
  137.  
  138.         wl_buffer_add_listener (buffer->buffer, &buffer_listener, buffer);
  139.  
  140.         wl_shm_pool_destroy(pool);
  141.         close (fd);
  142.  
  143.         buffer->shm_data = fd_contenu;
  144.         memset(buffer->shm_data, 0xff, 320*240*4);
  145.     }
  146.  
  147.          // XXXXXXX TO BE CONTINUED ! DRAW SOMETHING !
  148.         //paint_pixels(buffer->shm_data, 20, window->width, window->height, time);
  149.  
  150.         uint32_t *pixels = buffer->shm_data;
  151.         pixels += 20*320;
  152.        
  153.         wl_surface_attach(window->surface, buffer->buffer, 0, 0);
  154.         wl_surface_damage(window->surface, 0, 0, 320, 240);
  155.  
  156.             // callback on itself
  157.     if (callback)
  158.         wl_callback_destroy (callback);
  159.     window->callback = wl_surface_frame (window->surface);
  160.     wl_callback_add_listener (window->callback, &frame_listener, window);
  161.     wl_surface_commit (window->surface);
  162.  
  163.         buffer->busy = 1;
  164. }
  165.  
  166. static const struct wl_callback_listener frame_listener = {
  167.     redraw
  168. };
  169.  
  170.  
  171.  
  172. int main ()
  173. {
  174.     struct display *display;
  175.     display = malloc (sizeof *display);
  176.  
  177.     display->display = wl_display_connect (NULL);
  178.     assert (display->display != NULL);
  179.  
  180.     display->registry = wl_display_get_registry (display->display);
  181.     wl_registry_add_listener (display->registry, &registry_listener, display);
  182.  
  183.     wl_display_roundtrip (display->display);
  184.     assert (display->shm != NULL);
  185.     wl_display_roundtrip (display->display);
  186.     wl_display_get_fd (display->display);
  187.  
  188.     struct window *window;
  189.     window = malloc (sizeof *window);
  190.      // 2nd WINDOW !
  191.     struct window *window2;
  192.     window2 = malloc (sizeof *window);
  193.  
  194.         window->display = display;
  195.         window2->display = display;
  196.         window->buffers[0].busy = 0;
  197.         window->buffers[1].busy = 0;
  198.         window->callback = NULL;
  199.         window2->buffers[0].busy = 0;
  200.         window2->buffers[1].busy = 0;
  201.         window2->callback = NULL;
  202.     window->surface = wl_compositor_create_surface (display->compositor);
  203.     window->shell_surface = wl_shell_get_shell_surface (display->shell, window->surface);
  204.     window2->surface = wl_compositor_create_surface (display->compositor);
  205.     window2->shell_surface = wl_shell_get_shell_surface (display->shell, window2->surface);
  206.  
  207.      // LET'S POSITION THEM RELATIVELY
  208. wl_shell_surface_set_transient (window2->shell_surface, window->surface, 100, 100, 0);
  209.  
  210.     wl_shell_surface_add_listener(window->shell_surface, &shell_surface_listener, window);
  211.     wl_shell_surface_add_listener(window2->shell_surface, &shell_surface_listener, window2);
  212.  
  213.     wl_shell_surface_set_title (window->shell_surface, "Exemple SHM");
  214.     wl_shell_surface_set_toplevel (window->shell_surface);
  215.     wl_shell_surface_set_title (window2->shell_surface, "Exemple SHM 2");
  216.     wl_shell_surface_set_toplevel (window2->shell_surface);
  217.     wl_surface_damage (window->surface, 0, 0, 320, 240);
  218.     wl_surface_damage (window2->surface, 0, 0, 320, 240);
  219.  
  220.      // MAIN DRAWING FUNCTION
  221.     redraw (window, NULL, 0);
  222.     redraw (window2, NULL, 0);
  223.  
  224.  
  225.      // MAIN LOOP
  226.     int result = 0;
  227.     while (result != -1)
  228.         result = wl_display_dispatch (display->display);
  229.  
  230.  
  231.     wl_registry_destroy (display->registry);
  232.     wl_display_flush (display->display);
  233.     wl_display_disconnect (display->display);
  234.  
  235.  
  236.     return 0;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement