]> Pileus Git - wmpus/blob - sys-swc.c
Work on Wayland/SWC backend
[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_window  *swc;
35         struct wl_listener  evt;
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         exit(0);
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                 execvp(cmd[0], cmd);
66                 exit(0);
67         }
68 }
69
70 /*************
71  * Callbacks *
72  *************/
73
74 #define listener2win(listener) \
75         (((win_sys_t*)((size_t)listener - \
76                (size_t)(&((win_sys_t*)NULL)->evt)))->win)
77
78 static const char *map_window[] = {
79         [SWC_WINDOW_DESTROYED]                = "destroyed",
80         [SWC_WINDOW_TITLE_CHANGED]            = "title_changed",
81         [SWC_WINDOW_CLASS_CHANGED]            = "class_changed",
82         [SWC_WINDOW_STATE_CHANGED]            = "state_changed",
83         [SWC_WINDOW_ENTERED]                  = "entered",
84         [SWC_WINDOW_RESIZED]                  = "resized",
85         [SWC_WINDOW_PARENT_CHANGED]           = "parent_changed",
86 };
87
88 static const char *map_screen[] = {
89         [SWC_SCREEN_DESTROYED]                = "destroyed",
90         [SWC_SCREEN_GEOMETRY_CHANGED]         = "geometry_changed",
91         [SWC_SCREEN_USABLE_GEOMETRY_CHANGED]  = "usable_geometry_changed",
92 };
93
94 static void evt_window(struct wl_listener *listener, void *_event)
95 {
96         struct swc_event *event = _event;
97         win_t *win = listener2win(listener);
98
99         printf("evt_window: %p -> %p - %s\n", listener, win,
100                         map_window[event->type]);
101
102         switch (event->type) {
103                 case SWC_WINDOW_STATE_CHANGED:
104                         if (win->sys->swc->state == SWC_WINDOW_STATE_NORMAL) {
105                                 swc_window_show(win->sys->swc);
106                                 swc_window_focus(win->sys->swc);
107                         }
108                         break;
109         }
110 }
111
112 static void evt_screen(struct wl_listener *listener, void *_event)
113 {
114         struct swc_event *event = _event;
115         printf("evt_screen: %p - %s\n", listener,
116                         map_screen[event->type]);
117 }
118
119 static void new_window(struct swc_window *swc)
120 {
121         printf("new_window: %p\n", swc);
122
123         win_t *win = new0(win_t);
124         win->sys   = new0(win_sys_t);
125         win->sys->win = win;
126         win->sys->swc = swc;
127         win->sys->evt.notify = evt_window;
128
129         wl_signal_add(&win->sys->swc->event_signal, &win->sys->evt);
130 }
131
132 static void new_screen(struct swc_screen *screen)
133 {
134         printf("new_screen: %p\n", screen);
135
136         struct wl_listener *listener = new0(struct wl_listener);
137         listener->notify = evt_screen;
138
139         wl_signal_add(&screen->event_signal, listener);
140 }
141
142 /********************
143  * System functions *
144  ********************/
145 void sys_move(win_t *win, int x, int y, int w, int h)
146 {
147         printf("sys_move: %p - %d,%d  %dx%d\n",
148                         win, x, y, w, h);
149 }
150
151 void sys_raise(win_t *win)
152 {
153         printf("sys_raise: %p\n", win);
154 }
155
156 void sys_focus(win_t *win)
157 {
158         printf("sys_focus: %p\n", win);
159 }
160
161 void sys_show(win_t *win, state_t state)
162 {
163         printf("sys_show: %p: %d", win, state);
164 }
165
166 void sys_watch(win_t *win, event_t ev, mod_t mod)
167 {
168         printf("sys_watch: %p - %x %hhx\n",
169                         win, ev, mod2int(mod));
170 }
171
172 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
173 {
174         printf("sys_unwatch: %p - %x %hhx\n",
175                         win, ev, mod2int(mod));
176 }
177
178 list_t *sys_info(win_t *win)
179 {
180         printf("sys_info: %p\n", win);
181         return list_insert(NULL, win);
182 }
183
184 win_t *sys_init(void)
185 {
186         static struct swc_manager manager = {
187                 &new_window,
188                 &new_screen,
189         };
190
191         printf("sys_init\n");
192
193         /* Register log handler */
194         wl_log_set_handler_server((wl_log_func_t)vprintf);
195
196         /* Open the display */
197         if (!(display = wl_display_create()))
198                 error("Unable to  create display");
199         if (wl_display_add_socket(display, NULL) != 0)
200                 error("Unable to add socket");
201         if (!swc_initialize(display, NULL, &manager))
202                 error("Unable to initialize SWC");
203         if (!(events = wl_display_get_event_loop(display)))
204                 error("Unable to get event loop");
205
206         /* Fail-safe key bindings */
207         swc_add_binding(SWC_BINDING_KEY, SWC_MOD_LOGO|SWC_MOD_SHIFT, XKB_KEY_q, &cmd_exit,  NULL);
208         swc_add_binding(SWC_BINDING_KEY, SWC_MOD_LOGO, XKB_KEY_Return, &cmd_spawn, cmd_term);
209         swc_add_binding(SWC_BINDING_KEY, SWC_MOD_LOGO, XKB_KEY_r,      &cmd_spawn, cmd_menu);
210
211         return new0(win_t);
212 }
213
214 void sys_run(win_t *root)
215 {
216         printf("sys_run: %p\n", root);
217         wl_display_run(display);
218 }
219
220 void sys_exit(void)
221 {
222         printf("sys_exit\n");
223         exit(0);
224 }
225
226 void sys_free(win_t *root)
227 {
228         printf("sys_free: %p\n", root);
229 }