display-layers.c (2885B)
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 "layer.h" 23#include "log.h" 24 25#include <guacamole/client.h> 26 27#include <stdlib.h> 28 29guacenc_layer* guacenc_display_get_layer(guacenc_display* display, 30 int index) { 31 32 /* Do not lookup / allocate if index is invalid */ 33 if (index < 0 || index >= GUACENC_DISPLAY_MAX_LAYERS) { 34 guacenc_log(GUAC_LOG_WARNING, "Layer index out of bounds: %i", index); 35 return NULL; 36 } 37 38 /* Lookup layer, allocating a new layer if necessary */ 39 guacenc_layer* layer = display->layers[index]; 40 if (layer == NULL) { 41 42 /* Attempt to allocate layer */ 43 layer = guacenc_layer_alloc(); 44 if (layer == NULL) { 45 guacenc_log(GUAC_LOG_WARNING, "Layer allocation failed"); 46 return NULL; 47 } 48 49 /* The default layer has no parent */ 50 if (index == 0) 51 layer->parent_index = GUACENC_LAYER_NO_PARENT; 52 53 /* Store layer within display for future retrieval / management */ 54 display->layers[index] = layer; 55 56 } 57 58 return layer; 59 60} 61 62int guacenc_display_get_depth(guacenc_display* display, guacenc_layer* layer) { 63 64 /* Non-existent layers have a depth of 0 */ 65 if (layer == NULL) 66 return 0; 67 68 /* Layers with no parent have a depth of 0 */ 69 if (layer->parent_index == GUACENC_LAYER_NO_PARENT) 70 return 0; 71 72 /* Retrieve parent layer */ 73 guacenc_layer* parent = 74 guacenc_display_get_layer(display, layer->parent_index); 75 76 /* Current layer depth is the depth of the parent + 1 */ 77 return guacenc_display_get_depth(display, parent) + 1; 78 79} 80 81int guacenc_display_free_layer(guacenc_display* display, 82 int index) { 83 84 /* Do not lookup / free if index is invalid */ 85 if (index < 0 || index >= GUACENC_DISPLAY_MAX_LAYERS) { 86 guacenc_log(GUAC_LOG_WARNING, "Layer index out of bounds: %i", index); 87 return 1; 88 } 89 90 /* Free layer (if allocated) */ 91 guacenc_layer_free(display->layers[index]); 92 93 /* Mark layer as freed */ 94 display->layers[index] = NULL; 95 96 return 0; 97 98} 99