Loading

owl-mach-ipc demo client.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <wayland-client.h>
  4. #include "owl-mach-ipc-unstable-v1-client-procotol.h"
  5.  
  6. #include <mach/mach.h>
  7. #include <servers/bootstrap.h>
  8. #include "owl-mach-ipc-unstable-v1-mig-user.h"
  9.  
  10. static mach_port_t sport;
  11.  
  12. static void mach_ipc_port_secret_handler
  13. (
  14.     void *data,
  15.     struct zowl_mach_ipc_port_v1 *mach_ipc_port,
  16.     const char *secret
  17. ) {
  18.     mach_port_t the_port;
  19.     kern_return_t kr = owl_mach_ipc_v1_retrieve_port(sport, secret, &the_port);
  20.     if (kr != KERN_SUCCESS) {
  21.         fprintf(stderr, "owl_mach_ipc_v1_retrieve_port(%s) = %x\n", secret, kr);
  22.         return;
  23.     }
  24.     fprintf(stderr, "owl_mach_ipc_v1_retrieve_port(%s) SUCCESS -> %d\n", secret, the_port);
  25.     exit(0);
  26. }
  27.  
  28. static const struct zowl_mach_ipc_port_v1_listener mach_ipc_port_listener = {
  29.     .secret = mach_ipc_port_secret_handler
  30. };
  31.  
  32. static void mach_ipc_bootstrap_name_handler
  33. (
  34.     void *data,
  35.     struct zowl_mach_ipc_v1 *mach_ipc,
  36.     const char *name
  37. ) {
  38.     kern_return_t kr = bootstrap_look_up(bootstrap_port, name, &sport);
  39.     if (kr != KERN_SUCCESS) {
  40.         fprintf(stderr, "bootstrap_look_up(%s) = %x\n", name, kr);
  41.         return;
  42.     }
  43.  
  44.     fprintf(stderr, "bootstrap_look_up(%s) SUCCESS -> %d\n", name, sport);
  45.     struct zowl_mach_ipc_port_v1 *mach_ipc_port = zowl_mach_ipc_v1_create_port(mach_ipc);
  46.     zowl_mach_ipc_port_v1_add_listener(mach_ipc_port, &mach_ipc_port_listener, NULL);
  47. }
  48.  
  49. static struct zowl_mach_ipc_v1_listener mach_ipc_listener = {
  50.     .bootstrap_name = mach_ipc_bootstrap_name_handler
  51. };
  52.  
  53. static void registry_global_handler
  54. (
  55.     void *data,
  56.     struct wl_registry *registry,
  57.     uint32_t name,
  58.     const char *interface,
  59.     uint32_t version
  60. ) {
  61.     if (strcmp(interface, "zowl_mach_ipc_v1") == 0) {
  62.         struct zowl_mach_ipc_v1 *mach_ipc = wl_registry_bind(registry, name, &zowl_mach_ipc_v1_interface, 1);
  63.         zowl_mach_ipc_v1_add_listener(mach_ipc, &mach_ipc_listener, NULL);
  64.     }
  65. }
  66.  
  67. static void registry_global_remove_handler
  68. (
  69.     void *data,
  70.     struct wl_registry *registry,
  71.     uint32_t name
  72. ) {}
  73.  
  74. static const struct wl_registry_listener registry_listener = {
  75.     .global = registry_global_handler,
  76.     .global_remove = registry_global_remove_handler
  77. };
  78.  
  79. int main() {
  80.     struct wl_display *display = wl_display_connect(NULL);
  81.  
  82.     if (display == NULL) {
  83.         perror("wl_display_connect");
  84.         return 1;
  85.     }
  86.  
  87.     struct wl_registry *registry = wl_display_get_registry(display);
  88.     wl_registry_add_listener(registry, &registry_listener, NULL);
  89.  
  90.     while (1) {
  91.         int ret = wl_display_dispatch(display);
  92.         if (ret < 0) {
  93.             perror("wl_display_dispatch");
  94.             return 1;
  95.         }
  96.     }
  97. }