cscg24-guacamole

CSCG 2024 Challenge 'Guacamole Mashup'
git clone https://git.sinitax.com/sinitax/cscg24-guacamole
Log | Files | Refs | sfeed.txt

display-buffers.c (3125B)


      1/*
      2 * Licensed to the Apache Software Foundation (ASF) under one
      3 * or more contributor license agreements.  See the NOTICE file
      4 * distributed with this work for additional information
      5 * regarding copyright ownership.  The ASF licenses this file
      6 * to you under the Apache License, Version 2.0 (the
      7 * "License"); you may not use this file except in compliance
      8 * with the License.  You may obtain a copy of the License at
      9 *
     10 *   http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing,
     13 * software distributed under the License is distributed on an
     14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     15 * KIND, either express or implied.  See the License for the
     16 * specific language governing permissions and limitations
     17 * under the License.
     18 */
     19
     20#include "config.h"
     21#include "display.h"
     22#include "buffer.h"
     23#include "layer.h"
     24#include "log.h"
     25
     26#include <guacamole/client.h>
     27
     28#include <stdlib.h>
     29
     30guacenc_buffer* guacenc_display_get_buffer(guacenc_display* display,
     31        int index) {
     32
     33    /* Transform index to buffer space */
     34    int internal_index = -index - 1;
     35
     36    /* Do not lookup / allocate if index is invalid */
     37    if (internal_index < 0 || internal_index >= GUACENC_DISPLAY_MAX_BUFFERS) {
     38        guacenc_log(GUAC_LOG_WARNING, "Buffer index out of bounds: %i", index);
     39        return NULL;
     40    }
     41
     42    /* Lookup buffer, allocating a new buffer if necessary */
     43    guacenc_buffer* buffer = display->buffers[internal_index];
     44    if (buffer == NULL) {
     45
     46        /* Attempt to allocate buffer */
     47        buffer = guacenc_buffer_alloc();
     48        if (buffer == NULL) {
     49            guacenc_log(GUAC_LOG_WARNING, "Buffer allocation failed");
     50            return NULL;
     51        }
     52
     53        /* All non-layer buffers must autosize */
     54        buffer->autosize = true;
     55
     56        /* Store buffer within display for future retrieval / management */
     57        display->buffers[internal_index] = buffer;
     58
     59    }
     60
     61    return buffer;
     62
     63}
     64
     65int guacenc_display_free_buffer(guacenc_display* display,
     66        int index) {
     67
     68    /* Transform index to buffer space */
     69    int internal_index = -index - 1;
     70
     71    /* Do not lookup / free if index is invalid */
     72    if (internal_index < 0 || internal_index >= GUACENC_DISPLAY_MAX_BUFFERS) {
     73        guacenc_log(GUAC_LOG_WARNING, "Buffer index out of bounds: %i", index);
     74        return 1;
     75    }
     76
     77    /* Free buffer (if allocated) */
     78    guacenc_buffer_free(display->buffers[internal_index]);
     79
     80    /* Mark buffer as freed */
     81    display->buffers[internal_index] = NULL;
     82
     83    return 0;
     84
     85}
     86
     87guacenc_buffer* guacenc_display_get_related_buffer(guacenc_display* display,
     88        int index) {
     89
     90    /* Retrieve underlying buffer of layer if a layer is requested */
     91    if (index >= 0) {
     92
     93        /* Retrieve / allocate layer (if possible */
     94        guacenc_layer* layer = guacenc_display_get_layer(display, index);
     95        if (layer == NULL)
     96            return NULL;
     97
     98        /* Return underlying buffer */
     99        return layer->buffer;
    100
    101    }
    102
    103    /* Otherwise retrieve buffer directly */
    104    return guacenc_display_get_buffer(display, index);
    105
    106}
    107