config: initial support for handling system controller resource management.
[profile/ivi/murphy.git] / packaging.in / murphy.lua
1 with_system_controller = false
2
3 m = murphy.get()
4
5 -- try loading the various logging plugins
6 m:try_load_plugin('systemd')
7 m:try_load_plugin('dlog')
8
9 -- load the console plugin
10 m:try_load_plugin('console')
11
12 m:try_load_plugin('console.disabled', 'webconsole', {
13                   address = 'wsck:127.0.0.1:3000/murphy',
14                   httpdir = '/usr/share/murphy/webconsole' });
15
16 -- load the dbus plugin
17 if m:plugin_exists('dbus') then
18     m:load_plugin('dbus')
19 end
20
21 -- load the native resource plugin
22 if m:plugin_exists('resource-native') then
23     m:load_plugin('resource-native')
24     m:info("native resource plugin loaded")
25 else
26     m:info("No native resource plugin found...")
27 end
28
29 -- load the dbus resource plugin
30 m:try_load_plugin('resource-dbus', {
31     dbus_bus = "system",
32     dbus_service = "org.Murphy",
33     dbus_track = true,
34     default_zone = "driver",
35     default_class = "implicit"
36 })
37
38 -- load the domain control plugin
39 if m:plugin_exists('domain-control') then
40     m:load_plugin('domain-control')
41 else
42     m:info("No domain-control plugin found...")
43 end
44
45 -- load the AMB plugin
46 if m:plugin_exists('amb') then
47     m:load_plugin('amb')
48 else
49     m:info("No amb plugin found...")
50 end
51
52 -- load the ASM resource plugin
53 if m:plugin_exists('resource-asm') then
54     m:load_plugin('resource-asm', {
55         zone = "driver",
56         share_mmplayer = "player:AVP,mandatory,exclusive,strict",
57         ignored_argv0 = "WebProcess"
58     })
59 else
60     m:info("No audio session manager plugin found...")
61 end
62
63 if m:plugin_exists('ivi-resource-manager') and not with_system_controller then
64     m:load_plugin('ivi-resource-manager')
65 end
66
67 -- define application classes
68 application_class {
69     name     = "interrupt",
70     priority = 99,
71     modal    = true ,
72     share    = false,
73     order    = "fifo"
74 }
75
76 application_class {
77     name     = "emergency",
78     priority = 80,
79     modal    = false,
80     share    = false,
81     order    = "fifo"
82 }
83 application_class {
84     name     = "alert",
85     priority = 51,
86     modal    = false,
87     share    = false,
88     order    = "fifo"
89 }
90
91 application_class {
92     name     = "navigator",
93     priority = 50,
94     modal    = false,
95     share    = true,
96     order    = "fifo"
97 }
98
99 application_class {
100     name     = "phone",
101     priority = 6 ,
102     modal    = false,
103     share    = true ,
104     order    = "lifo"
105 }
106 application_class {
107     name     = "camera",
108     priority = 5,
109     modal    = false,
110     share    = false,
111     order    = "lifo"
112 }
113
114 application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
115 application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
116 --# doesn't need to be created here, ivi-resource-manager creates it if loaded
117 --#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
118 application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
119 application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }
120
121 -- define zone attributes
122 zone.attributes {
123     type = {mdb.string, "common", "rw"},
124     location = {mdb.string, "anywhere", "rw"}
125 }
126
127 -- define zones
128 zone {
129      name = "driver",
130      attributes = {
131          type = "common",
132          location = "front-left"
133      }
134 }
135
136 zone {
137      name = "passanger1",
138      attributes = {
139          type = "private",
140          location = "front-right"
141      }
142 }
143
144 zone {
145      name = "passanger2",
146      attributes = {
147          type = "private",
148          location = "back-left"
149      }
150 }
151
152 zone {
153      name = "passanger3",
154      attributes = {
155          type = "private",
156          location = "back-right"
157      }
158 }
159
160 zone {
161      name = "passanger4",
162      attributes = {
163          type = "private",
164          location = "back-left"
165      }
166 }
167
168
169 -- define resource classes
170 if not m:plugin_exists('ivi-resource-manager') and
171    not with_system_controller
172 then
173    resource.class {
174         name = "audio_playback",
175         shareable = true,
176         attributes = {
177             role = { mdb.string, "music", "rw" },
178             pid = { mdb.string, "<unknown>", "rw" },
179             policy = { mdb.string, "relaxed", "rw" }
180         }
181    }
182 end
183
184 resource.class {
185      name = "audio_recording",
186      shareable = true,
187      attributes = {
188          role   = { mdb.string, "music"    , "rw" },
189          pid    = { mdb.string, "<unknown>", "rw" },
190          policy = { mdb.string, "relaxed"  , "rw" }
191      }
192 }
193
194 resource.class {
195      name = "video_playback",
196      shareable = false,
197 }
198
199 resource.class {
200      name = "video_recording",
201      shareable = false
202 }
203
204 if not m:plugin_exists('ivi-resource-manager') then
205 resource.method.veto = {
206     function(zone, rset, grant, owners)
207         rset_priority = application_class[rset.application_class].priority
208
209         owner_id = owners.audio_playback.resource_set
210         rset_id = rset.id
211
212         if (rset_priority >= 50 and owner_id ~= rset_id) then
213             print("*** resource-set "..rset_id.." - veto")
214             return false
215         end
216
217         return true
218     end
219 }
220 end
221
222 -- test for creating selections
223 mdb.select {
224            name = "audio_owner",
225            table = "audio_playback_owner",
226            columns = {"application_class"},
227            condition = "zone_name = 'driver'"
228 }
229
230 mdb.select {
231            name = "vehicle_speed",
232            table = "amb_vehicle_speed",
233            columns = {"value"},
234            condition = "key = 'VehicleSpeed'"
235 }
236
237 element.lua {
238    name    = "speed2volume",
239    inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
240    outputs = {  mdb.table { name = "speedvol",
241                             index = {"zone", "device"},
242                             columns = {{"zone", mdb.string, 16},
243                                        {"device", mdb.string, 16},
244                                        {"value", mdb.floating}},
245                             create = true
246                            }
247              },
248    oldvolume = 0.0,
249    update  = function(self)
250                 speed = self.inputs.speed.single_value
251                 if (speed) then
252                     volume = (speed - 144.0) / 7.0
253                 else
254                     volume = 0.0
255                 end
256                 diff = volume - self.oldvolume
257                 if (diff*diff > self.inputs.param) then
258                     print("*** element "..self.name.." update "..volume)
259                     self.oldvolume = volume
260                     mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
261                 end
262              end
263 }
264
265 -- Night mode processing chain
266
267 mdb.select {
268     name = "exterior_brightness",
269     table = "amb_exterior_brightness",
270     columns = { "value" },
271     condition = "key = 'ExteriorBrightness'"
272 }
273
274 element.lua {
275     name    = "nightmode",
276     inputs  = { brightness = mdb.select.exterior_brightness },
277     oldmode = -1;
278     outputs = {
279         mdb.table {
280             name = "amb_nightmode",
281             index = { "id" },
282             create = true,
283             columns = {
284                 { "id", mdb.unsigned },
285                 { "night_mode", mdb.unsigned }
286             }
287         }
288     },
289     update = function(self)
290         -- This is a trivial function to calculate night mode. Later, we will
291         -- need a better threshold value and hysteresis to prevent oscillation.
292
293         brightness = self.inputs.brightness.single_value
294
295         if not brightness then
296             return
297         end
298
299         print("*** element "..self.name.." update brightness: "..brightness)
300
301         if brightness > 300 then
302             mode = 0
303         else
304             mode = 1
305         end
306
307         print("*** resulting mode: ".. mode)
308
309         if not (mode == self.oldmode) then
310             mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
311         end
312
313         self.oldmode = mode
314     end
315 }
316
317 mdb.select {
318     name = "select_night_mode",
319     table = "amb_nightmode",
320     columns = { "night_mode" },
321     condition = "id = 0"
322 }
323
324 sink.lua {
325     name = "night_mode",
326     inputs = { owner = mdb.select.select_night_mode },
327     property = "NightMode",
328     type = "b",
329     initiate = builtin.method.amb_initiate,
330     update = builtin.method.amb_update
331 }
332
333
334 -- Driving mode processing chain
335
336
337 element.lua {
338     name    = "drivingmode",
339     inputs  = { speed = mdb.select.vehicle_speed },
340     oldmode = -1;
341     outputs = {
342         mdb.table {
343             name = "amb_drivingmode",
344             index = { "id" },
345             create = true,
346             columns = {
347                 { "id", mdb.unsigned },
348                 { "driving_mode", mdb.unsigned }
349             }
350         }
351     },
352     update = function(self)
353
354         speed = self.inputs.speed.single_value
355
356         if not speed then
357             return
358         end
359
360         if speed == 0 then
361             mode = 0
362         else
363             mode = 1
364         end
365
366         if not (mode == self.oldmode) then
367             mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
368         end
369
370         self.oldmode = mode
371     end
372 }
373
374 mdb.select {
375     name = "select_driving_mode",
376     table = "amb_drivingmode",
377     columns = { "driving_mode" },
378     condition = "id = 0"
379 }
380
381 sink.lua {
382     name = "driving_mode",
383     inputs = { owner = mdb.select.select_driving_mode },
384     property = "DrivingMode",
385     type = "u",
386     initiate = builtin.method.amb_initiate,
387     update = builtin.method.amb_update
388 }
389
390 -- turn signals (left, right)
391
392 mdb.select {
393     name = "winker",
394     table = "amb_turn_signal",
395     columns = { "value" },
396     condition = "key = 'TurnSignal'"
397 }
398
399 -- regulation (on), use "select_driving_mode"
400
401 -- shift position (parking, reverse, other)
402
403 mdb.select {
404     name = "gear_position",
405     table = "amb_gear_position",
406     columns = { "value" },
407     condition = "key = 'GearPosition'"
408 }
409
410 -- cameras (back, front, left, right)
411
412 element.lua {
413     name    = "camera_state",
414     inputs  = { winker = mdb.select.winker, gear = mdb.select.gear_position },
415     oldmode = -1;
416     outputs = {
417         mdb.table {
418             name = "target_camera_state",
419             index = { "id" },
420             create = true,
421             columns = {
422                 { "id", mdb.unsigned },
423                 { "front_camera", mdb.unsigned },
424                 { "back_camera", mdb.unsigned },
425                 { "right_camera", mdb.unsigned },
426                 { "left_camera", mdb.unsigned }
427             }
428         }
429     },
430     update = function(self)
431
432         front_camera = 0
433         back_camera = 0
434         right_camera = 0
435         left_camera = 0
436
437         if self.inputs.gear == 128 then
438             back_camera = 1
439         elseif self.inputs.winker == 1 then
440             right_camera = 1
441         elseif self.inputs.winker == 2 then
442             left_camera = 1
443         end
444
445         mdb.table.target_camera_state:replace({ id = 0, front_camera = front_camera, back_camera = back_camera, right_camera = right_camera, left_camera = left_camera })
446
447     end
448 }
449
450 -- load the telephony plugin
451 m:try_load_plugin('telephony')
452
453
454 -- system controller test setup
455
456 if not with_system_controller then
457    return
458 end
459
460 window_operation_names = {
461     [1] = "create",
462     [2] = "destroy",
463     [3] = "name_change",
464     [4] = "visible",
465     [5] = "configure",
466     [6] = "active"
467 }
468
469 function window_operation_name(oper)
470     local name = window_operation_names[oper]
471     if name then return name end
472     return "<unknown " .. tostring(oper) .. ">"
473 end
474
475 command_names = {
476     [0x00001] = "send_appid",
477     [0x10001] = "create",
478     [0x10002] = "destroy",
479     [0x10003] = "show",
480     [0x10004] = "hide",
481     [0x10005] = "move",
482     [0x10006] = "change_active",
483     [0x10007] = "change_layer",
484     [0x10008] = "change_attr",
485     [0x10009] = "name",
486     [0x10011] = "map_thumb",
487     [0x10012] = "unmap_thumb",
488     [0x10020] = "show layer",
489     [0x10021] = "hide_layer",
490     [0x10022] = "change_layer_attr",
491     [0x20001] = "add_input",
492     [0x20002] = "del_input",
493     [0x20003] = "send_input"
494 }
495
496 function command_name(command)
497     local name = command_names[command]
498     if name then return name end
499     return "<unknown " .. tostring(command) .. ">"
500 end
501
502 wmgr = window_manager({
503   geometry = function(self, w,h, v)
504                   if type(v) == "function" then
505                       return v(w,h)
506                   end
507                   return v
508              end,
509
510   outputs = { { name  = "Center",
511                 id    = 0,
512                 areas = { Status = {
513                               id     = 100,
514                               pos_x  = 0,
515                               pos_y  = 0,
516                               width  = function(w,h) return w end,
517                               height = 64
518                           },
519                           Full = {
520                               id     = 1,
521                               pos_x  = 0,
522                               pos_y  = 64,
523                               width  = function(w,h) return w end,
524                               height = function(w,h) return h-64-128 end
525                           },
526                           Upper = {
527                               id     = 2,
528                               pos_x  = 0,
529                               pos_y  = 64,
530                               width  = function(w,h) return w end,
531                               height = function(w,h) return (h-64-128)/2 end
532                           },
533                           Lower = {
534                               id     = 3,
535                               pos_x  = 0,
536                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
537                               width  = function(w,h) return w end,
538                               height = function(w,h) return (h-64-128)/2 end
539                           },
540                           UpperLeft = {
541                               id     = 4,
542                               pos_x  = 0,
543                               pos_y  = 64,
544                               width  = function(w,h) return w/2 end,
545                               height = function(w,h) return (h-64-128)/2 end
546                           },
547                           UpperRight = {
548                               id     = 5,
549                               pos_x  = function(w,h) return w/2 end,
550                               pos_y  = 64,
551                               width  = function(w,h) return w/2 end,
552                               height = function(w,h) return (h-64-128)/2 end
553                           },
554                           LowerLeft = {
555                               id     = 6,
556                               pos_x  = 0,
557                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
558                               width  = function(w,h) return w/2 end,
559                               height = function(w,h) return (h-64-128)/2 end
560                           },
561                           LowerRight = {
562                               id     = 7,
563                               pos_x  = function(w,h) return w/2 end,
564                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
565                               width  = function(w,h) return w/2 end,
566                               height = function(w,h) return (h-64-128)/2 end
567                           },
568                           SysApp = {
569                               id     = 8,
570                               pos_x  = 0,
571                               pos_y  = 64,
572                               width  = function(w,h) return w end,
573                               height = function(w,h) return h-64-128 end
574                           },
575                           ["SysApp.Left"] = {
576                               id     = 9,
577                               pos_x  = 0,
578                               pos_y  = 64,
579                               width  = function(w,h) return w/2-181 end,
580                               height = function(w,h) return h-64-128 end
581                           },
582                           ["SysApp.Right"] = {
583                               id     = 10,
584                               pos_x  = function(w,h) return w/2+181 end,
585                               pos_y  = 64,
586                               width  = function(w,h) return w/2-181 end,
587                               height = function(w,h) return h-64-128 end
588                           },
589                         }
590                },
591                { name  = "Mid",
592                  id    = 1
593                }
594
595   },
596   layers = { {      0, "Background"   , 1 },
597              {      1, "Application"  , 2 },
598              {      2, "Softkeyboard" , 4 },
599              {      3, "HomeScreen"   , 2 },
600              {      4, "ControlBar"   , 2 },
601              {      5, "InterruptApp" , 2 },
602              {      6, "OnScreen"     , 2 },
603              {    101, "Input"        , 3 },
604              {    102, "Touch"        , 4 },
605              {    103, "Cursor"       , 5 },
606              { 0x1000, "Background"   , 1 },
607              { 0x2000, "Normal"       , 2 },
608              { 0x3000, "Fullscreen"   , 2 },
609              { 0x4000, "InputPanel"   , 3 },
610              { 0xA000, "Touch"        , 4 },
611              { 0xB000, "Cursor"       , 5 },
612              { 0xC000, "Startup"      , 6 }
613   },
614
615
616   window_update = function(self, oper, win, mask)
617                       if (verbose) then
618                           print("*** WINDOW UPDATE oper:"..window_operation_name(oper).." mask: "..tostring(mask))
619                           print(win)
620                       end
621
622                       local arg = m:JSON({ surface = win.surface,
623                                            winname = win.name,
624                       })
625                       local command = 0
626
627                       if oper == 1 then -- create
628                            if win.layertype and win.layertype == 3 then
629                                print("ignoring input panel window creation")
630                                return
631                            end
632                            command     = 0x10001
633                       elseif oper == 2 then -- destroy
634                            command     = 0x10002
635                       elseif oper == 3 then  -- namechange
636                            command     = 0x10009
637                       elseif oper == 4 or oper == 5 then --visible or configure
638                            command     = 0x10008
639                            arg.zone    = win.area
640                            arg.node    = win.node
641                            arg.layer   = win.layer
642                            arg.pos_x   = win.pos_x
643                            arg.pos_y   = win.pos_y
644                            arg.width   = win.width
645                            arg.height  = win.height
646                            arg.raise   = win.raise
647                            arg.visible = win.visible
648                            arg.active  = win.active
649                       elseif oper == 6 then -- active
650                            if false and win.active ~= 0 then
651                                print("### already active")
652                                return
653                            end
654                            command = 0x10006
655                       else
656                            print("### nothing to do")
657                            return
658                       end
659
660                       local msg = m:JSON({ command = command,
661                                            appid   = win.appid,
662                                            pid     = win.pid,
663                                            arg     = arg
664                       })
665                       print("### sending window message: " .. command_name(msg.command))
666                       print(msg)
667                       sc:send_message(msg.appid, msg)
668
669                       if (oper == 1) then -- create
670                           local a = animation({})
671                           local r = m:JSON({surface = win.surface,
672                                             visible = 1,
673                                             raise   = 1})
674                           self:window_request(r,a,0)
675                       end
676                   end,
677
678   layer_update = function(self, oper, j, mask)
679                       if verbose then
680                           print("*** LAYER UPDATE oper:"..oper.." mask: "..tostring(mask))
681                           print(j)
682                       end
683                  end,
684   output_update = function(self, oper, out, mask)
685                       local idx = out.index
686                       if verbose then
687                           print("*** OUTPUT UPDATE oper:"..oper.." mask: "..tostring(mask))
688                       end
689                       print(out)
690                       if (oper == 1) then -- create
691                           local outdef = self.outputs[idx+1]
692                           if outdef then
693                               self:output_request(m:JSON({index = idx,
694                                                           id    = outdef.id,
695                                                           name  = outdef.name
696                                                           }))
697                           end
698                       elseif (oper == 5) then -- done
699                           local ads = self.outputs[idx+1].areas
700                           if ads then
701                               for name,area in pairs(ads) do
702                                   local a = m:JSON({name=name,output=out.index})
703                                   for fld,val in pairs(area) do
704                                       a[fld] = self:geometry(out.width,out.height,val)
705                                    end
706                                    self:area_create(a)
707                               end
708                           end
709                       end
710                   end
711 })
712
713
714 sc = m:get_system_controller()
715
716 -- resource sets
717 sets = {}
718
719 connected = false
720
721 -- these shoud be before wmgr:connect() is called
722 print("====== creating applications")
723 application {
724     appid      = "default",
725     area       = "Center.Full",
726     privileges = { screen = "none", audio = "none" }
727 }
728
729 application {
730     appid      = "org.tizen.ico.homescreen",
731     area       = "Center.Full",
732     privileges = { screen = "system", audio = "system" }
733 }
734
735 application {
736     appid      = "org.tizen.ico.statusbar",
737     area       = "Center.Status",
738     privileges = { screen = "system", audio = "none" }
739 }
740
741
742 if sc then
743     sc.client_handler = function (self, cid, msg)
744         print('*** client handler: ' .. tostring(msg))
745         if not connected then
746             print('Trying to connect to wayland...')
747             connected = wmgr:connect()
748         else
749             print('Window manager already connected...')
750         end
751     end
752
753     sc.generic_handler = function (self, cid, msg)
754         print('*** generic handler: ' .. tostring(msg))
755     end
756
757     sc.window_handler = function (self, cid, msg)
758         print('*** window handler: ' .. command_name(msg.command) .. ' ' .. tostring(msg))
759
760         local a = animation({})
761         if msg.command == 0x10003 then       -- ico SHOW command
762             local raise_mask = 0x01000000
763             local lower_mask = 0x02000000
764             msg.arg.visible = 1
765             if msg.arg and msg.arg.anim_name then
766                 local time = msg.arg.time
767                 time = m:AND(time, m:NEG(m:OR(raise_mask, lower_mask)))
768                 time = 200
769                 if m:AND(msg.arg.anim_time, raise_mask) then
770                     msg.arg.raise = 1
771                 elseif m:AND(msg.arg.anim_time, lower_mask) then
772                     msg.arg.raise = 0
773                 end
774                 a.show = { msg.arg.anim_name, time }
775                 print('time: ' .. tostring(a.show[2]))
776             end
777             print('##### SHOW')
778             print(tostring(msg.arg))
779             wmgr:window_request(msg.arg, a, 0)
780         elseif msg.command == 0x10004 then   -- ico HIDE command
781             local raise_mask = 0x01000000
782             local lower_mask = 0x02000000
783             msg.arg.visible = 0
784             if msg.arg and msg.arg.anim_name then
785                 local time = msg.arg.time
786                 time = m:AND(time, m:NEG(m:OR(raise_mask, lower_mask)))
787                 time = 200
788                 if m:AND(msg.arg.anim_time, raise_mask) then
789                     msg.arg.raise = 1
790                 end
791                 if m:AND(msg.arg.anim_time, lower_mask) then
792                     msg.arg.raise = 0
793                 end
794                 a.hide = { msg.arg.anim_name, time }
795                 print('hide animation time: ' .. tostring(a.hide[2]))
796             end
797             print('##### HIDE')
798             print(tostring(msg.arg))
799             wmgr:window_request(msg.arg, a, 0)
800         elseif msg.command == 0x10005 then   -- ico MOVE
801             print('##### MOVE')
802             print(tostring(msg.arg))
803             wmgr:window_request(msg.arg, a, 0)
804         elseif msg.command == 0x10006 then   -- ico ACTIVE
805             print('##### ACTIVE')
806             if not msg.arg.active then
807                 msg.arg.active = 0
808             end
809             print(tostring(msg.arg))
810             wmgr:window_request(msg.arg, a, 0)
811         elseif msg.command == 0x10007 then   -- ico CHANGE_LAYER
812             print('##### CHANGE_LAYER')
813             print(tostring(msg.arg))
814             --[[
815             if msg.arg.layer ~= 4 or msg.arg.layer ~= 5 then
816                 print("do not change layer for other than cursor or touch")
817                 return
818             end
819             --]]
820             wmgr:window_request(msg.arg, a, 0)
821         elseif msg.command == 0x10011 then   -- ico MAP_THUMB
822             local framerate = msg.arg.framerate
823             print('##### MAP_THUMB')
824             msg.arg.map = 1
825             if not framerate or framerate < 0 then
826                 framerate = 0
827             end
828             wmgr:window_request(msg.arg, a, framerate)
829         elseif msg.command == 0x10012 then   -- ico UNMAP_THUMB
830             print('##### UNMAP_THUMB')
831             msg.arg.map = 0
832             wmgr:window_request(msg.arg, a, 0)
833         elseif msg.command == 0x10020 then   -- ico SHOW_LAYER command
834             msg.arg.visible = 1
835             print('##### SHOW_LAYER')
836             wmgr:layer_request(msg.arg)
837         end
838     end
839     sc.input_handler = function (self, cid, msg)
840         print('*** input handler: ' .. tostring(msg))
841     end
842     sc.user_handler = function (self, cid, msg)
843         print('*** user handler: ' .. tostring(msg))
844     end
845     sc.resource_handler = function (self, cid, msg)
846         print('*** resource handler: ' .. tostring(msg))
847
848         createResourceSet = function (ctl, client, msg)
849             cb = function(rset, data)
850                 print("> resource callback")
851
852                 -- type is either basic (0) or interrupt (1)
853                 requestType = 0
854                 if msg.res.type then
855                     requestType = msg.res.type
856                 end
857
858                 if rset.acquired then
859                     cmd = 0x00040001 -- acquire
860                 else
861                     cmd = 0x00040002 -- release
862                 end
863
864                 reply = m.JSON({
865                         appid = data.client,
866                         command = cmd,
867                         res = {
868                             type = requestType
869                         }
870                     })
871
872                 if rset.resources.audio_playback then
873                     reply.res.sound = {
874                         zone = "driver",
875                         name = msg.appid,
876                         adjust = 0,
877                         -- id = "0"
878                     }
879                 end
880
881                 if rset.resources.display then
882                     reply.res.window = {
883                         zone = "driver",
884                         name = msg.appid,
885                         -- id = "0"
886                     }
887                 end
888
889                 if rset.resources.input then
890                     reply.res.input = {
891                         name = msg.appid,
892                         event = 0
893                     }
894                 end
895                 print("sending message to client: " .. data.client)
896
897                 if sc:send_message(data.client, reply) then
898                     print('*** reply OK')
899                 else
900                     print('*** reply FAILED')
901                 end
902             end
903
904             rset = m:ResourceSet({
905                     application_class = "player",
906                     zone = "driver", -- msg.zone ("full")
907                     callback = cb
908                 })
909
910             rset.data = {
911                 cid = cid,
912                 ctl = ctl
913             }
914
915             if msg.res.sound then
916                 rset:addResource({
917                         resource_name = "audio_playback"
918                     })
919                 rset.resources.audio_playback.attributes.pid = tostring(msg.pid)
920                 rset.resources.audio_playback.attributes.appid = msg.appid
921                 print("sound name: " .. msg.res.sound.name)
922                 print("sound zone:" .. msg.res.sound.zone)
923                 print("sound adjust: " .. tostring(msg.res.sound.adjust))
924                 if msg.res.sound.id then
925                     print("sound id: " .. msg.res.sound.id)
926                 end
927             end
928
929             if msg.res.input then
930                 rset:addResource({
931                         resource_name = "input"
932                     })
933                 rset.resources.input.attributes.pid = tostring(msg.pid)
934                 rset.resources.input.attributes.appid = msg.appid
935                 print("input name: " .. msg.res.sound.name)
936                 print("input event:" .. tostring(msg.res.input.event))
937             end
938
939             if msg.res.window then
940                 rset:addResource({
941                         resource_name = "display"
942                     })
943                 rset.resources.display.attributes.pid = tostring(msg.pid)
944                 rset.resources.display.attributes.appid = msg.appid
945                 print("display name: " .. msg.res.display.name)
946                 print("display zone:" .. msg.res.display.zone)
947                 if msg.res.display.id then
948                     print("display id: " .. msg.res.display.id)
949                 end
950             end
951
952             return rset
953         end
954
955         -- parse the message
956
957         -- fields common to all messages:
958         --      msg.command
959         --      msg.appid
960         --      msg.pid
961
962         if msg.command == 0x00040011 then -- MSG_CMD_CREATE_RES
963             print("command CREATE")
964
965             if not sets.cid then
966                 sets.cid = createResourceSet(self, cid, msg)
967             end
968
969         elseif msg.command == 0x00040012 then -- MSG_CMD_DESTORY_RES
970             print("command DESTROY")
971
972             if sets.cid then
973                 sets.cid:release()
974             end
975
976             sets.cid = nil -- garbage collecting
977
978         elseif msg.command == 0x00040001 then -- MSG_CMD_ACQUIRE_RES
979             print("command ACQUIRE")
980
981             if not sets.cid then
982                 sets.cid = createResourceSet(self, cid, msg)
983             end
984
985             sets.cid:acquire()
986
987         elseif msg.command == 0x00040002 then -- MSG_CMD_RELEASE_RES
988             print("command RELEASE")
989
990             if sets.cid then
991                 sets.cid:release()
992             end
993
994         elseif msg.command == 0x00040003 then -- MSG_CMD_DEPRIVE_RES
995             print("command DEPRIVE")
996
997         elseif msg.command == 0x00040004 then -- MSG_CMD_WAITING_RES
998             print("command WAITING")
999
1000         elseif msg.command == 0x00040005 then -- MSG_CMD_REVERT_RES
1001             print("command REVERT")
1002         end
1003     end
1004     sc.inputdev_handler = function (self, cid, msg)
1005         print('*** inputdev handler: ' .. tostring(msg))
1006     end
1007 end