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