blob: ce7823a19b822235385576c8eadc2c410b7aedd2 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg61017b12008-11-02 18:51:48 -050023#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050027#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <time.h>
31#include <cairo.h>
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050032#include <glib.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050033
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050034#include <linux/input.h>
Kristian Høgsberg61017b12008-11-02 18:51:48 -050035#include "wayland-client.h"
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050036#include "wayland-glib.h"
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050037
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050038#include "cairo-util.h"
Kristian Høgsberg61017b12008-11-02 18:51:48 -050039
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050040#include "window.h"
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050041
Kristian Høgsberg61017b12008-11-02 18:51:48 -050042struct window {
Kristian Høgsberg40979232008-11-25 22:40:39 -050043 struct wl_display *display;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -050044 struct wl_compositor *compositor;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050045 struct wl_surface *surface;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050046 const char *title;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050047 struct rectangle allocation, saved_allocation, screen_allocation;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050048 int minimum_width, minimum_height;
Kristian Høgsberg40979232008-11-25 22:40:39 -050049 int margin;
Kristian Høgsbergc492b482008-12-12 12:00:02 -050050 int drag_x, drag_y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050051 int state;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050052 int fullscreen;
Kristian Høgsberg94448c02008-12-30 11:03:33 -050053 struct wl_input_device *grab_device;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050054 uint32_t name;
55 int fd;
Kristian Høgsberg78231c82008-11-08 15:06:01 -050056
57 struct buffer *buffer;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -050058
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050059 window_resize_handler_t resize_handler;
Kristian Høgsberg6e83d582008-12-08 00:01:36 -050060 window_key_handler_t key_handler;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050061 void *user_data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050062};
63
Kristian Høgsberge4feb562008-11-08 18:53:37 -050064static void
65rounded_rect(cairo_t *cr, int x0, int y0, int x1, int y1, int radius)
66{
67 cairo_move_to(cr, x0, y0 + radius);
68 cairo_arc(cr, x0 + radius, y0 + radius, radius, M_PI, 3 * M_PI / 2);
69 cairo_line_to(cr, x1 - radius, y0);
70 cairo_arc(cr, x1 - radius, y0 + radius, radius, 3 * M_PI / 2, 2 * M_PI);
71 cairo_line_to(cr, x1, y1 - radius);
72 cairo_arc(cr, x1 - radius, y1 - radius, radius, 0, M_PI / 2);
73 cairo_line_to(cr, x0 + radius, y1);
74 cairo_arc(cr, x0 + radius, y1 - radius, radius, M_PI / 2, M_PI);
75 cairo_close_path(cr);
76}
77
Kristian Høgsberg0395f302008-12-22 12:14:50 -050078static void
79window_draw_decorations(struct window *window)
Kristian Høgsberg61017b12008-11-02 18:51:48 -050080{
81 cairo_surface_t *surface;
82 cairo_t *cr;
Kristian Høgsberg40979232008-11-25 22:40:39 -050083 int border = 2, radius = 5;
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -050084 cairo_text_extents_t extents;
Kristian Høgsberge4feb562008-11-08 18:53:37 -050085 cairo_pattern_t *gradient, *outline, *bright, *dim;
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -050086 struct wl_visual *visual;
Kristian Høgsberg0395f302008-12-22 12:14:50 -050087 int width, height;
Kristian Høgsberg61017b12008-11-02 18:51:48 -050088
89 surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
Kristian Høgsberg0395f302008-12-22 12:14:50 -050090 window->allocation.width,
91 window->allocation.height);
Kristian Høgsberg61017b12008-11-02 18:51:48 -050092
Kristian Høgsberge4feb562008-11-08 18:53:37 -050093 outline = cairo_pattern_create_rgb(0.1, 0.1, 0.1);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -050094 bright = cairo_pattern_create_rgb(0.8, 0.8, 0.8);
Kristian Høgsberge4feb562008-11-08 18:53:37 -050095 dim = cairo_pattern_create_rgb(0.4, 0.4, 0.4);
96
Kristian Høgsberg61017b12008-11-02 18:51:48 -050097 cr = cairo_create(surface);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050098
Kristian Høgsberg0395f302008-12-22 12:14:50 -050099 width = window->allocation.width - window->margin * 2;
100 height = window->allocation.height - window->margin * 2;
101
Kristian Høgsberg40979232008-11-25 22:40:39 -0500102 cairo_translate(cr, window->margin + 7, window->margin + 5);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500103 cairo_set_line_width (cr, border);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500104 cairo_set_source_rgba(cr, 0, 0, 0, 0.7);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500105 rounded_rect(cr, 0, 0, width, height, radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500106 cairo_fill(cr);
Kristian Høgsberg87330262008-11-17 22:23:55 -0500107 blur_surface(surface, 24 + radius);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500108
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500109 cairo_translate(cr, -7, -5);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500110 cairo_set_line_width (cr, border);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500111 rounded_rect(cr, 1, 1, width - 1, height - 1, radius);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500112 cairo_set_source(cr, outline);
113 cairo_stroke(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500114 rounded_rect(cr, 2, 2, width - 2, height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500115 cairo_set_source(cr, bright);
116 cairo_stroke(cr);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500117 rounded_rect(cr, 3, 3, width - 2, height - 2, radius - 1);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500118 cairo_set_source(cr, dim);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500119 cairo_stroke(cr);
Kristian Høgsbergca1d1f62008-11-03 06:59:52 -0500120
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500121 rounded_rect(cr, 2, 2, width - 2, height - 2, radius - 1);
Kristian Høgsberg6e635f32008-11-09 09:15:46 -0500122 gradient = cairo_pattern_create_linear (0, 0, 0, 100);
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500123 cairo_pattern_add_color_stop_rgb(gradient, 0, 0.6, 0.6, 0.4);
124 cairo_pattern_add_color_stop_rgb(gradient, 1, 0.8, 0.8, 0.7);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500125 cairo_set_source(cr, gradient);
126 cairo_fill(cr);
127 cairo_pattern_destroy(gradient);
128
129 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
130 cairo_move_to(cr, 10, 50);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500131 cairo_line_to(cr, width - 10, 50);
132 cairo_line_to(cr, width - 10, height - 10);
133 cairo_line_to(cr, 10, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500134 cairo_close_path(cr);
135 cairo_set_source(cr, dim);
136 cairo_stroke(cr);
137
138 cairo_move_to(cr, 11, 51);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500139 cairo_line_to(cr, width - 10, 51);
140 cairo_line_to(cr, width - 10, height - 10);
141 cairo_line_to(cr, 11, height - 10);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500142 cairo_close_path(cr);
143 cairo_set_source(cr, bright);
144 cairo_stroke(cr);
145
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500146 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
147 cairo_set_font_size(cr, 14);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500148 cairo_text_extents(cr, window->title, &extents);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500149 cairo_move_to(cr, (width - extents.width) / 2, 10 - extents.y_bearing);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500150 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
151 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
152 cairo_set_line_width (cr, 4);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500153 cairo_text_path(cr, window->title);
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500154 cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);
155 cairo_stroke_preserve(cr);
156 cairo_set_source_rgb(cr, 1, 1, 1);
157 cairo_fill(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500158 cairo_destroy(cr);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500159
Kristian Høgsberg40979232008-11-25 22:40:39 -0500160 window->buffer = buffer_create_from_cairo_surface(window->fd, surface);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500161 cairo_surface_destroy(surface);
162
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500163 visual = wl_display_get_premultiplied_argb_visual(window->display);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500164 wl_surface_attach(window->surface,
165 window->buffer->name,
166 window->buffer->width,
167 window->buffer->height,
Kristian Høgsbergde31d5c2008-12-18 17:55:33 -0500168 window->buffer->stride,
169 visual);
Kristian Høgsbergb7a01922008-11-08 15:39:41 -0500170
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -0500171 wl_surface_map(window->surface,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500172 window->allocation.x - window->margin,
173 window->allocation.y - window->margin,
174 window->allocation.width,
175 window->allocation.height);
176}
177
178static void
179window_draw_fullscreen(struct window *window)
180{
181 struct wl_visual *visual;
182 int stride = window->allocation.width * 4;
183
184 window->buffer = buffer_create(window->fd,
185 window->allocation.width,
186 window->allocation.height,
187 stride);
188
189 visual = wl_display_get_premultiplied_argb_visual(window->display);
190 wl_surface_attach(window->surface,
191 window->buffer->name,
192 window->buffer->width,
193 window->buffer->height,
194 window->buffer->stride,
195 visual);
196
197 wl_surface_map(window->surface,
198 window->allocation.x,
199 window->allocation.y,
200 window->allocation.width,
201 window->allocation.height);
202}
203
204void
205window_draw(struct window *window)
206{
207 if (window->fullscreen)
208 window_draw_fullscreen(window);
209 else
210 window_draw_decorations(window);
Kristian Høgsberg44f36e32008-11-26 12:57:31 -0500211}
212
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500213static void
214window_handle_acknowledge(void *data,
215 struct wl_compositor *compositor,
216 uint32_t key, uint32_t frame)
217{
218 struct window *window = data;
219
220 /* The acknowledge event means that the server
221 * processed our last commit request and we can now
222 * safely free the old window buffer if we resized and
223 * render the next frame into our back buffer.. */
224
225 if (key == 0 && window->buffer != NULL) {
226 buffer_destroy(window->buffer, window->fd);
227 window->buffer = NULL;
228 }
229}
230
231static void
232window_handle_frame(void *data,
233 struct wl_compositor *compositor,
234 uint32_t frame, uint32_t timestamp)
235{
236}
237
238static const struct wl_compositor_listener compositor_listener = {
239 window_handle_acknowledge,
240 window_handle_frame,
241};
242
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500243enum window_state {
244 WINDOW_STABLE,
245 WINDOW_MOVING,
246 WINDOW_RESIZING_UPPER_LEFT,
247 WINDOW_RESIZING_UPPER_RIGHT,
248 WINDOW_RESIZING_LOWER_LEFT,
249 WINDOW_RESIZING_LOWER_RIGHT
250};
251
252enum location {
253 LOCATION_INTERIOR,
254 LOCATION_UPPER_LEFT,
255 LOCATION_UPPER_RIGHT,
256 LOCATION_LOWER_LEFT,
257 LOCATION_LOWER_RIGHT,
258 LOCATION_OUTSIDE
259};
260
Kristian Høgsberge4feb562008-11-08 18:53:37 -0500261static void
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500262window_handle_motion(void *data, struct wl_input_device *input_device,
263 int32_t x, int32_t y, int32_t sx, int32_t sy)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500264{
265 struct window *window = data;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500266
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500267 switch (window->state) {
268 case WINDOW_MOVING:
269 if (window->fullscreen)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500270 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500271 if (window->grab_device != input_device)
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500272 break;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500273 window->allocation.x = window->drag_x + x;
274 window->allocation.y = window->drag_y + y;
275 wl_surface_map(window->surface,
276 window->allocation.x - window->margin,
277 window->allocation.y - window->margin,
278 window->allocation.width,
279 window->allocation.height);
280 wl_compositor_commit(window->compositor, 1);
281 break;
282 case WINDOW_RESIZING_LOWER_RIGHT:
283 if (window->fullscreen)
284 break;
285 if (window->grab_device != input_device)
286 break;
287 window->allocation.width = window->drag_x + x;
288 window->allocation.height = window->drag_y + y;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500289
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500290 if (window->resize_handler)
291 (*window->resize_handler)(window,
292 window->user_data);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500293
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500294 break;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500295 }
296}
297
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500298static void window_handle_button(void *data, struct wl_input_device *input_device,
299 uint32_t button, uint32_t state,
300 int32_t x, int32_t y, int32_t sx, int32_t sy)
301{
302 struct window *window = data;
303 int32_t left = window->allocation.x;
304 int32_t right = window->allocation.x +
305 window->allocation.width - window->margin * 2;
306 int32_t top = window->allocation.y;
307 int32_t bottom = window->allocation.y +
308 window->allocation.height - window->margin * 2;
309 int grip_size = 16, location;
310
311 if (right - grip_size <= x && x < right &&
312 bottom - grip_size <= y && y < bottom) {
313 location = LOCATION_LOWER_RIGHT;
314 } else if (left <= x && x < right && top <= y && y < bottom) {
315 location = LOCATION_INTERIOR;
316 } else {
317 location = LOCATION_OUTSIDE;
318 }
319
320 if (button == BTN_LEFT && state == 1) {
321 switch (location) {
322 case LOCATION_INTERIOR:
323 window->drag_x = window->allocation.x - x;
324 window->drag_y = window->allocation.y - y;
325 window->state = WINDOW_MOVING;
326 window->grab_device = input_device;
327 break;
328 case LOCATION_LOWER_RIGHT:
329 window->drag_x = window->allocation.width - x;
330 window->drag_y = window->allocation.height - y;
331 window->state = WINDOW_RESIZING_LOWER_RIGHT;
332 window->grab_device = input_device;
333 break;
334 default:
335 window->state = WINDOW_STABLE;
336 break;
337 }
338 } else if (button == BTN_LEFT &&
339 state == 0 && window->grab_device == input_device) {
340 window->state = WINDOW_STABLE;
341 }
342}
343
344static void
345window_handle_key(void *data, struct wl_input_device *input_device,
346 uint32_t button, uint32_t state)
347{
348 struct window *window = data;
349
350 if (window->key_handler)
351 (*window->key_handler)(window, button, state,
352 window->user_data);
353}
354
355static const struct wl_input_device_listener input_device_listener = {
356 window_handle_motion,
357 window_handle_button,
358 window_handle_key,
359};
360
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500361void
362window_get_child_rectangle(struct window *window,
363 struct rectangle *rectangle)
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500364{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500365 if (window->fullscreen) {
366 *rectangle = window->allocation;
367 } else {
368 rectangle->x = window->margin + 10;
369 rectangle->y = window->margin + 50;
370 rectangle->width = window->allocation.width - 20 - window->margin * 2;
371 rectangle->height = window->allocation.height - 60 - window->margin * 2;
372 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500373}
374
375void
Kristian Høgsberg22106762008-12-08 13:50:07 -0500376window_set_child_size(struct window *window,
377 struct rectangle *rectangle)
378{
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500379 if (!window->fullscreen) {
380 window->allocation.width = rectangle->width + 20 + window->margin * 2;
381 window->allocation.height = rectangle->height + 60 + window->margin * 2;
382 }
Kristian Høgsberg22106762008-12-08 13:50:07 -0500383}
384
385void
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500386window_copy(struct window *window,
387 struct rectangle *rectangle,
388 uint32_t name, uint32_t stride)
389{
390 wl_surface_copy(window->surface,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500391 rectangle->x,
392 rectangle->y,
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500393 name, stride,
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500394 0, 0,
395 rectangle->width,
396 rectangle->height);
397}
398
399void
400window_set_fullscreen(struct window *window, int fullscreen)
401{
402 window->fullscreen = fullscreen;
403 if (window->fullscreen) {
404 window->saved_allocation = window->allocation;
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500405 window->allocation = window->screen_allocation;
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500406 } else {
407 window->allocation = window->saved_allocation;
408 }
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500409}
410
411void
412window_set_resize_handler(struct window *window,
413 window_resize_handler_t handler, void *data)
414{
415 window->resize_handler = handler;
416 window->user_data = data;
417}
418
419void
Kristian Høgsberg6e83d582008-12-08 00:01:36 -0500420window_set_key_handler(struct window *window,
421 window_key_handler_t handler, void *data)
422{
423 window->key_handler = handler;
424 window->user_data = data;
425}
426
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500427static void
428window_handle_geometry(void *data,
429 struct wl_output *output,
430 int32_t width, int32_t height)
431{
432 struct window *window = data;
433
434 window->screen_allocation.x = 0;
435 window->screen_allocation.y = 0;
436 window->screen_allocation.width = width;
437 window->screen_allocation.height = height;
438}
439
440static const struct wl_output_listener output_listener = {
441 window_handle_geometry,
442};
443
444static void
445window_handle_global(struct wl_display *display,
446 struct wl_object *object, void *data)
447{
448 struct window *window = data;
449
450 if (wl_object_implements(object, "compositor", 1)) {
451 window->compositor = (struct wl_compositor *) object;
452 wl_compositor_add_listener(window->compositor,
453 &compositor_listener, window);
454 } else if (wl_object_implements(object, "output", 1)) {
455 struct wl_output *output = (struct wl_output *) object;
456
457 wl_output_add_listener(output,
458 &output_listener, window);
459 } else if (wl_object_implements(object, "input_device", 1)) {
460 struct wl_input_device *input_device =
461 (struct wl_input_device *) object;
462
463 wl_input_device_add_listener(input_device,
464 &input_device_listener, window);
465 }
466}
467
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500468struct window *
469window_create(struct wl_display *display, int fd,
470 const char *title,
471 int32_t x, int32_t y, int32_t width, int32_t height)
472{
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500473 struct window *window;
474
475 window = malloc(sizeof *window);
476 if (window == NULL)
477 return NULL;
478
Kristian Høgsberg78231c82008-11-08 15:06:01 -0500479 memset(window, 0, sizeof *window);
Kristian Høgsberg40979232008-11-25 22:40:39 -0500480 window->display = display;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500481 window->title = strdup(title);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -0500482 window->compositor = wl_display_get_compositor(display);
483 window->surface = wl_compositor_create_surface(window->compositor);
Kristian Høgsberg0395f302008-12-22 12:14:50 -0500484 window->allocation.x = x;
485 window->allocation.y = y;
486 window->allocation.width = width;
487 window->allocation.height = height;
488 window->saved_allocation = window->allocation;
Kristian Høgsberg40979232008-11-25 22:40:39 -0500489 window->margin = 16;
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -0500490 window->state = WINDOW_STABLE;
491 window->fd = fd;
Kristian Høgsberg8a9cda82008-11-03 15:31:30 -0500492
Kristian Høgsberg94448c02008-12-30 11:03:33 -0500493 wl_display_add_global_listener(display,
494 window_handle_global, window);
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500495
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500496 return window;
Kristian Høgsberg61017b12008-11-02 18:51:48 -0500497}