Skip to content
Snippets Groups Projects
Commit 0270d786 authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

Use a single return type for API functions.

parent 873421e6
No related branches found
No related tags found
No related merge requests found
Pipeline #5439 passed
CFLAGS += -I../xpdev -I../cutest -pthread CFLAGS += -I../xpdev -I../cutest -pthread
CFLAGS += -DHAS_STDINT_H CFLAGS += -DHAS_STDINT_H
CFLAGS += -DHAS_INTTYPES_H CFLAGS += -DHAS_INTTYPES_H
CFLAGS += -g -std=c11 -Wall -Wextra -Wpedantic
LIBS += -lpthread LIBS += -lpthread
VPATH = ../cutest:../xpdev VPATH = ../cutest:../xpdev
...@@ -9,8 +10,8 @@ ifdef TESTS ...@@ -9,8 +10,8 @@ ifdef TESTS
CFLAGS += -DBUILD_TESTS CFLAGS += -DBUILD_TESTS
all: runtest all: runtest
test: alltests.o CuTest.o root_window.o threadwrap.o test: alltests.o CuTest.o threadwrap.o libnewifc.a
cc ${LDFLAGS} ${CFLAGS} $^ -o $@ ${LIBS} cc ${LDFLAGS} -L. ${CFLAGS} $^ -o $@ ${LIBS} -lnewifc
runtest: test runtest: test
./test ./test
...@@ -29,7 +30,6 @@ newifc.o: newifc.c internal_macros.h root_window.c ...@@ -29,7 +30,6 @@ newifc.o: newifc.c internal_macros.h root_window.c
libnewifc.a: libnewifc.a(newifc.o) libnewifc.a: libnewifc.a(newifc.o)
ranlib $@ ranlib $@
strip --strip-unneeded $@
clean: clean:
rm -f *.o genapi test newifc_internal.h new_ifc.h new_ifc.c libnewifc.a newifc.c newifc.h rm -f *.o genapi test newifc_internal.h new_ifc.h new_ifc.c libnewifc.a newifc.c newifc.h
...@@ -33,18 +33,26 @@ struct attribute_info { ...@@ -33,18 +33,26 @@ struct attribute_info {
const struct attribute_info const struct attribute_info
attributes[] = { attributes[] = {
{"bottomchild", "NewIfcObj", attr_impl_global, 1},
{"child_height", "uint16_t", attr_impl_global, 1}, {"child_height", "uint16_t", attr_impl_global, 1},
{"child_width", "uint16_t", attr_impl_global, 1}, {"child_width", "uint16_t", attr_impl_global, 1},
{"child_xpos", "uint16_t", attr_impl_global, 1}, {"child_xpos", "uint16_t", attr_impl_global, 1},
{"child_ypos", "uint16_t", attr_impl_global, 1}, {"child_ypos", "uint16_t", attr_impl_global, 1},
{"dirty", "bool", attr_impl_root, 1}, {"dirty", "bool", attr_impl_root, 1},
{"height", "uint16_t", attr_impl_global, 0}, {"height", "uint16_t", attr_impl_global, 0},
{"higherpeer", "NewIfcObj", attr_impl_global, 1},
{"last_error", "NI_err", attr_impl_global, 1},
{"locked", "bool", attr_impl_root, 0}, {"locked", "bool", attr_impl_root, 0},
{"locked_by_me", "bool", attr_impl_root, 1}, {"locked_by_me", "bool", attr_impl_root, 1},
{"lowerpeer", "NewIfcObj", attr_impl_global, 1},
{"parent", "NewIfcObj", attr_impl_global, 1},
{"root", "NewIfcObj", attr_impl_global, 1},
{"show_help", "bool", attr_impl_object, 0}, {"show_help", "bool", attr_impl_object, 0},
{"show_title", "bool", attr_impl_object, 0}, {"show_title", "bool", attr_impl_object, 0},
{"title", "const char *", attr_impl_object, 0}, {"title", "const char *", attr_impl_object, 0},
{"topchild", "NewIfcObj", attr_impl_global, 1},
{"transparent", "bool", attr_impl_object, 0}, {"transparent", "bool", attr_impl_object, 0},
{"type", "enum NewIfc_object", attr_impl_global, 1},
{"width", "uint16_t", attr_impl_global, 0}, {"width", "uint16_t", attr_impl_global, 0},
{"xpos", "uint16_t", attr_impl_global, 0}, {"xpos", "uint16_t", attr_impl_global, 0},
{"ypos", "uint16_t", attr_impl_global, 0}, {"ypos", "uint16_t", attr_impl_global, 0},
...@@ -52,17 +60,19 @@ attributes[] = { ...@@ -52,17 +60,19 @@ attributes[] = {
struct error_info { struct error_info {
const char *name; const char *name;
int value;
}; };
const struct error_info const struct error_info
error_inf[] = { error_inf[] = {
{"error_none"}, {"error_none", 0},
{"error_allocation_failure"}, {"error_allocation_failure", -1},
{"error_out_of_range"}, {"error_invalid_arg", -1},
{"error_not_implemented"}, {"error_lock_failed", -1},
{"error_lock_failed"}, {"error_needs_parent", -1},
{"error_needs_parent"}, {"error_not_implemented", -1},
{"error_wont_fit"}, {"error_out_of_range", -1},
{"error_wont_fit", -1},
}; };
int int
...@@ -74,7 +84,7 @@ main(int argc, char **argv) ...@@ -74,7 +84,7 @@ main(int argc, char **argv)
size_t nitems; size_t nitems;
size_t i; size_t i;
header = fopen("newifc.h", "wb"); header = fopen("newifc.h", "w");
if (header == NULL) { if (header == NULL) {
perror("Opening header"); perror("Opening header");
return EXIT_FAILURE; return EXIT_FAILURE;
...@@ -91,36 +101,39 @@ main(int argc, char **argv) ...@@ -91,36 +101,39 @@ main(int argc, char **argv)
fputs("typedef struct newifc_api *NewIfcObj;\n\n", header); fputs("typedef struct newifc_api *NewIfcObj;\n\n", header);
fputs("enum NewIfc_error {\n", header); fputs("typedef enum NewIfc_error {\n", header);
nitems = sizeof(error_inf) / sizeof(error_inf[0]); nitems = sizeof(error_inf) / sizeof(error_inf[0]);
for (i = 0; i < nitems; i++) { for (i = 0; i < nitems; i++) {
fprintf(header, "\tNewIfc_%s,\n", error_inf[i].name); if (error_inf[i].value >= 0)
fprintf(header, " NewIfc_%s = %d,\n", error_inf[i].name, error_inf[i].value);
else
fprintf(header, " NewIfc_%s,\n", error_inf[i].name);
} }
fputs("};\n\n", header); fputs("} NI_err;\n\n", header);
fputs("enum NewIfc_object {\n", header); fputs("enum NewIfc_object {\n", header);
nitems = sizeof(objtypes) / sizeof(objtypes[0]); nitems = sizeof(objtypes) / sizeof(objtypes[0]);
for (i = 0; i < nitems; i++) { for (i = 0; i < nitems; i++) {
fprintf(header, "\tNewIfc_%s,\n", objtypes[i].name); fprintf(header, " NewIfc_%s,\n", objtypes[i].name);
} }
fputs("};\n\n", header); fputs("};\n\n", header);
fputs("NewIfcObj NI_copy(NewIfcObj obj);\n", header); fputs("NI_err NI_copy(NewIfcObj obj, NewIfcObj *newobj);\n", header);
fputs("NewIfcObj NI_create(enum NewIfc_object obj, NewIfcObj parent);\n", header); fputs("NI_err NI_create(enum NewIfc_object obj, NewIfcObj parent, NewIfcObj *newobj);\n", header);
fputs("enum NewIfc_error NI_error(NewIfcObj obj);\n", header); fputs("NI_err NI_error(NewIfcObj obj);\n", header);
fputs("bool NI_walk_children(NewIfcObj obj, bool (*cb)(NewIfcObj obj, void *cb_data), void *cbdata);\n\n", header); fputs("NI_err NI_walk_children(NewIfcObj obj, NI_err (*cb)(NewIfcObj obj, void *cb_data), void *cbdata);\n\n", header);
nitems = sizeof(attributes) / sizeof(attributes[0]); nitems = sizeof(attributes) / sizeof(attributes[0]);
for (i = 0; i < nitems; i++) { for (i = 0; i < nitems; i++) {
if (!attributes[i].read_only) if (!attributes[i].read_only)
fprintf(header, "bool NI_set_%s(NewIfcObj obj, %s value);\n", attributes[i].name, attributes[i].type); fprintf(header, "NI_err NI_set_%s(NewIfcObj obj, %s value);\n", attributes[i].name, attributes[i].type);
fprintf(header, "bool NI_get_%s(NewIfcObj obj, %s* value);\n", attributes[i].name, attributes[i].type); fprintf(header, "NI_err NI_get_%s(NewIfcObj obj, %s* value);\n", attributes[i].name, attributes[i].type);
} }
fputs("\n#endif\n", header); fputs("\n#endif\n", header);
fclose(header); fclose(header);
internal_header = fopen("newifc_internal.h", "wb"); internal_header = fopen("newifc_internal.h", "w");
if (internal_header == NULL) { if (internal_header == NULL) {
perror("Opening internal header"); perror("Opening internal header");
return EXIT_FAILURE; return EXIT_FAILURE;
...@@ -133,31 +146,31 @@ main(int argc, char **argv) ...@@ -133,31 +146,31 @@ main(int argc, char **argv)
"#define NEWIFC_INTERNAL_H\n" "#define NEWIFC_INTERNAL_H\n"
"\n" "\n"
"struct newifc_api {\n" "struct newifc_api {\n"
"\tbool (*set)(NewIfcObj niobj, const int attr, ...);\n" " NI_err (*set)(NewIfcObj niobj, const int attr, ...);\n"
"\tbool (*get)(NewIfcObj niobj, const int attr, ...);\n" " NI_err (*get)(NewIfcObj niobj, const int attr, ...);\n"
"\tNewIfcObj (*copy)(NewIfcObj obj);\n" " NI_err (*copy)(NewIfcObj obj, NewIfcObj *newobj);\n"
"\tNewIfcObj root;\n" " NewIfcObj root;\n"
"\tNewIfcObj parent;\n" " NewIfcObj parent;\n"
"\tNewIfcObj higherpeer;\n" " NewIfcObj higherpeer;\n"
"\tNewIfcObj lowerpeer;\n" " NewIfcObj lowerpeer;\n"
"\tNewIfcObj topchild;\n" " NewIfcObj topchild;\n"
"\tNewIfcObj bottomchild;\n" " NewIfcObj bottomchild;\n"
"\tenum NewIfc_object type;\n" " enum NewIfc_object type;\n"
"\tenum NewIfc_error last_error;\n" " NI_err last_error;\n"
"\tuint16_t height;\n" " uint16_t height;\n"
"\tuint16_t width;\n" " uint16_t width;\n"
"\tuint16_t xpos;\n" " uint16_t xpos;\n"
"\tuint16_t ypos;\n" " uint16_t ypos;\n"
"\tuint16_t child_height;\n" " uint16_t child_height;\n"
"\tuint16_t child_width;\n" " uint16_t child_width;\n"
"\tuint16_t child_xpos;\n" " uint16_t child_xpos;\n"
"\tuint16_t child_ypos;\n" " uint16_t child_ypos;\n"
"};\n\n", internal_header); "};\n\n", internal_header);
fputs("enum NewIfc_attribute {\n", internal_header); fputs("enum NewIfc_attribute {\n", internal_header);
nitems = sizeof(attributes) / sizeof(attributes[0]); nitems = sizeof(attributes) / sizeof(attributes[0]);
for (i = 0; i < nitems; i++) { for (i = 0; i < nitems; i++) {
fprintf(internal_header, "\tNewIfc_%s,\n", attributes[i].name); fprintf(internal_header, " NewIfc_%s,\n", attributes[i].name);
} }
fputs("};\n\n", internal_header); fputs("};\n\n", internal_header);
...@@ -165,7 +178,7 @@ main(int argc, char **argv) ...@@ -165,7 +178,7 @@ main(int argc, char **argv)
fclose(internal_header); fclose(internal_header);
c_code = fopen("newifc.c", "wb"); c_code = fopen("newifc.c", "w");
if (c_code == NULL) { if (c_code == NULL) {
perror("Opening c source"); perror("Opening c source");
return EXIT_FAILURE; return EXIT_FAILURE;
...@@ -182,52 +195,57 @@ main(int argc, char **argv) ...@@ -182,52 +195,57 @@ main(int argc, char **argv)
} }
fputs("\n", c_code); fputs("\n", c_code);
fputs("NewIfcObj\n" fputs("NI_err\n"
"NI_create(enum NewIfc_object obj, NewIfcObj parent) {\n" "NI_create(enum NewIfc_object obj, NewIfcObj parent, NewIfcObj *newobj) {\n"
"\tNewIfcObj ret = NULL;\n" " NI_err ret = NewIfc_error_none;\n"
"\n" "\n"
"\tswitch(obj) {\n", c_code); " if (newobj == NULL)\n"
" return NewIfc_error_invalid_arg;\n"
" *newobj = NULL;\n"
" switch(obj) {\n", c_code);
nitems = sizeof(objtypes) / sizeof(objtypes[0]); nitems = sizeof(objtypes) / sizeof(objtypes[0]);
for (i = 0; i < nitems; i++) { for (i = 0; i < nitems; i++) {
fprintf(c_code, "\t\tcase NewIfc_%s:\n", objtypes[i].name); fprintf(c_code, " case NewIfc_%s:\n", objtypes[i].name);
if (objtypes[i].needs_parent) { if (objtypes[i].needs_parent) {
fputs("\t\t\tif (parent == NULL) {\n" fputs(" if (parent == NULL) {\n"
"\t\t\t\tbreak;\n" " ret = NewIfc_error_invalid_arg;\n"
"\t\t\t}\n", c_code); " break;\n"
" }\n", c_code);
} }
else { else {
fputs("\t\t\tif (parent != NULL) {\n" fputs(" if (parent != NULL) {\n"
"\t\t\t\tbreak;\n" " ret = NewIfc_error_invalid_arg;\n"
"\t\t\t}\n", c_code); " break;\n"
" }\n", c_code);
} }
fprintf(c_code, "\t\t\tret = NewIFC_%s();\n" fprintf(c_code, " ret = NewIFC_%s(newobj);\n"
"\t\t\tif (ret == NULL) {\n" " if (ret != NewIfc_error_none) {\n"
"\t\t\t\tbreak;\n" " break;\n"
"\t\t\t}\n" " }\n"
"\t\t\tret->type = obj;\n" " (*newobj)->type = obj;\n"
"\t\t\tbreak;\n", objtypes[i].name); " break;\n", objtypes[i].name);
} }
fputs("\t}\n" fputs(" }\n"
"\tif (ret) {\n" " if (ret == NewIfc_error_none) {\n"
"\t\tret->parent = parent;\n" " (*newobj)->parent = parent;\n"
"\t\tret->higherpeer = NULL;\n" " (*newobj)->higherpeer = NULL;\n"
"\t\tret->topchild = NULL;\n" " (*newobj)->topchild = NULL;\n"
"\t\tret->bottomchild = NULL;\n" " (*newobj)->bottomchild = NULL;\n"
"\t\tif (parent) {\n" " if (parent) {\n"
"\t\t\tret->root = parent->root;\n" " (*newobj)->root = parent->root;\n"
"\t\t\tret->lowerpeer = parent->topchild;\n" " (*newobj)->lowerpeer = parent->topchild;\n"
"\t\t\tparent->topchild = ret;\n" " parent->topchild = *newobj;\n"
"\t\t\tif (parent->bottomchild == NULL) {\n" " if (parent->bottomchild == NULL) {\n"
"\t\t\t\tparent->bottomchild = ret;\n" " parent->bottomchild = *newobj;\n"
"\t\t\t}\n" " }\n"
"\t\t}\n" " }\n"
"\t\telse {\n" " else {\n"
"\t\t\tret->root = ret;\n" " (*newobj)->root = *newobj;\n"
"\t\t\tret->lowerpeer = NULL;\n" " (*newobj)->lowerpeer = NULL;\n"
"\t\t}\n" " }\n"
"\t}\n" " }\n"
"\treturn ret;\n" " return ret;\n"
"};\n\n", c_code); "}\n\n", c_code);
fputs("#include \"newifc_nongen.c\"\n\n", c_code); fputs("#include \"newifc_nongen.c\"\n\n", c_code);
...@@ -236,79 +254,99 @@ main(int argc, char **argv) ...@@ -236,79 +254,99 @@ main(int argc, char **argv)
switch (attributes[i].impl) { switch (attributes[i].impl) {
case attr_impl_object: case attr_impl_object:
if (!attributes[i].read_only) { if (!attributes[i].read_only) {
fprintf(c_code, "bool\n" fprintf(c_code, "NI_err\n"
"NI_set_%s(NewIfcObj obj, %s value) {\n" "NI_set_%s(NewIfcObj obj, %s value) {\n"
"\tbool ret;\n" " NI_err ret;\n"
"\tif (NI_set_locked(obj, true)) {\n" " if (obj == NULL)\n"
"\t\tret = obj->set(obj, NewIfc_%s, value);\n" " return NewIfc_error_invalid_arg;\n"
"\t\tNI_set_locked(obj, false);\n" " if (NI_set_locked(obj, true)) {\n"
"\t}\n" " ret = obj->set(obj, NewIfc_%s, value);\n"
"\telse\n" " NI_set_locked(obj, false);\n"
"\t\tret = false;\n" " }\n"
"\treturn ret;\n" " else\n"
" ret = NewIfc_error_lock_failed;\n"
" return ret;\n"
"}\n\n", attributes[i].name, attributes[i].type, attributes[i].name); "}\n\n", attributes[i].name, attributes[i].type, attributes[i].name);
} }
fprintf(c_code, "bool\n" fprintf(c_code, "NI_err\n"
"NI_get_%s(NewIfcObj obj, %s* value) {\n" "NI_get_%s(NewIfcObj obj, %s* value) {\n"
"\tbool ret;\n" " NI_err ret;\n"
"\tif (NI_set_locked(obj, true)) {\n" " if (obj == NULL)\n"
"\t\tret = obj->get(obj, NewIfc_%s, value);\n" " return NewIfc_error_invalid_arg;\n"
"\t\tNI_set_locked(obj, false);\n" " if (value == NULL)\n"
"\t}\n" " return NewIfc_error_invalid_arg;\n"
"\telse\n" " if (NI_set_locked(obj, true)) {\n"
"\t\tret = false;\n" " ret = obj->get(obj, NewIfc_%s, value);\n"
"\treturn ret;\n" " NI_set_locked(obj, false);\n"
" }\n"
" else\n"
" ret = NewIfc_error_lock_failed;\n"
" return ret;\n"
"}\n\n", attributes[i].name, attributes[i].type, attributes[i].name); "}\n\n", attributes[i].name, attributes[i].type, attributes[i].name);
break; break;
case attr_impl_global: case attr_impl_global:
if (!attributes[i].read_only) { if (!attributes[i].read_only) {
fprintf(c_code, "bool\n" fprintf(c_code, "NI_err\n"
"NI_set_%s(NewIfcObj obj, %s value) {\n" "NI_set_%s(NewIfcObj obj, %s value) {\n"
"\tbool ret;\n" " NI_err ret;\n"
"\tif (NI_set_locked(obj, true)) {\n" " if (obj == NULL)\n"
"\t\tif ((!obj->set(obj, NewIfc_%s, value)) && obj->last_error != NewIfc_error_not_implemented) {\n" " return NewIfc_error_invalid_arg;\n"
"\t\t\tobj->%s = value;\n" " if (NI_set_locked(obj, true)) {\n"
"\t\t\tobj->last_error = NewIfc_error_none;\n" " ret = obj->set(obj, NewIfc_%s, value);\n"
"\t\t}\n" " if (ret != NewIfc_error_none && obj->last_error != NewIfc_error_not_implemented) {\n"
"\t\tNI_set_locked(obj, false);\n" " obj->%s = value;\n"
"\t}\n" " obj->last_error = NewIfc_error_none;\n"
"\telse\n" " }\n"
"\t\tret = false;\n" " NI_set_locked(obj, false);\n"
"\treturn ret;\n" " }\n"
" else\n"
" ret = NewIfc_error_lock_failed;\n"
" return ret;\n"
"}\n\n", attributes[i].name, attributes[i].type, attributes[i].name, attributes[i].name); "}\n\n", attributes[i].name, attributes[i].type, attributes[i].name, attributes[i].name);
} }
fprintf(c_code, "bool\n" fprintf(c_code, "NI_err\n"
"NI_get_%s(NewIfcObj obj, %s* value) {\n" "NI_get_%s(NewIfcObj obj, %s* value) {\n"
"\tbool ret;\n" " NI_err ret;\n"
"\tif (NI_set_locked(obj, true)) {\n" " if (obj == NULL)\n"
"\t\tif ((!obj->get(obj, NewIfc_%s, value)) && obj->last_error != NewIfc_error_not_implemented) {\n" " return NewIfc_error_invalid_arg;\n"
"\t\t\t*value = obj->%s;\n" " if (value == NULL)\n"
"\t\t\tobj->last_error = NewIfc_error_none;\n" " return NewIfc_error_invalid_arg;\n"
"\t\t}\n" " if (NI_set_locked(obj, true)) {\n"
"\t\tNI_set_locked(obj, false);\n" " ret = obj->get(obj, NewIfc_%s, value);\n"
"\t}\n" " if ((ret != NewIfc_error_none) && obj->last_error != NewIfc_error_not_implemented) {\n"
"\telse\n" " *value = obj->%s;\n"
"\t\tret = false;\n" " obj->last_error = NewIfc_error_none;\n"
"\treturn ret;\n" " }\n"
" NI_set_locked(obj, false);\n"
" }\n"
" else\n"
" ret = NewIfc_error_lock_failed;\n"
" return ret;\n"
"}\n\n", attributes[i].name, attributes[i].type, attributes[i].name, attributes[i].name); "}\n\n", attributes[i].name, attributes[i].type, attributes[i].name, attributes[i].name);
break; break;
case attr_impl_root: case attr_impl_root:
if (!attributes[i].read_only) { if (!attributes[i].read_only) {
fprintf(c_code, "bool\n" fprintf(c_code, "NI_err\n"
"NI_set_%s(NewIfcObj obj, %s value) {\n" "NI_set_%s(NewIfcObj obj, %s value) {\n"
"\tbool ret = obj->root->set(obj, NewIfc_%s, value);\n" " if (obj == NULL)\n"
"\tobj->last_error = obj->root->last_error;\n" " return NewIfc_error_invalid_arg;\n"
"\treturn ret;\n" " NI_err ret = obj->root->set(obj, NewIfc_%s, value);\n"
" obj->last_error = obj->root->last_error;\n"
" return ret;\n"
"}\n\n", attributes[i].name, attributes[i].type, attributes[i].name); "}\n\n", attributes[i].name, attributes[i].type, attributes[i].name);
} }
fprintf(c_code, "bool\n" fprintf(c_code, "NI_err\n"
"NI_get_%s(NewIfcObj obj, %s* value) {\n" "NI_get_%s(NewIfcObj obj, %s* value) {\n"
"\tbool ret = obj->root->get(obj, NewIfc_%s, value);\n" " if (obj == NULL)\n"
"\tobj->last_error = obj->root->last_error;\n" " return NewIfc_error_invalid_arg;\n"
"\treturn ret;\n" " if (value == NULL)\n"
" return NewIfc_error_invalid_arg;\n"
" NI_err ret = obj->root->get(obj, NewIfc_%s, value);\n"
" obj->last_error = obj->root->last_error;\n"
" return ret;\n"
"}\n\n", attributes[i].name, attributes[i].type, attributes[i].name); "}\n\n", attributes[i].name, attributes[i].type, attributes[i].name);
break; break;
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
#define SET_VARS \ #define SET_VARS \
va_list ap; \ va_list ap; \
char *buf; \ char *buf; \
size_t sz
#define SET_BOOL(st, field) do { \ #define SET_BOOL(st, field) do { \
st->field = va_arg(ap, int); \ st->field = va_arg(ap, int); \
...@@ -32,7 +31,6 @@ ...@@ -32,7 +31,6 @@
#define GET_VARS \ #define GET_VARS \
va_list ap; \ va_list ap; \
size_t sz; \
#define GET_BOOL(st, field) *(va_arg(ap, bool *)) = st->field #define GET_BOOL(st, field) *(va_arg(ap, bool *)) = st->field
#define GET_UINT32_T(st, field) *(va_arg(ap, uint32_t *)) = st->field; #define GET_UINT32_T(st, field) *(va_arg(ap, uint32_t *)) = st->field;
......
NewIfcObj #include "newifc.h"
NI_copy(NewIfcObj obj) {
return obj->copy(obj);
}
enum NewIfc_error NI_err
NI_error(NewIfcObj obj) NI_copy(NewIfcObj obj, NewIfcObj *newobj) {
{ if (newobj == NULL) {
return obj->last_error; return NewIfc_error_invalid_arg;
}
*newobj = NULL;
if (obj == NULL) {
return NewIfc_error_invalid_arg;
}
NI_err ret = NewIfc_error_none;
if (NI_set_locked(obj, true)) {
ret = obj->copy(obj, newobj);
if (ret == NewIfc_error_none) {
(*newobj)->root = NULL;
(*newobj)->parent = NULL;
(*newobj)->higherpeer = NULL;
(*newobj)->lowerpeer = NULL;
(*newobj)->topchild = NULL;
(*newobj)->bottomchild = NULL;
}
NI_set_locked(obj, false);
}
else
ret = NewIfc_error_lock_failed;
return ret;
} }
static bool static NI_err
NI_walk_children_recurse(NewIfcObj obj, bool (*cb)(NewIfcObj obj, void *cb_data), void *cbdata) NI_walk_children_recurse(NewIfcObj obj, NI_err (*cb)(NewIfcObj obj, void *cb_data), void *cbdata)
{ {
NI_err err;
if (!obj) if (!obj)
return true; return NewIfc_error_none;
if (!cb(obj, cbdata)) err = cb(obj, cbdata);
return false; if (err != NewIfc_error_none)
return err;
if (obj->bottomchild != NULL) { if (obj->bottomchild != NULL) {
if (!NI_walk_children_recurse(obj->bottomchild, cb, cbdata)) err = NI_walk_children_recurse(obj->bottomchild, cb, cbdata);
return false; if (err != NewIfc_error_none)
return err;
} }
if (!obj->higherpeer) if (!obj->higherpeer)
return true; return NewIfc_error_none;
return NI_walk_children_recurse(obj->higherpeer, cb, cbdata); return NI_walk_children_recurse(obj->higherpeer, cb, cbdata);
} }
bool NI_err
NI_walk_children(NewIfcObj obj, bool (*cb)(NewIfcObj obj, void *cb_data), void *cbdata) NI_walk_children(NewIfcObj obj, NI_err (*cb)(NewIfcObj obj, void *cb_data), void *cbdata)
{ {
bool ret; NI_err ret;
if (NI_set_locked(obj, true)) {
NI_walk_children_recurse(obj->bottomchild, cb, cbdata); if (obj == NULL)
return NewIfc_error_invalid_arg;
if (cb == NULL)
return NewIfc_error_invalid_arg;
ret = NI_set_locked(obj, true);
if (ret == NewIfc_error_none) {
ret = NI_walk_children_recurse(obj->bottomchild, cb, cbdata);
NI_set_locked(obj, false); NI_set_locked(obj, false);
} }
else else
ret = false; ret = NewIfc_error_lock_failed;
return ret; return ret;
} }
...@@ -34,16 +34,16 @@ struct rw_recalc_child_cb_params { ...@@ -34,16 +34,16 @@ struct rw_recalc_child_cb_params {
uint16_t width; uint16_t width;
}; };
static bool static NI_err
rw_recalc_child_cb(NewIfcObj obj, void *cbdata) rw_recalc_child_cb(NewIfcObj obj, void *cbdata)
{ {
struct rw_recalc_child_cb_params *nsz = cbdata; struct rw_recalc_child_cb_params *nsz = cbdata;
if (obj->height > nsz->height) if (obj->height > nsz->height)
return false; return NewIfc_error_wont_fit;
if (obj->width > nsz->width) if (obj->width > nsz->width)
return false; return NewIfc_error_wont_fit;
return true; return NewIfc_error_none;
} }
static void static void
...@@ -64,7 +64,7 @@ rw_recalc_child(struct root_window *rw, uint16_t height, uint16_t width) ...@@ -64,7 +64,7 @@ rw_recalc_child(struct root_window *rw, uint16_t height, uint16_t width)
rw->api.last_error = NewIfc_error_wont_fit; rw->api.last_error = NewIfc_error_wont_fit;
return; return;
} }
if (!NI_walk_children((NewIfcObj)rw, rw_recalc_child_cb, &nsz)) { if (NI_walk_children((NewIfcObj)rw, rw_recalc_child_cb, &nsz) != NewIfc_error_none) {
rw->api.last_error = NewIfc_error_wont_fit; rw->api.last_error = NewIfc_error_wont_fit;
return; return;
} }
...@@ -73,7 +73,7 @@ rw_recalc_child(struct root_window *rw, uint16_t height, uint16_t width) ...@@ -73,7 +73,7 @@ rw_recalc_child(struct root_window *rw, uint16_t height, uint16_t width)
return; return;
} }
static bool static NI_err
rw_set(NewIfcObj obj, int attr, ...) rw_set(NewIfcObj obj, int attr, ...)
{ {
struct root_window *rw = (struct root_window *)obj; struct root_window *rw = (struct root_window *)obj;
...@@ -142,10 +142,10 @@ rw_set(NewIfcObj obj, int attr, ...) ...@@ -142,10 +142,10 @@ rw_set(NewIfcObj obj, int attr, ...)
} }
va_end(ap); va_end(ap);
return rw->api.last_error == NewIfc_error_none; return rw->api.last_error;
} }
static bool static NI_err
rw_get(NewIfcObj obj, int attr, ...) rw_get(NewIfcObj obj, int attr, ...)
{ {
struct root_window *rw = (struct root_window *)obj; struct root_window *rw = (struct root_window *)obj;
...@@ -200,54 +200,66 @@ rw_get(NewIfcObj obj, int attr, ...) ...@@ -200,54 +200,66 @@ rw_get(NewIfcObj obj, int attr, ...)
break; break;
} }
return rw->api.last_error == NewIfc_error_none; return rw->api.last_error;
} }
static NewIfcObj static NI_err
rw_copy(NewIfcObj old) rw_copy(NewIfcObj old, NewIfcObj *newobj)
{ {
struct root_window *ret; struct root_window **newrw = (struct root_window **)newobj;
struct root_window *oldrw = (struct root_window *)old;
ret = malloc(sizeof(struct root_window)); *newrw = malloc(sizeof(struct root_window));
memcpy(ret, old, sizeof(struct root_window)); if (*newrw == NULL) {
ret->title = strdup(ret->title); return NewIfc_error_allocation_failure;
}
memcpy(*newrw, old, sizeof(struct root_window));
(*newrw)->title = strdup(oldrw->title);
if ((*newrw)->title == NULL) {
free(*newrw);
return NewIfc_error_allocation_failure;
}
return (struct newifc_api *)ret; return NewIfc_error_none;
} }
NewIfcObj static NI_err
NewIFC_root_window(void) NewIFC_root_window(NewIfcObj *newobj)
{ {
struct root_window *ret; struct root_window **newrw = (struct root_window **)newobj;
ret = malloc(sizeof(struct root_window)); *newrw = malloc(sizeof(struct root_window));
if (ret) { if (*newrw == NULL) {
ret->api.get = rw_get; return NewIfc_error_allocation_failure;
ret->api.set = rw_set;
ret->api.copy = rw_copy;
ret->api.last_error = NewIfc_error_none;
ret->api.width = 80;
ret->api.height = 25;
ret->api.xpos = 0;
ret->api.ypos = 0;
ret->api.child_xpos = 0;
ret->api.child_ypos = 1;
ret->api.child_width = 80;
ret->api.child_height = 23;
ret->transparent = false;
ret->show_title = true;
ret->help = true;
ret->title_sz = 0;
ret->title = strdup(default_title);
ret->locks = 0;
if (ret->title == NULL) {
free(ret);
return NULL;
} }
ret->mtx = pthread_mutex_initializer_np(true); (*newrw)->api.get = rw_get;
(*newrw)->api.set = rw_set;
(*newrw)->api.copy = rw_copy;
(*newrw)->api.last_error = NewIfc_error_none;
(*newrw)->api.width = 80;
(*newrw)->api.height = 25;
(*newrw)->api.xpos = 0;
(*newrw)->api.ypos = 0;
(*newrw)->api.child_xpos = 0;
(*newrw)->api.child_ypos = 1;
(*newrw)->api.child_width = 80;
(*newrw)->api.child_height = 23;
// TODO: This is only needed by the unit tests...
(*newrw)->api.root = *newobj;
(*newrw)->transparent = false;
(*newrw)->show_title = true;
(*newrw)->help = true;
(*newrw)->title_sz = 0;
(*newrw)->title = strdup(default_title);
(*newrw)->locks = 0;
if ((*newrw)->title == NULL) {
free(*newrw);
return NewIfc_error_allocation_failure;
} }
return (struct newifc_api *)ret; (*newrw)->mtx = pthread_mutex_initializer_np(true);
return NewIfc_error_none;
} }
#ifdef BUILD_TESTS #ifdef BUILD_TESTS
...@@ -260,7 +272,7 @@ void test_root_window(CuTest *ct) ...@@ -260,7 +272,7 @@ void test_root_window(CuTest *ct)
NewIfcObj obj; NewIfcObj obj;
static const char *new_title = "New Title"; static const char *new_title = "New Title";
obj = NewIFC_root_window(); CuAssertTrue(ct, !NewIFC_root_window(&obj));
CuAssertPtrNotNull(ct, obj); CuAssertPtrNotNull(ct, obj);
CuAssertPtrNotNull(ct, obj->get); CuAssertPtrNotNull(ct, obj->get);
CuAssertPtrNotNull(ct, obj->set); CuAssertPtrNotNull(ct, obj->set);
...@@ -268,52 +280,53 @@ void test_root_window(CuTest *ct) ...@@ -268,52 +280,53 @@ void test_root_window(CuTest *ct)
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertTrue(ct, obj->width == 80); CuAssertTrue(ct, obj->width == 80);
CuAssertTrue(ct, obj->height == 25); CuAssertTrue(ct, obj->height == 25);
CuAssertTrue(ct, obj->get(obj, NewIfc_transparent, &b) && !b); CuAssertTrue(ct, obj->get(obj, NewIfc_transparent, &b) == NewIfc_error_none && !b);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertTrue(ct, obj->get(obj, NewIfc_show_title, &b) && b); CuAssertTrue(ct, obj->get(obj, NewIfc_show_title, &b) == NewIfc_error_none && b);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertTrue(ct, obj->get(obj, NewIfc_show_help, &b) && b); CuAssertTrue(ct, obj->get(obj, NewIfc_show_help, &b) == NewIfc_error_none && b);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertStrEquals(ct, (obj->get(obj, NewIfc_title, &s), s), default_title); CuAssertTrue(ct, obj->get(obj, NewIfc_title, &s) == NewIfc_error_none);
CuAssertStrEquals(ct, s, default_title);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertTrue(ct, obj->get(obj, NewIfc_locked, &b) && !b); CuAssertTrue(ct, obj->get(obj, NewIfc_locked, &b) == NewIfc_error_none && !b);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertTrue(ct, obj->get(obj, NewIfc_locked_by_me, &b) && !b); CuAssertTrue(ct, obj->get(obj, NewIfc_locked_by_me, &b) == NewIfc_error_none && !b);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertTrue(ct, obj->set(obj, NewIfc_transparent, true)); CuAssertTrue(ct, obj->set(obj, NewIfc_transparent, true) == NewIfc_error_none);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertTrue(ct, obj->set(obj, NewIfc_show_title, false)); CuAssertTrue(ct, obj->set(obj, NewIfc_show_title, false) == NewIfc_error_none);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertTrue(ct, obj->set(obj, NewIfc_show_help, false)); CuAssertTrue(ct, obj->set(obj, NewIfc_show_help, false) == NewIfc_error_none);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertTrue(ct, obj->set(obj, NewIfc_title, new_title)); CuAssertTrue(ct, obj->set(obj, NewIfc_title, new_title) == NewIfc_error_none);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertTrue(ct, !obj->set(obj, NewIfc_locked, false)); CuAssertTrue(ct, obj->set(obj, NewIfc_locked, false) == NewIfc_error_lock_failed);
CuAssertTrue(ct, obj->last_error == NewIfc_error_lock_failed); CuAssertTrue(ct, obj->last_error == NewIfc_error_lock_failed);
CuAssertTrue(ct, obj->set(obj, NewIfc_locked, true)); CuAssertTrue(ct, obj->set(obj, NewIfc_locked, true) == NewIfc_error_none);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertTrue(ct, !obj->set(obj, NewIfc_locked_by_me, &b)); CuAssertTrue(ct, obj->set(obj, NewIfc_locked_by_me, &b) == NewIfc_error_not_implemented);
CuAssertTrue(ct, obj->last_error == NewIfc_error_not_implemented); CuAssertTrue(ct, obj->last_error == NewIfc_error_not_implemented);
CuAssertTrue(ct, obj->get(obj, NewIfc_transparent, &b) && b); CuAssertTrue(ct, obj->get(obj, NewIfc_transparent, &b) == NewIfc_error_none && b);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertTrue(ct, obj->get(obj, NewIfc_show_title, &b) && !b); CuAssertTrue(ct, obj->get(obj, NewIfc_show_title, &b) == NewIfc_error_none && !b);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertTrue(ct, obj->get(obj, NewIfc_show_help, &b) && !b); CuAssertTrue(ct, obj->get(obj, NewIfc_show_help, &b) == NewIfc_error_none && !b);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertTrue(ct, obj->get(obj, NewIfc_title, &s)); CuAssertTrue(ct, obj->get(obj, NewIfc_title, &s) == NewIfc_error_none);
CuAssertStrEquals(ct, s, new_title); CuAssertStrEquals(ct, s, new_title);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertTrue(ct, obj->get(obj, NewIfc_locked, &b) && b); CuAssertTrue(ct, obj->get(obj, NewIfc_locked, &b) == NewIfc_error_none && b);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertTrue(ct, obj->get(obj, NewIfc_locked_by_me, &b) && b); CuAssertTrue(ct, obj->get(obj, NewIfc_locked_by_me, &b) == NewIfc_error_none && b);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertTrue(ct, obj->set(obj, NewIfc_locked, false)); CuAssertTrue(ct, obj->set(obj, NewIfc_locked, false) == NewIfc_error_none);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
CuAssertTrue(ct, !obj->set(obj, NewIfc_locked, false)); CuAssertTrue(ct, obj->set(obj, NewIfc_locked, false) == NewIfc_error_lock_failed);
CuAssertTrue(ct, obj->last_error == NewIfc_error_lock_failed); CuAssertTrue(ct, obj->last_error == NewIfc_error_lock_failed);
CuAssertTrue(ct, obj->get(obj, NewIfc_locked_by_me, &b) && !b); CuAssertTrue(ct, obj->get(obj, NewIfc_locked_by_me, &b) == NewIfc_error_none && !b);
CuAssertTrue(ct, obj->last_error == NewIfc_error_none); CuAssertTrue(ct, obj->last_error == NewIfc_error_none);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment