8 #include <sys/socket.h>
13 #include "connection.h"
14 #include "wayland-client.h"
16 static const char socket_name[] = "\0wayland";
19 struct wl_display *display;
24 struct wl_proxy proxy;
25 struct wl_connection *connection;
29 wl_display_event_func_t event_handler;
30 void *event_handler_data;
34 struct wl_proxy proxy;
38 wl_display_create(const char *address,
39 wl_connection_update_func_t update, void *data)
41 struct wl_display *display;
42 struct sockaddr_un name;
47 display = malloc(sizeof *display);
51 memset(display, 0, sizeof *display);
52 display->fd = socket(PF_LOCAL, SOCK_STREAM, 0);
53 if (display->fd < 0) {
58 name.sun_family = AF_LOCAL;
59 memcpy(name.sun_path, address, strlen(address + 1) + 2);
61 size = offsetof (struct sockaddr_un, sun_path) + sizeof socket_name;
63 if (connect(display->fd, (struct sockaddr *) &name, size) < 0) {
69 /* FIXME: We'll need a protocol for getting a new range, I
71 read(display->fd, &display->id, sizeof display->id);
73 /* FIXME: actually discover advertised objects here. */
74 read(display->fd, &id, sizeof id);
75 read(display->fd, &length, sizeof length);
76 read(display->fd, buffer, (length + 3) & ~3);
78 display->proxy.display = display;
79 display->proxy.id = id;
81 display->connection = wl_connection_create(display->fd,
88 wl_display_destroy(struct wl_display *display)
90 wl_connection_destroy(display->connection);
96 wl_display_get_fd(struct wl_display *display)
102 handle_event(struct wl_display *display, uint32_t opcode, uint32_t size)
106 wl_connection_copy(display->connection, p, size);
107 if (display->event_handler != NULL)
108 display->event_handler(display, opcode, p[2], p[3],
109 display->event_handler_data);
110 wl_connection_consume(display->connection, size);
114 wl_display_iterate(struct wl_display *display, uint32_t mask)
116 uint32_t p[2], opcode, size;
119 len = wl_connection_data(display->connection, mask);
124 wl_connection_copy(display->connection, p, sizeof p);
125 opcode = p[1] & 0xffff;
130 handle_event(display, opcode, size);
135 fprintf(stderr, "read error: %m\n");
141 wl_display_set_event_handler(struct wl_display *display,
142 wl_display_event_func_t handler,
145 /* FIXME: This needs something more generic... */
146 display->event_handler = handler;
147 display->event_handler_data = data;
150 #define WL_DISPLAY_CREATE_SURFACE 0
153 wl_display_create_surface(struct wl_display *display)
155 struct wl_surface *surface;
158 surface = malloc(sizeof *surface);
162 surface->proxy.id = display->id++;
163 surface->proxy.display = display;
165 request[0] = display->proxy.id;
166 request[1] = WL_DISPLAY_CREATE_SURFACE | ((sizeof request) << 16);
167 request[2] = surface->proxy.id;
168 wl_connection_write(display->connection, request, sizeof request);
173 #define WL_SURFACE_DESTROY 0
174 #define WL_SURFACE_ATTACH 1
175 #define WL_SURFACE_MAP 2
177 void wl_surface_destroy(struct wl_surface *surface)
181 request[0] = surface->proxy.id;
182 request[1] = WL_SURFACE_DESTROY | ((sizeof request) << 16);
184 wl_connection_write(surface->proxy.display->connection,
185 request, sizeof request);
188 void wl_surface_attach(struct wl_surface *surface,
189 uint32_t name, int width, int height, int stride)
193 request[0] = surface->proxy.id;
194 request[1] = WL_SURFACE_ATTACH | ((sizeof request) << 16);
200 wl_connection_write(surface->proxy.display->connection,
201 request, sizeof request);
205 void wl_surface_map(struct wl_surface *surface,
206 int32_t x, int32_t y, int32_t width, int32_t height)
210 request[0] = surface->proxy.id;
211 request[1] = WL_SURFACE_MAP | ((sizeof request) << 16);
217 wl_connection_write(surface->proxy.display->connection,
218 request, sizeof request);