#include "pebbleobject.h" #include "calc.h" #include "object.h" Object pebbleOther(enum PebbleType type, void* data, void (* cleanup)(Object*), Object (* clone)(struct Other*)) { struct Object o = otherObject(); struct PebbleObject* po = malloc(sizeof(struct PebbleObject)); o.other->data = po; o.other->cleanup = cleanup; o.other->clone = clone; po->type = type; po->ptr = data; return o; } enum PebbleType getPebbleType(const Object o1) { if (o1.type == TYPE_OTHER) { struct PebbleObject* po = o1.other->data; enum PebbleType t = po->type; return t; } return P_ERROR; } struct PebbleObject* accessPebbleObject(Object obj) { if (getPebbleType(obj) != P_ERROR) { return obj.other->data; } return NULL; } void custom_window_load(Window* window) { } void custom_window_unload(Window* window) { window_destroy(window); } Object createWindow(Object* params, int length, struct Environment* env) { Window* window = window_create(); WindowHandlers wh = {.load = custom_window_load, .unload = custom_window_unload}; window_set_window_handlers(window, wh); return pebbleOther(WINDOW, window, NULL, NULL); } Object deleteWindow(Object* params, int length, struct Environment* env) { Object window = params[0]; /* Maintain a list of layers to delete? */ if (getPebbleType(window) == WINDOW) { window_stack_remove(accessPebbleObject(window)->window, true); return trueObject(); } return falseObject(); } Object pushWindow(Object* params, int length, struct Environment* env) { Object window = params[0]; if (getPebbleType(window) == WINDOW) { window_stack_push(accessPebbleObject(window)->window, true); return trueObject(); } return falseObject(); } Object updateTextLayer(Object* params, int length, struct Environment* env) { Object textLayer = params[0]; Object text = params[1]; if (getPebbleType(textLayer) == TEXT_LAYER) { struct PebbleObject* po = accessPebbleObject(textLayer); size_t l; char* string = stringObj(&text, &l); snprintf(po->textLayer->text, 999, "%s", string); free(string); text_layer_set_text(po->textLayer->layer, po->textLayer->text); return trueObject(); } return falseObject(); } Object addTextLayer(Object* params, int length, struct Environment* env) { Object window = params[0]; Object text = params[1]; if (getPebbleType(window) != WINDOW) { return errorObject(0); } Layer* window_layer = window_get_root_layer(accessPebbleObject(window)->window); GRect bounds = layer_get_bounds(window_layer); struct PLTextLayer* textLayer = malloc(sizeof(struct PLTextLayer)); textLayer->text = calloc(sizeof(char), 100); textLayer->layer = text_layer_create(bounds); size_t l; char* string = stringObj(&text, &l); snprintf(textLayer->text, 999, "%s", string); free(string); text_layer_set_text(textLayer->layer, textLayer->text); text_layer_set_font(textLayer->layer, fonts_get_system_font(FONT_KEY_BITHAM_42_BOLD)); layer_add_child(window_layer, text_layer_get_layer(textLayer->layer)); return pebbleOther(TEXT_LAYER, textLayer, NULL, NULL); } Object subscription; struct Environment* subscriptionEnv; static void subscriptionHandler(struct tm* tick_time, TimeUnits changed) { eval(&subscription, subscriptionEnv); } Object subscribe(Object* params, int length, struct Environment* env) { Object function = params[0]; Object time = params[1]; if (function.type == TYPE_LAMBDA) { subscription = cloneObject(function); subscriptionEnv = env; int unit = time.type != TYPE_NUMBER ? MINUTE_UNIT : time.number == 1 ? SECOND_UNIT : time.number == 2 ? MINUTE_UNIT : time.number == 3 ? HOUR_UNIT : time.number == 4 ? DAY_UNIT : time.number == 5 ? MONTH_UNIT : time.number == 6 ? YEAR_UNIT : MINUTE_UNIT; tick_timer_service_subscribe(unit, subscriptionHandler); return trueObject(); } return falseObject(); }