chiark / gitweb /
fstab-generator: Small cleanup
[elogind.git] / src / console / consoled-workspace.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 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 #include <errno.h>
23 #include <inttypes.h>
24 #include <stdlib.h>
25 #include "consoled.h"
26 #include "grdev.h"
27 #include "idev.h"
28 #include "list.h"
29 #include "macro.h"
30 #include "util.h"
31
32 int workspace_new(Workspace **out, Manager *m) {
33         _cleanup_(workspace_unrefp) Workspace *w = NULL;
34         int r;
35
36         assert(out);
37
38         w = new0(Workspace, 1);
39         if (!w)
40                 return -ENOMEM;
41
42         w->ref = 1;
43         w->manager = m;
44         LIST_PREPEND(workspaces_by_manager, m->workspace_list, w);
45
46         r = terminal_new(&w->current, w);
47         if (r < 0)
48                 return r;
49
50         *out = w;
51         w = NULL;
52         return 0;
53 }
54
55 static void workspace_cleanup(Workspace *w) {
56         Terminal *t;
57
58         assert(w);
59         assert(w->ref == 0);
60         assert(w->manager);
61         assert(!w->session_list);
62
63         w->current = NULL;
64         while ((t = w->terminal_list))
65                 terminal_free(t);
66
67         LIST_REMOVE(workspaces_by_manager, w->manager->workspace_list, w);
68         free(w);
69 }
70
71 Workspace *workspace_ref(Workspace *w) {
72         assert(w);
73
74         ++w->ref;
75         return w;
76 }
77
78 Workspace *workspace_unref(Workspace *w) {
79         if (!w)
80                 return NULL;
81
82         assert(w->ref > 0);
83
84         if (--w->ref == 0)
85                 workspace_cleanup(w);
86
87         return NULL;
88 }
89
90 Workspace *workspace_attach(Workspace *w, Session *s) {
91         assert(w);
92         assert(s);
93
94         LIST_PREPEND(sessions_by_workspace, w->session_list, s);
95         workspace_refresh(w);
96         return workspace_ref(w);
97 }
98
99 Workspace *workspace_detach(Workspace *w, Session *s) {
100         assert(w);
101         assert(s);
102         assert(s->active_ws == w);
103
104         LIST_REMOVE(sessions_by_workspace, w->session_list, s);
105         workspace_refresh(w);
106         return workspace_unref(w);
107 }
108
109 void workspace_refresh(Workspace *w) {
110         uint32_t width, height;
111         Terminal *t;
112         Session *s;
113         Display *d;
114
115         assert(w);
116
117         width = 0;
118         height = 0;
119
120         /* find out minimum dimension of all attached displays */
121         LIST_FOREACH(sessions_by_workspace, s, w->session_list) {
122                 LIST_FOREACH(displays_by_session, d, s->display_list) {
123                         assert(d->width > 0 && d->height > 0);
124
125                         if (width == 0 || d->width < width)
126                                 width = d->width;
127                         if (height == 0 || d->height < height)
128                                 height = d->height;
129                 }
130         }
131
132         /* either both are zero, or none is zero */
133         assert(!(!width ^ !height));
134
135         /* update terminal-sizes if dimensions changed */
136         if (w->width != width || w->height != height) {
137                 w->width = width;
138                 w->height = height;
139
140                 LIST_FOREACH(terminals_by_workspace, t, w->terminal_list)
141                         terminal_resize(t);
142
143                 workspace_dirty(w);
144         }
145 }
146
147 void workspace_dirty(Workspace *w) {
148         Session *s;
149
150         assert(w);
151
152         LIST_FOREACH(sessions_by_workspace, s, w->session_list)
153                 session_dirty(s);
154 }
155
156 void workspace_feed(Workspace *w, idev_data *data) {
157         assert(w);
158         assert(data);
159
160         terminal_feed(w->current, data);
161 }
162
163 bool workspace_draw(Workspace *w, const grdev_display_target *target) {
164         assert(w);
165         assert(target);
166
167         return terminal_draw(w->current, target);
168 }