chiark / gitweb /
terminal: add graphics interface
[elogind.git] / src / libsystemd-terminal / grdev-internal.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #pragma once
23
24 #include <inttypes.h>
25 #include <stdbool.h>
26 #include <stdlib.h>
27 #include <systemd/sd-bus.h>
28 #include <systemd/sd-event.h>
29 #include "grdev.h"
30 #include "hashmap.h"
31 #include "list.h"
32 #include "util.h"
33
34 typedef struct grdev_tile               grdev_tile;
35 typedef struct grdev_display_cache      grdev_display_cache;
36
37 typedef struct grdev_pipe_vtable        grdev_pipe_vtable;
38 typedef struct grdev_pipe               grdev_pipe;
39 typedef struct grdev_card_vtable        grdev_card_vtable;
40 typedef struct grdev_card               grdev_card;
41
42 /*
43  * Displays
44  */
45
46 enum {
47         GRDEV_TILE_LEAF,
48         GRDEV_TILE_NODE,
49         GRDEV_TILE_CNT
50 };
51
52 struct grdev_tile {
53         LIST_FIELDS(grdev_tile, childs_by_node);
54         grdev_tile *parent;
55         grdev_display *display;
56
57         uint32_t x;
58         uint32_t y;
59         unsigned int rotate;
60         unsigned int flip;
61         uint32_t cache_w;
62         uint32_t cache_h;
63
64         unsigned int type;
65
66         union {
67                 struct {
68                         grdev_pipe *pipe;
69                 } leaf;
70
71                 struct {
72                         size_t n_childs;
73                         LIST_HEAD(grdev_tile, child_list);
74                 } node;
75         };
76 };
77
78 int grdev_tile_new_leaf(grdev_tile **out, grdev_pipe *pipe);
79 int grdev_tile_new_node(grdev_tile **out);
80 grdev_tile *grdev_tile_free(grdev_tile *tile);
81
82 DEFINE_TRIVIAL_CLEANUP_FUNC(grdev_tile*, grdev_tile_free);
83
84 struct grdev_display {
85         grdev_session *session;
86         char *name;
87
88         size_t n_leafs;
89         grdev_tile *tile;
90
91         size_t n_pipes;
92         size_t max_pipes;
93
94         uint32_t width;
95         uint32_t height;
96
97         struct grdev_display_cache {
98                 grdev_pipe *pipe;
99                 grdev_display_target target;
100
101                 bool incomplete : 1;
102         } *pipes;
103
104         bool enabled : 1;
105         bool public : 1;
106         bool modified : 1;
107         bool framed : 1;
108 };
109
110 grdev_display *grdev_find_display(grdev_session *session, const char *name);
111
112 int grdev_display_new(grdev_display **out, grdev_session *session, const char *name);
113 grdev_display *grdev_display_free(grdev_display *display);
114
115 DEFINE_TRIVIAL_CLEANUP_FUNC(grdev_display*, grdev_display_free);
116
117 /*
118  * Pipes
119  */
120
121 struct grdev_pipe_vtable {
122         void (*free) (grdev_pipe *pipe);
123         void (*enable) (grdev_pipe *pipe);
124         void (*disable) (grdev_pipe *pipe);
125         grdev_fb *(*target) (grdev_pipe *pipe);
126 };
127
128 struct grdev_pipe {
129         const grdev_pipe_vtable *vtable;
130         grdev_card *card;
131         char *name;
132
133         grdev_tile *tile;
134         grdev_display_cache *cache;
135
136         uint32_t width;
137         uint32_t height;
138
139         size_t max_fbs;
140         grdev_fb *front;
141         grdev_fb *back;
142         grdev_fb **fbs;
143
144         bool enabled : 1;
145         bool running : 1;
146         bool flip : 1;
147         bool flipping : 1;
148 };
149
150 #define GRDEV_PIPE_INIT(_vtable, _card) ((grdev_pipe){ \
151                 .vtable = (_vtable), \
152                 .card = (_card), \
153         })
154
155 grdev_pipe *grdev_find_pipe(grdev_card *card, const char *name);
156
157 int grdev_pipe_add(grdev_pipe *pipe, const char *name, size_t n_fbs);
158 grdev_pipe *grdev_pipe_free(grdev_pipe *pipe);
159
160 DEFINE_TRIVIAL_CLEANUP_FUNC(grdev_pipe*, grdev_pipe_free);
161
162 void grdev_pipe_ready(grdev_pipe *pipe, bool running);
163 void grdev_pipe_frame(grdev_pipe *pipe);
164
165 /*
166  * Cards
167  */
168
169 struct grdev_card_vtable {
170         void (*free) (grdev_card *card);
171         void (*enable) (grdev_card *card);
172         void (*disable) (grdev_card *card);
173         void (*commit) (grdev_card *card);
174         void (*restore) (grdev_card *card);
175 };
176
177 struct grdev_card {
178         const grdev_card_vtable *vtable;
179         grdev_session *session;
180         char *name;
181
182         Hashmap *pipe_map;
183
184         bool enabled : 1;
185         bool modified : 1;
186 };
187
188 #define GRDEV_CARD_INIT(_vtable, _session) ((grdev_card){ \
189                 .vtable = (_vtable), \
190                 .session = (_session), \
191         })
192
193 grdev_card *grdev_find_card(grdev_session *session, const char *name);
194
195 int grdev_card_add(grdev_card *card, const char *name);
196 grdev_card *grdev_card_free(grdev_card *card);
197
198 DEFINE_TRIVIAL_CLEANUP_FUNC(grdev_card*, grdev_card_free);
199
200 /*
201  * Sessions
202  */
203
204 struct grdev_session {
205         grdev_context *context;
206         char *name;
207         char *path;
208         grdev_event_fn event_fn;
209         void *userdata;
210
211         unsigned long n_pins;
212
213         Hashmap *card_map;
214         Hashmap *display_map;
215
216         bool custom : 1;
217         bool managed : 1;
218         bool enabled : 1;
219         bool modified : 1;
220 };
221
222 grdev_session *grdev_session_pin(grdev_session *session);
223 grdev_session *grdev_session_unpin(grdev_session *session);
224
225 DEFINE_TRIVIAL_CLEANUP_FUNC(grdev_session*, grdev_session_unpin);
226
227 /*
228  * Contexts
229  */
230
231 struct grdev_context {
232         unsigned long ref;
233         sd_event *event;
234         sd_bus *sysbus;
235
236         Hashmap *session_map;
237 };