Created
June 18, 2017 14:14
-
-
Save embik/c3d0f86d030a1b2bba3d615c5323a6dd to your computer and use it in GitHub Desktop.
C vs Rust Wayland Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] extern crate wayland_client; | |
//#[macro_use] extern crate wayland_sys; | |
use wayland_client::{EventQueueHandle, default_connect}; | |
use wayland_client::protocol::wl_registry; | |
struct RegistryHandler { /* ... */ } | |
impl wl_registry::Handler for RegistryHandler { | |
fn global( | |
&mut self, | |
evqh: &mut EventQueueHandle, | |
proxy: &wl_registry::WlRegistry, | |
name: u32, | |
interface: String, | |
version: u32 | |
) { | |
println!("Got a registry event for {} id {}", interface, name); | |
} | |
fn global_remove( | |
&mut self, | |
evqh: &mut EventQueueHandle, | |
proxy: &wl_registry::WlRegistry, | |
name: u32 | |
) { | |
println!("Got a registry losing event for {}", name); | |
} | |
} | |
declare_handler!(RegistryHandler, wl_registry::Handler, wl_registry::WlRegistry); | |
fn main() { | |
let (display, mut event_queue) = default_connect().unwrap(); | |
let handler_id = event_queue.add_handler(RegistryHandler{}); | |
loop { | |
display.flush().unwrap(); | |
event_queue.dispatch().unwrap(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <wayland-client.h> | |
struct wl_display *display = NULL; | |
struct wl_compositor *compositor = NULL; | |
static void | |
global_registry_handler(void *data, struct wl_registry *registry, uint32_t id, | |
const char *interface, uint32_t version) | |
{ | |
printf("Got a registry event for %s id %d\n", interface, id); | |
if (strcmp(interface, "wl_compositor") == 0) | |
compositor = wl_registry_bind(registry, | |
id, | |
&wl_compositor_interface, | |
1); | |
} | |
static void | |
global_registry_remover(void *data, struct wl_registry *registry, uint32_t id) | |
{ | |
printf("Got a registry losing event for %d\n", id); | |
} | |
static const struct wl_registry_listener registry_listener = { | |
global_registry_handler, | |
global_registry_remover | |
}; | |
int main(int argc, char **argv) { | |
display = wl_display_connect(NULL); | |
if (display == NULL) { | |
fprintf(stderr, "Can't connect to display\n"); | |
exit(1); | |
} | |
printf("connected to display\n"); | |
struct wl_registry *registry = wl_display_get_registry(display); | |
wl_registry_add_listener(registry, ®istry_listener, NULL); | |
wl_display_dispatch(display); | |
wl_display_roundtrip(display); | |
if (compositor == NULL) { | |
fprintf(stderr, "Can't find compositor\n"); | |
exit(1); | |
} else { | |
fprintf(stderr, "Found compositor\n"); | |
} | |
wl_display_disconnect(display); | |
printf("disconnected from display\n"); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment