This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on May 5th, 2011  |  syntax: C  |  size: 6.11 KB  |  views: 1,978  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  * Copyright © 2011 Alberto Ruiz <alberto.ruiz@codethink.co.uk>
  3.  * Copyright © 2011 Codethink Ltd.
  4.  *
  5.  * Permission to use, copy, modify, distribute, and sell this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided that
  7.  * the above copyright notice appear in all copies and that both that copyright
  8.  * notice and this permission notice appear in supporting documentation, and
  9.  * that the name of the copyright holders not be used in advertising or
  10.  * publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.  The copyright holders make no representations
  12.  * about the suitability of this software for any purpose.  It is provided "as
  13.  * is" without express or implied warranty.
  14.  *
  15.  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  16.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  17.  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  18.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  19.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  20.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  21.  * OF THIS SOFTWARE.
  22.  */
  23.  
  24. #include <wayland-client.h>
  25. #include <wayland-egl.h>
  26.  
  27. #include <cairo.h>
  28. #include <cairo-gl.h>
  29.  
  30. #include <EGL/egl.h>
  31.  
  32. #include <stdio.h>
  33. #include <string.h>
  34.  
  35. #define FALSE 0
  36. #define TRUE  1
  37.  
  38. struct client_info {
  39.         uint32_t               mask;
  40.         struct wl_compositor  *comp;
  41.         struct wl_egl_display *native;
  42.         struct wl_egl_window  *window;
  43.         struct wl_display     *disp;
  44.         EGLDisplay             egl_display;
  45.         EGLSurface             egl_surface;
  46.         EGLConfig              conf;
  47.         EGLContext             context;
  48.         struct wl_surface     *surface;
  49.         cairo_device_t        *cr_device;
  50.         cairo_surface_t       *cr_surface;
  51.         int                    run;
  52. };
  53.  
  54. void
  55. wl_egl_init (struct client_info *info)
  56. {
  57.         EGLint major, minor;
  58.         EGLint n;
  59.  
  60.         static const EGLint config_attribs[] = {
  61.                 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PIXMAP_BIT,
  62.                 EGL_RED_SIZE, 1,
  63.                 EGL_GREEN_SIZE, 1,
  64.                 EGL_BLUE_SIZE, 1,
  65.                 EGL_ALPHA_SIZE, 1,
  66.                 EGL_DEPTH_SIZE, 1,
  67.                 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
  68.                 EGL_NONE
  69.         };
  70.  
  71.         info->egl_display = eglGetDisplay(info->native);
  72.  
  73.         eglInitialize(info->egl_display, &major, &minor);
  74.  
  75.         eglBindAPI(EGL_OPENGL_API);
  76.  
  77.         eglChooseConfig(info->egl_display, config_attribs, &info->conf, 1, &n);
  78.  
  79.         info->context = eglCreateContext(info->egl_display,
  80.                                          info->conf,
  81.                                          EGL_NO_CONTEXT,
  82.                                          NULL);
  83.  
  84.         eglMakeCurrent(info->egl_display, NULL, NULL, info->context);
  85.         info->cr_device = cairo_egl_device_create(info->egl_display, info->context);
  86.  
  87.         return;
  88. }
  89.  
  90. static int
  91. event_mask_update (uint32_t mask, void *data)
  92. {
  93.         ((struct client_info*)data)->mask = mask;
  94.  
  95.         return 0;
  96. }
  97.  
  98. /* This callback is called by wl_display_frame_callback and performs cairo operations
  99.  * over the window surface to then swap buffers and show the results in the client window
  100.  */
  101. static void
  102. redraw(struct wl_surface *surface, void *data, uint32_t time)
  103. {
  104.         struct client_info *info = (struct client_info*)data;
  105.         cairo_t     *cr;
  106.  
  107.         cr = cairo_create (info->cr_surface);
  108.  
  109.         cairo_set_line_width (cr, 1.0);
  110.         cairo_rectangle      (cr, 0.25, 0.25, 70.0, 70.0);
  111.         cairo_set_source_rgb (cr, (time % 9 + 1) * 0.1, (time % 5 + 1) * 0.1, (time % 3 + 1) * 0.3);
  112.         cairo_fill (cr);
  113.         cairo_destroy (cr);
  114.         cairo_gl_surface_swapbuffers(info->cr_surface);
  115.         /* We need to call wl_display_frame_callback again if we want to redraw the surface past this point */
  116. }
  117.  
  118. static void
  119. display_handle_global (struct wl_display *disp,
  120.                        uint32_t           id,
  121.                        const char        *iface,
  122.                        uint32_t           version,
  123.                        void              *data)
  124. {
  125.         struct client_info* info = (struct client_info*)data;
  126.  
  127.         fprintf (stderr, "- WAYLAND DISPLAY INTERFACE: %s\n", iface);
  128.         if (!strcmp(iface, "compositor"))
  129.                 info->comp = wl_compositor_create (disp, id);
  130. }
  131.  
  132. /* This callback is called when the connection with the display server
  133.  * is finished and gets hold of the compositor object to create a window
  134.  * surface.
  135.  */
  136. static void
  137. sync_handle (void *data)
  138. {
  139.         struct wl_visual *visual;
  140.         struct client_info *info = (struct client_info*)data;
  141.  
  142.         if (!info->comp) {
  143.                 fprintf (stderr, "No compositor object was found\n");
  144.                 info->run = FALSE;
  145.                 return;
  146.         }
  147.  
  148.         /* The compositor is now available, we crate a window surface */
  149.         info->surface = wl_compositor_create_surface(info->comp);      
  150.  
  151.         visual = wl_display_get_premultiplied_argb_visual(info->disp);
  152.         info->window = wl_egl_window_create(info->egl_display,
  153.                                             info->surface,
  154.                                             100,
  155.                                             100,
  156.                                             visual);
  157.  
  158.         info->egl_surface = eglCreateWindowSurface(info->egl_display,
  159.                                                    info->conf,
  160.                                                    info->window,
  161.                                                    NULL);
  162.         eglMakeCurrent(info->egl_display, info->egl_surface,
  163.                        info->egl_surface, info->context);
  164.  
  165.         info->cr_surface  = cairo_gl_surface_create_for_egl(info->cr_device,
  166.                                                             info->egl_surface,
  167.                                                             100, 100);
  168.         wl_surface_map_toplevel(info->surface);
  169.         wl_display_frame_callback(info->disp, info->surface, redraw, info);
  170. }
  171.  
  172. int
  173. main (int argc, char** argv)
  174. {
  175.         struct client_info info = {0};
  176.         struct wl_display *disp = wl_display_connect (NULL);
  177.         info.run  = TRUE;
  178.         info.disp = disp;
  179.  
  180.         wl_display_add_global_listener(disp, display_handle_global, &info);
  181.  
  182.         info.native = wl_egl_display_create(disp);
  183.  
  184.         wl_egl_init (&info);
  185.        
  186.         wl_display_get_fd (disp, event_mask_update, &info);
  187.         wl_display_sync_callback(disp, sync_handle, &info);
  188.  
  189.         while (info.run)
  190.                 wl_display_iterate (disp, info.mask);
  191.  
  192.         return 0;
  193. }
clone this paste RAW Paste Data