Kristian Høgsberg | 97f1ebe | 2008-09-30 09:46:10 -0400 | [diff] [blame^] | 1 | #include <stdlib.h> |
| 2 | #include <stdint.h> |
| 3 | #include <stddef.h> |
| 4 | #include <stdio.h> |
| 5 | #include <errno.h> |
| 6 | #include <string.h> |
| 7 | #include <unistd.h> |
| 8 | #include <sys/socket.h> |
| 9 | #include <sys/un.h> |
| 10 | #include <ctype.h> |
| 11 | |
| 12 | static const char socket_name[] = "\0wayland"; |
| 13 | |
| 14 | struct method { |
| 15 | const char *name; |
| 16 | uint32_t opcode; |
| 17 | }; |
| 18 | |
| 19 | static const struct method display_methods[] = { |
| 20 | { "get_interface", 0 }, |
| 21 | { "create_surface", 1 }, |
| 22 | { NULL } |
| 23 | }; |
| 24 | |
| 25 | static const struct method surface_methods[] = { |
| 26 | { "get_interface", 0 }, |
| 27 | { "post", 1 }, |
| 28 | { NULL } |
| 29 | }; |
| 30 | |
| 31 | struct interface { |
| 32 | const char *name; |
| 33 | const struct method *methods; |
| 34 | }; |
| 35 | |
| 36 | static const struct interface interfaces[] = { |
| 37 | { "display", display_methods }, |
| 38 | { "surface", surface_methods }, |
| 39 | { NULL }, |
| 40 | }; |
| 41 | |
| 42 | int send_request(int sock, const char *buffer, int buffer_len) |
| 43 | { |
| 44 | const struct method *methods; |
| 45 | char interface[32], method[32]; |
| 46 | uint32_t request[3], id, new_id; |
| 47 | int i; |
| 48 | |
| 49 | if (sscanf(buffer, "%d/%32[^:]:%32s %d\n", |
| 50 | &id, interface, method, &new_id) != 4) { |
| 51 | printf("invalid input, expected <id>/<interface>:<method>\n"); |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | printf("got object id %d, interface \"%s\", name \"%s\"\n", |
| 56 | id, interface, method); |
| 57 | |
| 58 | for (i = 0; interfaces[i].name != NULL; i++) { |
| 59 | if (strcmp(interfaces[i].name, interface) == 0) |
| 60 | break; |
| 61 | } |
| 62 | if (interfaces[i].name == NULL) { |
| 63 | printf("unknown interface \"%s\"\n", interface); |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | methods = interfaces[i].methods; |
| 68 | for (i = 0; methods[i].name != NULL; i++) { |
| 69 | if (strcmp(methods[i].name, method) == 0) |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | if (methods[i].name == NULL) { |
| 74 | printf("unknown request \"%s\"\n", method); |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | request[0] = id; |
| 79 | request[1] = methods[i].opcode; |
| 80 | request[2] = new_id; |
| 81 | |
| 82 | return write(sock, request, sizeof request); |
| 83 | } |
| 84 | |
| 85 | int main(int argc, char *argv[]) |
| 86 | { |
| 87 | struct sockaddr_un name; |
| 88 | socklen_t size; |
| 89 | int sock, len; |
| 90 | char buffer[256]; |
| 91 | |
| 92 | sock = socket(PF_LOCAL, SOCK_STREAM, 0); |
| 93 | if (sock < 0) |
| 94 | return -1; |
| 95 | |
| 96 | name.sun_family = AF_LOCAL; |
| 97 | memcpy(name.sun_path, socket_name, sizeof socket_name); |
| 98 | |
| 99 | size = offsetof (struct sockaddr_un, sun_path) + sizeof socket_name; |
| 100 | |
| 101 | if (connect (sock, (struct sockaddr *) &name, size) < 0) |
| 102 | return -1; |
| 103 | |
| 104 | while (len = read(STDIN_FILENO, buffer, sizeof buffer), len > 0) |
| 105 | if (send_request(sock, buffer, len) < 0) |
| 106 | break; |
| 107 | |
| 108 | return 0; |
| 109 | } |