config: added resources for speech recognition and synthesis.
[profile/ivi/murphy.git] / packaging.in / murphy.lua
1 with_system_controller = false
2 with_amb = false
3 verbose = 0
4
5 m = murphy.get()
6
7 -- try loading the various logging plugins
8 m:try_load_plugin('systemd')
9 m:try_load_plugin('dlog')
10
11 -- load the console plugin
12 m:try_load_plugin('console')
13
14 m:try_load_plugin('console.disabled', 'webconsole', {
15                   address = 'wsck:127.0.0.1:3000/murphy',
16                   httpdir = '/usr/share/murphy/webconsole' });
17
18 -- load the dbus plugin
19 if m:plugin_exists('dbus') then
20     m:load_plugin('dbus')
21 end
22
23 -- load the native resource plugin
24 if m:plugin_exists('resource-native') then
25     m:load_plugin('resource-native')
26     m:info("native resource plugin loaded")
27 else
28     m:info("No native resource plugin found...")
29 end
30
31 -- load the dbus resource plugin
32 m:try_load_plugin('resource-dbus', {
33     dbus_bus = "system",
34     dbus_service = "org.Murphy",
35     dbus_track = true,
36     default_zone = "driver",
37     default_class = "implicit"
38 })
39
40 -- load the domain control plugin
41 if m:plugin_exists('domain-control') then
42     m:load_plugin('domain-control')
43 else
44     m:info("No domain-control plugin found...")
45 end
46
47 if m:plugin_exists('glib') then
48     m:load_plugin('glib')
49 else
50     m:info("No glib plugin found...")
51 end
52
53 -- load the AMB plugin
54 if m:plugin_exists('amb') then
55     m:try_load_plugin('amb')
56
57     if builtin.method.amb_initiate and
58        builtin.method.amb_update
59     then
60         with_amb = true
61     end
62 else
63     m:info("No amb plugin found...")
64 end
65
66 -- load the ASM resource plugin
67 if m:plugin_exists('resource-asm') then
68     m:try_load_plugin('resource-asm', {
69         zone = "driver",
70         share_mmplayer = "player:AVP,mandatory,exclusive,strict",
71         ignored_argv0 = "WebProcess"
72     })
73 else
74     m:info("No audio session manager plugin found...")
75 end
76
77 if m:plugin_exists('system-controller') then
78     with_system_controller = true
79 elseif m:plugin_exists('ivi-resource-manager') then
80     m:load_plugin('ivi-resource-manager')
81     with_system_controller = false
82 end
83
84 -- define application classes
85 application_class {
86     name     = "interrupt",
87     priority = 99,
88     modal    = true ,
89     share    = false,
90     order    = "fifo"
91 }
92
93 application_class {
94     name     = "emergency",
95     priority = 80,
96     modal    = false,
97     share    = false,
98     order    = "fifo"
99 }
100 application_class {
101     name     = "alert",
102     priority = 51,
103     modal    = false,
104     share    = false,
105     order    = "fifo"
106 }
107
108 application_class {
109     name     = "navigator",
110     priority = 50,
111     modal    = false,
112     share    = true,
113     order    = "fifo"
114 }
115
116 application_class {
117     name     = "phone",
118     priority = 6 ,
119     modal    = false,
120     share    = true ,
121     order    = "lifo"
122 }
123 application_class {
124     name     = "camera",
125     priority = 5,
126     modal    = false,
127     share    = false,
128     order    = "lifo"
129 }
130
131 application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
132 application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
133 --# doesn't need to be created here, ivi-resource-manager creates it if loaded
134 --#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
135 application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
136 application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }
137
138 -- define zone attributes
139 zone.attributes {
140     type = {mdb.string, "common", "rw"},
141     location = {mdb.string, "anywhere", "rw"}
142 }
143
144 -- define zones
145 zone {
146      name = "driver",
147      attributes = {
148          type = "common",
149          location = "front-left"
150      }
151 }
152
153 zone {
154      name = "passanger1",
155      attributes = {
156          type = "private",
157          location = "front-right"
158      }
159 }
160
161 zone {
162      name = "passanger2",
163      attributes = {
164          type = "private",
165          location = "back-left"
166      }
167 }
168
169 zone {
170      name = "passanger3",
171      attributes = {
172          type = "private",
173          location = "back-right"
174      }
175 }
176
177 zone {
178      name = "passanger4",
179      attributes = {
180          type = "private",
181          location = "back-left"
182      }
183 }
184
185
186 -- define resource classes
187 if not m:plugin_exists('ivi-resource-manager') and
188    not with_system_controller
189 then
190    resource.class {
191         name = "audio_playback",
192         shareable = true,
193         attributes = {
194             role = { mdb.string, "music", "rw" },
195             pid = { mdb.string, "<unknown>", "rw" },
196             policy = { mdb.string, "relaxed", "rw" }
197         }
198    }
199 end
200
201 resource.class {
202      name = "audio_recording",
203      shareable = true,
204      attributes = {
205          role   = { mdb.string, "music"    , "rw" },
206          pid    = { mdb.string, "<unknown>", "rw" },
207          policy = { mdb.string, "relaxed"  , "rw" }
208      }
209 }
210
211 resource.class {
212      name = "video_playback",
213      shareable = false,
214 }
215
216 resource.class {
217      name = "video_recording",
218      shareable = false
219 }
220
221 resource.class {
222      name = "speech_recognition",
223      shareable = true
224 }
225
226 resource.class {
227      name = "speech_synthesis",
228      shareable = true
229 }
230
231 if not m:plugin_exists('ivi-resource-manager') and
232    not with_system_controller
233 then
234     resource.method.veto = {
235         function(zone, rset, grant, owners, req_set)
236             return true
237          end
238     }
239 end
240
241 -- test for creating selections
242 mdb.select {
243            name = "audio_owner",
244            table = "audio_playback_owner",
245            columns = {"application_class"},
246            condition = "zone_name = 'driver'"
247 }
248
249 mdb.select {
250            name = "vehicle_speed",
251            table = "amb_vehicle_speed",
252            columns = {"value"},
253            condition = "key = 'VehicleSpeed'"
254 }
255
256 element.lua {
257    name    = "speed2volume",
258    inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
259    outputs = {  mdb.table { name = "speedvol",
260                             index = {"zone", "device"},
261                             columns = {{"zone", mdb.string, 16},
262                                        {"device", mdb.string, 16},
263                                        {"value", mdb.floating}},
264                             create = true
265                            }
266              },
267    oldvolume = 0.0,
268    update  = function(self)
269                 speed = self.inputs.speed.single_value
270                 if (speed) then
271                     volume = (speed - 144.0) / 7.0
272                 else
273                     volume = 0.0
274                 end
275                 diff = volume - self.oldvolume
276                 if (diff*diff > self.inputs.param) then
277                     print("*** element "..self.name.." update "..volume)
278                     self.oldvolume = volume
279                     mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
280                 end
281              end
282 }
283
284 -- Night mode processing chain
285
286 mdb.select {
287     name = "exterior_brightness",
288     table = "amb_exterior_brightness",
289     columns = { "value" },
290     condition = "key = 'ExteriorBrightness'"
291 }
292
293 element.lua {
294     name    = "nightmode",
295     inputs  = { brightness = mdb.select.exterior_brightness },
296     oldmode = -1;
297     outputs = {
298         mdb.table {
299             name = "amb_nightmode",
300             index = { "id" },
301             create = true,
302             columns = {
303                 { "id", mdb.unsigned },
304                 { "night_mode", mdb.unsigned }
305             }
306         }
307     },
308     update = function(self)
309         -- This is a trivial function to calculate night mode. Later, we will
310         -- need a better threshold value and hysteresis to prevent oscillation.
311
312         brightness = self.inputs.brightness.single_value
313
314         if not brightness then
315             return
316         end
317
318         print("*** element "..self.name.." update brightness: "..brightness)
319
320         if brightness > 300 then
321             mode = 0
322         else
323             mode = 1
324         end
325
326         print("*** resulting mode: ".. mode)
327
328         if not (mode == self.oldmode) then
329             mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
330         end
331
332         self.oldmode = mode
333     end
334 }
335
336 mdb.select {
337     name = "select_night_mode",
338     table = "amb_nightmode",
339     columns = { "night_mode" },
340     condition = "id = 0"
341 }
342
343 if with_amb then
344     sink.lua {
345         name = "night_mode",
346         inputs = { owner = mdb.select.select_night_mode },
347         property = "NightMode",
348         type = "b",
349         initiate = builtin.method.amb_initiate,
350         update = builtin.method.amb_update
351     }
352 end
353
354 -- Night mode general handlers
355
356 sink.lua {
357     name = "nightmode_homescreen",
358     inputs = { owner = mdb.select.select_night_mode },
359     initiate = function(self)
360         -- data = mdb.select.select_night_mode.single_value
361         return true
362     end,
363     update = function(self)
364         data = mdb.select.select_night_mode.single_value
365
366         if verbose > 1 then
367             print("Night mode updated: " .. tostring(data))
368         end
369
370         if sc then
371             -- tell homescreen that night mode was updated
372             sc:send_message(homescreen, m:JSON({command=0x60001,arg=m:JSON({stateid=2,state=data})}))
373         end
374         return true
375     end
376 }
377
378 -- Driving mode processing chain
379
380 element.lua {
381     name    = "drivingmode",
382     inputs  = { speed = mdb.select.vehicle_speed },
383     oldmode = -1;
384     outputs = {
385         mdb.table {
386             name = "amb_drivingmode",
387             index = { "id" },
388             create = true,
389             columns = {
390                 { "id", mdb.unsigned },
391                 { "driving_mode", mdb.unsigned }
392             }
393         }
394     },
395     update = function(self)
396
397         speed = self.inputs.speed.single_value
398
399         if not speed then
400             return
401         end
402
403         if speed == 0 then
404             mode = 0
405         else
406             mode = 1
407         end
408
409         if not (mode == self.oldmode) then
410             mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
411         end
412
413         self.oldmode = mode
414     end
415 }
416
417 mdb.select {
418     name = "select_driving_mode",
419     table = "amb_drivingmode",
420     columns = { "driving_mode" },
421     condition = "id = 0"
422 }
423
424 if with_amb then
425     sink.lua {
426         name = "driving_mode",
427         inputs = { owner = mdb.select.select_driving_mode },
428         property = "DrivingMode",
429         type = "u",
430         initiate = builtin.method.amb_initiate,
431         update = builtin.method.amb_update
432     }
433 end
434
435 -- turn signals (left, right)
436
437 mdb.select {
438     name = "winker",
439     table = "amb_turn_signal",
440     columns = { "value" },
441     condition = "key = 'TurnSignal'"
442 }
443
444 -- define three categories
445
446 mdb.select {
447     name = "undefined_applications",
448     table = "aul_applications",
449     columns = { "appid" },
450     condition = "category = '<undefined>'"
451 }
452
453 mdb.select {
454     name = "basic_applications",
455     table = "aul_applications",
456     columns = { "appid" },
457     condition = "category = 'basic'"
458 }
459
460 mdb.select {
461     name = "entertainment_applications",
462     table = "aul_applications",
463     columns = { "appid" },
464     condition = "category = 'entertainment'"
465 }
466
467 function ft(t)
468     -- filter the object garbage out of the tables
469     ret = {}
470
471     for k,v in pairs(t) do
472         if k ~= "userdata" and k ~= "new" then
473             ret[k] = v
474         end
475     end
476
477     return ret
478 end
479
480 function getApplication(appid)
481     local conf = nil
482
483     -- find the correct local application definition
484
485     for k,v in pairs(ft(application)) do
486         if appid == v.appid then
487             conf = v
488             break
489         end
490     end
491
492     return conf
493 end
494
495 function regulateApplications(t, regulation)
496     for k,v in pairs(ft(t)) do
497
498         -- iterate through the undefined and entertainment apps, see if
499         -- they have been overruled in local config
500
501         local conf = getApplication(v.appid)
502
503         if conf and conf.resource_class ~= "player" then
504             -- override, don't disable
505             resmgr:disable_screen_by_appid("*", "*", v.appid, false)
506         else
507             resmgr:disable_screen_by_appid("*", "*", v.appid, regulation == 1)
508         end
509     end
510 end
511
512 -- regulation (on), use "select_driving_mode"
513
514 sink.lua {
515     name = "driving_regulation",
516     inputs = { owner = mdb.select.select_driving_mode },
517     initiate = function(self)
518         -- local data = mdb.select.select_driving_mode.single_value
519         return true
520     end,
521     update = function(self)
522         local data = mdb.select.select_driving_mode.single_value
523
524         if verbose > 1 then
525             print("Driving mode updated: " .. tostring(data))
526         end
527
528         if not sc then
529             return true
530         end
531
532         -- tell homescreen that driving mode was updated
533         sc:send_message(homescreen, m:JSON({command=0x60001,arg=m:JSON({stateid=1,state=data})}))
534
535         --[[
536         -- bulk handle the applications that need requisites
537         r = requisite { driving = true }
538         resmgr:disable_screen_by_requisite("*", "*", r, data == 1)
539         --]]
540
541         regulateApplications(ft(mdb.select.entertainment_applications), data)
542         regulateApplications(ft(mdb.select.undefined_applications), data)
543
544         return true
545     end
546 }
547 --[[
548 sink.lua {
549     name = "regulated_app_change",
550     inputs = { undef = mdb.select.undefined_applications,
551                entertainment = mdb.select.entertainment_applications },
552     initiate = function(self)
553         return true
554     end,
555     update = function(self)
556         local data = mdb.select.select_driving_mode.single_value
557
558         if not sc then
559             return
560         end
561
562         if verbose > 1 then
563             print("regulated application list was changed")
564         end
565
566         regulateApplications(ft(mdb.select.entertainment_applications), data)
567         regulateApplications(ft(mdb.select.undefined_applications), data)
568
569         return true
570     end
571 }
572 --]]
573
574 -- shift position (parking, reverse, other)
575
576 mdb.select {
577     name = "gear_position",
578     table = "amb_gear_position",
579     columns = { "value" },
580     condition = "key = 'GearPosition'"
581 }
582
583 -- cameras (back, front, left, right)
584
585 element.lua {
586     name    = "camera_state",
587     inputs  = { winker = mdb.select.winker, gear = mdb.select.gear_position },
588     oldmode = -1;
589     outputs = {
590         mdb.table {
591             name = "target_camera_state",
592             index = { "id" },
593             create = true,
594             columns = {
595                 { "id", mdb.unsigned },
596                 { "front_camera", mdb.unsigned },
597                 { "back_camera", mdb.unsigned },
598                 { "right_camera", mdb.unsigned },
599                 { "left_camera", mdb.unsigned }
600             }
601         }
602     },
603     update = function(self)
604
605         front_camera = 0
606         back_camera = 0
607         right_camera = 0
608         left_camera = 0
609
610         if self.inputs.gear == 128 then
611             back_camera = 1
612         elseif self.inputs.winker == 1 then
613             right_camera = 1
614         elseif self.inputs.winker == 2 then
615             left_camera = 1
616         end
617
618         mdb.table.target_camera_state:replace({ id = 0, front_camera = front_camera, back_camera = back_camera, right_camera = right_camera, left_camera = left_camera })
619
620     end
621 }
622
623 -- load the telephony plugin
624 m:try_load_plugin('telephony')
625
626
627 -- system controller test setup
628
629 if not with_system_controller then
630    return
631 end
632
633 m:load_plugin('system-controller')
634
635 window_manager_operation_names = {
636     [1] = "create",
637     [2] = "destroy"
638 }
639
640 function window_manager_operation_name(oper)
641     local name = window_manager_operation_names[oper]
642     if name then return name end
643     return "<unknown " .. tostring(oper) .. ">"
644 end
645
646 window_operation_names = {
647     [1] = "create",
648     [2] = "destroy",
649     [3] = "name_change",
650     [4] = "visible",
651     [5] = "configure",
652     [6] = "active"
653 }
654
655 function window_operation_name(oper)
656     local name = window_operation_names[oper]
657     if name then return name end
658     return "<unknown " .. tostring(oper) .. ">"
659 end
660
661 layer_operation_names = {
662     [1] = "create",
663     [2] = "destroy",
664     [3] = "visible"
665 }
666
667 function layer_operation_name(oper)
668     local name = layer_operation_names[oper]
669     if name then return name end
670     return "<unknown " .. tostring(oper) .. ">"
671 end
672
673 input_manager_operation_names = {
674     [1] = "create",
675     [2] = "destroy",
676     [3] = "ready"
677 }
678
679 function input_manager_operation_name(oper)
680     local name = input_manager_operation_names[oper]
681     if name then return name end
682     return "<unknown " .. tostring(oper) .. ">"
683 end
684
685 input_operation_names = {
686     [1] = "create",
687     [2] = "destroy",
688     [3] = "update"
689 }
690
691 function input_operation_name(oper)
692     local name = input_operation_names[oper]
693     if name then return name end
694     return "<unknown " .. tostring(oper) .. ">"
695 end
696
697 code_operation_names = {
698     [1] = "create",
699     [2] = "destroy",
700     [3] = "state_change"
701 }
702
703 function code_operation_name(oper)
704     local name = code_operation_names[oper]
705     if name then return name end
706     return "<unknown " .. tostring(oper) .. ">"
707 end
708
709 command_names = {
710     [0x00001] = "send_appid",
711     [0x10001] = "create",
712     [0x10002] = "destroy",
713     [0x10003] = "show",
714     [0x10004] = "hide",
715     [0x10005] = "move",
716     [0x10006] = "change_active",
717     [0x10007] = "change_layer",
718     [0x10008] = "change_attr",
719     [0x10009] = "name",
720     [0x10011] = "map_thumb",
721     [0x10012] = "unmap_thumb",
722     [0x10020] = "show layer",
723     [0x10021] = "hide_layer",
724     [0x10022] = "change_layer_attr",
725     [0x20001] = "add_input",
726     [0x20002] = "del_input",
727     [0x20003] = "send_input",
728     [0x40001] = "acquire_res",
729     [0x40002] = "release_res",
730     [0x40003] = "deprive_res",
731     [0x40004] = "waiting_res",
732     [0x40005] = "revert_res",
733     [0x40011] = "create_res",
734     [0x40012] = "destroy_res",
735     [0x50001] = "set_region",
736     [0x50002] = "unset_region",
737     [0x60001] = "change_state"
738 }
739
740 function command_name(command)
741     local name = command_names[command]
742     if name then return name end
743     return "<unknown " .. tostring(command) .. ">"
744 end
745
746 input_layer = {
747    [3] = true, -- input
748    [4] = true, -- touch
749    [5] = true  -- cursor
750 }
751
752 resmgr = resource_manager {
753   screen_event_handler = function(self, ev)
754                              local event = ev.event
755                              local surface = ev.surface
756
757                              if event == "grant" then
758                                  if verbose > 0 then
759                                     print("*** make visible surface "..surface)
760                                  end
761                                  local a = animation({})
762                                  local r = m:JSON({surface = surface,
763                                                    visible = 1,
764                                                    raise   = 1})
765                                  wmgr:window_request(r,a,0)
766                                  elseif event == "revoke" then
767                                  if verbose > 0 then
768                                     print("*** hide surface "..surface)
769                                  end
770                                  local a = animation({})
771                                  local r = m:JSON({surface = ev.surface,
772                                                    visible = 0})
773                                  wmgr:window_request(r,a,0)
774
775                              elseif event == "create" then
776
777                                if verbose > 0 then
778                                     print("*** screen resource event: " ..
779                                           tostring(ev))
780                                  end
781
782                                 -- For now, consider every application to be an "entertainment"
783                                 -- application. Whitelist those applications that we know. Later
784                                 -- check the aul_applications table for actual category (when
785                                 -- we get the categories there).
786
787                                 local regulation = mdb.select.select_driving_mode.single_value
788
789                                 if regulation == 1 then
790                                     conf = getApplication(ev.appid)
791
792                                     -- disable only non-whitelisted applications
793                                     if not conf or conf.resource_class == "player" then
794                                         resmgr:disable_screen_by_appid("*", "*", ev.appid, true)
795                                     end
796                                 end
797
798                              elseif event == "destroy" then
799                                if verbose > 0 then
800                                     print("*** screen resource event: " ..
801                                           tostring(ev))
802                                  end
803                              else
804                                  if verbose > 0 then
805                                     print("*** screen resource event: " ..
806                                           tostring(ev))
807                                  end
808                              end
809                          end,
810   audio_event_handler = function(self, ev)
811                              local event = ev.event
812                              local appid = ev.appid
813                              local audioid = ev.audioid
814
815                              if event == "grant" then
816                                  if verbose > 0 or true then
817                                     print("*** grant audio to "..appid..
818                                           " ("..audioid..") in '" ..
819                                           ev.zone .. "' zone")
820                                  end
821                              elseif event == "revoke" then
822                                  if verbose > 0 or true then
823                                     print("*** revoke audio from "..appid..
824                                           " ("..audioid..") in '" ..
825                                           ev.zone .. "' zone")
826                                  end
827                              else
828                                  if verbose > 0 or true then
829                                     print("*** audio resource event: " ..
830                                           tostring(ev))
831                                  end
832                              end
833                         end
834 }
835
836 resclnt = resource_client {}
837
838 wmgr = window_manager {
839   geometry = function(self, w,h, v)
840                   if type(v) == "function" then
841                       return v(w,h)
842                   end
843                   return v
844              end,
845
846   application = function(self, appid)
847                      if appid then
848                          local app = application_lookup(appid)
849                          if not app then
850                              app = application_lookup("default")
851                          end
852                          return app
853                      end
854                      return { privileges = {screen="none", audio="none"} }
855                 end,
856
857   outputs = { { name  = "Center",
858                 id    = 0,
859                 zone  = "driver",
860                 areas = { Status = {
861                               id     = 0,
862                               pos_x  = 0,
863                               pos_y  = 0,
864                               width  = function(w,h) return w end,
865                               height = 64
866                           },
867                           Full = {
868                               id     = 1,
869                               pos_x  = 0,
870                               pos_y  = 64,
871                               width  = function(w,h) return w end,
872                               height = function(w,h) return h-64-128 end
873                           },
874                           Upper = {
875                               id     = 2,
876                               pos_x  = 0,
877                               pos_y  = 64,
878                               width  = function(w,h) return w end,
879                               height = function(w,h) return (h-64-128)/2 end
880                           },
881                           Lower = {
882                               id     = 3,
883                               pos_x  = 0,
884                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
885                               width  = function(w,h) return w end,
886                               height = function(w,h) return (h-64-128)/2 end
887                           },
888                           UpperLeft = {
889                               id     = 4,
890                               pos_x  = 0,
891                               pos_y  = 64,
892                               width  = function(w,h) return w/2 end,
893                               height = function(w,h) return (h-64-128)/2 end
894                           },
895                           UpperRight = {
896                               id     = 5,
897                               pos_x  = function(w,h) return w/2 end,
898                               pos_y  = 64,
899                               width  = function(w,h) return w/2 end,
900                               height = function(w,h) return (h-64-128)/2 end
901                           },
902                           LowerLeft = {
903                               id     = 6,
904                               pos_x  = 0,
905                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
906                               width  = function(w,h) return w/2 end,
907                               height = function(w,h) return (h-64-128)/2 end
908                           },
909                           LowerRight = {
910                               id     = 7,
911                               pos_x  = function(w,h) return w/2 end,
912                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
913                               width  = function(w,h) return w/2 end,
914                               height = function(w,h) return (h-64-128)/2 end
915                           },
916                           SysApp = {
917                               id     = 8,
918                               pos_x  = 0,
919                               pos_y  = 64,
920                               width  = function(w,h) return w end,
921                               height = function(w,h) return h-64-128 end
922                           },
923                           ["SysApp.Left"] = {
924                               id     = 9,
925                               pos_x  = 0,
926                               pos_y  = 64,
927                               width  = function(w,h) return w/2-181 end,
928                               height = function(w,h) return h-64-128 end
929                           },
930                           ["SysApp.Right"] = {
931                               id     = 10,
932                               pos_x  = function(w,h) return w/2+181 end,
933                               pos_y  = 64,
934                               width  = function(w,h) return w/2-181 end,
935                               height = function(w,h) return h-64-128 end
936                           },
937                         }
938                },
939                { name  = "Mid",
940                  zone  = "driver",
941                  id    = 1
942                }
943
944   },
945   layers = { {      0, "Background"   , 1 },
946              {      1, "Application"  , 2 },
947              {      2, "Softkeyboard" , 4 },
948              {      3, "HomeScreen"   , 2 },
949              {      4, "ControlBar"   , 2 },
950              {      5, "InterruptApp" , 2 },
951              {      6, "OnScreen"     , 2 },
952              {    101, "Input"        , 3 },
953              {    102, "Cursor"       , 5 },
954              {    103, "Startup"      , 6 },
955              { 0x1000, "Background"   , 1 },
956              { 0x2000, "Normal"       , 2 },
957              { 0x3000, "Fullscreen"   , 2 },
958              { 0x4000, "InputPanel"   , 3 },
959              { 0xA000, "Touch"        , 4 },
960              { 0xB000, "Cursor"       , 5 },
961              { 0xC000, "Startup"      , 6 }
962   },
963
964
965   manager_update = function(self, oper)
966                        if verbose > 0 then
967                            print("### <== WINDOW MANAGER UPDATE:" ..
968                                  window_manager_operation_name(oper))
969                        end
970                        if oper == 1 then
971                            local umask = window_mask { raise   = true,
972                                                        visible = true,
973                                                        active  = true }
974                            local rmask = window_mask { active  = true }
975                            local req = m:JSON({
976                                        passthrough_update  = umask:tointeger(),
977                                        passthrough_request = rmask:tointeger()
978                            })
979                            self:manager_request(req)
980                        end
981                    end,
982
983   window_update = function(self, oper, win, mask)
984                       if verbose > 0 then
985                           print("### <== WINDOW UPDATE oper:" ..
986                                 window_operation_name(oper) ..
987                                 " mask: " .. tostring(mask))
988                           if verbose > 1 then
989                               print(win)
990                           end
991                       end
992
993                       local arg = m:JSON({ surface = win.surface,
994                                            winname = win.name,
995                       })
996                       local command = 0
997
998                       if oper == 1 then -- create
999                            local layertype = win.layertype
1000                            if layertype and input_layer[layertype] then
1001                                if verbose > 0 then
1002                                    print("ignoring input panel creation")
1003                                end
1004                                return
1005                            end
1006                            command     = 0x10001
1007                       elseif oper == 2 then -- destroy
1008                            command     = 0x10002
1009                       elseif oper == 3 then  -- namechange
1010                            command     = 0x10009
1011                       elseif oper == 4 or oper == 5 then --visible or configure
1012                            command     = 0x10008
1013                            arg.zone    = win.area
1014                            arg.node    = win.node
1015                            arg.layer   = win.layer
1016                            arg.pos_x   = win.pos_x
1017                            arg.pos_y   = win.pos_y
1018                            arg.width   = win.width
1019                            arg.height  = win.height
1020                            arg.raise   = win.raise
1021                            arg.visible = win.visible
1022                            arg.active  = win.active
1023                       elseif oper == 6 then -- active
1024                            command     = 0x10006
1025                            arg.active  = win.active
1026                       else
1027                            if verbose > 0 then
1028                                print("### nothing to do")
1029                            end
1030                            return
1031                       end
1032
1033                       local msg = m:JSON({ command = command,
1034                                            appid   = win.appid,
1035                                            pid     = win.pid,
1036                                            arg     = arg
1037                       })
1038                       if verbose > 0 then
1039                           print("### <== sending " ..
1040                                 command_name(msg.command) ..
1041                                 " window message to '" .. win.name .. "'")
1042                           if verbose > 1 then
1043                               print(msg)
1044                           end
1045                       end
1046                       sc:send_message(homescreen, msg)
1047
1048                       if oper == 1 then -- create
1049                           local i = input_layer[win.layertype]
1050                           local p = self:application(win.appid)
1051                           local s = p.privileges.screen
1052
1053                           if s == "system" then
1054                               local a = animation({})
1055                               local r = m:JSON({surface = win.surface,
1056                                                 visible = 1,
1057                                                 raise   = 1})
1058                               self:window_request(r,a,0)
1059                           else
1060                               if i then
1061                                   if verbose > 0 then
1062                                       print("do not make resource for " ..
1063                                             "input window")
1064                                   end
1065                               else
1066                                   resclnt:resource_set_create("screen",
1067                                                                "driver",
1068                                                                win.appid,
1069                                                                win.surface)
1070                               end
1071                           end
1072                       elseif oper == 2 then -- destroy
1073                           resclnt:resource_set_destroy("screen", win.surface)
1074                       elseif oper == 6 then -- active
1075                           if win.active then
1076                               local i = input_layer[win.layertype]
1077                               local p = self:application(win.appid)
1078                               local s = p.privileges.screen
1079                               local surface = win.surface
1080                               if not i and s ~= "system" then
1081                                  resclnt:resource_set_acquire("screen",surface)
1082                                  resmgr:window_raise(win.appid, surface, 1)
1083                               end
1084                           end
1085                       end
1086                   end,
1087
1088   layer_update = function(self, oper, layer, mask)
1089                       if verbose > 0 then
1090                           print("### LAYER UPDATE:" ..
1091                                 layer_operation_name(oper) ..
1092                                 " mask: " .. tostring(mask))
1093                           if verbose > 1 then
1094                               print(layer)
1095                           end
1096                       end
1097                       if oper == 3 then -- visible
1098                          local command = 0x10022
1099                          local msg = m:JSON({
1100                                          command = command,
1101                                          appid = "",
1102                                          arg = m:JSON({layer = layer.id,
1103                                                        visible = layer.visible
1104                                          })
1105                                 })
1106                          if verbose > 0 then
1107                             print("### <== sending "..command_name(command)..
1108                                   " layer message")
1109                             if verbose > 1 then
1110                                 print(msg)
1111                             end
1112                          end
1113                          sc:send_message(homescreen, msg)
1114                       else
1115                            if verbose > 0 then
1116                                print("### nothing to do")
1117                            end
1118                       end
1119                  end,
1120
1121   output_update = function(self, oper, out, mask)
1122                       local idx = out.index
1123                       if verbose > 0 then
1124                           print("### OUTPUT UPDATE:" .. oper ..
1125                                 " mask: "..tostring(mask))
1126                       end
1127                       print(out)
1128                       local outdef = self.outputs[idx+1]
1129                       if (oper == 1) then -- create
1130                           if outdef then
1131                               self:output_request(m:JSON({index = idx,
1132                                                           id    = outdef.id,
1133                                                           name  = outdef.name
1134                                                           }))
1135                           end
1136                       elseif (oper == 5) then -- done
1137                           local ads = outdef.areas
1138                           local on = outdef.name
1139                           if ads then
1140                               for name,ad in pairs(ads) do
1141                                   local can = wmgr:canonical_name(on.."."..name)
1142                                   local a = m:JSON({name   = name,
1143                                                     output = out.index})
1144                                   for fld,val in pairs(ad) do
1145                                       a[fld] = self:geometry(out.width,
1146                                                              out.height,
1147                                                              val)
1148                                    end
1149                                    self:area_create(a)
1150                                    resmgr:area_create(area[can], outdef.zone)
1151                               end
1152                           end
1153                       end
1154                   end
1155 }
1156
1157
1158 imgr = input_manager {
1159   inputs = {{ name = "G27 Racing Wheel",
1160               id = 0,
1161               switch = { [2] = {appid="org.tizen.ico.app-soundsample"      },
1162                          [3] = {appid="org.tizen.ico.homescreen", keycode=1},
1163                          [4] = {appid="org.tizen.ico.app-soundsample"      },
1164                          [5] = {appid="org.tizen.ico.homescreen", keycode=2}
1165              }}
1166   },
1167
1168   manager_update = function(self, oper)
1169                        if verbose > 0 then
1170                            print("### <== INPUT MANAGER UPDATE:" ..
1171                                  input_manager_operation_name(oper))
1172                        end
1173                    end,
1174
1175   input_update = function(self, oper, inp, mask)
1176                      if verbose > 0 then
1177                          print("### INPUT UPDATE:" ..
1178                                 input_operation_name(oper) ..
1179                                 " mask: " .. tostring(mask))
1180                           if verbose > 1 then
1181                               print(inp)
1182                           end
1183                       end
1184                  end,
1185   code_update  = function(self, oper, code, mask)
1186                      if verbose > 0 then
1187                          print("### CODE UPDATE: mask: " .. tostring(mask))
1188                          if verbose > 1 then
1189                              print(code)
1190                          end
1191                      end
1192                      local msg = m:JSON({ command = 1,
1193                                           appid = "org.tizen.ico.homescreen",
1194                                           arg = m:JSON({ device = code.device,
1195                                                          time = code.time,
1196                                                          input = code.input,
1197                                                          code = code.id,
1198                                                          state = code.state
1199                                            })
1200                      })
1201                      if verbose > 0 then
1202                          print("### <== sending " ..
1203                                command_name(msg.command) ..
1204                                " input message")
1205                          if verbose > 1 then
1206                              print(msg)
1207                          end
1208                      end
1209                      sc:send_message(homescreen, msg)
1210                  end
1211 }
1212
1213 sc = m:get_system_controller()
1214
1215 -- resource sets
1216 sets = {}
1217
1218 -- user manager
1219 um = m:UserManager()
1220
1221 connected = false
1222 homescreen = ""
1223
1224 -- these shoud be before wmgr:connect() is called
1225 if verbose > 0 then
1226    print("====== creating applications ======")
1227 end
1228 application {
1229     appid          = "default",
1230     area           = "Center.Full",
1231     privileges     = { screen = "none", audio = "none" },
1232     resource_class = "player",
1233     screen_priority = 0
1234 }
1235
1236 application {
1237     appid           = "weston",
1238     area            = "Center.Full",
1239     privileges      = { screen = "system", audio = "none" },
1240     resource_class  = "implicit",
1241     screen_priority = 30
1242 }
1243
1244 application {
1245     appid           = "org.tizen.ico.homescreen",
1246     area            = "Center.Full",
1247     privileges      = { screen = "system", audio = "system" },
1248     resource_class  = "player",
1249     screen_priority = 20
1250 }
1251
1252 application {
1253     appid           = "org.tizen.ico.statusbar",
1254     area            = "Center.Status",
1255     privileges      = { screen = "system", audio = "none" },
1256     resource_class  = "player",
1257     screen_priority = 20
1258 }
1259
1260 application {
1261     appid           = "org.tizen.ico.login",
1262     area            = "Center.Full",
1263     privileges      = { screen = "system", audio = "system" },
1264     resource_class  = "player",
1265     screen_priority = 20
1266 }
1267
1268 application {
1269     appid           = "org.tizen.ico.camera_left",
1270     area            = "Center.SysApp.Left",
1271     privileges      = { screen = "system", audio = "none" },
1272     requisites      = { screen = "blinker_left", audio = "none" },
1273     resource_class  = "player",
1274     screen_priority = 30
1275 }
1276
1277 application {
1278     appid           = "org.tizen.ico.camera_right",
1279     area            = "Center.SysApp.Right",
1280     privileges      = { screen = "system", audio = "none" },
1281     requisites      = { screen = "blinker_right", audio = "none" },
1282     resource_class  = "player",
1283     screen_priority = 30
1284 }
1285
1286 application {
1287     appid           = "net.zmap.navi",
1288     area            = "Center.Full",
1289     privileges      = { screen = "none", audio = "none" },
1290     resource_class  = "navigator",
1291     screen_priority = 30
1292 }
1293
1294 if sc then
1295     sc.client_handler = function (self, cid, msg)
1296         local command = msg.command
1297         if verbose > 0 then
1298             print('### ==> client handler:')
1299             if verbose > 1 then
1300                 print(msg)
1301             end
1302         end
1303         if not connected then
1304             print('Setting homescreen='..msg.appid)
1305             homescreen = msg.appid
1306             print('Trying to connect to wayland...')
1307             connected = wmgr:connect()
1308         end
1309         if connected and command then
1310             if command == 1 then -- send_appid
1311                 local driving_mode = mdb.select.select_driving_mode.single_value
1312                 local night_mode = mdb.select.select_night_mode.single_value
1313
1314                 if not driving_mode then driving_mode = 0 end
1315                 if not night_mode then night_mode = 0 end
1316
1317                 local reply = m:JSON({ command = 0x60001,
1318                                        arg     = m:JSON({ stateid = 1,
1319                                                           state   = driving_mode})
1320                                      })
1321                  if verbose > 0 then
1322                      print("### <== sending " ..
1323                            command_name(command) .. " message")
1324                      if verbose > 1 then
1325                          print(reply)
1326                      end
1327                  end
1328                  sc:send_message(homescreen, reply)
1329
1330                  reply = m:JSON({ command = 0x60001,
1331                                   arg     = m:JSON({ stateid = 2,
1332                                                      state   = night_mode})
1333                                 })
1334                  if verbose > 0 then
1335                      print("### <== sending " ..
1336                            command_name(command) .. " message")
1337                      if verbose > 1 then
1338                          print(reply)
1339                      end
1340                  end
1341                  sc:send_message(homescreen, reply)
1342             end
1343         end
1344     end
1345
1346     sc.generic_handler = function (self, cid, msg)
1347         if verbose > 0 then
1348             print('### ==> generic handler:')
1349             if verbose > 1 then
1350                print(msg)
1351             end
1352         end
1353     end
1354
1355     sc.window_handler = function (self, cid, msg)
1356         if verbose > 0 then
1357             print('### ==> received ' ..
1358                    command_name(msg.command) .. ' message')
1359             if verbose > 1 then
1360                 print(tostring(msg))
1361             end
1362         end
1363
1364         local a = animation({})
1365         local nores = false
1366         if msg.command == 0x10003 then       -- ico SHOW command
1367             local raise_mask = 0x01000000
1368             local lower_mask = 0x02000000
1369             local nores_mask = 0x40000000
1370             local time_mask  = 0x00ffffff
1371             msg.arg.visible = 1
1372             if msg.arg then
1373                 local time = 200
1374                 if  msg.arg.anim_time then
1375                     local t = msg.arg.anim_time
1376                     time = m:AND(t, time_mask)
1377                     nores = not m:AND(t, nores_mask)
1378                     if m:AND(t, raise_mask) then
1379                         msg.arg.raise = 1
1380                     elseif m:AND(t, lower_mask) then
1381                         msg.arg.raise = 0
1382                     end
1383                 end
1384                 if msg.arg.anim_name then
1385                     a.show = { msg.arg.anim_name, time }
1386                     print('time: ' .. tostring(a.show[2]))
1387                 end
1388             end
1389             if not nores then
1390                 local p = wmgr:application(msg.appid)
1391                 local s = p.privileges.screen
1392                 if s == "system" then
1393                     nores = true
1394                 end
1395             end
1396             if verbose > 2 then
1397                 print('### ==> SHOW')
1398                 print(tostring(msg.arg))
1399             end
1400             if nores then
1401                 wmgr:window_request(msg.arg, a, 0)
1402             else
1403                 local surface = msg.arg.surface
1404                 resclnt:resource_set_acquire("screen", surface)
1405                 resmgr:window_raise(msg.appid, surface, 1)
1406             end
1407         elseif msg.command == 0x10004 then   -- ico HIDE command
1408             local raise_mask = 0x01000000
1409             local lower_mask = 0x02000000
1410             local nores_mask = 0x40000000
1411             local time_mask  = 0x00ffffff
1412             msg.arg.visible = 0
1413             if msg.arg then
1414                 local time = 200
1415                 if msg.arg.anim_time then
1416                     local t = msg.arg.anim_time
1417                     time = m:AND(t, time_mask)
1418                     nores = not m:AND(t, nores_mask)
1419                 end
1420                 if msg.arg.anim_name then
1421                     a.hide = { msg.arg.anim_name, time }
1422                     print('hide animation time: ' .. tostring(a.hide[2]))
1423                 end
1424             end
1425             if not nores then
1426                 local p = wmgr:application(msg.appid)
1427                 local s = p.privileges.screen
1428                 if s == "system" then
1429                     nores = true
1430                 end
1431             end
1432             if verbose > 2 then
1433                 print('### ==> HIDE REQUEST')
1434                 print(tostring(msg.arg))
1435             end
1436             if nores then
1437                 wmgr:window_request(msg.arg, a, 0)
1438             else
1439                 resmgr:window_raise(msg.appid, msg.arg.surface, -1)
1440             end
1441         elseif msg.command == 0x10005 then   -- ico MOVE
1442             if verbose > 2 then
1443                 print('### ==> MOVE REQUEST')
1444                 print(tostring(msg.arg))
1445             end
1446             wmgr:window_request(msg.arg, a, 0)
1447             -- TODO: handle if area changed
1448         elseif msg.command == 0x10006 then   -- ico ACTIVE
1449             if not msg.arg.active then
1450                 msg.arg.active = 3 -- pointer + keyboard
1451             end
1452             if verbose > 2 then
1453                 print('### ==> ACTIVE REQUEST')
1454                 print(tostring(msg.arg))
1455             end
1456             wmgr:window_request(msg.arg, a, 0)
1457         elseif msg.command == 0x10007 then   -- ico CHANGE_LAYER
1458             if verbose > 2 then
1459                 print('### ==> CHANGE_LAYER REQUEST')
1460                 print(tostring(msg.arg))
1461             end
1462             --[[
1463             if msg.arg.layer ~= 4 or msg.arg.layer ~= 5 then
1464                 print("do not change layer for other than cursor or touch")
1465                 return
1466             end
1467             --]]
1468             wmgr:window_request(msg.arg, a, 0)
1469         elseif msg.command == 0x10011 then   -- ico MAP_THUMB
1470             local framerate = msg.arg.framerate
1471             if not framerate or framerate < 0 then
1472                 framerate = 0
1473             end
1474             msg.arg.map = 1
1475             if verbose > 2 then
1476                 print('### ==> MAP_THUMB REQUEST')
1477                 print(msg.arg)
1478                 print('framerate: '..framerate)
1479             end
1480             wmgr:window_request(msg.arg, a, framerate)
1481         elseif msg.command == 0x10012 then   -- ico UNMAP_THUMB
1482             msg.arg.map = 0
1483             if verbose > 2 then
1484                 print('### ==> UNMAP_THUMB REQUEST')
1485                 print(msg.arg)
1486             end
1487             wmgr:window_request(msg.arg, a, 0)
1488         elseif msg.command == 0x10020 then   -- ico SHOW_LAYER command
1489             msg.arg.visible = 1
1490             if verbose > 2 then
1491                 print('### ==> SHOW_LAYER REQUEST')
1492                 print(msg.arg)
1493             end
1494             wmgr:layer_request(msg.arg)
1495         elseif msg.command == 0x10021 then   -- ico HIDE_LAYER command
1496             msg.arg.visible = 0
1497             if verbose > 2 then
1498                 print('### ==> HIDE_LAYER REQUEST')
1499                 print(msg.arg)
1500             end
1501             wmgr:layer_request(msg.arg)
1502         end
1503     end
1504
1505     sc.input_handler = function (self, cid, msg)
1506         if verbose > 0 then
1507             print('### ==> input handler: ' .. command_name(msg.command))
1508             if verbose > 1 then
1509                 print(msg)
1510             end
1511         end
1512         if msg.command == 0x20001 then -- add_input
1513             msg.arg.appid = msg.appid
1514             if verbose > 2 then
1515                 print('### ==> ADD_INPUT REQUEST')
1516                 print(tostring(msg.arg))
1517             end
1518             imgr:input_request(msg.arg)
1519         elseif msg.command == 0x20002 then -- del_input
1520             msg.arg.appid = ''
1521             if verbose > 2 then
1522                 print('### ==> DEL_INPUT REQUEST')
1523                 print(tostring(msg.arg))
1524             end
1525             imgr:input_request(msg.arg)
1526         elseif msg.command == 0x20003 then -- send_input
1527         end
1528     end
1529
1530     sc.user_handler = function (self, cid, msg)
1531         if verbose > 0 then
1532             print('### ==> user handler: ' .. command_name(msg.command))
1533             if verbose > 1 then
1534                 print(msg)
1535             end
1536         end
1537
1538         if not um then
1539             print("User Manager not initialized")
1540             return
1541         end
1542
1543         if msg.command == 0x00030001 then -- MSG_CMD_CHANGE_USER
1544             print("command CHANGE_USER")
1545             if not msg.arg then
1546                 print("invalid message")
1547                 return
1548             end
1549
1550             username = msg.arg.user
1551             passwd = msg.arg.pass
1552
1553             if not username then
1554                 username = ""
1555             end
1556
1557             if not passwd then
1558                 passwd = ""
1559             end
1560
1561             success = um:changeUser(username, passwd)
1562
1563             if not success then
1564                 reply = m.JSON({
1565                     appid = msg.appid,
1566                     command = cmd
1567                 })
1568                 if sc:send_message(msg.appid, reply) then
1569                     print('*** sent authentication failed message')
1570                 else
1571                     print('*** failed to send authentication failed message')
1572                 end
1573             end
1574
1575         elseif msg.command == 0x00030002 then -- MSG_CMD_GET_USERLIST
1576             print("command GET_USERLIST")
1577             if not msg.appid then
1578                 print("invalid message")
1579                 return
1580             end
1581
1582             users, currentUser = um:getUserList()
1583
1584             if not users then
1585                 print("failed to get user list")
1586                 return
1587             end
1588
1589             nUsers = 0
1590
1591             for i,v in pairs(users) do
1592                 nUsers = nUsers + 1
1593             end
1594
1595             if not currentUser then
1596                 currentUser = ""
1597             end
1598
1599             if verbose > 1 then
1600                 print("current user: " .. currentUser)
1601                 print("user list:")
1602                 for i,v in pairs(users) do
1603                     print(v)
1604                 end
1605             end
1606
1607             reply = m.JSON({
1608                 appid = msg.appid,
1609                 command = cmd,
1610                 arg = m.JSON({
1611                     user_num = nUsers,
1612                     user_list = users,
1613                     user_login = currentUser
1614                 })
1615             })
1616
1617             if verbose > 1 then
1618                 print("### <== GetUserList reply: " .. tostring(reply))
1619             end
1620
1621             if sc:send_message(msg.appid, reply) then
1622                 print('*** reply OK')
1623             else
1624                 print('*** reply FAILED')
1625             end
1626
1627         elseif msg.command == 0x00030003 then -- MSG_CMD_GET_LASTINFO
1628             print("command GET_LASTINFO")
1629             if not msg.appid then
1630                 print("invalid message")
1631                 return
1632             end
1633
1634             lastInfo = um:getLastInfo(msg.appid)
1635
1636             if not lastInfo then
1637                 print("failed to get last info for app" .. msg.appid)
1638                 return
1639             end
1640
1641             reply = m.JSON({
1642                 appid = msg.appid,
1643                 command = cmd,
1644                 arg = m.JSON({
1645                     lastinfo = lastinfo
1646                 })
1647             })
1648
1649             if sc:send_message(msg.appid, reply) then
1650                 print('*** reply OK')
1651             else
1652                 print('*** reply FAILED')
1653             end
1654
1655         elseif msg.command == 0x00030004 then -- MSG_CMD_SET_LASTINFO
1656             print("command SET_LASTINFO")
1657             if not msg.arg or not msg.appid then
1658                 print("invalid message")
1659                 return
1660             end
1661
1662             lastInfo = um:setLastInfo(msg.appid, msg.arg.lastinfo)
1663         end
1664     end
1665
1666     sc.resource_handler = function (self, cid, msg)
1667         if verbose > 0 then
1668             print('### ==> resource handler: ' .. command_name(msg.command))
1669             if verbose > 1 then
1670                 print(msg)
1671             end
1672         end
1673
1674         createResourceSet = function (ctl, client, msg)
1675             cb = function(rset, data)
1676                 print("> resource callback")
1677
1678                 -- type is either basic (0) or interrupt (1)
1679                 requestType = 0
1680                 if msg.res.type then
1681                     requestType = msg.res.type
1682                 end
1683
1684                 if rset.acquired then
1685                     cmd = 0x00040001 -- acquire
1686                 else
1687                     cmd = 0x00040002 -- release
1688                 end
1689
1690                 reply = m.JSON({
1691                         appid = data.client,
1692                         command = cmd,
1693                         res = {
1694                             type = requestType
1695                         }
1696                     })
1697
1698                 if rset.resources.audio_playback then
1699                     reply.res.sound = {
1700                         zone = "driver",
1701                         name = msg.appid,
1702                         adjust = 0,
1703                         -- id = "0"
1704                     }
1705                 end
1706
1707                 if rset.resources.display then
1708                     reply.res.window = {
1709                         zone = "driver",
1710                         name = msg.appid,
1711                         -- id = "0"
1712                     }
1713                 end
1714
1715                 if rset.resources.input then
1716                     reply.res.input = {
1717                         name = msg.appid,
1718                         event = 0
1719                     }
1720                 end
1721                 print("sending message to client: " .. data.client)
1722
1723                 if sc:send_message(data.client, reply) then
1724                     print('*** reply OK')
1725                 else
1726                     print('*** reply FAILED')
1727                 end
1728             end
1729
1730             rset = m:ResourceSet({
1731                     application_class = "player",
1732                     zone = "driver", -- msg.zone ("full")
1733                     callback = cb
1734                 })
1735
1736             rset.data = {
1737                 cid = cid,
1738                 ctl = ctl
1739             }
1740
1741             if msg.res.sound then
1742                 rset:addResource({
1743                         resource_name = "audio_playback"
1744                     })
1745                 rset.resources.audio_playback.attributes.pid = tostring(msg.pid)
1746                 rset.resources.audio_playback.attributes.appid = msg.appid
1747                 print("sound name: " .. msg.res.sound.name)
1748                 print("sound zone:" .. msg.res.sound.zone)
1749                 print("sound adjust: " .. tostring(msg.res.sound.adjust))
1750                 if msg.res.sound.id then
1751                     print("sound id: " .. msg.res.sound.id)
1752                 end
1753             end
1754
1755             if msg.res.input then
1756                 rset:addResource({
1757                         resource_name = "input"
1758                     })
1759                 rset.resources.input.attributes.pid = tostring(msg.pid)
1760                 rset.resources.input.attributes.appid = msg.appid
1761                 print("input name: " .. msg.res.sound.name)
1762                 print("input event:" .. tostring(msg.res.input.event))
1763             end
1764
1765             if msg.res.window then
1766                 rset:addResource({
1767                         resource_name = "display"
1768                     })
1769                 rset.resources.display.attributes.pid = tostring(msg.pid)
1770                 rset.resources.display.attributes.appid = msg.appid
1771                 print("display name: " .. msg.res.display.name)
1772                 print("display zone:" .. msg.res.display.zone)
1773                 if msg.res.display.id then
1774                     print("display id: " .. msg.res.display.id)
1775                 end
1776             end
1777
1778             return rset
1779         end
1780
1781         -- parse the message
1782
1783         -- fields common to all messages:
1784         --      msg.command
1785         --      msg.appid
1786         --      msg.pid
1787
1788         if msg.command == 0x00040011 then -- MSG_CMD_CREATE_RES
1789             print("command CREATE")
1790
1791             if not sets.cid then
1792                 sets.cid = createResourceSet(self, cid, msg)
1793             end
1794
1795         elseif msg.command == 0x00040012 then -- MSG_CMD_DESTORY_RES
1796             print("command DESTROY")
1797
1798             if sets.cid then
1799                 sets.cid:release()
1800             end
1801
1802             sets.cid = nil -- garbage collecting
1803
1804         elseif msg.command == 0x00040001 then -- MSG_CMD_ACQUIRE_RES
1805             print("command ACQUIRE")
1806
1807             if not sets.cid then
1808                 sets.cid = createResourceSet(self, cid, msg)
1809             end
1810
1811             sets.cid:acquire()
1812
1813         elseif msg.command == 0x00040002 then -- MSG_CMD_RELEASE_RES
1814             print("command RELEASE")
1815
1816             if sets.cid then
1817                 sets.cid:release()
1818             end
1819
1820         elseif msg.command == 0x00040003 then -- MSG_CMD_DEPRIVE_RES
1821             print("command DEPRIVE")
1822
1823         elseif msg.command == 0x00040004 then -- MSG_CMD_WAITING_RES
1824             print("command WAITING")
1825
1826         elseif msg.command == 0x00040005 then -- MSG_CMD_REVERT_RES
1827             print("command REVERT")
1828         end
1829     end
1830
1831     sc.inputdev_handler = function (self, cid, msg)
1832         if verbose > 0 then
1833             print('*** inputdev handler: ' .. command_name(msg.command))
1834             if verbose > 1 then
1835                 print(msg)
1836             end
1837         end
1838     end
1839 end