xdg_dialog.c (2900B)
1 /* xdg-dialog-v1 */ 2 3 #include "xdg_dialog.h" 4 #include "util.h" 5 #include "window.h" 6 #include "xdg_shell.h" 7 8 #include <stdlib.h> 9 #include <wayland-server.h> 10 #include "xdg-dialog-v1-server-protocol.h" 11 12 struct dialog { 13 struct wl_resource *resource; 14 struct wl_resource *toplevel; 15 struct wl_listener toplevel_destroy; 16 }; 17 18 static void 19 set(struct wl_resource *resource, bool modal) 20 { 21 struct dialog *dialog = wl_resource_get_user_data(resource); 22 struct window *window; 23 24 if (dialog->toplevel && (window = xdg_toplevel_window(dialog->toplevel))) { 25 window_set_modal(window, modal); 26 } 27 } 28 29 static void 30 set_modal(struct wl_client *client, struct wl_resource *resource) 31 { 32 (void)client; 33 set(resource, true); 34 } 35 36 static void 37 unset_modal(struct wl_client *client, struct wl_resource *resource) 38 { 39 (void)client; 40 set(resource, false); 41 } 42 43 static const struct xdg_dialog_v1_interface dialog_impl = { 44 .destroy = destroy_resource, 45 .set_modal = set_modal, 46 .unset_modal = unset_modal, 47 }; 48 49 static void 50 handle_toplevel_destroy(struct wl_listener *listener, void *data) 51 { 52 struct dialog *dialog = wl_container_of(listener, dialog, toplevel_destroy); 53 54 (void)data; 55 dialog->toplevel = NULL; 56 } 57 58 static void 59 destroy_dialog(struct wl_resource *resource) 60 { 61 struct dialog *dialog = wl_resource_get_user_data(resource); 62 63 /* no longer a dialog: stop blocking */ 64 set(resource, false); 65 if (dialog->toplevel) { 66 wl_list_remove(&dialog->toplevel_destroy.link); 67 } 68 free(dialog); 69 } 70 71 static void 72 get_xdg_dialog(struct wl_client *client, struct wl_resource *resource, uint32_t id, 73 struct wl_resource *toplevel) 74 { 75 struct dialog *dialog = calloc(1, sizeof *dialog); 76 77 if (!dialog) { 78 wl_resource_post_no_memory(resource); 79 return; 80 } 81 dialog->resource = wl_resource_create(client, &xdg_dialog_v1_interface, 82 wl_resource_get_version(resource), id); 83 if (!dialog->resource) { 84 free(dialog); 85 wl_resource_post_no_memory(resource); 86 return; 87 } 88 dialog->toplevel = toplevel; 89 dialog->toplevel_destroy.notify = handle_toplevel_destroy; 90 wl_resource_add_destroy_listener(toplevel, &dialog->toplevel_destroy); 91 wl_resource_set_implementation(dialog->resource, &dialog_impl, dialog, destroy_dialog); 92 } 93 94 static const struct xdg_wm_dialog_v1_interface manager_impl = { 95 .destroy = destroy_resource, 96 .get_xdg_dialog = get_xdg_dialog, 97 }; 98 99 static void 100 bind_manager(struct wl_client *client, void *data, uint32_t version, uint32_t id) 101 { 102 struct wl_resource *resource; 103 104 (void)data; 105 resource = wl_resource_create(client, &xdg_wm_dialog_v1_interface, version, id); 106 if (!resource) { 107 wl_client_post_no_memory(client); 108 return; 109 } 110 wl_resource_set_implementation(resource, &manager_impl, NULL, NULL); 111 } 112 113 struct wl_global * 114 xdg_dialog_manager_create(struct wl_display *display) 115 { 116 return wl_global_create(display, &xdg_wm_dialog_v1_interface, 1, NULL, &bind_manager); 117 }