| 1 | /* |
|---|
| 2 | * Copyright 2011 Dewey Taylor (james.dewey.taylor@gmail.com) |
|---|
| 3 | * All rights reserved. Distributed under the terms of the MIT license. |
|---|
| 4 | */ |
|---|
| 5 | |
|---|
| 6 | #include "Drafter.h" |
|---|
| 7 | #include "DrafterPrivateDefs.h" |
|---|
| 8 | |
|---|
| 9 | #include <iostream> |
|---|
| 10 | |
|---|
| 11 | // TODO: add parameters like width, height, team id, workspace, etc |
|---|
| 12 | uint32 |
|---|
| 13 | CreateContext() |
|---|
| 14 | { |
|---|
| 15 | #ifdef __HAIKU__ |
|---|
| 16 | // set sizes etc |
|---|
| 17 | uint32 size = sizeof(ContextData) * DRAFTER_MAX_CONTEXTS + sizeof(uint32); |
|---|
| 18 | if (size > B_PAGE_SIZE) |
|---|
| 19 | { |
|---|
| 20 | //panic("Drafter context area size larger than page size\n"); |
|---|
| 21 | cout<<"Context area size larger than page size"<<endl; |
|---|
| 22 | return D_ERR_NO_CONTEXT_AVAILABLE; |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | // check if we already know about an existing area |
|---|
| 26 | if (context_area == B_ERROR) |
|---|
| 27 | { |
|---|
| 28 | char* temp_addr = NULL; |
|---|
| 29 | // check if an area already exists |
|---|
| 30 | area_id temp_area = find_area(CONTEXT_AREA_NAME); |
|---|
| 31 | // if not, create one, then clone it |
|---|
| 32 | if (temp_area == B_NAME_NOT_FOUND) |
|---|
| 33 | { |
|---|
| 34 | // create a new area |
|---|
| 35 | temp_area = create_area(CONTEXT_AREA_NAME, &(void*)temp_addr, B_ANY_ADDRESS, B_PAGE_SIZE, B_CONTIGUOUS, B_READ_AREA | B_WRITE_AREA); |
|---|
| 36 | cout<<"Creating Area: "<<temp_area<<endl; |
|---|
| 37 | } |
|---|
| 38 | // clone the area |
|---|
| 39 | context_area = clone_area(CONTEXT_AREA_CLONE_NAME, &(void*)context_addr, B_ANY_ADDRESS, B_READ_AREA | B_WRITE_AREA, temp_area); |
|---|
| 40 | // increment usage counter |
|---|
| 41 | if (context_area < B_OK) |
|---|
| 42 | { |
|---|
| 43 | cout<<"Cannot clone area"<<endl; |
|---|
| 44 | return D_ERR_NO_CONTEXT_AVAILABLE; |
|---|
| 45 | } |
|---|
| 46 | contexts = (ContextData*)context_addr; |
|---|
| 47 | contextCount = (uint32*)context_addr + sizeof(ContextData) * DRAFTER_MAX_CONTEXTS; |
|---|
| 48 | *contextCount++; |
|---|
| 49 | } |
|---|
| 50 | #endif |
|---|
| 51 | // TODO: add locking |
|---|
| 52 | bool foundcontext = false; |
|---|
| 53 | int a; |
|---|
| 54 | for (a = 0; a < DRAFTER_MAX_CONTEXTS; a++) |
|---|
| 55 | { |
|---|
| 56 | if ((contexts[a].flags & DRAFTER_CONTEXT_USED) == 0) |
|---|
| 57 | { |
|---|
| 58 | // use this context! |
|---|
| 59 | contexts[a].flags |= DRAFTER_CONTEXT_USED; |
|---|
| 60 | foundcontext = true; |
|---|
| 61 | break; |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | if (foundcontext == false) |
|---|
| 65 | { |
|---|
| 66 | a = D_ERR_NO_CONTEXT_AVAILABLE; |
|---|
| 67 | } |
|---|
| 68 | return a; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | uint32 |
|---|
| 72 | SwitchContext(uint32 context) |
|---|
| 73 | { |
|---|
| 74 | if (context < DRAFTER_MAX_CONTEXTS) |
|---|
| 75 | { |
|---|
| 76 | if (contexts[context].flags & DRAFTER_CONTEXT_USED) |
|---|
| 77 | { |
|---|
| 78 | // how do we specify the current context? local variable in the app? |
|---|
| 79 | current_context = context; |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | return D_ERR_BAD_CONTEXT_ID; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | uint32 |
|---|
| 86 | DeleteContext(uint32 context) |
|---|
| 87 | { |
|---|
| 88 | // TODO: add locking |
|---|
| 89 | // TODO: check the teamID too |
|---|
| 90 | if (context < DRAFTER_MAX_CONTEXTS) |
|---|
| 91 | { |
|---|
| 92 | if (contexts[context].flags & DRAFTER_CONTEXT_USED) |
|---|
| 93 | { |
|---|
| 94 | contexts[context].flags &= 0xFFFFFFFF - DRAFTER_CONTEXT_USED; |
|---|
| 95 | return D_OK; |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | return D_ERR_BAD_CONTEXT_ID; |
|---|
| 99 | } |
|---|