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