libxputty  0.1
A damn tiny abstraction Layer to create X11 window/widgets with cairo surfaces
xchildlist.c
Go to the documentation of this file.
1 /*
2  * 0BSD
3  *
4  * BSD Zero Clause License
5  *
6  * Copyright (c) 2019 Hermann Meyer
7  *
8  * Permission to use, copy, modify, and/or distribute this software for any
9  * purpose with or without fee is hereby granted.
10 
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  *
19  */
20 
21 #include "xchildlist.h"
22 #include "xchildlist_private.h"
23 
24 
25 void childlist_init(Childlist_t *childlist) {
26  childlist->childs = (Widget_t**)malloc(sizeof(Widget_t*) * 4);
27  assert(childlist->childs != NULL);
28  memset(childlist->childs, 0, 4 * sizeof(Widget_t*));
29  childlist->cap =4;
30  childlist->size = sizeof(childlist);
31  childlist->elem = 0;
32  int i = 0;
33  for(;i<childlist->cap;i++) {
34  childlist->childs[i] = NULL;
35  }
36 }
37 
38 void childlist_destroy(Childlist_t *childlist) {
39  if(childlist) free(childlist->childs);
40 }
41 
42 void childlist_add_child(Childlist_t *childlist, Widget_t *child) {
43  if(!childlist) childlist_init(childlist);
44  if (childlist->cap < childlist->elem+2) {
45  _childlist_add_elem(childlist);
46  }
47  childlist->childs[childlist->elem] = child;
48  debug_print("Childlist_t add_child\n");
49  if (child->flags & IS_WINDOW) {
50  Atom WM_DELETE_WINDOW;
51  WM_DELETE_WINDOW = XInternAtom(child->app->dpy, "WM_DELETE_WINDOW", True);
52  XSetWMProtocols(child->app->dpy, child->widget, &WM_DELETE_WINDOW, 1);
53  }
54  childlist->elem +=1;
55 }
56 
57 void childlist_remove_child(Childlist_t *childlist, Widget_t *child) {
58  if (!childlist) return;
59  int it = childlist_find_child(childlist, child);
60  if(it >= 0){
61  childlist->childs[it] = NULL;
62  childlist->elem--;
63  int i = it;
64  for(;i<childlist->elem;i++) {
65  childlist->childs[i] = childlist->childs[i+1];
66  }
67  childlist->childs[childlist->elem+1] = NULL;
68  }
69 }
70 
71 int childlist_find_child(Childlist_t *childlist, Widget_t *child) {
72  int i = 0;
73  for(;i<childlist->elem;i++) {
74  if(childlist->childs[i] == child) {
75  return i;
76  }
77  }
78  return -1;
79 }
80 
81 int childlist_find_widget(Childlist_t *childlist, Window child_window) {
82  int i = childlist->elem-1;
83  for(;i>-1;i--) {
84  if(childlist->childs[i]->widget == child_window) {
85  return i;
86  }
87  }
88  return -1;
89 }
90 
92  return childlist->elem;
93 }
childlist_add_child
void childlist_add_child(Childlist_t *childlist, Widget_t *child)
childlist_add_child - internal use to add a child to the Childlist_t You usually didn't need to cal...
Definition: xchildlist.c:42
_childlist_add_elem
void _childlist_add_elem(Childlist_t *childlist)
_childlist_add_elem - internal use to reallocate the childlist array to new size You didn't need to...
Definition: xchildlist_private.c:23
Childlist_t::childs
Widget_t ** childs
Definition: xchildlist.h:51
IS_WINDOW
@ IS_WINDOW
Definition: xwidget.h:235
xchildlist_private.h
Widget_t::flags
long long flags
Definition: xwidget.h:324
childlist_init
void childlist_init(Childlist_t *childlist)
childlist_init - internal use to allocate the array to min size You usually didn't need to call thi...
Definition: xchildlist.c:25
Childlist_t::cap
int cap
Definition: xchildlist.h:55
childlist_remove_child
void childlist_remove_child(Childlist_t *childlist, Widget_t *child)
childlist_remove_child - internal use to remove a child from the childlist You usually didn't need ...
Definition: xchildlist.c:57
Widget_t::app
Xputty * app
Definition: xwidget.h:300
Xputty::dpy
Display * dpy
Definition: xputty.h:181
Widget_t::widget
Window widget
Definition: xwidget.h:302
debug_print
#define debug_print(...)
debug_print - print out state messages when compiled with the -DDEBUG flag
Definition: xputty.h:64
childlist_find_widget
int childlist_find_widget(Childlist_t *childlist, Window child_window)
childlist_find_widget - find a child Widget_t in a the childlist by given the Window id
Definition: xchildlist.c:81
Widget_t
Widget_t - struct to hold the basic Widget_t info.
Definition: xwidget.h:298
xchildlist.h
Childlist_t
Childlist_t - struct to hold a Widget_t child list Xputty main holds a list of any Widget_t created...
Definition: xchildlist.h:49
Childlist_t::size
size_t size
Definition: xchildlist.h:53
childlist_has_child
int childlist_has_child(Childlist_t *childlist)
childlist_has_child - check if a Widget_t Childlist_t contain a child
Definition: xchildlist.c:91
childlist_destroy
void childlist_destroy(Childlist_t *childlist)
childlist_destroy - internal use to free the Childlist_t You usually didn't need to call this
Definition: xchildlist.c:38
Childlist_t::elem
int elem
Definition: xchildlist.h:57
childlist_find_child
int childlist_find_child(Childlist_t *childlist, Widget_t *child)
childlist_find_child - find a child in a the Childlist_t this could be sued to check if a Widget_t is...
Definition: xchildlist.c:71