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