blob: acd4f2bda2954f268605fb1adeefad32f5727087 [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>
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050031#include <pty.h>
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050032#include <cairo.h>
33#include <glib.h>
34
35#include <GL/gl.h>
36#include <eagle.h>
37
38#include "wayland-client.h"
39#include "wayland-glib.h"
40
41#include "cairo-util.h"
42#include "window.h"
43
44static const char gem_device[] = "/dev/dri/card0";
45static const char socket_name[] = "\0wayland";
46
47struct terminal {
48 struct window *window;
49 struct wl_display *display;
50 int resize_scheduled;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050051 char *data;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050052 int width, height, tail, row, column;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050053 int fd;
54 struct buffer *buffer;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050055 GIOChannel *channel;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -050056};
57
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050058static void
59terminal_draw_contents(struct terminal *terminal)
60{
61 struct rectangle rectangle;
62 cairo_surface_t *surface;
63 cairo_t *cr;
64 cairo_font_extents_t extents;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050065 int i, line;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050066
67 window_get_child_rectangle(terminal->window, &rectangle);
68
69 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
70 rectangle.width, rectangle.height);
71 cr = cairo_create(surface);
72 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
73 cairo_set_source_rgba(cr, 0, 0, 0, 0.9);
74 cairo_paint(cr);
75 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
76 cairo_set_source_rgba(cr, 0, 0.5, 0, 1);
77
78 cairo_select_font_face (cr, "mono",
79 CAIRO_FONT_SLANT_NORMAL,
80 CAIRO_FONT_WEIGHT_NORMAL);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050081 cairo_set_font_size(cr, 14);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050082
83 cairo_font_extents(cr, &extents);
84 for (i = 0; i < terminal->height; i++) {
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050085 line = (terminal->tail + i) % terminal->height;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050086 cairo_move_to(cr, 0, extents.ascent + extents.height * i);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -050087 cairo_show_text(cr, &terminal->data[line * (terminal->width + 1)]);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -050088 }
89 cairo_destroy(cr);
90
91 if (terminal->buffer != NULL)
92 buffer_destroy(terminal->buffer, terminal->fd);
93
94 terminal->buffer = buffer_create_from_cairo_surface(terminal->fd, surface);
95 cairo_surface_destroy(surface);
96
97 window_copy(terminal->window,
98 &rectangle,
99 terminal->buffer->name, terminal->buffer->stride);
100}
101
102static void
103terminal_draw(struct terminal *terminal)
104{
105 window_draw(terminal->window);
106 terminal_draw_contents(terminal);
107 wl_display_commit(terminal->display, 0);
108}
109
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500110static gboolean
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500111idle_redraw(void *data)
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500112{
113 struct terminal *terminal = data;
114
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500115 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500116
117 return FALSE;
118}
119
120static void
121resize_handler(struct window *window, int32_t width, int32_t height, void *data)
122{
123 struct terminal *terminal = data;
124
125 if (!terminal->resize_scheduled) {
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500126 g_idle_add(idle_redraw, terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500127 terminal->resize_scheduled = 1;
128 }
129}
130
131static void
132acknowledge_handler(struct window *window, uint32_t key, void *data)
133{
134 struct terminal *terminal = data;
135
136 terminal->resize_scheduled = 0;
137}
138
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500139static void
140terminal_data(struct terminal *terminal, const char *data, size_t length)
141{
142 int i;
143 char *row;
144
145 for (i = 0; i < length; i++) {
146 row = &terminal->data[terminal->row * (terminal->width + 1)];
147 switch (data[i]) {
148 case '\r':
149 terminal->column = 0;
150 break;
151 case '\n':
152 terminal->row++;
153 terminal->column = 0;
154 if (terminal->row == terminal->height)
155 terminal->row = 0;
156 break;
157 case '\t':
158 memset(&row[terminal->column], ' ', -terminal->column & 7);
159 terminal->column = (terminal->column + 7) & ~7;
160 break;
161 default:
162 if (terminal->column < terminal->width)
163 row[terminal->column++] = data[i];
164 break;
165 }
166 }
167}
168
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500169static struct terminal *
170terminal_create(struct wl_display *display, int fd)
171{
172 struct terminal *terminal;
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500173 int size;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500174
175 terminal = malloc(sizeof *terminal);
176 if (terminal == NULL)
177 return terminal;
178
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500179 memset(terminal, 0, sizeof *terminal);
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500180 terminal->fd = fd;
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500181 terminal->window = window_create(display, fd, "Wayland Terminal",
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500182 500, 100, 500, 400);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500183 terminal->display = display;
184 terminal->resize_scheduled = 1;
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500185 terminal->width = 80;
186 terminal->height = 25;
187 size = (terminal->width + 1) * terminal->height;
188 terminal->data = malloc(size);
189 memset(terminal->data, 0, size);
190
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500191 window_set_resize_handler(terminal->window, resize_handler, terminal);
192 window_set_acknowledge_handler(terminal->window, acknowledge_handler, terminal);
193
194 return terminal;
195}
196
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500197static gboolean
198io_handler(GIOChannel *source,
199 GIOCondition condition,
200 gpointer data)
201{
202 struct terminal *terminal = data;
203 gchar buffer[256];
204 gsize bytes_read;
205 GError *error = NULL;
206
207 g_io_channel_read_chars(source, buffer, sizeof buffer,
208 &bytes_read, &error);
209 printf("got data: %.*s\n", bytes_read, buffer);
210
211 terminal_data(terminal, buffer, bytes_read);
212
213 if (!terminal->resize_scheduled) {
214 g_idle_add(idle_redraw, terminal);
215 terminal->resize_scheduled = 1;
216 }
217
218 return TRUE;
219}
220
221static int
222terminal_run(struct terminal *terminal, const char *path)
223{
224 int master, slave;
225 pid_t pid;
226
227 pid = forkpty(&master, NULL, NULL, NULL);
228 if (pid == 0) {
229 close(master);
230 if (execl(path, path, NULL)) {
231 printf("exec failed: %m\n");
232 exit(EXIT_FAILURE);
233 }
234 }
235
236 close(slave);
237 terminal->channel = g_io_channel_unix_new(master);
238 fcntl(master, F_SETFL, O_NONBLOCK);
239 g_io_add_watch(terminal->channel, G_IO_IN,
240 io_handler, terminal);
241
242 return 0;
243}
244
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500245int main(int argc, char *argv[])
246{
247 struct wl_display *display;
248 int fd;
249 GMainLoop *loop;
250 GSource *source;
251 struct terminal *terminal;
252
253 fd = open(gem_device, O_RDWR);
254 if (fd < 0) {
255 fprintf(stderr, "drm open failed: %m\n");
256 return -1;
257 }
258
259 display = wl_display_create(socket_name, sizeof socket_name);
260 if (display == NULL) {
261 fprintf(stderr, "failed to create display: %m\n");
262 return -1;
263 }
264
265 loop = g_main_loop_new(NULL, FALSE);
266 source = wl_glib_source_new(display);
267 g_source_attach(source, NULL);
268
269 terminal = terminal_create(display, fd);
Kristian Høgsberg269d6e32008-12-07 23:17:31 -0500270 terminal_run(terminal, "/bin/bash");
Kristian Høgsberg44e3c5e2008-12-07 21:51:58 -0500271 terminal_draw(terminal);
Kristian Høgsberg0c4457f2008-12-07 19:59:11 -0500272
273 g_main_loop_run(loop);
274
275 return 0;
276}