aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/wm/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/wm/main.c')
-rw-r--r--src/cmd/wm/main.c177
1 files changed, 177 insertions, 0 deletions
diff --git a/src/cmd/wm/main.c b/src/cmd/wm/main.c
new file mode 100644
index 0000000..2607801
--- /dev/null
+++ b/src/cmd/wm/main.c
@@ -0,0 +1,177 @@
+#include "wm.h"
+
+Server server = {
+ .event = {
+ .make_input = { .notify = make_input },
+ .make_monitor = { .notify = make_monitor },
+ .make_xdg_surface = { .notify = make_xdg_surface },
+ .make_layer_surface = { .notify = make_layer_surface },
+
+ .monitor_change = { .notify = monitor_change },
+ .monitor_test = { .notify = monitor_test },
+ .monitor_apply = { .notify = monitor_apply },
+
+ .cursor_move = { .notify = cursor_move },
+ .cursor_move_abs = { .notify = cursor_move_abs },
+ .cursor_button = { .notify = cursor_button },
+ .cursor_axis = { .notify = cursor_axis },
+ .cursor_frame = { .notify = cursor_frame },
+
+ .request_activate = { .notify = request_activate },
+ .request_cursor = { .notify = request_cursor },
+ .request_set_selection = { .notify = request_set_selection },
+ },
+};
+
+// -----------------------------------------------------------------------
+// helper functions
+
+static inline
+void
+init(void)
+{
+ /* compositor initialization */
+ server.display = wl_display_create();
+ server.backend = wlr_backend_autocreate(server.display);
+ server.renderer = wlr_backend_get_renderer(server.backend);
+ server.present = wlr_presentation_create(server.display, server.backend);
+
+ wlr_renderer_init_wl_display(server.renderer, server.display);
+
+ wlr_compositor_create(server.display, server.renderer);
+ wlr_export_dmabuf_manager_v1_create(server.display);
+ wlr_screencopy_manager_v1_create(server.display);
+ wlr_data_control_manager_v1_create(server.display);
+ wlr_data_device_manager_create(server.display);
+ wlr_gamma_control_manager_v1_create(server.display);
+ wlr_primary_selection_v1_device_manager_create(server.display);
+ wlr_viewporter_create(server.display);
+
+ server.activate = wlr_xdg_activation_v1_create(server.display);
+ wl_signal_add(&server.activate->events.request_activate, &server.event.request_activate);
+
+ wlr_data_device_manager_create(server.display);
+
+ server.monitor.layout = wlr_output_layout_create();
+ wl_signal_add(&server.monitor.layout->events.change, &server.event.monitor_change);
+ wlr_xdg_output_manager_v1_create(server.display, server.monitor.layout);
+
+ wl_list_init(&server.monitor.list);
+ wl_signal_add(&server.backend->events.new_output, &server.event.make_monitor);
+
+ server.monitor.manager = wlr_output_manager_v1_create(server.display);
+ wl_signal_add(&server.monitor.manager->events.test, &server.event.monitor_test);
+ wl_signal_add(&server.monitor.manager->events.apply, &server.event.monitor_apply);
+
+ /* shell initialization */
+ wl_list_init(&server.client.list);
+ wl_list_init(&server.client.stack);
+ wl_list_init(&server.client.focus);
+
+ server.shell.xdg = wlr_xdg_shell_create(server.display);
+ wl_signal_add(&server.shell.xdg->events.new_surface, &server.event.make_xdg_surface);
+
+ server.shell.layer = wlr_layer_shell_v1_create(server.display);
+ wl_signal_add(&server.shell.layer->events.new_surface, &server.event.make_layer_surface);
+
+ wlr_server_decoration_manager_set_default_mode(
+ wlr_server_decoration_manager_create(server.display),
+ WLR_SERVER_DECORATION_MANAGER_MODE_SERVER
+ );
+ wlr_xdg_decoration_manager_v1_create(server.display);
+
+ /* input initialization */
+ server.cursor.dot = wlr_cursor_create();
+ wlr_cursor_attach_output_layout(server.cursor.dot, server.monitor.layout);
+
+ server.cursor.manager = wlr_xcursor_manager_create(nil, 24);
+ wlr_xcursor_manager_load(server.cursor.manager, 1);
+
+ wl_signal_add(&server.cursor.dot->events.motion, &server.event.cursor_move);
+ wl_signal_add(&server.cursor.dot->events.motion_absolute, &server.event.cursor_move_abs);
+ wl_signal_add(&server.cursor.dot->events.button, &server.event.cursor_button);
+ wl_signal_add(&server.cursor.dot->events.axis, &server.event.cursor_axis);
+ wl_signal_add(&server.cursor.dot->events.frame, &server.event.cursor_frame);
+
+ wl_list_init(&server.input.keyboards);
+ wl_signal_add(&server.backend->events.new_input, &server.event.make_input);
+
+ server.input.idle = wlr_idle_create(server.display);
+ server.input.seat = wlr_seat_create(server.display, "seat0");
+
+ wl_signal_add(&server.input.seat->events.request_set_cursor, &server.event.request_cursor);
+ wl_signal_add(&server.input.seat->events.request_set_selection, &server.event.request_set_selection);
+}
+
+static inline
+void
+fini(void)
+{
+ wl_display_destroy_clients(server.display);
+
+ wlr_backend_destroy(server.backend);
+ wlr_xcursor_manager_destroy(server.cursor.manager);
+ wlr_output_layout_destroy(server.monitor.layout);
+ wlr_seat_destroy(server.input.seat);
+
+ wl_display_destroy(server.display);
+}
+
+// -----------------------------------------------------------------------
+// main point of entry
+
+int
+usage(void)
+{
+ fprintf(stderr, "usage: %s [-s startup command]\n", argv0);
+ return 1;
+}
+
+
+int
+main(int argc, char *argv[])
+{
+ char *socket, *cmd=nil;
+
+ ARGBEGIN{
+ case 's':
+ cmd = ARGF();
+ break;
+ default:
+ return usage();
+ } ARGEND
+
+ if(argc != 0)
+ return usage();
+
+ wlr_log_init(WLR_DEBUG, nil);
+
+ init();
+
+ if(!(socket=(char*)wl_display_add_socket_auto(server.display))) {
+ wlr_backend_destroy(server.backend);
+ return 1;
+ }
+
+ if(!(wlr_backend_start(server.backend))) {
+ wlr_backend_destroy(server.backend);
+ wl_display_destroy(server.display);
+ return 1;
+ }
+
+ setenv("WAYLAND_DISPLAY", socket, true);
+ if(cmd) {
+ if(fork()==0)
+ execl("/bin/sh", "/bin/sh", "-c", cmd, nil);
+ }
+ wlr_log(WLR_INFO, "Running Wayland compositor on WAYLAND_DISPLAY=%s", socket);
+
+ server.monitor.selected = monitor_at(server.cursor.dot->x, server.cursor.dot->y);
+ wlr_cursor_warp_closest(server.cursor.dot, nil, server.cursor.dot->x, server.cursor.dot->y);
+ wlr_xcursor_manager_set_cursor_image(server.cursor.manager, "left_ptr", server.cursor.dot);
+
+ wl_display_run(server.display); /* event loop */
+
+ fini();
+ return 0;
+}