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 #pragma once 22 23 #ifndef XPUTTY1_H_ 24 #define XPUTTY1_H_ 25 26 #include <string.h> 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <stdbool.h> 30 #include <stddef.h> 31 #include <assert.h> 32 #include <limits.h> 33 34 #include <math.h> 35 #include <cairo.h> 36 #include <cairo-xlib.h> 37 #include <X11/Xutil.h> 38 #include <X11/keysym.h> 39 #include <X11/Xatom.h> 40 41 42 #ifdef __cplusplus 43 extern "C" { 44 #endif 45 46 /*--------------------------------------------------------------------- 47 ----------------------------------------------------------------------- 48 define debug print 49 ----------------------------------------------------------------------- 50 ----------------------------------------------------------------------*/ 51 52 #ifndef DEBUG 53 #define DEBUG 0 54 #ifndef NDEBUG 55 #define NDEBUG // switch of assertion checks 56 #endif 57 #endif 58 59 /** 60 * @brief debug_print - print out state messages when compiled with 61 * the -DDEBUG flag 62 */ 63 64 #define debug_print(...) \ 65 ((void)((DEBUG) ? fprintf(stderr, __VA_ARGS__) : 0)) 66 67 /*--------------------------------------------------------------------- 68 ----------------------------------------------------------------------- 69 define min/max if not defined already 70 ----------------------------------------------------------------------- 71 ----------------------------------------------------------------------*/ 72 73 /** 74 * @brief min - set a maximal value (x) as return value 75 */ 76 77 #ifndef min 78 #define min(x, y) ((x) < (y) ? (x) : (y)) 79 #endif 80 81 /** 82 * @brief max - set a minimal value (x) as return value 83 */ 84 85 #ifndef max 86 #define max(x, y) ((x) < (y) ? (y) : (x)) 87 #endif 88 89 /*--------------------------------------------------------------------- 90 ----------------------------------------------------------------------- 91 define check if char holds UTF 8 string 92 ----------------------------------------------------------------------- 93 ----------------------------------------------------------------------*/ 94 95 /** 96 * @brief IS_UTF8 - check if a char contain UTF 8 formated signs 97 */ 98 99 #define IS_UTF8(c) (((c)&0xc0)==0xc0) 100 101 /*--------------------------------------------------------------------- 102 ----------------------------------------------------------------------- 103 forward declareted structs 104 ----------------------------------------------------------------------- 105 ----------------------------------------------------------------------*/ 106 107 /** 108 * @brief Childlist_t - maintain a Widget_t list of childs for a Widget_t 109 */ 110 111 typedef struct Childlist_t Childlist_t; 112 113 /** 114 * @brief Adjustment_t - Adjustment_t for a Widget_t 115 */ 116 117 typedef struct Adjustment_t Adjustment_t ; 118 119 /** 120 * @brief Widget_t - the Widget_t base struct 121 */ 122 123 typedef struct Widget_t Widget_t; 124 125 /** 126 * @brief XColor_t - the Widget_t Color struct 127 */ 128 129 typedef struct XColor_t XColor_t; 130 131 /** 132 * @brief Xputty - the main struct.It should be declared 133 * before any other call to a Xputty function. 134 */ 135 136 typedef struct Xputty Xputty; 137 138 #ifdef __cplusplus 139 } 140 #endif 141 142 /*--------------------------------------------------------------------- 143 ----------------------------------------------------------------------- 144 xputty library headers 145 ----------------------------------------------------------------------- 146 ----------------------------------------------------------------------*/ 147 148 // library header 149 #include "xwidget.h" 150 #include "xadjustment.h" 151 #include "xchildlist.h" 152 #include "xcolor.h" 153 #include "xpngloader.h" 154 155 156 #ifdef __cplusplus 157 extern "C" { 158 #endif 159 160 /** 161 * @brief Xputty - the main struct. 162 * \n It should be declared before any other call to a Xputty function. 163 * \n Xputty store a pointer in the childlist, 164 * to any Widget_t related to this instance of libxputty. 165 * \n The first created Widget_t is the toplevel window. 166 * \n When the toplevel window call destroy_widget(), Xputty call 167 * destroy_widget() for all remaining Widget_t's in the main childlist. 168 * \n So any allocated memory should be released before the 169 * toplevel window finaly close. 170 * @param *childlist - pointer to the main childlist 171 * @param *dpy - pointer to the display in use 172 * @param run - bool to quit the main loop 173 * @param *color_scheme - theming scheme for all Widget_t 174 * @param *hold_grab - pointer to a modal Widget_t 175 */ 176 177 struct Xputty{ 178 /** pointer to the main childlist */ 179 Childlist_t *childlist; 180 /** pointer to the display in use */ 181 Display *dpy; 182 /** theming scheme for all Widget_t */ 183 XColor_t *color_scheme; 184 /** pointer to a modal Widget_t */ 185 Widget_t *hold_grab; 186 /** bool to quit the main loop */ 187 bool run; 188 /** small fontsize for all Widget_t*/ 189 int small_font; 190 /** normal fontsize for all Widget_t*/ 191 int normal_font; 192 /** big fontsize for all Widget_t*/ 193 int big_font; 194 }; 195 196 /** 197 * @brief main_init - open the Display and init the 198 * main->childlist. 199 * \n Set the bool run to true. 200 * \n The bool run is used to terminate the main event loop. 201 * \n main_init() should be called directly after the declaration of Xputty 202 * before the first Widget_t get created. 203 * \n Any Widget_t created afterwards will be added to the main childlist. 204 * \n The main childlist is used to check if a Widget_t is valid to receive a Event. 205 * \n Xputty check if a Widget_t is registerd in the main childlist, and only forward 206 * events when it found the Widget_t in the list. 207 * \n When a Widget_t call destroy_widget() any childs of this Widget_t receive 208 * a call to destroy_widget() to release there memory, they get removed from the main childlist 209 * and finaly the Widget_t itself will be removed from the main childlist as well. 210 * On main_quit() any remaining Widget_t from the main childlist will be destroyed, 211 * to ensure that we leave the memory clean. 212 * @param *main - pointer to the main Xputty struct 213 * @return void 214 */ 215 216 void main_init(Xputty *main); 217 218 /** 219 * @brief main_run - start the main event loop. 220 * \n It should be start after your Widget_t's been created. 221 * \n You could create and destroy additional Widget_t's 222 * at any time later during run. 223 * @param *main - pointer to the main Xputty struct 224 * @return void 225 */ 226 227 void main_run(Xputty *main); 228 229 /** 230 * @brief run_embedded - the main event loop to run embedded UI's. 231 * \n It should be start after your Widget_t's been created. 232 * \n You could create and destroy additional Widget_t's 233 * at any time later during run. 234 * @param *main - pointer to the main Xputty struct 235 * @return void 236 */ 237 238 void run_embedded(Xputty *main); 239 240 /** 241 * @brief main_quit - destroy all remaining Widget_t's from the 242 * main->childlist. 243 * \n Free all resources which may be allocated between init 244 * and quit. 245 * \n It should be called after main_run()/run_embedded(); 246 * @param *main - pointer to the main Xputty struct 247 * @return void 248 */ 249 250 void main_quit(Xputty *main); 251 252 #ifdef __cplusplus 253 } 254 #endif 255 256 #endif //XPUTTY_H_ 257 258