wl-app

example app to learn the basic of wayland
Log | Files | Refs | README

wl.c (2332B)


      1 #include <assert.h>
      2 #include <errno.h>
      3 #include <stdint.h>
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 #include <string.h>
      7 #include <sys/socket.h>
      8 #include <sys/un.h>
      9 
     10 #include "types.h"
     11 
     12 #define roundup_4(n) (((n) + 3) & -4)
     13 
     14 int wayland_display_connect(string *wsp) {
     15 
     16   struct sockaddr_un addr = {.sun_family = AF_UNIX};
     17   memcpy(addr.sun_path, wsp->data,
     18          wsp->len); // only way to assign our path to the addr .sun_path[],
     19                     // compiler expects length at runtime but it is variable
     20 
     21   int fd = socket(AF_UNIX, SOCK_STREAM, 0);
     22 
     23   if (fd == -1)
     24     exit(errno);
     25   // int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
     26   if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
     27     exit(errno);
     28 
     29   return fd;
     30 }
     31 
     32 void wayland_handle_message(int fd, state_t *state, char **msg,
     33                                    uint64_t *msg_len) {
     34   assert(*msg_len >= 8);
     35 
     36   uint32_t object_id = buf_read_u32(msg, msg_len);
     37   assert(object_id <= wayland_current_id);
     38 
     39   uint16_t opcode = buf_read_u16(msg, msg_len);
     40 
     41   uint16_t announced_size = buf_read_u16(msg, msg_len);
     42   assert(roundup_4(announced_size) <= announced_size);
     43 
     44   uint32_t header_size =
     45       sizeof(object_id) + sizeof(opcode) + sizeof(announced_size);
     46   assert(announced_size <= header_size + *msg_len);
     47 
     48   if (object_id == state->wl_registry &&
     49       opcode == wayland_wl_registry_event_global) {
     50     // TODO
     51   }
     52   // Following: Lots of `if (opcode == ...) {... } else if (opcode = ...) { ...
     53   // } [...]`
     54 }
     55 
     56 uint32_t wayland_wl_display_get_registry(int fd) {
     57   uint64_t msg_size = 0;
     58   char msg[128] = "";
     59   buf_write_u32(msg, &msg_size, sizeof(msg), wayland_display_object_id);
     60 
     61   buf_write_u16(msg, &msg_size, sizeof(msg),
     62                 wayland_wl_display_get_registry_opcode);
     63 
     64   uint16_t msg_announced_size =
     65       wayland_header_size + sizeof(wayland_current_id);
     66   assert(roundup_4(msg_announced_size) == msg_announced_size);
     67   buf_write_u16(msg, &msg_size, sizeof(msg), msg_announced_size);
     68 
     69   wayland_current_id++;
     70   buf_write_u32(msg, &msg_size, sizeof(msg), wayland_current_id);
     71 
     72   if ((int64_t)msg_size != send(fd, msg, msg_size, MSG_DONTWAIT))
     73     exit(errno);
     74 
     75   printf("-> wl_display@%u.get_registry: wl_registry=%u\n",
     76          wayland_display_object_id, wayland_current_id);
     77 
     78   return wayland_current_id;
     79 }