2 * Copyright (c) 2014, Andy Spencer <andy753421@gmail.com>
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.
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
21 #include <xkbcommon/xkbcommon.h>
22 #include <wayland-server.h>
23 #include <wayland-client.h>
31 /* Internal structures */
34 struct swc_screen *ss;
35 struct swc_window *sw;
39 static struct wl_display *display;
40 static struct wl_event_loop *events;
45 static const char * cmd_term[] = { "st-wl", NULL };
46 static const char * cmd_menu[] = { "dmenu_run-wl", NULL };
48 static void cmd_exit(void *data, uint32_t time, uint32_t value, uint32_t state)
50 if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
57 static void cmd_spawn(void *data, uint32_t time, uint32_t value, uint32_t state)
60 if (state != WL_KEYBOARD_KEY_STATE_PRESSED)
63 printf("cmd_spawn: %s\n", cmd[0]);
75 /* Screen callbacks */
76 static void screen_entered(void *data)
78 printf("screen_entered: %p\n", data);
81 static void screen_geometry(void *data)
83 printf("screen_geometry: %p\n", data);
86 static void screen_usable(void *data)
88 printf("screen_usable: %p\n", data);
91 static void screen_destroy(void *data)
93 printf("screen_destroy: %p\n", data);
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,
103 /* Window callbacks */
104 static void window_entered(void *_win)
107 printf("window_entered: %p\n", win);
108 swc_window_show(win->sys->sw);
109 swc_window_focus(win->sys->sw);
112 static void window_title(void *_win)
115 printf("window_title: %p\n", win);
118 static void window_class(void *_win)
121 printf("window_class: %p\n", win);
124 static void window_parent(void *_win)
127 printf("window_parent: %p\n", win);
130 static void window_destroy(void *_win)
133 printf("window_destroy: %p\n", win);
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,
144 /* System callbacks */
145 static void new_screen(struct swc_screen *ss)
147 printf("new_screen: %p\n", ss);
149 win_t *win = new0(win_t);
150 win->sys = new0(win_sys_t);
154 swc_screen_set_handler(ss, &screen_handler, win);
157 static void new_window(struct swc_window *sw)
159 printf("new_window: %p\n", sw);
161 win_t *win = new0(win_t);
162 win->sys = new0(win_sys_t);
166 swc_window_set_handler(sw, &window_handler, win);
167 swc_window_set_tiled(NULL);
170 /********************
172 ********************/
173 void sys_move(win_t *win, int x, int y, int w, int h)
175 printf("sys_move: %p - %d,%d %dx%d\n",
179 void sys_raise(win_t *win)
181 printf("sys_raise: %p\n", win);
184 void sys_focus(win_t *win)
186 printf("sys_focus: %p\n", win);
189 void sys_show(win_t *win, state_t state)
191 printf("sys_show: %p: %d", win, state);
194 void sys_watch(win_t *win, event_t ev, mod_t mod)
196 printf("sys_watch: %p - %x %hhx\n",
197 win, ev, mod2int(mod));
200 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
202 printf("sys_unwatch: %p - %x %hhx\n",
203 win, ev, mod2int(mod));
206 list_t *sys_info(win_t *win)
208 printf("sys_info: %p\n", win);
209 return list_insert(NULL, win);
212 win_t *sys_init(void)
214 static struct swc_manager manager = {
215 .new_screen = &new_screen,
216 .new_window = &new_window,
219 printf("sys_init\n");
221 /* Register log handler */
222 wl_log_set_handler_server((wl_log_func_t)vprintf);
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");
234 /* Fail-safe key bindings */
235 swc_add_binding(SWC_BINDING_KEY, SWC_MOD_LOGO|SWC_MOD_SHIFT, XKB_KEY_q,
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);
245 void sys_run(win_t *root)
247 printf("sys_run: %p\n", root);
248 wl_display_run(display);
249 wl_display_destroy(display);
254 printf("sys_exit\n");
255 wl_display_terminate(display);
258 void sys_free(win_t *root)
260 printf("sys_free: %p\n", root);