File: osdobjectsbase.c

package info (click to toggle)
vdr-plugin-osdserver 0.1.3-23
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 392 kB
  • sloc: ansic: 3,165; perl: 923; sh: 180; makefile: 46
file content (166 lines) | stat: -rw-r--r-- 3,794 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166


#include <stdlib.h>
#include "osdobjectsbase.h"
#include "osdserver.h"

// --------------------
//   cOsdServerObject
// --------------------

cMutex cShadowBase::LockShared;


int cOsdServerObject::nextId = 0;

cOsdServerObject::cOsdServerObject() {
    context = NULL;
    id = nextId++;
}


cOsdServerObject::~cOsdServerObject() {
    if (context) context->Remove(this);
}


// ---------------------
//   cOsdServerContext
// ---------------------

cOsdServerObject* cServerContext::GetName(const char *Name) {
    if (!Name) return NULL;
        
    if (Name[0] == '_' && (Name[1] < '0' || Name[1] > '9')) {
        // Reserved name, but not _[0-9]*   
        if (0 == strcasecmp(Name, "_FOCUS")) {
            return GetFocusObject();
        }
    }
    
    ObjectList::iterator i = objects.find(Name);
    if (i != objects.end())
       return i->second;
    return NULL;
}

void cServerContext::Add(cOsdServerObject *Object, cString Name) {
    if (localContext)
        localContext->Add(Object, Name);

    if (!(const char*)Name)
        Name = cString::sprintf("_%i", Object->id);

    ObjectList::iterator i = objects.find(Name);
    if (i != objects.end()) {
        // Already other object of same name. Rename the other.
        cOsdServerObject *prev = i->second;
        cString pname = cString::sprintf("_%i", prev->id);
        
        objects.erase(i);
        objects.insert(std::make_pair(pname, prev));
        
        IdList::iterator id = idList.find(prev->id);
        if (id != idList.end())
            idList.erase(id);
        idList.insert(std::make_pair(prev->id, pname));
    }
    objects.insert(std::make_pair(Name, Object));
    idList.insert(std::make_pair(Object->id, Name));

    Object->context = this;
}

void cServerContext::Remove(cOsdServerObject *Object) {
    if (Object == focusObject)
        focusObject = NULL;

    if (localContext)
        localContext->Remove(Object);
    
    IdList::iterator id = idList.find(Object->id);
    if (id == idList.end())
        return;
        
    ObjectList::iterator i = objects.find(id->second); // search by name
    if (i != objects.end() && i->second == Object) {        
        objects.erase(i);
    }
    
    idList.erase(id);
}


cServerContext* cServerContext::MakeLocalContext() {
    // Create local context as alias of this one
    if (localContext)
        return localContext;
    
    localContext = new cServerContext;
    
    localContext->objects = objects;
    localContext->idList = idList;
    localContext->parentContext = this;
    
    return localContext;
}

void cServerContext::DestroyLocalContext() {
    if (localContext) {
        delete localContext;
        localContext = NULL;
    }
}

cOsdServerObject* cServerContext::GetFocusObject() {
    if (focusObject)
        return focusObject;
    if (parentContext)
        return parentContext->GetFocusObject();
    return NULL;
}

cServerContext::cServerContext() {
    localContext = NULL;
    parentContext = NULL;
    
    focusObject = NULL;
}

cServerContext::~cServerContext() {
    // Avoid the default clear destructor - objects removes itself from list
    DestroyLocalContext();
    
    while (!objects.empty()) {
        cOsdServerObject *obj = objects.begin()->second;
        objects.erase(objects.begin());
        if (obj->context == this)
            delete obj;
    }
    
    if (parentContext)
        parentContext->localContext = NULL;    
}




// ---------------
//   cShadowBase
// ---------------


cShadowBase::cShadowBase() {
    Dirty=true;
}

cShadowBase::cPrivateBase::cPrivateBase(cShadowBase *parent) {
    Parent=parent;
}

cShadowBase::cPrivateBase::~cPrivateBase() {
    if (!Parent) return;
    esyslog("osdserver: hard detaching of OSD VDR object");
    Parent->Detach();
}