<commit_before>use na::{clamp, Norm};

use {lerp, Point3f, Vector3f};

/// Perlin noise
pub fn noise3(p: &Point3f) -> f32 {
    noise(p.x, p.y, p.z)
}

pub fn noise(x: f32, y: f32, z: f32) -> f32 {
    // Compute noise cell coordinates and offsets
    let mut ix = x.floor() as usize;
    let mut iy = x.floor() as usize;
    let mut iz = x.floor() as usize;
    let dx = x - ix as f32;
    let dy = y - iy as f32;
    let dz = z - iz as f32;

    // Compute gradient weights
    ix &= NOISE_PERM_SIZE - 1;
    iy &= NOISE_PERM_SIZE - 1;
    iz &= NOISE_PERM_SIZE - 1;
    let w000 = grad(ix, iy, iz, dx, dy, dz);
    let w100 = grad(ix + 1, iy, iz, dx - 1.0, dy, dz);
    let w010 = grad(ix, iy + 1, iz, dx, dy - 1.0, dz);
    let w110 = grad(ix + 1, iy + 1, iz, dx - 1.0, dy - 1.0, dz);
    let w001 = grad(ix, iy, iz + 1, dx, dy, dz - 1.0);
    let w101 = grad(ix + 1, iy, iz + 1, dx - 1.0, dy, dz - 1.0);
    let w011 = grad(ix, iy + 1, iz + 1, dx, dy - 1.0, dz - 1.0);
    let w111 = grad(ix + 1, iy + 1, iz + 1, dx - 1.0, dy - 1.0, dz - 1.0);

    // Compute trilinear interpolation of weights
    let wx = noise_weight(dx);
    let wy = noise_weight(dy);
    let wz = noise_weight(dz);
    let x00 = lerp(wx, w000, w100);
    let x10 = lerp(wx, w010, w110);
    let x01 = lerp(wx, w001, w101);
    let x11 = lerp(wx, w011, w111);
    let y0 = lerp(wy, x00, x10);
    let y1 = lerp(wy, x10, x11);

    lerp(wz, y0, y1)
}

/// Fractional Brownian Motion
pub fn fbm(p: &Point3f, dpdx: &Vector3f, dpdy: &Vector3f, omega: f32, max_octaves: u32) -> f32 {
    // Compute number of octaves for antialiased FBm
    let len2 = dpdx.norm_squared().max(dpdy.norm_squared());
    let n = clamp(-1.0 - 0.5 * len2.log2(), 0.0, max_octaves as f32);
    let n_int = n.floor() as u32;

    // Compute sum of octaves of noise for FBm
    let mut sum = 0.0;
    let mut lambda = 1.0;
    let mut o = 1.0;
    for i in 0..n_int {
        sum += o * noise3(&(lambda * *p));
        lambda *= 1.99;
        o *= omega;
    }
    let n_partial = n - n_int as f32;
    sum += o * smooth_step(0.3, 0.7, n_partial) * noise3(&(lambda * *p));

    sum
}

#[inline]
fn grad(x: usize, y: usize, z: usize, dx: f32, dy: f32, dz: f32) -> f32 {
    let mut h = NOISE_PERM[NOISE_PERM[NOISE_PERM[x as usize] + y as usize] + z as usize];
    h &= 15;
    let u = if h < 8 || h == 12 || h == 13 { dx } else { dy };
    let v = if h < 4 || h == 12 || h == 13 { dy } else { dz };

    (if h & 1 != 0 { -u } else { u }) + (if h & 2 != 0 { -v } else { v })
}

#[inline]
fn noise_weight(t: f32) -> f32 {
    let t3 = t * t * t;
    let t4 = t3 * t;
    6.0 * t4 * t - 15.0 * t4 + 10.0 * t3
}

#[inline]
fn smooth_step(min: f32, max: f32, value: f32) -> f32 {
    let v = clamp((value - min) / (max - min), 0.0, 1.0);

    v * v * (-2.0 * v + 3.0)
}

const NOISE_PERM_SIZE: usize = 256;
const NOISE_PERM: [usize; 2 * NOISE_PERM_SIZE] = [151,
                                                  160,
                                                  137,
                                                  91,
                                                  90,
                                                  15,
                                                  131,
                                                  13,
                                                  201,
                                                  95,
                                                  96,
                                                  53,
                                                  194,
                                                  233,
                                                  7,
                                                  225,
                                                  140,
                                                  36,
                                                  103,
                                                  30,
                                                  69,
                                                  142,
                                                  // Remainder of the noise permutation table
                                                  8,
                                                  99,
                                                  37,
                                                  240,
                                                  21,
                                                  10,
                                                  23,
                                                  190,
                                                  6,
                                                  148,
                                                  247,
                                                  120,
                                                  234,
                                                  75,
                                                  0,
                                                  26,
                                                  197,
                                                  62,
                                                  94,
                                                  252,
                                                  219,
                                                  203,
                                                  117,
                                                  35,
                                                  11,
                                                  32,
                                                  57,
                                                  177,
                                                  33,
                                                  88,
                                                  237,
                                                  149,
                                                  56,
                                                  87,
                                                  174,
                                                  20,
                                                  125,
                                                  136,
                                                  171,
                                                  168,
                                                  68,
                                                  175,
                                                  74,
                                                  165,
                                                  71,
                                                  134,
                                                  139,
                                                  48,
                                                  27,
                                                  166,
                                                  77,
                                                  146,
                                                  158,
                                                  231,
                                                  83,
                                                  111,
                                                  229,
                                                  122,
                                                  60,
                                                  211,
                                                  133,
                                                  230,
                                                  220,
                                                  105,
                                                  92,
                                                  41,
                                                  55,
                                                  46,
                                                  245,
                                                  40,
                                                  244,
                                                  102,
                                                  143,
                                                  54,
                                                  65,
                                                  25,
                                                  63,
                                                  161,
                                                  1,
                                                  216,
                                                  80,
                                                  73,
                                                  209,
                                                  76,
                                                  132,
                                                  187,
                                                  208,
                                                  89,
                                                  18,
                                                  169,
                                                  200,
                                                  196,
                                                  135,
                                                  130,
                                                  116,
                                                  188,
                                                  159,
                                                  86,
                                                  164,
                                                  100,
                                                  109,
                                                  198,
                                                  173,
                                                  186,
                                                  3,
                                                  64,
                                                  52,
                                                  217,
                                                  226,
                                                  250,
                                                  124,
                                                  123,
                                                  5,
                                                  202,
                                                  38,
                                                  147,
                                                  118,
                                                  126,
                                                  255,
                                                  82,
                                                  85,
                                                  212,
                                                  207,
                                                  206,
                                                  59,
                                                  227,
                                                  47,
                                                  16,
                                                  58,
                                                  17,
                                                  182,
                                                  189,
                                                  28,
                                                  42,
                                                  223,
                                                  183,
                                                  170,
                                                  213,
                                                  119,
                                                  248,
                                                  152,
                                                  2,
                                                  44,
                                                  154,
                                                  163,
                                                  70,
                                                  221,
                                                  153,
                                                  101,
                                                  155,
                                                  167,
                                                  43,
                                                  172,
                                                  9,
                                                  129,
                                                  22,
                                                  39,
                                                  253,
                                                  19,
                                                  98,
                                                  108,
                                                  110,
                                                  79,
                                                  113,
                                                  224,
                                                  232,
                                                  178,
                                                  185,
                                                  112,
                                                  104,
                                                  218,
                                                  246,
                                                  97,
                                                  228,
                                                  251,
                                                  34,
                                                  242,
                                                  193,
                                                  238,
                                                  210,
                                                  144,
                                                  12,
                                                  191,
                                                  179,
                                                  162,
                                                  241,
                                                  81,
                                                  51,
                                                  145,
                                                  235,
                                                  249,
                                                  14,
                                                  239,
                                                  107,
                                                  49,
                                                  192,
                                                  214,
                                                  31,
                                                  181,
                                                  199,
                                                  106,
                                                  157,
                                                  184,
                                                  84,
                                                  204,
                                                  176,
                                                  115,
                                                  121,
                                                  50,
                                                  45,
                                                  127,
                                                  4,
                                                  150,
                                                  254,
                                                  138,
                                                  236,
                                                  205,
                                                  93,
                                                  222,
                                                  114,
                                                  67,
                                                  29,
                                                  24,
                                                  72,
                                                  243,
                                                  141,
                                                  128,
                                                  195,
                                                  78,
                                                  66,
                                                  215,
                                                  61,
                                                  156,
                                                  180,
                                                  151,
                                                  160,
                                                  137,
                                                  91,
                                                  90,
                                                  15,
                                                  131,
                                                  13,
                                                  201,
                                                  95,
                                                  96,
                                                  53,
                                                  194,
                                                  233,
                                                  7,
                                                  225,
                                                  140,
                                                  36,
                                                  103,
                                                  30,
                                                  69,
                                                  142,
                                                  8,
                                                  99,
                                                  37,
                                                  240,
                                                  21,
                                                  10,
                                                  23,
                                                  190,
                                                  6,
                                                  148,
                                                  247,
                                                  120,
                                                  234,
                                                  75,
                                                  0,
                                                  26,
                                                  197,
                                                  62,
                                                  94,
                                                  252,
                                                  219,
                                                  203,
                                                  117,
                                                  35,
                                                  11,
                                                  32,
                                                  57,
                                                  177,
                                                  33,
                                                  88,
                                                  237,
                                                  149,
                                                  56,
                                                  87,
                                                  174,
                                                  20,
                                                  125,
                                                  136,
                                                  171,
                                                  168,
                                                  68,
                                                  175,
                                                  74,
                                                  165,
                                                  71,
                                                  134,
                                                  139,
                                                  48,
                                                  27,
                                                  166,
                                                  77,
                                                  146,
                                                  158,
                                                  231,
                                                  83,
                                                  111,
                                                  229,
                                                  122,
                                                  60,
                                                  211,
                                                  133,
                                                  230,
                                                  220,
                                                  105,
                                                  92,
                                                  41,
                                                  55,
                                                  46,
                                                  245,
                                                  40,
                                                  244,
                                                  102,
                                                  143,
                                                  54,
                                                  65,
                                                  25,
                                                  63,
                                                  161,
                                                  1,
                                                  216,
                                                  80,
                                                  73,
                                                  209,
                                                  76,
                                                  132,
                                                  187,
                                                  208,
                                                  89,
                                                  18,
                                                  169,
                                                  200,
                                                  196,
                                                  135,
                                                  130,
                                                  116,
                                                  188,
                                                  159,
                                                  86,
                                                  164,
                                                  100,
                                                  109,
                                                  198,
                                                  173,
                                                  186,
                                                  3,
                                                  64,
                                                  52,
                                                  217,
                                                  226,
                                                  250,
                                                  124,
                                                  123,
                                                  5,
                                                  202,
                                                  38,
                                                  147,
                                                  118,
                                                  126,
                                                  255,
                                                  82,
                                                  85,
                                                  212,
                                                  207,
                                                  206,
                                                  59,
                                                  227,
                                                  47,
                                                  16,
                                                  58,
                                                  17,
                                                  182,
                                                  189,
                                                  28,
                                                  42,
                                                  223,
                                                  183,
                                                  170,
                                                  213,
                                                  119,
                                                  248,
                                                  152,
                                                  2,
                                                  44,
                                                  154,
                                                  163,
                                                  70,
                                                  221,
                                                  153,
                                                  101,
                                                  155,
                                                  167,
                                                  43,
                                                  172,
                                                  9,
                                                  129,
                                                  22,
                                                  39,
                                                  253,
                                                  19,
                                                  98,
                                                  108,
                                                  110,
                                                  79,
                                                  113,
                                                  224,
                                                  232,
                                                  178,
                                                  185,
                                                  112,
                                                  104,
                                                  218,
                                                  246,
                                                  97,
                                                  228,
                                                  251,
                                                  34,
                                                  242,
                                                  193,
                                                  238,
                                                  210,
                                                  144,
                                                  12,
                                                  191,
                                                  179,
                                                  162,
                                                  241,
                                                  81,
                                                  51,
                                                  145,
                                                  235,
                                                  249,
                                                  14,
                                                  239,
                                                  107,
                                                  49,
                                                  192,
                                                  214,
                                                  31,
                                                  181,
                                                  199,
                                                  106,
                                                  157,
                                                  184,
                                                  84,
                                                  204,
                                                  176,
                                                  115,
                                                  121,
                                                  50,
                                                  45,
                                                  127,
                                                  4,
                                                  150,
                                                  254,
                                                  138,
                                                  236,
                                                  205,
                                                  93,
                                                  222,
                                                  114,
                                                  67,
                                                  29,
                                                  24,
                                                  72,
                                                  243,
                                                  141,
                                                  128,
                                                  195,
                                                  78,
                                                  66,
                                                  215,
                                                  61,
                                                  156,
                                                  180];
<commit_msg>Fix bug in perlin noise function<commit_after>use na::{clamp, Norm};

use {lerp, Point3f, Vector3f};

/// Perlin noise
pub fn noise3(p: &Point3f) -> f32 {
    noise(p.x, p.y, p.z)
}

pub fn noise(x: f32, y: f32, z: f32) -> f32 {
    // Compute noise cell coordinates and offsets
    let mut ix = x.floor() as usize;
    let mut iy = x.floor() as usize;
    let mut iz = x.floor() as usize;
    let dx = x - ix as f32;
    let dy = y - iy as f32;
    let dz = z - iz as f32;

    // Compute gradient weights
    ix &= NOISE_PERM_SIZE - 1;
    iy &= NOISE_PERM_SIZE - 1;
    iz &= NOISE_PERM_SIZE - 1;
    let w000 = grad(ix, iy, iz, dx, dy, dz);
    let w100 = grad(ix + 1, iy, iz, dx - 1.0, dy, dz);
    let w010 = grad(ix, iy + 1, iz, dx, dy - 1.0, dz);
    let w110 = grad(ix + 1, iy + 1, iz, dx - 1.0, dy - 1.0, dz);
    let w001 = grad(ix, iy, iz + 1, dx, dy, dz - 1.0);
    let w101 = grad(ix + 1, iy, iz + 1, dx - 1.0, dy, dz - 1.0);
    let w011 = grad(ix, iy + 1, iz + 1, dx, dy - 1.0, dz - 1.0);
    let w111 = grad(ix + 1, iy + 1, iz + 1, dx - 1.0, dy - 1.0, dz - 1.0);

    // Compute trilinear interpolation of weights
    let wx = noise_weight(dx);
    let wy = noise_weight(dy);
    let wz = noise_weight(dz);
    let x00 = lerp(wx, w000, w100);
    let x10 = lerp(wx, w010, w110);
    let x01 = lerp(wx, w001, w101);
    let x11 = lerp(wx, w011, w111);
    let y0 = lerp(wy, x00, x10);
    let y1 = lerp(wy, x01, x11);

    lerp(wz, y0, y1)
}

/// Fractional Brownian Motion
pub fn fbm(p: &Point3f, dpdx: &Vector3f, dpdy: &Vector3f, omega: f32, max_octaves: u32) -> f32 {
    // Compute number of octaves for antialiased FBm
    let len2 = dpdx.norm_squared().max(dpdy.norm_squared());
    let n = clamp(-1.0 - 0.5 * len2.log2(), 0.0, max_octaves as f32);
    let n_int = n.floor() as u32;

    // Compute sum of octaves of noise for FBm
    let mut sum = 0.0;
    let mut lambda = 1.0;
    let mut o = 1.0;
    for i in 0..n_int {
        sum += o * noise3(&(lambda * *p));
        lambda *= 1.99;
        o *= omega;
    }
    let n_partial = n - n_int as f32;
    sum += o * smooth_step(0.3, 0.7, n_partial) * noise3(&(lambda * *p));

    sum
}

#[inline]
fn grad(x: usize, y: usize, z: usize, dx: f32, dy: f32, dz: f32) -> f32 {
    let mut h = NOISE_PERM[NOISE_PERM[NOISE_PERM[x as usize] + y as usize] + z as usize];
    h &= 15;
    let u = if h < 8 || h == 12 || h == 13 { dx } else { dy };
    let v = if h < 4 || h == 12 || h == 13 { dy } else { dz };

    (if h & 1 != 0 { -u } else { u }) + (if h & 2 != 0 { -v } else { v })
}

#[inline]
fn noise_weight(t: f32) -> f32 {
    let t3 = t * t * t;
    let t4 = t3 * t;
    6.0 * t4 * t - 15.0 * t4 + 10.0 * t3
}

#[inline]
fn smooth_step(min: f32, max: f32, value: f32) -> f32 {
    let v = clamp((value - min) / (max - min), 0.0, 1.0);

    v * v * (-2.0 * v + 3.0)
}

const NOISE_PERM_SIZE: usize = 256;
const NOISE_PERM: [usize; 2 * NOISE_PERM_SIZE] = [151,
                                                  160,
                                                  137,
                                                  91,
                                                  90,
                                                  15,
                                                  131,
                                                  13,
                                                  201,
                                                  95,
                                                  96,
                                                  53,
                                                  194,
                                                  233,
                                                  7,
                                                  225,
                                                  140,
                                                  36,
                                                  103,
                                                  30,
                                                  69,
                                                  142,
                                                  // Remainder of the noise permutation table
                                                  8,
                                                  99,
                                                  37,
                                                  240,
                                                  21,
                                                  10,
                                                  23,
                                                  190,
                                                  6,
                                                  148,
                                                  247,
                                                  120,
                                                  234,
                                                  75,
                                                  0,
                                                  26,
                                                  197,
                                                  62,
                                                  94,
                                                  252,
                                                  219,
                                                  203,
                                                  117,
                                                  35,
                                                  11,
                                                  32,
                                                  57,
                                                  177,
                                                  33,
                                                  88,
                                                  237,
                                                  149,
                                                  56,
                                                  87,
                                                  174,
                                                  20,
                                                  125,
                                                  136,
                                                  171,
                                                  168,
                                                  68,
                                                  175,
                                                  74,
                                                  165,
                                                  71,
                                                  134,
                                                  139,
                                                  48,
                                                  27,
                                                  166,
                                                  77,
                                                  146,
                                                  158,
                                                  231,
                                                  83,
                                                  111,
                                                  229,
                                                  122,
                                                  60,
                                                  211,
                                                  133,
                                                  230,
                                                  220,
                                                  105,
                                                  92,
                                                  41,
                                                  55,
                                                  46,
                                                  245,
                                                  40,
                                                  244,
                                                  102,
                                                  143,
                                                  54,
                                                  65,
                                                  25,
                                                  63,
                                                  161,
                                                  1,
                                                  216,
                                                  80,
                                                  73,
                                                  209,
                                                  76,
                                                  132,
                                                  187,
                                                  208,
                                                  89,
                                                  18,
                                                  169,
                                                  200,
                                                  196,
                                                  135,
                                                  130,
                                                  116,
                                                  188,
                                                  159,
                                                  86,
                                                  164,
                                                  100,
                                                  109,
                                                  198,
                                                  173,
                                                  186,
                                                  3,
                                                  64,
                                                  52,
                                                  217,
                                                  226,
                                                  250,
                                                  124,
                                                  123,
                                                  5,
                                                  202,
                                                  38,
                                                  147,
                                                  118,
                                                  126,
                                                  255,
                                                  82,
                                                  85,
                                                  212,
                                                  207,
                                                  206,
                                                  59,
                                                  227,
                                                  47,
                                                  16,
                                                  58,
                                                  17,
                                                  182,
                                                  189,
                                                  28,
                                                  42,
                                                  223,
                                                  183,
                                                  170,
                                                  213,
                                                  119,
                                                  248,
                                                  152,
                                                  2,
                                                  44,
                                                  154,
                                                  163,
                                                  70,
                                                  221,
                                                  153,
                                                  101,
                                                  155,
                                                  167,
                                                  43,
                                                  172,
                                                  9,
                                                  129,
                                                  22,
                                                  39,
                                                  253,
                                                  19,
                                                  98,
                                                  108,
                                                  110,
                                                  79,
                                                  113,
                                                  224,
                                                  232,
                                                  178,
                                                  185,
                                                  112,
                                                  104,
                                                  218,
                                                  246,
                                                  97,
                                                  228,
                                                  251,
                                                  34,
                                                  242,
                                                  193,
                                                  238,
                                                  210,
                                                  144,
                                                  12,
                                                  191,
                                                  179,
                                                  162,
                                                  241,
                                                  81,
                                                  51,
                                                  145,
                                                  235,
                                                  249,
                                                  14,
                                                  239,
                                                  107,
                                                  49,
                                                  192,
                                                  214,
                                                  31,
                                                  181,
                                                  199,
                                                  106,
                                                  157,
                                                  184,
                                                  84,
                                                  204,
                                                  176,
                                                  115,
                                                  121,
                                                  50,
                                                  45,
                                                  127,
                                                  4,
                                                  150,
                                                  254,
                                                  138,
                                                  236,
                                                  205,
                                                  93,
                                                  222,
                                                  114,
                                                  67,
                                                  29,
                                                  24,
                                                  72,
                                                  243,
                                                  141,
                                                  128,
                                                  195,
                                                  78,
                                                  66,
                                                  215,
                                                  61,
                                                  156,
                                                  180,
                                                  151,
                                                  160,
                                                  137,
                                                  91,
                                                  90,
                                                  15,
                                                  131,
                                                  13,
                                                  201,
                                                  95,
                                                  96,
                                                  53,
                                                  194,
                                                  233,
                                                  7,
                                                  225,
                                                  140,
                                                  36,
                                                  103,
                                                  30,
                                                  69,
                                                  142,
                                                  8,
                                                  99,
                                                  37,
                                                  240,
                                                  21,
                                                  10,
                                                  23,
                                                  190,
                                                  6,
                                                  148,
                                                  247,
                                                  120,
                                                  234,
                                                  75,
                                                  0,
                                                  26,
                                                  197,
                                                  62,
                                                  94,
                                                  252,
                                                  219,
                                                  203,
                                                  117,
                                                  35,
                                                  11,
                                                  32,
                                                  57,
                                                  177,
                                                  33,
                                                  88,
                                                  237,
                                                  149,
                                                  56,
                                                  87,
                                                  174,
                                                  20,
                                                  125,
                                                  136,
                                                  171,
                                                  168,
                                                  68,
                                                  175,
                                                  74,
                                                  165,
                                                  71,
                                                  134,
                                                  139,
                                                  48,
                                                  27,
                                                  166,
                                                  77,
                                                  146,
                                                  158,
                                                  231,
                                                  83,
                                                  111,
                                                  229,
                                                  122,
                                                  60,
                                                  211,
                                                  133,
                                                  230,
                                                  220,
                                                  105,
                                                  92,
                                                  41,
                                                  55,
                                                  46,
                                                  245,
                                                  40,
                                                  244,
                                                  102,
                                                  143,
                                                  54,
                                                  65,
                                                  25,
                                                  63,
                                                  161,
                                                  1,
                                                  216,
                                                  80,
                                                  73,
                                                  209,
                                                  76,
                                                  132,
                                                  187,
                                                  208,
                                                  89,
                                                  18,
                                                  169,
                                                  200,
                                                  196,
                                                  135,
                                                  130,
                                                  116,
                                                  188,
                                                  159,
                                                  86,
                                                  164,
                                                  100,
                                                  109,
                                                  198,
                                                  173,
                                                  186,
                                                  3,
                                                  64,
                                                  52,
                                                  217,
                                                  226,
                                                  250,
                                                  124,
                                                  123,
                                                  5,
                                                  202,
                                                  38,
                                                  147,
                                                  118,
                                                  126,
                                                  255,
                                                  82,
                                                  85,
                                                  212,
                                                  207,
                                                  206,
                                                  59,
                                                  227,
                                                  47,
                                                  16,
                                                  58,
                                                  17,
                                                  182,
                                                  189,
                                                  28,
                                                  42,
                                                  223,
                                                  183,
                                                  170,
                                                  213,
                                                  119,
                                                  248,
                                                  152,
                                                  2,
                                                  44,
                                                  154,
                                                  163,
                                                  70,
                                                  221,
                                                  153,
                                                  101,
                                                  155,
                                                  167,
                                                  43,
                                                  172,
                                                  9,
                                                  129,
                                                  22,
                                                  39,
                                                  253,
                                                  19,
                                                  98,
                                                  108,
                                                  110,
                                                  79,
                                                  113,
                                                  224,
                                                  232,
                                                  178,
                                                  185,
                                                  112,
                                                  104,
                                                  218,
                                                  246,
                                                  97,
                                                  228,
                                                  251,
                                                  34,
                                                  242,
                                                  193,
                                                  238,
                                                  210,
                                                  144,
                                                  12,
                                                  191,
                                                  179,
                                                  162,
                                                  241,
                                                  81,
                                                  51,
                                                  145,
                                                  235,
                                                  249,
                                                  14,
                                                  239,
                                                  107,
                                                  49,
                                                  192,
                                                  214,
                                                  31,
                                                  181,
                                                  199,
                                                  106,
                                                  157,
                                                  184,
                                                  84,
                                                  204,
                                                  176,
                                                  115,
                                                  121,
                                                  50,
                                                  45,
                                                  127,
                                                  4,
                                                  150,
                                                  254,
                                                  138,
                                                  236,
                                                  205,
                                                  93,
                                                  222,
                                                  114,
                                                  67,
                                                  29,
                                                  24,
                                                  72,
                                                  243,
                                                  141,
                                                  128,
                                                  195,
                                                  78,
                                                  66,
                                                  215,
                                                  61,
                                                  156,
                                                  180];
<|endoftext|>


<commit_before>use wayland_client::EventQueueHandle;
use wayland_client::protocol::wl_keyboard::{self, WlKeyboard, KeyState, KeymapFormat};
use wayland_client::protocol::wl_surface::WlSurface;

use std::fs::File;
use std::ptr;
use std::os::unix::io::{FromRawFd, RawFd};

use memmap::{Mmap, Protection};

use ffi;
use ffi::XKBCOMMON_HANDLE as XKBH;

struct KbState {
    xkb_context: *mut ffi::xkb_context,
    xkb_keymap: *mut ffi::xkb_keymap,
    xkb_state: *mut ffi::xkb_state
}

impl KbState {
    fn update_modifiers(&mut self, mods_depressed: u32, mods_latched: u32, mods_locked: u32, group: u32) {
        unsafe {
            (XKBH.xkb_state_update_mask)(
                self.xkb_state, mods_depressed, mods_latched, mods_locked, 0, 0, group);
        }
    }

    pub fn get_one_sym(&self, keycode: u32) -> u32 {
        unsafe {
            (XKBH.xkb_state_key_get_one_sym)(
                self.xkb_state, keycode + 8)
        }
    }

    pub fn get_utf8(&self, keycode: u32) -> Option<String> {
        let size = unsafe {
            (XKBH.xkb_state_key_get_utf8)(self.xkb_state, keycode + 8, ptr::null_mut(), 0)
        } + 1;
        if size <= 1 { return None };
        let mut buffer = Vec::with_capacity(size as usize);
        unsafe {
            buffer.set_len(size as usize);
            (XKBH.xkb_state_key_get_utf8)(
                self.xkb_state, keycode + 8, buffer.as_mut_ptr() as *mut _, size as usize);
        };
        // remove the final `\0`
        buffer.pop();
        // libxkbcommon will always provide valid UTF8
        Some(unsafe { String::from_utf8_unchecked(buffer) } )
    }
}

impl Drop for KbState {
    fn drop(&mut self) {
        unsafe {
            (XKBH.xkb_state_unref)(self.xkb_state);
            (XKBH.xkb_keymap_unref)(self.xkb_keymap);
            (XKBH.xkb_context_unref)(self.xkb_context);
        }
    }
}

/// A wayland keyboard mapped to its keymap
///
/// It wraps an event iterator on this keyboard, catching the Keymap, Key, and Modifiers
/// events of the keyboard to handle them using libxkbcommon. All other events are directly
/// forwarded.
pub struct MappedKeyboard<H: Handler> {
    state: KbState,
    handler: H
}

pub enum MappedKeyboardError {
    XKBNotFound,
    NoKeyboardOnSeat
}

impl<H: Handler> MappedKeyboard<H> {
    pub fn new(handler: H) -> Result<MappedKeyboard<H>, MappedKeyboardError> {
        let xkbh = match ffi::XKBCOMMON_OPTION.as_ref() {
            Some(h) => h,
            None => return Err(MappedKeyboardError::XKBNotFound)
        };
        let xkb_context = unsafe {
            (xkbh.xkb_context_new)(ffi::xkb_context_flags::XKB_CONTEXT_NO_FLAGS)
        };
        if xkb_context.is_null() { return Err(MappedKeyboardError::XKBNotFound) }

        Ok(MappedKeyboard {
            state: KbState {
                xkb_context: xkb_context,
                xkb_keymap: ptr::null_mut(),
                xkb_state: ptr::null_mut()
            },
            handler: handler
        })
    }

    fn init(&mut self, fd: RawFd, size: usize) {
        let mut state = &mut self.state;

        let map = unsafe {
            Mmap::open_with_offset(&File::from_raw_fd(fd), Protection::Read, 0, size).unwrap()
        };

        let xkb_keymap = {
            unsafe {
                (XKBH.xkb_keymap_new_from_string)(
                    state.xkb_context,
                    map.ptr() as *const _,
                    ffi::xkb_keymap_format::XKB_KEYMAP_FORMAT_TEXT_V1,
                    ffi::xkb_keymap_compile_flags::XKB_KEYMAP_COMPILE_NO_FLAGS
                )
            }
        };

        if xkb_keymap.is_null() {
            panic!("Failed to load keymap!");
        }

        let xkb_state = unsafe {
            (XKBH.xkb_state_new)(xkb_keymap)
        };

        state.xkb_keymap = xkb_keymap;
        state.xkb_state = xkb_state;
    }

    pub fn handler(&mut self) -> &mut H {
        &mut self.handler
    }
}

#[allow(unused_variables)]
pub trait Handler {
    fn enter(&mut self, evqh: &mut EventQueueHandle, proxy: &WlKeyboard, serial: u32, surface: &WlSurface, rawkeys: &[u32], keysyms: &[u32]) {
    }

    fn leave(&mut self, evqh: &mut EventQueueHandle, proxy: &WlKeyboard, serial: u32, surface: &WlSurface) {
    }

    fn key(&mut self, evqh: &mut EventQueueHandle, proxy: &WlKeyboard, serial: u32, time: u32, rawkey: u32, keysym: u32, state: KeyState, utf8: Option<String>) {
    }

    fn repeat_info(&mut self, evqh: &mut EventQueueHandle, proxy: &WlKeyboard, rate: i32, delay: i32) {
    }
}

impl<H: Handler> wl_keyboard::Handler for MappedKeyboard<H> {
    fn keymap(&mut self, _: &mut EventQueueHandle, _: &WlKeyboard, _: KeymapFormat, fd: RawFd, size: u32) {
        self.init(fd, size as usize)
    }

    fn enter(&mut self, evqh: &mut EventQueueHandle, proxy: &WlKeyboard, serial: u32, surface: &WlSurface, keys: Vec<u8>) {
        let rawkeys: &[u32] = unsafe { ::std::slice::from_raw_parts(keys.as_ptr() as *const u32, keys.len()/4) };
        let keys: Vec<u32> = rawkeys.iter().map(|k| self.state.get_one_sym(*k)).collect();
        self.handler.enter(evqh, proxy, serial, surface, rawkeys, &keys)
    }

    fn leave(&mut self, evqh: &mut EventQueueHandle, proxy: &WlKeyboard, serial: u32, surface: &WlSurface) {
        self.handler.leave(evqh, proxy, serial, surface)
    }

    fn key(&mut self, evqh: &mut EventQueueHandle, proxy: &WlKeyboard, serial: u32, time: u32, key: u32, state: KeyState) {
        let sym = self.state.get_one_sym(key);
        let utf8 = self.state.get_utf8(key);
        self.handler.key(evqh, proxy, serial, time, key, sym, state, utf8)
    }

    fn modifiers(&mut self, _: &mut EventQueueHandle, _: &WlKeyboard, _: u32, mods_depressed: u32, mods_latched: u32, mods_locked: u32, group: u32) {
        self.state.update_modifiers(mods_depressed, mods_latched, mods_locked, group)
    }

    fn repeat_info(&mut self, evqh: &mut EventQueueHandle, proxy: &WlKeyboard, rate: i32, delay: i32) {
        self.handler.repeat_info(evqh, proxy, rate, delay)
    }
}

unsafe impl<H: Handler> ::wayland_client::Handler<WlKeyboard> for MappedKeyboard<H> {
    unsafe fn message(&mut self, evq: &mut EventQueueHandle, proxy: &WlKeyboard, opcode: u32, args: *const ::wayland_client::sys::wl_argument) -> Result<(),()> {
        <MappedKeyboard<H> as ::wayland_client::protocol::wl_keyboard::Handler>::__message(self, evq, proxy, opcode, args)
    }
}
<commit_msg>MappedKeyboard is Send<commit_after>use wayland_client::EventQueueHandle;
use wayland_client::protocol::wl_keyboard::{self, WlKeyboard, KeyState, KeymapFormat};
use wayland_client::protocol::wl_surface::WlSurface;

use std::fs::File;
use std::ptr;
use std::os::unix::io::{FromRawFd, RawFd};

use memmap::{Mmap, Protection};

use ffi;
use ffi::XKBCOMMON_HANDLE as XKBH;

struct KbState {
    xkb_context: *mut ffi::xkb_context,
    xkb_keymap: *mut ffi::xkb_keymap,
    xkb_state: *mut ffi::xkb_state
}

unsafe impl Send for KbState { }

impl KbState {
    fn update_modifiers(&mut self, mods_depressed: u32, mods_latched: u32, mods_locked: u32, group: u32) {
        unsafe {
            (XKBH.xkb_state_update_mask)(
                self.xkb_state, mods_depressed, mods_latched, mods_locked, 0, 0, group);
        }
    }

    pub fn get_one_sym(&self, keycode: u32) -> u32 {
        unsafe {
            (XKBH.xkb_state_key_get_one_sym)(
                self.xkb_state, keycode + 8)
        }
    }

    pub fn get_utf8(&self, keycode: u32) -> Option<String> {
        let size = unsafe {
            (XKBH.xkb_state_key_get_utf8)(self.xkb_state, keycode + 8, ptr::null_mut(), 0)
        } + 1;
        if size <= 1 { return None };
        let mut buffer = Vec::with_capacity(size as usize);
        unsafe {
            buffer.set_len(size as usize);
            (XKBH.xkb_state_key_get_utf8)(
                self.xkb_state, keycode + 8, buffer.as_mut_ptr() as *mut _, size as usize);
        };
        // remove the final `\0`
        buffer.pop();
        // libxkbcommon will always provide valid UTF8
        Some(unsafe { String::from_utf8_unchecked(buffer) } )
    }
}

impl Drop for KbState {
    fn drop(&mut self) {
        unsafe {
            (XKBH.xkb_state_unref)(self.xkb_state);
            (XKBH.xkb_keymap_unref)(self.xkb_keymap);
            (XKBH.xkb_context_unref)(self.xkb_context);
        }
    }
}

/// A wayland keyboard mapped to its keymap
///
/// It wraps an event iterator on this keyboard, catching the Keymap, Key, and Modifiers
/// events of the keyboard to handle them using libxkbcommon. All other events are directly
/// forwarded.
pub struct MappedKeyboard<H: Handler> {
    state: KbState,
    handler: H
}

pub enum MappedKeyboardError {
    XKBNotFound,
    NoKeyboardOnSeat
}

impl<H: Handler> MappedKeyboard<H> {
    pub fn new(handler: H) -> Result<MappedKeyboard<H>, MappedKeyboardError> {
        let xkbh = match ffi::XKBCOMMON_OPTION.as_ref() {
            Some(h) => h,
            None => return Err(MappedKeyboardError::XKBNotFound)
        };
        let xkb_context = unsafe {
            (xkbh.xkb_context_new)(ffi::xkb_context_flags::XKB_CONTEXT_NO_FLAGS)
        };
        if xkb_context.is_null() { return Err(MappedKeyboardError::XKBNotFound) }

        Ok(MappedKeyboard {
            state: KbState {
                xkb_context: xkb_context,
                xkb_keymap: ptr::null_mut(),
                xkb_state: ptr::null_mut()
            },
            handler: handler
        })
    }

    fn init(&mut self, fd: RawFd, size: usize) {
        let mut state = &mut self.state;

        let map = unsafe {
            Mmap::open_with_offset(&File::from_raw_fd(fd), Protection::Read, 0, size).unwrap()
        };

        let xkb_keymap = {
            unsafe {
                (XKBH.xkb_keymap_new_from_string)(
                    state.xkb_context,
                    map.ptr() as *const _,
                    ffi::xkb_keymap_format::XKB_KEYMAP_FORMAT_TEXT_V1,
                    ffi::xkb_keymap_compile_flags::XKB_KEYMAP_COMPILE_NO_FLAGS
                )
            }
        };

        if xkb_keymap.is_null() {
            panic!("Failed to load keymap!");
        }

        let xkb_state = unsafe {
            (XKBH.xkb_state_new)(xkb_keymap)
        };

        state.xkb_keymap = xkb_keymap;
        state.xkb_state = xkb_state;
    }

    pub fn handler(&mut self) -> &mut H {
        &mut self.handler
    }
}

#[allow(unused_variables)]
pub trait Handler {
    fn enter(&mut self, evqh: &mut EventQueueHandle, proxy: &WlKeyboard, serial: u32, surface: &WlSurface, rawkeys: &[u32], keysyms: &[u32]) {
    }

    fn leave(&mut self, evqh: &mut EventQueueHandle, proxy: &WlKeyboard, serial: u32, surface: &WlSurface) {
    }

    fn key(&mut self, evqh: &mut EventQueueHandle, proxy: &WlKeyboard, serial: u32, time: u32, rawkey: u32, keysym: u32, state: KeyState, utf8: Option<String>) {
    }

    fn repeat_info(&mut self, evqh: &mut EventQueueHandle, proxy: &WlKeyboard, rate: i32, delay: i32) {
    }
}

impl<H: Handler> wl_keyboard::Handler for MappedKeyboard<H> {
    fn keymap(&mut self, _: &mut EventQueueHandle, _: &WlKeyboard, _: KeymapFormat, fd: RawFd, size: u32) {
        self.init(fd, size as usize)
    }

    fn enter(&mut self, evqh: &mut EventQueueHandle, proxy: &WlKeyboard, serial: u32, surface: &WlSurface, keys: Vec<u8>) {
        let rawkeys: &[u32] = unsafe { ::std::slice::from_raw_parts(keys.as_ptr() as *const u32, keys.len()/4) };
        let keys: Vec<u32> = rawkeys.iter().map(|k| self.state.get_one_sym(*k)).collect();
        self.handler.enter(evqh, proxy, serial, surface, rawkeys, &keys)
    }

    fn leave(&mut self, evqh: &mut EventQueueHandle, proxy: &WlKeyboard, serial: u32, surface: &WlSurface) {
        self.handler.leave(evqh, proxy, serial, surface)
    }

    fn key(&mut self, evqh: &mut EventQueueHandle, proxy: &WlKeyboard, serial: u32, time: u32, key: u32, state: KeyState) {
        let sym = self.state.get_one_sym(key);
        let utf8 = self.state.get_utf8(key);
        self.handler.key(evqh, proxy, serial, time, key, sym, state, utf8)
    }

    fn modifiers(&mut self, _: &mut EventQueueHandle, _: &WlKeyboard, _: u32, mods_depressed: u32, mods_latched: u32, mods_locked: u32, group: u32) {
        self.state.update_modifiers(mods_depressed, mods_latched, mods_locked, group)
    }

    fn repeat_info(&mut self, evqh: &mut EventQueueHandle, proxy: &WlKeyboard, rate: i32, delay: i32) {
        self.handler.repeat_info(evqh, proxy, rate, delay)
    }
}

unsafe impl<H: Handler> ::wayland_client::Handler<WlKeyboard> for MappedKeyboard<H> {
    unsafe fn message(&mut self, evq: &mut EventQueueHandle, proxy: &WlKeyboard, opcode: u32, args: *const ::wayland_client::sys::wl_argument) -> Result<(),()> {
        <MappedKeyboard<H> as ::wayland_client::protocol::wl_keyboard::Handler>::__message(self, evq, proxy, opcode, args)
    }
}
<|endoftext|>