]> Pileus Git - wmpus/blob - sys-xwl.c
Work on gtk interface
[wmpus] / sys-xwl.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
25 #include <gtk/gtk.h>
26
27 #include "util.h"
28 #include "conf.h"
29 #include "sys.h"
30 #include "wm.h"
31
32 /* Internal structures */
33 struct win_sys {
34         win_t *win;
35 };
36
37 /* Global data */
38 static struct wl_display    *display;
39 static struct wl_event_loop *events;
40
41 static GtkWidget *window;
42
43 /*****************
44  * Gtk Callbacks *
45  *****************/
46
47 static void on_destroy(GtkWidget *widget, GdkEvent *event, gpointer user_data)
48 {
49         printf("on_destroy\n");
50         sys_exit();
51 }
52
53 static gboolean on_key(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
54 {
55         printf(g_ascii_isprint(event->keyval)
56                 ? "on_key: '%c'\n"
57                 : "on_key: 0x%X\n",
58                 event->keyval);
59         if (event->keyval == GDK_KEY_q)
60                 sys_exit();
61         if (event->keyval == GDK_KEY_t)
62                 g_spawn_command_line_async("st-wl", NULL);
63         return TRUE;
64 }
65
66 static gboolean on_button(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
67 {
68         printf("on_button\n");
69         return TRUE;
70 }
71
72 static gboolean on_move(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
73 {
74         //printf("on_motion\n");
75         return TRUE;
76 }
77
78 static gboolean on_wayland(gpointer user_data)
79 {
80         // TODO - convert to polled execution
81         wl_display_flush_clients(display);
82         wl_event_loop_dispatch(events, 0);
83         return TRUE;
84 }
85
86 /*********************
87  * Wayland Callbacks *
88  *********************/
89
90 #if 0
91 static void new_window(void)
92 {
93         printf("new_window\n");
94 }
95
96 static void new_screen(void)
97 {
98         printf("new_screen\n");
99 }
100 #endif
101
102 /***********************************
103  * Wayland Shared Memory Interface *
104  ***********************************/
105
106 struct wl_global *shm_ref;
107
108 static struct wl_shm_interface shm_iface = {
109         .create_pool = NULL,
110 };
111
112 static void shm_bind(struct wl_client *client, void *data, uint32_t version, uint32_t id)
113 {
114         printf("shm_bind\n");
115
116         struct wl_resource *res = wl_resource_create(client, &wl_shm_interface, version, id);
117         wl_resource_set_implementation(res, &shm_iface, NULL, NULL);
118
119         wl_shm_send_format(res, WL_SHM_FORMAT_XRGB8888);
120         wl_shm_send_format(res, WL_SHM_FORMAT_ARGB8888);
121 }
122
123 /********************
124  * System functions *
125  ********************/
126 void sys_move(win_t *win, int x, int y, int w, int h)
127 {
128         printf("sys_move: %p - %d,%d  %dx%d\n",
129                         win, x, y, w, h);
130 }
131
132 void sys_raise(win_t *win)
133 {
134         printf("sys_raise: %p\n", win);
135 }
136
137 void sys_focus(win_t *win)
138 {
139         printf("sys_focus: %p\n", win);
140 }
141
142 void sys_show(win_t *win, state_t state)
143 {
144         printf("sys_show: %p: %d", win, state);
145 }
146
147 void sys_watch(win_t *win, event_t ev, mod_t mod)
148 {
149         printf("sys_watch: %p - %x %hhx\n",
150                         win, ev, mod2int(mod));
151 }
152
153 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
154 {
155         printf("sys_unwatch: %p - %x %hhx\n",
156                         win, ev, mod2int(mod));
157 }
158
159 list_t *sys_info(win_t *win)
160 {
161         printf("sys_info: %p\n", win);
162         return list_insert(NULL, win);
163 }
164
165 win_t *sys_init(void)
166 {
167         printf("sys_init\n");
168
169         /* Register log handler */
170         wl_log_set_handler_server((wl_log_func_t)vprintf);
171
172         /* Open the display */
173         if (!(display = wl_display_create()))
174                 error("Unable to  create display");
175         if (wl_display_add_socket(display, NULL) != 0)
176                 error("Unable to add socket");
177         if (!(events = wl_display_get_event_loop(display)))
178                 error("Unable to get event loop");
179
180         /* Register interfaces */
181         shm_ref = wl_global_create(display, &wl_shm_interface, 1, NULL, &shm_bind);
182
183         /* Setup GTK display */
184         gtk_init(&conf_argc, &conf_argv);
185         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
186         gtk_widget_add_events(window,
187                         GDK_KEY_PRESS_MASK |
188                         GDK_BUTTON_PRESS_MASK |
189                         GDK_BUTTON_RELEASE_MASK |
190                         GDK_POINTER_MOTION_MASK);
191         g_signal_connect(window, "destroy",             G_CALLBACK(on_destroy), NULL);
192         g_signal_connect(window, "key-press-event",     G_CALLBACK(on_key),     NULL);
193         g_signal_connect(window, "button-press-event",  G_CALLBACK(on_button),  NULL);
194         g_signal_connect(window, "motion-notify-event", G_CALLBACK(on_move),    NULL);
195         g_timeout_add(1000/60, on_wayland, NULL);
196         gtk_widget_show(window);
197
198         return new0(win_t);
199 }
200
201 void sys_run(win_t *root)
202 {
203         printf("sys_run: %p\n", root);
204         gtk_main();
205 }
206
207 void sys_exit(void)
208 {
209         printf("sys_exit\n");
210         gtk_main_quit();
211 }
212
213 void sys_free(win_t *root)
214 {
215         printf("sys_free: %p\n", root);
216 }