]> Pileus Git - wmpus/blob - sys-swc.c
Add X11 Wayland interface
[wmpus] / sys-swc.c
1 /*
2  * Copyright (c) 2014, Andy Spencer <andy753421@gmail.com>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  */
15
16 #define _GNU_SOURCE
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20
21 #include <xkbcommon/xkbcommon.h>
22 #include <wayland-server.h>
23 #include <wayland-client.h>
24 #include <swc.h>
25
26 #include "util.h"
27 #include "conf.h"
28 #include "sys.h"
29 #include "wm.h"
30
31 /* Internal structures */
32 struct win_sys {
33         win_t              *win;
34         struct swc_screen  *ss;
35         struct swc_window  *sw;
36 };
37
38 /* Global data */
39 static struct wl_display    *display;
40 static struct wl_event_loop *events;
41
42 /*******************
43  * Debug functions *
44  *******************/
45 static const char * cmd_term[] = { "st-wl",        NULL };
46 static const char * cmd_menu[] = { "dmenu_run-wl", NULL };
47
48 static void cmd_exit(void *data, uint32_t time, uint32_t value, uint32_t state)
49 {
50         if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
51                 return;
52
53         printf("cmd_exit\n");
54         sys_exit();
55 }
56
57 static void cmd_spawn(void *data, uint32_t time, uint32_t value, uint32_t state)
58 {
59         char **cmd = data;
60         if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
61                 return;
62
63         printf("cmd_spawn: %s\n", cmd[0]);
64         if (fork() == 0) {
65                 close(2);
66                 execvp(cmd[0], cmd);
67                 exit(0);
68         }
69 }
70
71 /*************
72  * Callbacks *
73  *************/
74
75 /* Screen callbacks */
76 static void screen_entered(void *data)
77 {
78         printf("screen_entered: %p\n", data);
79 }
80
81 static void screen_geometry(void *data)
82 {
83         printf("screen_geometry: %p\n", data);
84 }
85
86 static void screen_usable(void *data)
87 {
88         printf("screen_usable: %p\n", data);
89 }
90
91 static void screen_destroy(void *data)
92 {
93         printf("screen_destroy: %p\n", data);
94 }
95
96 static const struct swc_screen_handler screen_handler = {
97         .entered                 = &screen_entered,
98         .geometry_changed        = &screen_geometry,
99         .usable_geometry_changed = &screen_usable,
100         .destroy                 = &screen_destroy,
101 };
102
103 /* Window callbacks */
104 static void window_entered(void *_win)
105 {
106         win_t *win = _win;
107         printf("window_entered: %p\n", win);
108         swc_window_show(win->sys->sw);
109         swc_window_focus(win->sys->sw);
110 }
111
112 static void window_title(void *_win)
113 {
114         win_t *win = _win;
115         printf("window_title: %p\n", win);
116 }
117
118 static void window_class(void *_win)
119 {
120         win_t *win = _win;
121         printf("window_class: %p\n", win);
122 }
123
124 static void window_parent(void *_win)
125 {
126         win_t *win = _win;
127         printf("window_parent: %p\n", win);
128 }
129
130 static void window_destroy(void *_win)
131 {
132         win_t *win = _win;
133         printf("window_destroy: %p\n", win);
134 }
135
136 static const struct swc_window_handler window_handler = {
137         .entered        = &window_entered,
138         .title_changed  = &window_title,
139         .class_changed  = &window_class,
140         .parent_changed = &window_parent,
141         .destroy        = &window_destroy,
142 };
143
144 /* System callbacks */
145 static void new_screen(struct swc_screen *ss)
146 {
147         printf("new_screen: %p\n", ss);
148
149         win_t *win = new0(win_t);
150         win->sys   = new0(win_sys_t);
151         win->sys->win = win;
152         win->sys->ss  = ss;
153
154         swc_screen_set_handler(ss, &screen_handler, win);
155 }
156
157 static void new_window(struct swc_window *sw)
158 {
159         printf("new_window: %p\n", sw);
160
161         win_t *win = new0(win_t);
162         win->sys   = new0(win_sys_t);
163         win->sys->win = win;
164         win->sys->sw  = sw;
165
166         swc_window_set_handler(sw, &window_handler, win);
167         swc_window_set_tiled(NULL);
168 }
169
170 /********************
171  * System functions *
172  ********************/
173 void sys_move(win_t *win, int x, int y, int w, int h)
174 {
175         printf("sys_move: %p - %d,%d  %dx%d\n",
176                         win, x, y, w, h);
177 }
178
179 void sys_raise(win_t *win)
180 {
181         printf("sys_raise: %p\n", win);
182 }
183
184 void sys_focus(win_t *win)
185 {
186         printf("sys_focus: %p\n", win);
187 }
188
189 void sys_show(win_t *win, state_t state)
190 {
191         printf("sys_show: %p: %d", win, state);
192 }
193
194 void sys_watch(win_t *win, event_t ev, mod_t mod)
195 {
196         printf("sys_watch: %p - %x %hhx\n",
197                         win, ev, mod2int(mod));
198 }
199
200 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
201 {
202         printf("sys_unwatch: %p - %x %hhx\n",
203                         win, ev, mod2int(mod));
204 }
205
206 list_t *sys_info(win_t *win)
207 {
208         printf("sys_info: %p\n", win);
209         return list_insert(NULL, win);
210 }
211
212 win_t *sys_init(void)
213 {
214         static struct swc_manager manager = {
215                 .new_screen = &new_screen,
216                 .new_window = &new_window,
217         };
218
219         printf("sys_init\n");
220
221         /* Register log handler */
222         wl_log_set_handler_server((wl_log_func_t)vprintf);
223
224         /* Open the display */
225         if (!(display = wl_display_create()))
226                 error("Unable to  create display");
227         if (wl_display_add_socket(display, NULL) != 0)
228                 error("Unable to add socket");
229         if (!swc_initialize(display, NULL, &manager))
230                 error("Unable to initialize SWC");
231         if (!(events = wl_display_get_event_loop(display)))
232                 error("Unable to get event loop");
233
234         /* Fail-safe key bindings */
235         swc_add_binding(SWC_BINDING_KEY, SWC_MOD_LOGO|SWC_MOD_SHIFT, XKB_KEY_q,
236                         &cmd_exit,  NULL);
237         swc_add_binding(SWC_BINDING_KEY, SWC_MOD_LOGO, XKB_KEY_Return,
238                         &cmd_spawn, cmd_term);
239         swc_add_binding(SWC_BINDING_KEY, SWC_MOD_LOGO, XKB_KEY_r,
240                         &cmd_spawn, cmd_menu);
241
242         return new0(win_t);
243 }
244
245 void sys_run(win_t *root)
246 {
247         printf("sys_run: %p\n", root);
248         wl_display_run(display);
249         wl_display_destroy(display);
250 }
251
252 void sys_exit(void)
253 {
254         printf("sys_exit\n");
255         wl_display_terminate(display);
256 }
257
258 void sys_free(win_t *root)
259 {
260         printf("sys_free: %p\n", root);
261 }