blob: e029d9bb965d4b4e0a3cc4410261b859ed1196b9 [file] [log] [blame]
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -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
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <math.h>
30#include <time.h>
31#include <cairo.h>
32#include <glib.h>
33
34#include <GL/gl.h>
35#include <eagle.h>
36
37#include "wayland-client.h"
38#include "wayland-glib.h"
39
40#include "cairo-util.h"
41#include "window.h"
42
43static const char gem_device[] = "/dev/dri/card0";
44static const char socket_name[] = "\0wayland";
45
46struct terminal {
47 struct window *window;
48 struct wl_display *display;
49 int resize_scheduled;
50};
51
52static gboolean
53resize_window(void *data)
54{
55 struct terminal *terminal = data;
56
57 window_draw(terminal->window);
58 wl_display_commit(terminal->display, 0);
59
60 return FALSE;
61}
62
63static void
64resize_handler(struct window *window, int32_t width, int32_t height, void *data)
65{
66 struct terminal *terminal = data;
67
68 if (!terminal->resize_scheduled) {
69 g_idle_add(resize_window, terminal);
70 terminal->resize_scheduled = 1;
71 }
72}
73
74static void
75acknowledge_handler(struct window *window, uint32_t key, void *data)
76{
77 struct terminal *terminal = data;
78
79 terminal->resize_scheduled = 0;
80}
81
82static struct terminal *
83terminal_create(struct wl_display *display, int fd)
84{
85 struct terminal *terminal;
86
87 terminal = malloc(sizeof *terminal);
88 if (terminal == NULL)
89 return terminal;
90
91 terminal->window = window_create(display, fd, "Wayland Terminal",
92 500, 100, 300, 200);
93 terminal->display = display;
94 terminal->resize_scheduled = 1;
95
96 window_set_resize_handler(terminal->window, resize_handler, terminal);
97 window_set_acknowledge_handler(terminal->window, acknowledge_handler, terminal);
98
99 return terminal;
100}
101
102int main(int argc, char *argv[])
103{
104 struct wl_display *display;
105 int fd;
106 GMainLoop *loop;
107 GSource *source;
108 struct terminal *terminal;
109
110 fd = open(gem_device, O_RDWR);
111 if (fd < 0) {
112 fprintf(stderr, "drm open failed: %m\n");
113 return -1;
114 }
115
116 display = wl_display_create(socket_name, sizeof socket_name);
117 if (display == NULL) {
118 fprintf(stderr, "failed to create display: %m\n");
119 return -1;
120 }
121
122 loop = g_main_loop_new(NULL, FALSE);
123 source = wl_glib_source_new(display);
124 g_source_attach(source, NULL);
125
126 terminal = terminal_create(display, fd);
127 window_draw(terminal->window);
128 wl_display_commit(display, 0);
129
130 g_main_loop_run(loop);
131
132 return 0;
133}